From 0ff4906f0e571555bce4113fdfdf30102045c100 Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 22 Mar 2016 10:16:14 +0100 Subject: [PATCH] implement better serialization of class names RTWYSIWYG-27 : poorly formed yet valid HTML caused hyperjson to produce element selectors which hyperscript could not parse. --- www/common/hyperjson.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/www/common/hyperjson.js b/www/common/hyperjson.js index db6d3f20f..2f0badbd4 100644 --- a/www/common/hyperjson.js +++ b/www/common/hyperjson.js @@ -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);