Avoid double-decoding on attribute value (#4797)

* 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
This commit is contained in:
jddxf 2017-02-01 01:03:49 +08:00 committed by Evan You
parent af1ec1ba99
commit d14bd64143

View File

@ -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) {