diff --git a/src/from_dom.js b/src/from_dom.js index 4684279..d7b0ef7 100644 --- a/src/from_dom.js +++ b/src/from_dom.js @@ -183,7 +183,8 @@ export class DOMParser { let rule = this.tags[i] if (matches(dom, rule.tag) && (rule.namespace === undefined || dom.namespaceURI == rule.namespace) && - (!rule.context || context.matchesContext(rule.context))) { + (!rule.context || context.matchesContext(rule.context)) && + (!rule.node || context.matchesType(this.schema.nodes[rule.node]))) { if (rule.getAttrs) { let result = rule.getAttrs(dom) if (result === false) continue @@ -672,6 +673,24 @@ class ParseContext { } return match(parts.length - 1, this.open) } + + // : (NodeType) → bool + // Determines whether the given node type + // matches this context. + matchesType(type) { + let option = this.options.context + let useRoot = !this.isOpen && (!option || option.parent.type == this.nodes[0].type) + let minDepth = -(option ? option.depth + 1 : 0) + (useRoot ? 0 : 1) + let match = (depth) => { + let next = depth > 0 || (depth == 0 && useRoot) ? this.nodes[depth].type + : option && depth >= minDepth ? option.node(depth - minDepth).type + : null + if (!next) return false + if (next.contentMatch.matchType(type)) return true + return match(depth - 1) + } + return match(this.open) + } textblockFromContext() { let $context = this.options.context