Skip to content

Commit

Permalink
Remove non-elements from handlers
Browse files Browse the repository at this point in the history
Previously, nodes could be handled by their `type`.
In combination with elements by their `tagName`.
This could result in bugs if for example `<root>`, `<comment>`, or
`<text>` or so was used.

Now, `handlers` only supports elements by their tag name, and a new
`nodeHandlers` exists for (custom) nodes.
  • Loading branch information
wooorm committed Jan 12, 2023
1 parent f3de1d3 commit 1f2dfb5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/**
* @typedef {import('./lib/state.js').State} State
* @typedef {import('./lib/types.js').Handle} Handle
* @typedef {import('./lib/types.js').NodeHandle} NodeHandle
* @typedef {import('./lib/types.js').Options} Options
*/

export {defaultHandlers, toMdast} from './lib/index.js'
export {toMdast} from './lib/index.js'

export {
handlers as defaultHandlers,
nodeHandlers as defaultNodeHandlers
} from './lib/handlers/index.js'
7 changes: 4 additions & 3 deletions lib/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ import {text} from './text.js'
import {textarea} from './textarea.js'
import {wbr} from './wbr.js'

export const handlers = {
// To do: move node types to a different map.
export const nodeHandlers = {
root,
text,
comment,
doctype: ignore,
doctype: ignore
}

export const handlers = {
applet: ignore,
area: ignore,
basefont: ignore,
Expand Down
2 changes: 0 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,3 @@ export function toMdast(tree, options) {

return mdast
}

export {handlers as defaultHandlers} from './handlers/index.js'
23 changes: 10 additions & 13 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @typedef {import('mdast').PhrasingContent} MdastPhrasingContent
* @typedef {import('./types.js').Options} Options
* @typedef {import('./types.js').Handle} Handle
* @typedef {import('./types.js').NodeHandle} NodeHandle
*/

/**
Expand Down Expand Up @@ -43,7 +44,9 @@
* @property {Map<string, Element>} elementById
* Elements by their `id`.
* @property {Record<string, Handle>} handlers
* Applied handlers.
* Applied element handlers.
* @property {Record<string, NodeHandle>} nodeHandlers
* Applied node handlers.
* @property {boolean} baseFound
* Whether a `<base>` element was seen.
* @property {string | undefined} frozenBaseUrl
Expand Down Expand Up @@ -94,7 +97,7 @@
*/

import {position} from 'unist-util-position'
import {handlers} from './handlers/index.js'
import {handlers, nodeHandlers} from './handlers/index.js'
import {wrap} from './util/wrap.js'

const own = {}.hasOwnProperty
Expand All @@ -118,7 +121,8 @@ export function createState(options) {
toSpecificContent,
options,
elementById: new Map(),
handlers: options.handlers ? {...handlers, ...options.handlers} : handlers,
nodeHandlers: {...nodeHandlers, ...options.nodeHandlers},
handlers: {...handlers, ...options.handlers},
baseFound: false,
inTable: false,
frozenBaseUrl: undefined,
Expand Down Expand Up @@ -155,23 +159,16 @@ function patch(origin, node) {
* mdast result.
*/
function one(node, parent) {
/** @type {Handle | undefined} */
let fn

if (node.type === 'element') {
if (node.properties && node.properties.dataMdast === 'ignore') {
return
}

if (own.call(this.handlers, node.tagName)) {
fn = this.handlers[node.tagName]
return this.handlers[node.tagName](this, node, parent)
}
} else if (own.call(this.handlers, node.type)) {
fn = this.handlers[node.type]
}

if (typeof fn === 'function') {
return fn(this, node, parent)
} else if (own.call(this.nodeHandlers, node.type)) {
return this.nodeHandlers[node.type](this, node, parent)
}

// Unknown literal.
Expand Down
15 changes: 9 additions & 6 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* @typedef {import('mdast').Content} MdastContent
* @typedef {import('mdast').Root} MdastRoot
* @typedef {import('hast').Content} Content
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Root} Root
* @typedef {import('hast').Element} Element
* @typedef {import('./state.js').State} State
*/

Expand All @@ -13,10 +13,12 @@
* @typedef {Content | Root} Node
* @typedef {Extract<Node, UnistParent>} Parent
*
* @typedef {(state: State, node: any, parent?: Parent) => MdastNode | Array<MdastNode> | void} Handle
* @typedef {(state: State, node: any, parent: Parent | undefined) => MdastNode | Array<MdastNode> | void} NodeHandle
*
* @typedef {(state: State, node: Element, parent: Parent | undefined) => MdastNode | Array<MdastNode> | void} Handle
*
* @typedef Options
* Configuration (optional).
* Configuration.
* @property {boolean | null | undefined} [newlines=false]
* Keep line endings when collapsing whitespace.
*
Expand Down Expand Up @@ -48,9 +50,10 @@
* paragraphs when needed, and otherwise they’re left as-is.
* The default checks for whether there’s mixed content: some phrasing nodes
* *and* some non-phrasing nodes.
* @property {Record<string, Handle> | null | undefined} [handlers]
* Object mapping tag names or node types to functions handling the
* corresponding nodes.
* @property {Record<string, Handle | null | undefined> | null | undefined} [handlers]
* Object mapping tag names to functions handling the corresponding elements.
* @property {Record<string, NodeHandle | null | undefined> | null | undefined} [nodeHandlers]
* Object mapping node types to functions handling the corresponding nodes.
*/

export {}

0 comments on commit 1f2dfb5

Please sign in to comment.