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
76 changes: 72 additions & 4 deletions src/mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,76 @@ const onlyValidChildren = {
'script',
'template',
]),
optgroup: new Set(['option']),
select: new Set(['optgroup', 'option', 'hr', 'button']),
option: new Set([
'div',
'a',
'abbr',
'area',
'audio',
'b',
'bdi',
'bdo',
'br',
'canvas',
'cite',
'code',
'data',
'del',
'dfn',
'em',
'i',
'img',
'input',
'ins',
'kbd',
'link',
'map',
'mark',
'math',
'meta',
'meter',
'noscript',
'output',
'picture',
'progress',
'q',
'ruby',
's',
'samp',
'script',
'slot',
'small',
'span',
'strong',
'sub',
'sup',
'svg',
'template',
'time',
'u',
'var',
'video',
'wbr',
]),
optgroup: new Set([
'option',
'legend',
'script',
'template',
'noscript',
'div',
]),
select: new Set([
'optgroup',
'option',
'hr',
'button',
'script',
'template',
'noscript',
'div',
]),
math: new Set(['mrow']),
script: new Set(),
// table
table: new Set(['caption', 'colgroup', 'tbody', 'tfoot', 'thead']),
tr: new Set(['td', 'th']),
Expand All @@ -29,11 +95,12 @@ const onlyValidChildren = {
thead: new Set(['tr']),
tfoot: new Set(['tr']),
// these elements can not have any children elements
script: emptySet,
iframe: emptySet,
option: emptySet,
textarea: emptySet,
style: emptySet,
title: emptySet,
selectedcontent: emptySet,
};

/** maps elements to set of elements which can be it's parent, no other */
Expand All @@ -60,6 +127,7 @@ const onlyValidParents = {
// li: new Set(["ul", "ol"]),
summary: new Set(['details']),
area: new Set(['map']),
selectedcontent: new Set(['button']),
};

/** maps element to set of elements that can not be it's children, others can */
Expand Down
14 changes: 14 additions & 0 deletions tests/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ test('select', () => {
expect(isValidHTMLNesting('select', 'button')).toBe(true);
});

test('customizable select', () => {
// invalid
expect(isValidHTMLNesting('select', 'selectedcontent')).toBe(false);
expect(isValidHTMLNesting('option', 'button')).toBe(false);

// using example from https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Forms/Customizable_select

// valid
expect(isValidHTMLNesting('select', 'button')).toBe(true);
expect(isValidHTMLNesting('button', 'selectedcontent')).toBe(true);
expect(isValidHTMLNesting('select', 'option')).toBe(true);
expect(isValidHTMLNesting('option', 'span')).toBe(true);
});

test('p', () => {
// invalid
expect(isValidHTMLNesting('p', 'p')).toBe(false);
Expand Down