Skip to content
Open
Show file tree
Hide file tree
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
32 changes: 22 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @import {ElementData, Element, Nodes, RootContent, Root} from 'hast'
* @import {ElementContent, ElementData, Element, Nodes, RootContent, Root} from 'hast'
* @import {DefaultTreeAdapterMap, Token} from 'parse5'
* @import {Schema} from 'property-information'
* @import {Point, Position} from 'unist'
Expand Down Expand Up @@ -196,18 +196,30 @@ function element(state, node) {
// Switch content.
if (result.tagName === 'template') {
const reference = /** @type {DefaultTreeAdapterMap['template']} */ (node)
const pos = reference.sourceCodeLocation
const startTag = pos && pos.startTag && position(pos.startTag)
const endTag = pos && pos.endTag && position(pos.endTag)

// Root in, root out.
const content = /** @type {Root} */ (one(state, reference.content))
// `template` in the SVG or MathML namespace does not get `content` from
// `parse5`, as that only applies to the HTML namespace: undo the
// `content` that `hastscript` always adds for `template`s.
if (reference.content) {
const pos = reference.sourceCodeLocation
const startTag = pos && pos.startTag && position(pos.startTag)
const endTag = pos && pos.endTag && position(pos.endTag)

if (startTag && endTag && state.file) {
content.position = {start: startTag.end, end: endTag.start}
}
// Root in, root out.
const content = /** @type {Root} */ (one(state, reference.content))

if (startTag && endTag && state.file) {
content.position = {start: startTag.end, end: endTag.start}
}

result.content = content
result.content = content
} else {
const placeholder = /** @type {Root} */ (result.content)
result.children = /** @type {Array<ElementContent>} */ (
placeholder.children
)
delete result.content
}
}

state.schema = schema
Expand Down
45 changes: 45 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,51 @@ test('fromParse5', async function (t) {
)
})

await t.test(
'should not crash on a `template` in the svg or mathml namespace',
async function () {
assert.deepEqual(fromParse5(parseFragment('<svg><template>')), {
type: 'root',
children: [
{
type: 'element',
tagName: 'svg',
properties: {},
children: [
{
type: 'element',
tagName: 'template',
properties: {},
children: []
}
]
}
],
data: {quirksMode: false}
})

assert.deepEqual(fromParse5(parseFragment('<math><template>')), {
type: 'root',
children: [
{
type: 'element',
tagName: 'math',
properties: {},
children: [
{
type: 'element',
tagName: 'template',
properties: {},
children: []
}
]
}
],
data: {quirksMode: false}
})
}
)

await t.test('should ignore prototypal props', async function () {
assert.deepEqual(
fromParse5(parseFragment('<x constructor y />'), {space: 'svg'}),
Expand Down
Loading