Skip to content

Commit

Permalink
do not decode text inside script/style tags (fix #5526)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 27, 2017
1 parent 3a6fd13 commit d8315c4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/compiler/parser/html-parser.js
Expand Up @@ -46,7 +46,7 @@ let IS_REGEX_CAPTURING_BROKEN = false
})

// Special Elements (can contain anything)
const isPlainTextElement = makeMap('script,style,textarea', true)
export const isPlainTextElement = makeMap('script,style,textarea', true)
const reCache = {}

const decodingMap = {
Expand Down
7 changes: 6 additions & 1 deletion src/compiler/parser/index.js
Expand Up @@ -252,7 +252,7 @@ export function parse (
}
const children = currentParent.children
text = inPre || text.trim()
? decodeHTMLCached(text)
? isTextTag(currentParent) ? text : decodeHTMLCached(text)
// only preserve whitespace if its not right after a starting tag
: preserveWhitespace && children.length ? ' ' : ''
if (text) {
Expand Down Expand Up @@ -544,6 +544,11 @@ function makeAttrsMap (attrs: Array<Object>): Object {
return map
}

// for script (e.g. type="x/template") or style, do not decode content
function isTextTag (el): boolean {
return el.tag === 'script' || el.tag === 'style'
}

function isForbiddenTag (el): boolean {
return (
el.tag === 'style' ||
Expand Down
7 changes: 7 additions & 0 deletions test/unit/modules/compiler/parser.spec.js
Expand Up @@ -541,4 +541,11 @@ describe('parser', () => {
expect(comment.children[0].type).toBe(3)
expect(comment.children[0].text).toBe('<!--comment-->')
})

// #5526
it('should not decode text in script tags', () => {
const options = extend({}, baseOptions)
const ast = parse(`<script type="x/template">&gt;<foo>&lt;</script>`, options)
expect(ast.children[0].text).toBe(`&gt;<foo>&lt;`)
})
})

0 comments on commit d8315c4

Please sign in to comment.