Skip to content

Commit 6823349

Browse files
nonaraRon S
authored and
Ron S
committed
fix: blockTextElements incorrectly matching partial tag (detail) (fixes #156 fixes #124)
Tags 'premises' is matched as 'pre', 'pstyle' as 'style', etc.
1 parent 9a9226c commit 6823349

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/nodes/html.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,8 +1019,8 @@ export function base_parse(data: string, options = { lowerCaseTagName: false, co
10191019
pre: true,
10201020
};
10211021
const element_names = Object.keys(elements);
1022-
const kBlockTextElements = element_names.map((it) => new RegExp(it, 'i'));
1023-
const kIgnoreElements = element_names.filter((it) => elements[it]).map((it) => new RegExp(it, 'i'));
1022+
const kBlockTextElements = element_names.map((it) => new RegExp(`^${it}$`, 'i'));
1023+
const kIgnoreElements = element_names.filter((it) => elements[it]).map((it) => new RegExp(`^${it}$`, 'i'));
10241024

10251025
function element_should_be_ignore(tag: string) {
10261026
return kIgnoreElements.some((it) => it.test(tag));

test/pre.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { parse } = require('../dist');
2+
const { HTMLElement } = require('../dist');
23

34
// https://github.com/taoqf/node-html-parser/issues/77
45
describe('pre tag', function () {
@@ -51,4 +52,10 @@ describe('pre tag', function () {
5152
const code = pre.firstChild;
5253
code.childNodes.length.should.eql(11);
5354
});
55+
// see: https://github.com/taoqf/node-html-parser/issues/156
56+
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+
})
5461
});

0 commit comments

Comments
 (0)