Skip to content

Commit

Permalink
ParseContext matchTag checks content type
Browse files Browse the repository at this point in the history
  • Loading branch information
kapouer committed Jul 15, 2019
1 parent 1d1c0c1 commit 3c41ed9
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/from_dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3c41ed9

Please sign in to comment.