Skip to content

Commit 2593be5

Browse files
committed
fix: blockTextElements incorrectly matching partial tag (detail) (fixes taoqf#124)
Tags 'premises' is matched as 'pre', 'pstyle' as 'style', etc.
1 parent f45dc46 commit 2593be5

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
@@ -974,8 +974,8 @@ export function base_parse(data: string, options = { lowerCaseTagName: false, co
974974
pre: true,
975975
};
976976
const element_names = Object.keys(elements);
977-
const kBlockTextElements = element_names.map((it) => new RegExp(it, 'i'));
978-
const kIgnoreElements = element_names.filter((it) => elements[it]).map((it) => new RegExp(it, 'i'));
977+
const kBlockTextElements = element_names.map((it) => new RegExp(`^${it}$`, 'i'));
978+
const kIgnoreElements = element_names.filter((it) => elements[it]).map((it) => new RegExp(`^${it}$`, 'i'));
979979

980980
function element_should_be_ignore(tag: string) {
981981
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)