From 99f92cb9a048e1c549aab89ebc36e2c517dffbef Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Thu, 26 Apr 2018 16:01:17 +0200 Subject: [PATCH] fix filtering --- src/Bindings/DomBinding/domToType.js | 27 ++++++++++++++++++++------- src/Bindings/DomBinding/filter.js | 12 ++++++++++++ src/Bindings/DomBinding/util.js | 9 ++++++++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/Bindings/DomBinding/domToType.js b/src/Bindings/DomBinding/domToType.js index fdb939e5..b906b53b 100644 --- a/src/Bindings/DomBinding/domToType.js +++ b/src/Bindings/DomBinding/domToType.js @@ -1,6 +1,7 @@ import { YXmlText, YXmlElement, YXmlHook } from '../../Types/YXml/YXml.js' import { createAssociation } from './util.js' +import { filterDomAttributes } from './filter.js' /** * Creates a Yjs type (YXml) based on the contents of a DOM Element. @@ -17,6 +18,7 @@ export default function domToType (element, _document = document, binding) { case _document.ELEMENT_NODE: let hookName = element.dataset.yjsHook let hook + // configure `hookName !== undefined` if element is a hook. if (hookName !== undefined) { hook = binding.opts.hooks[hookName] if (hook === undefined) { @@ -26,15 +28,26 @@ export default function domToType (element, _document = document, binding) { } } if (hookName === undefined) { - type = new YXmlElement(element.nodeName) - const attrs = element.attributes - for (let i = attrs.length - 1; i >= 0; i--) { - const attr = attrs[i] - type.setAttribute(attr.name, attr.value) + // Not a hook + const attrs = filterDomAttributes(element, binding.filter) + if (attrs === null) { + type = false + } else { + type = new YXmlElement(element.nodeName) + attrs.forEach((val, key) => { + type.setAttribute(key, val) + }) + const children = [] + for (let elem of element.childNodes) { + const type = domToType(elem, _document, binding) + if (type !== false) { + children.push(type) + } + } + type.insert(0, children) } - const children = Array.from(element.childNodes).map(e => domToType(e, _document, binding)) - type.insert(0, children) } else { + // Is a hook type = new YXmlHook(hookName) hook.fillType(element, type) } diff --git a/src/Bindings/DomBinding/filter.js b/src/Bindings/DomBinding/filter.js index 81fac728..4fea3f84 100644 --- a/src/Bindings/DomBinding/filter.js +++ b/src/Bindings/DomBinding/filter.js @@ -13,6 +13,18 @@ export function defaultFilter (nodeName, attrs) { return attrs } +/** + * + */ +export function filterDomAttributes (dom, filter) { + const attrs = new Map() + for (let i = dom.attributes.length - 1; i >= 0; i--) { + const attr = dom.attributes[i] + attrs.set(attr.name, attr.value) + } + return filter(dom.nodeName, attrs) +} + /** * Applies a filter on a type. * diff --git a/src/Bindings/DomBinding/util.js b/src/Bindings/DomBinding/util.js index 259a4f31..e3f0ed0e 100644 --- a/src/Bindings/DomBinding/util.js +++ b/src/Bindings/DomBinding/util.js @@ -54,7 +54,14 @@ export function createAssociation (domBinding, dom, type) { * @private */ export function insertDomElementsAfter (type, prev, doms, _document, binding) { - return type.insertAfter(prev, doms.map(dom => domToType(dom, _document, binding))) + const types = [] + for (let dom of doms) { + const t = domToType(dom, _document, binding) + if (t !== false) { + types.push(t) + } + } + return type.insertAfter(prev, types) } /**