Skip to content

Commit 01ff9f4

Browse files
committed
fix: #248 void tag
1 parent de57171 commit 01ff9f4

File tree

5 files changed

+38
-51
lines changed

5 files changed

+38
-51
lines changed

src/nodes/html.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -912,36 +912,6 @@ export default class HTMLElement extends Node {
912912
// https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
913913
const kMarkupPattern = /<!--[\s\S]*?-->|<(\/?)([a-zA-Z][-.:0-9_a-zA-Z]*)((?:\s+[^>]*?(?:(?:'[^']*')|(?:"[^"]*"))?)*)\s*(\/?)>/g;
914914
const kAttributePattern = /(?:^|\s)(id|class)\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+)/gi;
915-
const kSelfClosingElements = {
916-
area: true,
917-
AREA: true,
918-
base: true,
919-
BASE: true,
920-
br: true,
921-
BR: true,
922-
col: true,
923-
COL: true,
924-
hr: true,
925-
HR: true,
926-
img: true,
927-
IMG: true,
928-
input: true,
929-
INPUT: true,
930-
link: true,
931-
LINK: true,
932-
meta: true,
933-
META: true,
934-
source: true,
935-
SOURCE: true,
936-
embed: true,
937-
EMBED: true,
938-
param: true,
939-
PARAM: true,
940-
track: true,
941-
TRACK: true,
942-
wbr: true,
943-
WBR: true,
944-
} as Record<string, boolean>;
945915
const kElementsClosedByOpening = {
946916
li: { li: true, LI: true },
947917
LI: { li: true, LI: true },
@@ -1150,7 +1120,7 @@ export function base_parse(data: string, options = {} as Partial<Options>) {
11501120
}
11511121

11521122
// Handle closing tags or self-closed elements (ie </tag> or <br>)
1153-
if (leadingSlash || closingSlash || kSelfClosingElements[tagName]) {
1123+
if (leadingSlash || closingSlash || voidTag.isVoidElement(tagName)) {
11541124
while (true) {
11551125
if (noNestedTagIndex != null && (tagName === 'a' || tagName === 'A')) noNestedTagIndex = undefined;
11561126
if (currentParent.rawTagName === tagName) {

src/void-tag.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export default class VoidTag {
66
) {
77
if (Array.isArray(tags)) {
88
this.voidTags = tags.reduce((set, tag) => {
9-
return set.add(tag.toLowerCase());
9+
return set.add(tag.toLowerCase()).add(tag.toUpperCase()).add(tag);
1010
}, new Set<string>());
1111
} else {
1212
this.voidTags = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'].reduce((set, tag) => {
13-
return set.add(tag);
13+
return set.add(tag.toLowerCase()).add(tag.toUpperCase()).add(tag);
1414
}, new Set<string>());
1515
}
1616
}

test/tests/issues/242.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { parse } = require('@test/test-target');
22

3-
describe.only('issue 242', function () {
3+
describe('issue 242', function () {
44
it(`a.rawAttrs returns 'href="/" rel="home"' but a.getAttribute("href') returns undefined`, function () {
55
const html = `<div><a href="/" rel="home">Git Hub</a></div>`;
66
const root = parse(html);

test/tests/issues/248.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { valid } = require('@test/test-target');
2+
3+
describe('issue 248', function () {
4+
it('void tag', function () {
5+
const voidTags = ['x-tag'];
6+
const html = `<div><x-tag></div>`;
7+
// => false. Expected to be true
8+
valid(html, {
9+
voidTag: {
10+
tags: voidTags,
11+
}
12+
}).should.eql(true);
13+
});
14+
it('selfclosed tag', function () {
15+
const voidTags = ['x-tag'];
16+
const html = `<div><x-tag /></div>`;
17+
// => false. Expected to be true
18+
valid(html, {
19+
voidTag: {
20+
tags: voidTags,
21+
closingSlash: true
22+
}
23+
}).should.eql(true);
24+
});
25+
});

test/tests/pre.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,15 @@ describe('pre tag', function () {
2121
root.toString().should.eql(html);
2222
});
2323
it('should ignore pre tag', function () {
24-
const html = `
25-
<div class="language-python highlighter-rouge">
26-
<div class="highlight"> <pre class="highlight"><code><span class="k">print</span><span class="p">(</span><span class="s">'hello'</span><span class="p">)</span><br><span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><br></code></pre>
27-
</div>
28-
</div>
29-
`;
24+
const html = `<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">print</span><span class="p">(</span><span class="s">'hello'</span><span class="p">)</span><br><span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><br></code></pre></div></div>`;
3025
const root = parse(html, {
3126
blockTextElements: {
32-
pre: false
27+
pre: true
3328
}
3429
});
35-
root.toString().should.eql(`
36-
<div class="language-python highlighter-rouge">
37-
<div class="highlight"> <pre class="highlight"></pre>
38-
</div>
39-
</div>
40-
`);
30+
const div = root.firstChild.firstChild;
31+
const pre = div.firstChild;
32+
pre.text.should.eql(`<code><span class="k">print</span><span class="p">(</span><span class="s">'hello'</span><span class="p">)</span><br><span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><br></code>`);
4133
});
4234
it('do not treat pre as text block element', function () {
4335
const html = `<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">print</span><span class="p">(</span><span class="s">'hello'</span><span class="p">)</span><br><span class="n">i</span><span class="o">=</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><br></code></pre>
@@ -54,8 +46,8 @@ describe('pre tag', function () {
5446
});
5547
// see: https://github.com/taoqf/node-html-parser/issues/156
5648
it('does not treat pre* tag as pre (partial match)', () => {
57-
const docRoot = parse("<premises><color>Red</color></premises>");
58-
Object.getPrototypeOf(docRoot.firstChild.firstChild).should.eql(HTMLElement.prototype);
59-
docRoot.firstChild.firstChild.tagName.should.eql('COLOR');
60-
})
49+
const docRoot = parse("<premises><color>Red</color></premises>");
50+
Object.getPrototypeOf(docRoot.firstChild.firstChild).should.eql(HTMLElement.prototype);
51+
docRoot.firstChild.firstChild.tagName.should.eql('COLOR');
52+
})
6153
});

0 commit comments

Comments
 (0)