Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ParseContext matchTag checks content type #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
: option && depth >= minDepth ? option.node(depth - minDepth)
: null
if (!next) return false
if (next.match ? next.match.matchType(type) : next.type.contentMatch.matchType(type)) return true;
return match(depth - 1)
}
return match(this.open)
}

textblockFromContext() {
let $context = this.options.context
Expand Down