From cac41df37728ee36c7467c0138ea0d6d256c12ff Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Thu, 9 Mar 2017 13:25:53 +0800 Subject: [PATCH 1/2] fix #5121: parse content in textarea as plaintext --- src/compiler/parser/html-parser.js | 6 +++--- test/unit/modules/compiler/parser.spec.js | 25 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/compiler/parser/html-parser.js b/src/compiler/parser/html-parser.js index 7d444ece141..1e6a2c33354 100644 --- a/src/compiler/parser/html-parser.js +++ b/src/compiler/parser/html-parser.js @@ -46,7 +46,7 @@ let IS_REGEX_CAPTURING_BROKEN = false }) // Special Elements (can contain anything) -const isScriptOrStyle = makeMap('script,style', true) +const isPlainTextElement = makeMap('script,style,textarea', true) const reCache = {} const decodingMap = { @@ -73,7 +73,7 @@ export function parseHTML (html, options) { while (html) { last = html // Make sure we're not in a script or style element - if (!lastTag || !isScriptOrStyle(lastTag)) { + if (!lastTag || !isPlainTextElement(lastTag)) { let textEnd = html.indexOf('<') if (textEnd === 0) { // Comment: @@ -153,7 +153,7 @@ export function parseHTML (html, options) { var endTagLength = 0 var rest = html.replace(reStackedTag, function (all, text, endTag) { endTagLength = endTag.length - if (stackedTag !== 'script' && stackedTag !== 'style' && stackedTag !== 'noscript') { + if (!isPlainTextElement(stackedTag) && stackedTag !== 'noscript') { text = text .replace(//g, '$1') .replace(//g, '$1') diff --git a/test/unit/modules/compiler/parser.spec.js b/test/unit/modules/compiler/parser.spec.js index 06e3ffd88da..adeace751ef 100644 --- a/test/unit/modules/compiler/parser.spec.js +++ b/test/unit/modules/compiler/parser.spec.js @@ -506,4 +506,29 @@ describe('parser', () => { expect(ast.tag).toBe('div') expect(ast.children.length).toBe(0) }) + + it('parse content in textarea as text', () => { + const options = extend({}, baseOptions) + + const whitespace = parse(` + + `, options) + expect(whitespace.tag).toBe('textarea') + expect(whitespace.children.length).toBe(1) + expect(whitespace.children[0].type).toBe(3) + // textarea is whitespace sensitive + expect(whitespace.children[0].text).toBe(` +

Test 1

+ test2 + `) + + const comment = parse('', options) + expect(comment.tag).toBe('textarea') + expect(comment.children.length).toBe(1) + expect(comment.children[0].type).toBe(3) + expect(comment.children[0].text).toBe('') + }) }) From 70fa877994a7797afad0e69e6655a265b948f770 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Thu, 9 Mar 2017 13:30:35 +0800 Subject: [PATCH 2/2] update comment --- src/compiler/parser/html-parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/parser/html-parser.js b/src/compiler/parser/html-parser.js index 1e6a2c33354..c5be75d694c 100644 --- a/src/compiler/parser/html-parser.js +++ b/src/compiler/parser/html-parser.js @@ -72,7 +72,7 @@ export function parseHTML (html, options) { let last, lastTag while (html) { last = html - // Make sure we're not in a script or style element + // Make sure we're not in a plaintext content element like script/style if (!lastTag || !isPlainTextElement(lastTag)) { let textEnd = html.indexOf('<') if (textEnd === 0) {