Skip to content

Commit

Permalink
fix: blockTextElements incorrectly matching partial tag (detail) (fixes
Browse files Browse the repository at this point in the history
#156 fixes #124)

Tags 'premises' is matched as 'pre', 'pstyle' as 'style', etc.
  • Loading branch information
nonara authored and Ron S committed Oct 10, 2021
1 parent 9a9226c commit 6823349
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/nodes/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1019,8 +1019,8 @@ export function base_parse(data: string, options = { lowerCaseTagName: false, co
pre: true,
};
const element_names = Object.keys(elements);
const kBlockTextElements = element_names.map((it) => new RegExp(it, 'i'));
const kIgnoreElements = element_names.filter((it) => elements[it]).map((it) => new RegExp(it, 'i'));
const kBlockTextElements = element_names.map((it) => new RegExp(`^${it}$`, 'i'));
const kIgnoreElements = element_names.filter((it) => elements[it]).map((it) => new RegExp(`^${it}$`, 'i'));

function element_should_be_ignore(tag: string) {
return kIgnoreElements.some((it) => it.test(tag));
Expand Down
7 changes: 7 additions & 0 deletions test/pre.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { parse } = require('../dist');
const { HTMLElement } = require('../dist');

// https://github.com/taoqf/node-html-parser/issues/77
describe('pre tag', function () {
Expand Down Expand Up @@ -51,4 +52,10 @@ describe('pre tag', function () {
const code = pre.firstChild;
code.childNodes.length.should.eql(11);
});
// see: https://github.com/taoqf/node-html-parser/issues/156
it('does not treat pre* tag as pre (partial match)', () => {
const docRoot = parse("<premises><color>Red</color></premises>");
Object.getPrototypeOf(docRoot.firstChild.firstChild).should.eql(HTMLElement.prototype);
docRoot.firstChild.firstChild.tagName.should.eql('COLOR');
})
});

0 comments on commit 6823349

Please sign in to comment.