Skip to content

Commit

Permalink
Avoid double-decoding on attribute value (#4797)
Browse files Browse the repository at this point in the history
* Use a single `replace` instead so that we could avoid the double-decoding issue,i.e. get `"` after decoding `"` while `"` is expected.

* Don't instantiate RegExp on every call
  • Loading branch information
jddxf authored and yyx990803 committed Jan 31, 2017
1 parent af1ec1b commit d14bd64
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/compiler/parser/html-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,19 @@ let IS_REGEX_CAPTURING_BROKEN = false
const isScriptOrStyle = makeMap('script,style', true)
const reCache = {}

const ltRE = /</g
const gtRE = />/g
const nlRE = /
/g
const ampRE = /&/g
const quoteRE = /"/g
const decodingMap = {
'&lt;': '<',
'&gt;': '>',
'&quot;': '"',
'&amp;': '&',
'&#10;': '\n'
}
const encodedAttr = /&(lt|gt|quot|amp);/g
const encodedAttrWithNewLines = /&(lt|gt|quot|amp|#10);/g

function decodeAttr (value, shouldDecodeNewlines) {
if (shouldDecodeNewlines) {
value = value.replace(nlRE, '\n')
}
return value
.replace(ltRE, '<')
.replace(gtRE, '>')
.replace(ampRE, '&')
.replace(quoteRE, '"')
const re = shouldDecodeNewlines ? encodedAttrWithNewLines : encodedAttr
return value.replace(re, match => decodingMap[match])
}

export function parseHTML (html, options) {
Expand Down

0 comments on commit d14bd64

Please sign in to comment.