implement better serialization of class names

RTWYSIWYG-27 : poorly formed yet valid HTML caused hyperjson to produce element
               selectors which hyperscript could not parse.
pull/1/head
ansuz 9 years ago
parent fbe6225681
commit 0ff4906f0e

@ -17,7 +17,7 @@ define([], function () {
var callOnHyperJSON = function (hj, cb) {
var children;
if (hj && hj[2]) {
children = hj[2].map(function (child) {
if (isArray(child)) {
@ -39,6 +39,14 @@ define([], function () {
return cb(hj[0], hj[1], children);
};
var prependDot = function (token) {
return '.' + token;
};
var isTruthy = function (x) {
return x;
};
var DOM2HyperJSON = function(el){
if(!el.tagName && el.nodeType === Node.TEXT_NODE){
return el.textContent;
@ -73,7 +81,14 @@ define([], function () {
delete attributes.id;
}
if(attributes.class){
sel = sel +'.'+ attributes.class.replace(/ /g,".");
/* TODO this can be done with RegExps alone, and it will be faster
but this works and is a little less error prone, albeit slower
come back and speed it up when it comes time to optimize */
sel = sel +'.'+ attributes.class
.split(/\s+/)
.split(isTruthy)
.map(prependDot)
.join('');
delete attributes.class;
}
result.push(sel);

Loading…
Cancel
Save