fix filtering

This commit is contained in:
Kevin Jahns 2018-04-26 16:01:17 +02:00
parent e788ad1333
commit 99f92cb9a0
3 changed files with 40 additions and 8 deletions

View File

@ -1,6 +1,7 @@
import { YXmlText, YXmlElement, YXmlHook } from '../../Types/YXml/YXml.js' import { YXmlText, YXmlElement, YXmlHook } from '../../Types/YXml/YXml.js'
import { createAssociation } from './util.js' import { createAssociation } from './util.js'
import { filterDomAttributes } from './filter.js'
/** /**
* Creates a Yjs type (YXml) based on the contents of a DOM Element. * 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: case _document.ELEMENT_NODE:
let hookName = element.dataset.yjsHook let hookName = element.dataset.yjsHook
let hook let hook
// configure `hookName !== undefined` if element is a hook.
if (hookName !== undefined) { if (hookName !== undefined) {
hook = binding.opts.hooks[hookName] hook = binding.opts.hooks[hookName]
if (hook === undefined) { if (hook === undefined) {
@ -26,15 +28,26 @@ export default function domToType (element, _document = document, binding) {
} }
} }
if (hookName === undefined) { if (hookName === undefined) {
type = new YXmlElement(element.nodeName) // Not a hook
const attrs = element.attributes const attrs = filterDomAttributes(element, binding.filter)
for (let i = attrs.length - 1; i >= 0; i--) { if (attrs === null) {
const attr = attrs[i] type = false
type.setAttribute(attr.name, attr.value) } 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 { } else {
// Is a hook
type = new YXmlHook(hookName) type = new YXmlHook(hookName)
hook.fillType(element, type) hook.fillType(element, type)
} }

View File

@ -13,6 +13,18 @@ export function defaultFilter (nodeName, attrs) {
return 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. * Applies a filter on a type.
* *

View File

@ -54,7 +54,14 @@ export function createAssociation (domBinding, dom, type) {
* @private * @private
*/ */
export function insertDomElementsAfter (type, prev, doms, _document, binding) { 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)
} }
/** /**