Skip to content

Commit 8fca83d

Browse files
HerringtonDarkholmeyyx990803
authored andcommitted
fix #5121: parse content in textarea as plaintext (#5143)
* fix #5121: parse content in textarea as plaintext * update comment
1 parent 82bc8b7 commit 8fca83d

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/compiler/parser/html-parser.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let IS_REGEX_CAPTURING_BROKEN = false
4646
})
4747

4848
// Special Elements (can contain anything)
49-
const isScriptOrStyle = makeMap('script,style', true)
49+
const isPlainTextElement = makeMap('script,style,textarea', true)
5050
const reCache = {}
5151

5252
const decodingMap = {
@@ -72,8 +72,8 @@ export function parseHTML (html, options) {
7272
let last, lastTag
7373
while (html) {
7474
last = html
75-
// Make sure we're not in a script or style element
76-
if (!lastTag || !isScriptOrStyle(lastTag)) {
75+
// Make sure we're not in a plaintext content element like script/style
76+
if (!lastTag || !isPlainTextElement(lastTag)) {
7777
let textEnd = html.indexOf('<')
7878
if (textEnd === 0) {
7979
// Comment:
@@ -153,7 +153,7 @@ export function parseHTML (html, options) {
153153
var endTagLength = 0
154154
var rest = html.replace(reStackedTag, function (all, text, endTag) {
155155
endTagLength = endTag.length
156-
if (stackedTag !== 'script' && stackedTag !== 'style' && stackedTag !== 'noscript') {
156+
if (!isPlainTextElement(stackedTag) && stackedTag !== 'noscript') {
157157
text = text
158158
.replace(/<!--([\s\S]*?)-->/g, '$1')
159159
.replace(/<!\[CDATA\[([\s\S]*?)]]>/g, '$1')

test/unit/modules/compiler/parser.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,29 @@ describe('parser', () => {
506506
expect(ast.tag).toBe('div')
507507
expect(ast.children.length).toBe(0)
508508
})
509+
510+
it('parse content in textarea as text', () => {
511+
const options = extend({}, baseOptions)
512+
513+
const whitespace = parse(`
514+
<textarea>
515+
<p>Test 1</p>
516+
test2
517+
</textarea>
518+
`, options)
519+
expect(whitespace.tag).toBe('textarea')
520+
expect(whitespace.children.length).toBe(1)
521+
expect(whitespace.children[0].type).toBe(3)
522+
// textarea is whitespace sensitive
523+
expect(whitespace.children[0].text).toBe(`
524+
<p>Test 1</p>
525+
test2
526+
`)
527+
528+
const comment = parse('<textarea><!--comment--></textarea>', options)
529+
expect(comment.tag).toBe('textarea')
530+
expect(comment.children.length).toBe(1)
531+
expect(comment.children[0].type).toBe(3)
532+
expect(comment.children[0].text).toBe('<!--comment-->')
533+
})
509534
})

0 commit comments

Comments
 (0)