From fb91f5df76f9ad0922565d41edb9288562018316 Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 17 May 2018 10:35:48 +0200 Subject: [PATCH] compare flat-dom output against hyperjson --- www/assert/main.js | 34 ++++++++++++++++++++++++++++++++++ www/common/flat-dom.js | 14 ++++++++++---- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/www/assert/main.js b/www/assert/main.js index f8177a03f..657488b81 100644 --- a/www/assert/main.js +++ b/www/assert/main.js @@ -132,6 +132,40 @@ define([ strungJSON(orig); }); + HTML_list.forEach(function (sel) { + var el = $(sel)[0]; + + var pred = function (el) { + if (el.nodeName === 'DIV') { + return true; + } + }; + + var filter = function (x) { + console.log(x); + if (x[1]['class']) { + x[1]['class'] = x[1]['class'].replace(/cke/g, ''); + } + return x; + }; + + assert(function (cb) { + // FlatDOM output + var map = Flat.fromDOM(el, pred, filter); + + // Hyperjson output + var hj = Hyperjson.fromDOM(el, pred, filter); + + var x = Flat.toDOM(map); + var y = Hyperjson.toDOM(hj); + + console.error(x.outerHTML); + console.error(y.outerHTML); + + cb(x.outerHTML === y.outerHTML); + }, "Test equality of FlatDOM and HyperJSON"); + }); + // check that old hashes parse correctly assert(function (cb) { //if (1) { return cb(true); } // TODO(cjd): This is a test failure which is a known bug diff --git a/www/common/flat-dom.js b/www/common/flat-dom.js index 25b51d786..d54e9db55 100644 --- a/www/common/flat-dom.js +++ b/www/common/flat-dom.js @@ -17,8 +17,7 @@ define([], function () { return data; }; - var identity = function (x) { return x; }; - Flat.fromDOM = function (dom) { + Flat.fromDOM = function (dom, predicate, filter) { var data = { map: {}, }; @@ -34,14 +33,21 @@ define([], function () { return id; } if (!el || !el.attributes) { return; } + if (predicate) { + if (!predicate(el)) { return; } // shortcircuit + } + id = uid(); - data.map[id] = [ + var temp = [ el.tagName, getAttrs(el), slice(el.childNodes).map(function (e) { return process(e); - }).filter(identity) + }).filter(Boolean) ]; + + data.map[id] = filter? filter(temp): temp; + return id; };