implement optional filtering in hyperjson

Implemented via callback, return falsey if you want to filter an element
and all of its children from the serialized result.

Conflicts:
	www/common/convert.js
This commit is contained in:
ansuz
2016-03-24 12:11:31 +01:00
parent 475ca9bea8
commit c047d5310f
2 changed files with 33 additions and 6 deletions

View File

@@ -47,13 +47,20 @@ define([], function () {
return x;
};
var DOM2HyperJSON = function(el){
var DOM2HyperJSON = function(el, predicate){
if(!el.tagName && el.nodeType === Node.TEXT_NODE){
return el.textContent;
}
if(!el.attributes){
return;
}
if (predicate) {
if (!predicate(el)) {
// shortcircuit
return;
}
}
var attributes = {};
var i = 0;
@@ -102,10 +109,12 @@ define([], function () {
// js hint complains if we use 'var' here
i = 0;
for(; i < el.childNodes.length; i++){
children.push(DOM2HyperJSON(el.childNodes[i]));
children.push(DOM2HyperJSON(el.childNodes[i], predicate));
}
result.push(children);
result.push(children.filter(isTruthy));
return result;
};