From df1a700cb20e560722d7c8f553ada73287857fc7 Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 27 Oct 2017 11:01:22 +0200 Subject: [PATCH] disable thumbnail test. add test for flat dom --- www/assert/main.js | 16 +++++++++++++++- www/common/flat-dom.js | 10 +++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/www/assert/main.js b/www/assert/main.js index 0913674ae..ad7883de3 100644 --- a/www/assert/main.js +++ b/www/assert/main.js @@ -7,7 +7,8 @@ define([ '/drive/tests.js', '/common/test.js', '/common/common-thumbnail.js', -], function ($, Hyperjson, TextPatcher, Sortify, Cryptpad, Drive, Test, Thumb) { + '/common/flat-dom.js', +], function ($, Hyperjson, TextPatcher, Sortify, Cryptpad, Drive, Test, Thumb, Flat) { window.Hyperjson = Hyperjson; window.TextPatcher = TextPatcher; window.Sortify = Sortify; @@ -241,6 +242,7 @@ define([ return cb(true); }, "version 2 hash failed to parse correctly"); +/* assert(function (cb) { var getBlob = function (url, cb) { var xhr = new XMLHttpRequest(); @@ -266,9 +268,21 @@ define([ }); }); }); +*/ Drive.test(assert); + assert(function (cb) { + // extract dom elements into a flattened JSON representation + var flat = Flat.fromDOM(document.body); + // recreate a _mostly_ equivalent DOM + var dom = Flat.toDOM(flat); + // assume we don't care about comments + var bodyText = document.body.outerHTML.replace(//g, '') + // check for equality + cb(dom.outerHTML === bodyText); + }); + var swap = function (str, dict) { return str.replace(/\{\{(.*?)\}\}/g, function (all, key) { return typeof dict[key] !== 'undefined'? dict[key] : all; diff --git a/www/common/flat-dom.js b/www/common/flat-dom.js index c6b2bc4d9..51c3d07b3 100644 --- a/www/common/flat-dom.js +++ b/www/common/flat-dom.js @@ -17,6 +17,7 @@ define([], function () { return data; }; + var identity = function (x) { return x; }; Flat.fromDOM = function (dom) { var data = { map: {}, @@ -26,18 +27,20 @@ define([], function () { var uid = function () { return i++; }; var process = function (el) { - if (!el || el.attributes) { return void console.error(el); } - var id = uid(); + var id; if (!el.tagName && el.nodeType === Node.TEXT_NODE) { + id = uid(); data.map[id] = el.textContent; return id; } + if (!el || !el.attributes) { return void console.error(el); } + id = uid(); data.map[id] = [ el.tagName, getAttrs(el), slice(el.childNodes).map(function (e) { return process(e); - }) + }).filter(identity) ]; return id; }; @@ -49,6 +52,7 @@ define([], function () { Flat.toDOM = function (data) { var visited = {}; var process = function (key) { + if (!key) { return; } // ignore falsey keys if (visited[key]) { // TODO handle this more gracefully. throw new Error('duplicate id or loop detected');