Skip to content

Commit d8315c4

Browse files
committed
do not decode text inside script/style tags (fix vuejs#5526)
1 parent 3a6fd13 commit d8315c4

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

src/compiler/parser/html-parser.js

Lines changed: 1 addition & 1 deletion
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 isPlainTextElement = makeMap('script,style,textarea', true)
49+
export const isPlainTextElement = makeMap('script,style,textarea', true)
5050
const reCache = {}
5151

5252
const decodingMap = {

src/compiler/parser/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export function parse (
252252
}
253253
const children = currentParent.children
254254
text = inPre || text.trim()
255-
? decodeHTMLCached(text)
255+
? isTextTag(currentParent) ? text : decodeHTMLCached(text)
256256
// only preserve whitespace if its not right after a starting tag
257257
: preserveWhitespace && children.length ? ' ' : ''
258258
if (text) {
@@ -544,6 +544,11 @@ function makeAttrsMap (attrs: Array<Object>): Object {
544544
return map
545545
}
546546

547+
// for script (e.g. type="x/template") or style, do not decode content
548+
function isTextTag (el): boolean {
549+
return el.tag === 'script' || el.tag === 'style'
550+
}
551+
547552
function isForbiddenTag (el): boolean {
548553
return (
549554
el.tag === 'style' ||

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,11 @@ describe('parser', () => {
541541
expect(comment.children[0].type).toBe(3)
542542
expect(comment.children[0].text).toBe('<!--comment-->')
543543
})
544+
545+
// #5526
546+
it('should not decode text in script tags', () => {
547+
const options = extend({}, baseOptions)
548+
const ast = parse(`<script type="x/template">&gt;<foo>&lt;</script>`, options)
549+
expect(ast.children[0].text).toBe(`&gt;<foo>&lt;`)
550+
})
544551
})

0 commit comments

Comments
 (0)