Skip to content

Commit d14bd64

Browse files
jddxfyyx990803
authored andcommitted
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
1 parent af1ec1b commit d14bd64

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/compiler/parser/html-parser.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,19 @@ let IS_REGEX_CAPTURING_BROKEN = false
4949
const isScriptOrStyle = makeMap('script,style', true)
5050
const reCache = {}
5151

52-
const ltRE = /</g
53-
const gtRE = />/g
54-
const nlRE = /
/g
55-
const ampRE = /&/g
56-
const quoteRE = /"/g
52+
const decodingMap = {
53+
'&lt;': '<',
54+
'&gt;': '>',
55+
'&quot;': '"',
56+
'&amp;': '&',
57+
'&#10;': '\n'
58+
}
59+
const encodedAttr = /&(lt|gt|quot|amp);/g
60+
const encodedAttrWithNewLines = /&(lt|gt|quot|amp|#10);/g
5761

5862
function decodeAttr (value, shouldDecodeNewlines) {
59-
if (shouldDecodeNewlines) {
60-
value = value.replace(nlRE, '\n')
61-
}
62-
return value
63-
.replace(ltRE, '<')
64-
.replace(gtRE, '>')
65-
.replace(ampRE, '&')
66-
.replace(quoteRE, '"')
63+
const re = shouldDecodeNewlines ? encodedAttrWithNewLines : encodedAttr
64+
return value.replace(re, match => decodingMap[match])
6765
}
6866

6967
export function parseHTML (html, options) {

0 commit comments

Comments
 (0)