test using newest hyperjson. forget hyperscript

pull/1/head
ansuz 9 years ago
parent 92b617f96f
commit f607fcc0ad

@ -1,14 +1,12 @@
require.config({ paths: { 'json.sortify': '/bower_components/json.sortify/dist/JSON.sortify' } }); require.config({ paths: { 'json.sortify': '/bower_components/json.sortify/dist/JSON.sortify' } });
define([ define([
'/bower_components/jquery/dist/jquery.min.js', '/bower_components/jquery/dist/jquery.min.js',
'/bower_components/hyperjson/hyperjson.amd.js', // serializing classes as an attribute '/bower_components/hyperjson/hyperjson.js',
'/common/hyperscript.js', // using setAttribute
'/bower_components/textpatcher/TextPatcher.amd.js', '/bower_components/textpatcher/TextPatcher.amd.js',
'json.sortify', 'json.sortify',
], function (jQuery, Hyperjson, Hyperscript, TextPatcher, Sortify) { ], function (jQuery, Hyperjson, TextPatcher, Sortify) {
var $ = window.jQuery; var $ = window.jQuery;
window.Hyperjson = Hyperjson; window.Hyperjson = Hyperjson;
window.Hyperscript = Hyperscript;
window.TextPatcher = TextPatcher; window.TextPatcher = TextPatcher;
window.Sortify = Sortify; window.Sortify = Sortify;
@ -44,11 +42,11 @@ define([
var $body = $('body'); var $body = $('body');
var HJSON_list = [ var HJSON_list = [
'["DIV#target",{},[["P#bang",{"class":" alice bob charlie has.dot"},["pewpewpew"]]]]', '["DIV",{"id":"target"},[["P",{"class":" alice bob charlie has.dot","id":"bang"},["pewpewpew"]]]]',
'["DIV#quot",{},[["P",{},["\\"pewpewpew\\""]]]]', '["DIV",{"id":"quot"},[["P",{},["\\"pewpewpew\\""]]]]',
'["DIV#widget",{},[["DIV",{"class":"cke_widget_wrapper cke_widget_block","contenteditable":"false","data-cke-display-name":"macro:velocity","data-cke-filter":"off","data-cke-widget-id":"0","data-cke-widget-wrapper":"1","tabindex":"-1"},[["DIV",{"class":"macro cke_widget_element","data-cke-widget-data":"%7B%22classes%22%3A%7B%22macro%22%3A1%7D%7D","data-cke-widget-keep-attr":"0","data-cke-widget-upcasted":"1","data-macro":"startmacro:velocity|-||-|Here is a macro","data-widget":"xwiki-macro"},[["P",{},["Here is a macro"]]]],["SPAN",{"class":"cke_reset cke_widget_drag_handler_container","style":"background: rgba(220, 220, 220, 0.5) url(\\"/customize/cryptofist_small.png\\") repeat scroll 0% 0%; top: -15px; left: 0px; display: block;"},[["IMG",{"class":"cke_reset cke_widget_drag_handler","data-cke-widget-drag-handler":"1","height":"15","src":"data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==","title":"Click and drag to move","width":"15"},[]]]]]]]]', '["DIV",{"id":"widget"},[["DIV",{"class":"cke_widget_wrapper cke_widget_block","contenteditable":"false","data-cke-display-name":"macro:velocity","data-cke-filter":"off","data-cke-widget-id":"0","data-cke-widget-wrapper":"1","tabindex":"-1"},[["DIV",{"class":"macro cke_widget_element","data-cke-widget-data":"%7B%22classes%22%3A%7B%22macro%22%3A1%7D%7D","data-cke-widget-keep-attr":"0","data-cke-widget-upcasted":"1","data-macro":"startmacro:velocity|-||-|Here is a macro","data-widget":"xwiki-macro"},[["P",{},["Here is a macro"]]]],["SPAN",{"class":"cke_reset cke_widget_drag_handler_container","style":"background: rgba(220, 220, 220, 0.5) url(\\"/customize/cryptofist_small.png\\") repeat scroll 0% 0%; top: -15px; left: 0px; display: block;"},[["IMG",{"class":"cke_reset cke_widget_drag_handler","data-cke-widget-drag-handler":"1","height":"15","src":"data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==","title":"Click and drag to move","width":"15"},[]]]]]]]]',
]; ];
@ -75,7 +73,7 @@ define([
} }
// turn it into a DOM // turn it into a DOM
var DOM = Hyperjson.callOn(hjson, Hyperscript); var DOM = Hyperjson.toDOM(hjson);
// turn it back into stringified Hyperjson, but apply filters // turn it back into stringified Hyperjson, but apply filters
var shjson2 = Sortify(Hyperjson.fromDOM(DOM, elementFilter, attributeFilter)); var shjson2 = Sortify(Hyperjson.fromDOM(DOM, elementFilter, attributeFilter));
@ -83,7 +81,6 @@ define([
var success = shjson === shjson2; var success = shjson === shjson2;
var op = TextPatcher.diff(shjson, shjson2); var op = TextPatcher.diff(shjson, shjson2);
//console.log(TextPatcher.format(shjson, op));
var diff = TextPatcher.format(shjson, op); var diff = TextPatcher.format(shjson, op);
@ -98,18 +95,11 @@ define([
HJSON_list.map(HJSON_equal); HJSON_list.map(HJSON_equal);
/* FIXME
This test is not correct. It passes in Firefox, but fails in Chrome,
even though for our purposes the produced code is valid. Passing
`<p class="bob" id="alice"></p>` through the function yields
`<p id="alice" class="bob"></p>`. This is the same element, but string
equality is not a correct metric. */
var roundTrip = function (sel) { var roundTrip = function (sel) {
var target = $(sel)[0]; var target = $(sel)[0];
assert(function () { assert(function () {
var hjson = Hyperjson.fromDOM(target); var hjson = Hyperjson.fromDOM(target);
var cloned = Hyperjson.callOn(hjson, Hyperscript); var cloned = Hyperjson.toDOM(hjson);
var success = cloned.outerHTML === target.outerHTML; var success = cloned.outerHTML === target.outerHTML;
if (!success) { if (!success) {
@ -120,7 +110,6 @@ define([
B: cloned.outerHTML, B: cloned.outerHTML,
diff: op diff: op
}; };
//console.log(JSON.stringify(window.DEBUG, null, 2));
console.log("DIFF:"); console.log("DIFF:");
TextPatcher.log(target.outerHTML, op); TextPatcher.log(target.outerHTML, op);
} }
@ -131,7 +120,7 @@ define([
var HTML_list = [ var HTML_list = [
'#target', '#target',
'#widget', // fails in Firefox 19? '#widget',
'#quot', '#quot',
]; ];
@ -151,16 +140,6 @@ define([
strungJSON(orig); strungJSON(orig);
}); });
//assert(function () { }, "this is expected to fail");
/* TODO Test how browsers handle weird elements
like "_moz-resizing":"true"
and anything else you can think of.
Start with Hyperjson, turn it into a DOM and come back
*/
var swap = function (str, dict) { var swap = function (str, dict) {
return str.replace(/\{\{(.*?)\}\}/g, function (all, key) { return str.replace(/\{\{(.*?)\}\}/g, function (all, key) {
return typeof dict[key] !== 'undefined'? dict[key] : all; return typeof dict[key] !== 'undefined'? dict[key] : all;

Loading…
Cancel
Save