Skip to content

fix #5121: parse content in textarea as plaintext #5143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/compiler/parser/html-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -72,8 +72,8 @@ export function parseHTML (html, options) {
let last, lastTag
while (html) {
last = html
// Make sure we're not in a script or style element
if (!lastTag || !isScriptOrStyle(lastTag)) {
// Make sure we're not in a plaintext content element like script/style
if (!lastTag || !isPlainTextElement(lastTag)) {
let textEnd = html.indexOf('<')
if (textEnd === 0) {
// Comment:
Expand Down Expand Up @@ -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(/<!--([\s\S]*?)-->/g, '$1')
.replace(/<!\[CDATA\[([\s\S]*?)]]>/g, '$1')
Expand Down
25 changes: 25 additions & 0 deletions test/unit/modules/compiler/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
<textarea>
<p>Test 1</p>
test2
</textarea>
`, 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(`
<p>Test 1</p>
test2
`)

const comment = parse('<textarea><!--comment--></textarea>', 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('<!--comment-->')
})
})