From 5a0a79573d51da9eccf6d13647413447eba413b1 Mon Sep 17 00:00:00 2001 From: taoqf Date: Fri, 8 Sep 2023 16:17:31 +0800 Subject: [PATCH] fix: #249 --- src/nodes/html.ts | 4 ++++ test/tests/issues/249.js | 11 +++++++++++ 2 files changed, 15 insertions(+) create mode 100644 test/tests/issues/249.js diff --git a/src/nodes/html.ts b/src/nodes/html.ts index 853f02c..20a86e0 100644 --- a/src/nodes/html.ts +++ b/src/nodes/html.ts @@ -248,6 +248,10 @@ export default class HTMLElement extends Node { * @return {string} text content */ public get rawText() { + // https://github.com/taoqf/node-html-parser/issues/249 + if (/br/i.test(this.rawTagName)) { + return '\n'; + } return this.childNodes.reduce((pre, cur) => { return (pre += cur.rawText); }, ''); diff --git a/test/tests/issues/249.js b/test/tests/issues/249.js new file mode 100644 index 0000000..3c4c1f2 --- /dev/null +++ b/test/tests/issues/249.js @@ -0,0 +1,11 @@ +const { parse } = require('@test/test-target'); + +describe.only('issue 244', function () { + it('br in innertext should turn into \\n', function () { + const html = `
Hello
World
`; + const root = parse(html); + const div = root.querySelector('div'); + div.innerText.should.eql(`Hello +World`); + }); +});