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

@ -39,6 +39,14 @@ define([], function () {
return cb(hj[0], hj[1], children); return cb(hj[0], hj[1], children);
}; };
var prependDot = function (token) {
return '.' + token;
};
var isTruthy = function (x) {
return x;
};
var DOM2HyperJSON = function(el){ var DOM2HyperJSON = function(el){
if(!el.tagName && el.nodeType === Node.TEXT_NODE){ if(!el.tagName && el.nodeType === Node.TEXT_NODE){
return el.textContent; return el.textContent;
@ -73,7 +81,14 @@ define([], function () {
delete attributes.id; delete attributes.id;
} }
if(attributes.class){ 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; delete attributes.class;
} }
result.push(sel); result.push(sel);

Loading…
Cancel
Save