Skip to content

Commit 1f2dfb5

Browse files
committed
Remove non-elements from handlers
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.
1 parent f3de1d3 commit 1f2dfb5

File tree

5 files changed

+30
-25
lines changed

5 files changed

+30
-25
lines changed

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
/**
22
* @typedef {import('./lib/state.js').State} State
33
* @typedef {import('./lib/types.js').Handle} Handle
4+
* @typedef {import('./lib/types.js').NodeHandle} NodeHandle
45
* @typedef {import('./lib/types.js').Options} Options
56
*/
67

7-
export {defaultHandlers, toMdast} from './lib/index.js'
8+
export {toMdast} from './lib/index.js'
9+
10+
export {
11+
handlers as defaultHandlers,
12+
nodeHandlers as defaultNodeHandlers
13+
} from './lib/handlers/index.js'

lib/handlers/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ import {text} from './text.js'
3939
import {textarea} from './textarea.js'
4040
import {wbr} from './wbr.js'
4141

42-
export const handlers = {
43-
// To do: move node types to a different map.
42+
export const nodeHandlers = {
4443
root,
4544
text,
4645
comment,
47-
doctype: ignore,
46+
doctype: ignore
47+
}
4848

49+
export const handlers = {
4950
applet: ignore,
5051
area: ignore,
5152
basefont: ignore,

lib/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,3 @@ export function toMdast(tree, options) {
108108

109109
return mdast
110110
}
111-
112-
export {handlers as defaultHandlers} from './handlers/index.js'

lib/state.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @typedef {import('mdast').PhrasingContent} MdastPhrasingContent
1010
* @typedef {import('./types.js').Options} Options
1111
* @typedef {import('./types.js').Handle} Handle
12+
* @typedef {import('./types.js').NodeHandle} NodeHandle
1213
*/
1314

1415
/**
@@ -43,7 +44,9 @@
4344
* @property {Map<string, Element>} elementById
4445
* Elements by their `id`.
4546
* @property {Record<string, Handle>} handlers
46-
* Applied handlers.
47+
* Applied element handlers.
48+
* @property {Record<string, NodeHandle>} nodeHandlers
49+
* Applied node handlers.
4750
* @property {boolean} baseFound
4851
* Whether a `<base>` element was seen.
4952
* @property {string | undefined} frozenBaseUrl
@@ -94,7 +97,7 @@
9497
*/
9598

9699
import {position} from 'unist-util-position'
97-
import {handlers} from './handlers/index.js'
100+
import {handlers, nodeHandlers} from './handlers/index.js'
98101
import {wrap} from './util/wrap.js'
99102

100103
const own = {}.hasOwnProperty
@@ -118,7 +121,8 @@ export function createState(options) {
118121
toSpecificContent,
119122
options,
120123
elementById: new Map(),
121-
handlers: options.handlers ? {...handlers, ...options.handlers} : handlers,
124+
nodeHandlers: {...nodeHandlers, ...options.nodeHandlers},
125+
handlers: {...handlers, ...options.handlers},
122126
baseFound: false,
123127
inTable: false,
124128
frozenBaseUrl: undefined,
@@ -155,23 +159,16 @@ function patch(origin, node) {
155159
* mdast result.
156160
*/
157161
function one(node, parent) {
158-
/** @type {Handle | undefined} */
159-
let fn
160-
161162
if (node.type === 'element') {
162163
if (node.properties && node.properties.dataMdast === 'ignore') {
163164
return
164165
}
165166

166167
if (own.call(this.handlers, node.tagName)) {
167-
fn = this.handlers[node.tagName]
168+
return this.handlers[node.tagName](this, node, parent)
168169
}
169-
} else if (own.call(this.handlers, node.type)) {
170-
fn = this.handlers[node.type]
171-
}
172-
173-
if (typeof fn === 'function') {
174-
return fn(this, node, parent)
170+
} else if (own.call(this.nodeHandlers, node.type)) {
171+
return this.nodeHandlers[node.type](this, node, parent)
175172
}
176173

177174
// Unknown literal.

lib/types.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* @typedef {import('mdast').Content} MdastContent
44
* @typedef {import('mdast').Root} MdastRoot
55
* @typedef {import('hast').Content} Content
6-
* @typedef {import('hast').Element} Element
76
* @typedef {import('hast').Root} Root
7+
* @typedef {import('hast').Element} Element
88
* @typedef {import('./state.js').State} State
99
*/
1010

@@ -13,10 +13,12 @@
1313
* @typedef {Content | Root} Node
1414
* @typedef {Extract<Node, UnistParent>} Parent
1515
*
16-
* @typedef {(state: State, node: any, parent?: Parent) => MdastNode | Array<MdastNode> | void} Handle
16+
* @typedef {(state: State, node: any, parent: Parent | undefined) => MdastNode | Array<MdastNode> | void} NodeHandle
17+
*
18+
* @typedef {(state: State, node: Element, parent: Parent | undefined) => MdastNode | Array<MdastNode> | void} Handle
1719
*
1820
* @typedef Options
19-
* Configuration (optional).
21+
* Configuration.
2022
* @property {boolean | null | undefined} [newlines=false]
2123
* Keep line endings when collapsing whitespace.
2224
*
@@ -48,9 +50,10 @@
4850
* paragraphs when needed, and otherwise they’re left as-is.
4951
* The default checks for whether there’s mixed content: some phrasing nodes
5052
* *and* some non-phrasing nodes.
51-
* @property {Record<string, Handle> | null | undefined} [handlers]
52-
* Object mapping tag names or node types to functions handling the
53-
* corresponding nodes.
53+
* @property {Record<string, Handle | null | undefined> | null | undefined} [handlers]
54+
* Object mapping tag names to functions handling the corresponding elements.
55+
* @property {Record<string, NodeHandle | null | undefined> | null | undefined} [nodeHandlers]
56+
* Object mapping node types to functions handling the corresponding nodes.
5457
*/
5558

5659
export {}

0 commit comments

Comments
 (0)