Browse Source

do not decode text inside script/style tags (fix #5526)

dev
Evan You 8 years ago
parent
commit
d8315c42ef
  1. 2
      src/compiler/parser/html-parser.js
  2. 7
      src/compiler/parser/index.js
  3. 7
      test/unit/modules/compiler/parser.spec.js

2
src/compiler/parser/html-parser.js

@ -46,7 +46,7 @@ let IS_REGEX_CAPTURING_BROKEN = false
}) })
// Special Elements (can contain anything) // Special Elements (can contain anything)
const isPlainTextElement = makeMap('script,style,textarea', true) export const isPlainTextElement = makeMap('script,style,textarea', true)
const reCache = {} const reCache = {}
const decodingMap = { const decodingMap = {

7
src/compiler/parser/index.js

@ -252,7 +252,7 @@ export function parse (
} }
const children = currentParent.children const children = currentParent.children
text = inPre || text.trim() text = inPre || text.trim()
? decodeHTMLCached(text) ? isTextTag(currentParent) ? text : decodeHTMLCached(text)
// only preserve whitespace if its not right after a starting tag // only preserve whitespace if its not right after a starting tag
: preserveWhitespace && children.length ? ' ' : '' : preserveWhitespace && children.length ? ' ' : ''
if (text) { if (text) {
@ -544,6 +544,11 @@ function makeAttrsMap (attrs: Array<Object>): Object {
return map 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 { function isForbiddenTag (el): boolean {
return ( return (
el.tag === 'style' || el.tag === 'style' ||

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

@ -541,4 +541,11 @@ describe('parser', () => {
expect(comment.children[0].type).toBe(3) expect(comment.children[0].type).toBe(3)
expect(comment.children[0].text).toBe('<!--comment-->') 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;`)
})
}) })

Loading…
Cancel
Save