+30
-6
lines changedFilter options
+30
-6
lines changed Original file line number Diff line number Diff line change
@@ -59,6 +59,10 @@ const decodingMap = {
59
59
const encodedAttr = /&(?:lt|gt|quot|amp);/g
60
60
const encodedAttrWithNewLines = /&(?:lt|gt|quot|amp|#10);/g
61
61
62
+
// #5992
63
+
const isIgnoreNewlineTag = makeMap('pre,textarea', true)
64
+
const shouldIgnoreFirstNewline = (tag, html) => tag && isIgnoreNewlineTag(tag) && html[0] === '\n'
65
+
62
66
function decodeAttr (value, shouldDecodeNewlines) {
63
67
const re = shouldDecodeNewlines ? encodedAttrWithNewLines : encodedAttr
64
68
return value.replace(re, match => decodingMap[match])
@@ -75,6 +79,9 @@ export function parseHTML (html, options) {
75
79
last = html
76
80
// Make sure we're not in a plaintext content element like script/style
77
81
if (!lastTag || !isPlainTextElement(lastTag)) {
82
+
if (shouldIgnoreFirstNewline(lastTag, html)) {
83
+
advance(1)
84
+
}
78
85
let textEnd = html.indexOf('<')
79
86
if (textEnd === 0) {
80
87
// Comment:
@@ -152,16 +159,19 @@ export function parseHTML (html, options) {
152
159
options.chars(text)
153
160
}
154
161
} else {
155
-
var stackedTag = lastTag.toLowerCase()
156
-
var reStackedTag = reCache[stackedTag] || (reCache[stackedTag] = new RegExp('([\\s\\S]*?)(</' + stackedTag + '[^>]*>)', 'i'))
157
-
var endTagLength = 0
158
-
var rest = html.replace(reStackedTag, function (all, text, endTag) {
162
+
let endTagLength = 0
163
+
const stackedTag = lastTag.toLowerCase()
164
+
const reStackedTag = reCache[stackedTag] || (reCache[stackedTag] = new RegExp('([\\s\\S]*?)(</' + stackedTag + '[^>]*>)', 'i'))
165
+
const rest = html.replace(reStackedTag, function (all, text, endTag) {
159
166
endTagLength = endTag.length
160
167
if (!isPlainTextElement(stackedTag) && stackedTag !== 'noscript') {
161
168
text = text
162
169
.replace(/<!--([\s\S]*?)-->/g, '$1')
163
170
.replace(/<!\[CDATA\[([\s\S]*?)]]>/g, '$1')
164
171
}
172
+
if (shouldIgnoreFirstNewline(stackedTag, text)) {
173
+
text = text.slice(1)
174
+
}
165
175
if (options.chars) {
166
176
options.chars(text)
167
177
}
Original file line number Diff line number Diff line change
@@ -495,6 +495,21 @@ describe('parser', () => {
495
495
expect(span.children[0].text).toBe(' ')
496
496
})
497
497
498
+
// #5992
499
+
it('ignore the first newline in <pre> tag', function () {
500
+
const options = extend({}, baseOptions)
501
+
const ast = parse('<div><pre>\nabc</pre>\ndef<pre>\n\nabc</pre></div>', options)
502
+
const pre = ast.children[0]
503
+
expect(pre.children[0].type).toBe(3)
504
+
expect(pre.children[0].text).toBe('abc')
505
+
const text = ast.children[1]
506
+
expect(text.type).toBe(3)
507
+
expect(text.text).toBe('\ndef')
508
+
const pre2 = ast.children[2]
509
+
expect(pre2.children[0].type).toBe(3)
510
+
expect(pre2.children[0].text).toBe('\nabc')
511
+
})
512
+
498
513
it('forgivingly handle < in plain text', () => {
499
514
const options = extend({}, baseOptions)
500
515
const ast = parse('<p>1 < 2 < 3</p>', options)
@@ -530,8 +545,7 @@ describe('parser', () => {
530
545
expect(whitespace.children.length).toBe(1)
531
546
expect(whitespace.children[0].type).toBe(3)
532
547
// textarea is whitespace sensitive
533
-
expect(whitespace.children[0].text).toBe(`
534
-
<p>Test 1</p>
548
+
expect(whitespace.children[0].text).toBe(` <p>Test 1</p>
535
549
test2
536
550
`)
537
551
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4