Skip to content

Avoid double-decoding on attribute value #4797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions src/compiler/parser/html-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,17 @@ 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'
}

function decodeAttr (value, shouldDecodeNewlines) {
if (shouldDecodeNewlines) {
value = value.replace(nlRE, '\n')
}
return value
.replace(ltRE, '<')
.replace(gtRE, '>')
.replace(ampRE, '&')
.replace(quoteRE, '"')
const re = shouldDecodeNewlines ? /&(lt|gt|quot|amp|#10);/g : /&(lt|gt|quot|amp);/g
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we hoist the two RegExps out so that they don't get instantiated on every call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense!

return value.replace(re, match => decodingMap[match])
}

export function parseHTML (html, options) {
Expand Down