Skip to content

Commit 7c65a63

Browse files
committed
Add support for options
1 parent a1173bf commit 7c65a63

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2+
* @typedef {import('./lib/index.js').Options} Options
23
* @typedef {import('./lib/index.js').Space} Space
34
*/
45

lib/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@
2121

2222
/**
2323
* @typedef {Content | Root} Node
24-
* @typedef {'html'|'svg'} Space
24+
* @typedef {'html' | 'svg'} Space
25+
*
26+
* @typedef Options
27+
* Configuration.
28+
* @property {Space | null | undefined} [space='html']
29+
* Which space the document is in.
30+
*
31+
* When an `<svg>` element is found in the HTML space, this package already
32+
* automatically switches to and from the SVG space when entering and exiting
33+
* it.
2534
*/
2635

2736
import {stringify as commas} from 'comma-separated-tokens'
@@ -40,12 +49,13 @@ const one = zwitch('type', {handlers: {root, element, text, comment, doctype}})
4049
*
4150
* @param {Node} tree
4251
* Tree to transform.
43-
* @param {Space | null | undefined} [space='html']
52+
* @param {Options | Space | null | undefined} [options]
4453
* Current space.
4554
* @returns {P5Node}
4655
* `parse5` node.
4756
*/
48-
export function toParse5(tree, space) {
57+
export function toParse5(tree, options) {
58+
const space = options && typeof options === 'object' ? options.space : options
4959
return one(tree, space === 'svg' ? svg : html)
5060
}
5161

test/index.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ import stringify from 'json-stringify-safe'
44
import {parse, parseFragment} from 'parse5'
55
import {toParse5} from '../index.js'
66

7+
test('core', () => {
8+
assert.deepEqual(
9+
json(
10+
toParse5({
11+
type: 'root',
12+
children: [
13+
{type: 'doctype', name: 'html'},
14+
{
15+
type: 'element',
16+
tagName: 'html',
17+
children: [
18+
{type: 'element', tagName: 'head', children: []},
19+
{type: 'element', tagName: 'body', children: []}
20+
]
21+
}
22+
]
23+
})
24+
),
25+
json(parse('<!doctypehtml>')),
26+
'should transform a root (no-quirks)'
27+
)
28+
})
29+
730
test('root', () => {
831
assert.deepEqual(
932
json(
@@ -356,7 +379,31 @@ test('svg', () => {
356379
)
357380
),
358381
json(expectedSvgChild),
359-
'should transform SVG'
382+
'should transform SVG (given a space)'
383+
)
384+
385+
const expectedSvgOptions = parseFragment(
386+
'<svg><circle cx="60" cy="60" r="50" fill="red"/></svg>'
387+
).childNodes[0]
388+
assert(expectedSvgOptions && 'childNodes' in expectedSvg)
389+
const expectedSvgOptionsChild = expectedSvg.childNodes[0]
390+
// @ts-expect-error: p5 wants `null`.
391+
expectedSvgChild.parentNode = undefined
392+
393+
assert.deepEqual(
394+
json(
395+
toParse5(
396+
{
397+
type: 'element',
398+
tagName: 'circle',
399+
properties: {cx: '60', cy: '60', r: '50', fill: 'red'},
400+
children: []
401+
},
402+
{space: 'svg'}
403+
)
404+
),
405+
json(expectedSvgOptionsChild),
406+
'should transform SVG (given options)'
360407
)
361408
})
362409

0 commit comments

Comments
 (0)