From 3e328561fd883f74862c4dce064e90e34b7cbdfc Mon Sep 17 00:00:00 2001 From: taoqf Date: Tue, 27 Oct 2020 13:47:08 +0800 Subject: [PATCH] add option blockTextElements #78 --- README.md | 9 +++++--- src/nodes/html.ts | 53 ++++++++++++++++++++++++++++++----------------- test/html.js | 2 +- test/pre.js | 41 +++++++++++++++++++++++++++++++++++- 4 files changed, 81 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 2774977..22bddb0 100644 --- a/README.md +++ b/README.md @@ -74,10 +74,13 @@ Parse given data, and return root of the generated DOM. ```js { lowerCaseTagName: false, // convert tag name to lower case (hurt performance heavily) - script: true, // retrieve content in or ... const closeMarkup = ``; const index = (() => { @@ -737,7 +752,7 @@ export function parse(data: string, options = { pre: true, style: true, script: } return data.indexOf(closeMarkup, kMarkupPattern.lastIndex); })(); - if (options[match[2]]) { + if (element_should_be_ignore(match[2])) { let text: string; if (index === -1) { // there is no matching ending for the text element. diff --git a/test/html.js b/test/html.js index 3a04886..8231b00 100644 --- a/test/html.js +++ b/test/html.js @@ -539,7 +539,7 @@ describe('HTML Parser', function () { it('set content pre', function () { const root = parseHTML(``); const body = root.querySelector("body"); - body.set_content(`
this    is some    preformatted    text
`, { pre: true }); + body.set_content(`
this    is some    preformatted    text
`); root.toString().should.eql('
this    is some    preformatted    text
') }); }); diff --git a/test/pre.js b/test/pre.js index d0a28d7..8ea275d 100644 --- a/test/pre.js +++ b/test/pre.js @@ -9,7 +9,46 @@ describe('pre tag', function () { `; - const root = parse(html); + const root = parse(html, { + blockTextElements: { + script: true, + noscript: true, + style: true, + pre: true + } + }); root.toString().should.eql(html); }); + it('should ignore pre tag', function () { + const html = ` +
+
print('hello')
i = i + 1
+
+
+ `; + const root = parse(html, { + blockTextElements: { + pre: false + } + }); + root.toString().should.eql(` +
+

+      
+
+ `); + }); + it('do not treat pre as text block element', function () { + const html = `
print('hello')
i=i+1
+
+
+ `; + const root = parse(html, { + blockTextElements: {} + }); + const div = root.firstChild.firstChild; + const pre = div.firstChild; + const code = pre.firstChild; + code.childNodes.length.should.eql(11); + }); });