Skip to content

Commit ef286ae

Browse files
committed
fix vuejs#618 parsing html string without tag but with enitities
1 parent 537fcf7 commit ef286ae

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/parsers/template.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ map.rect = [
5656
'</svg>'
5757
]
5858

59-
var TAG_RE = /<([\w:]+)/
59+
var tagRE = /<([\w:]+)/
60+
var entityRE = /&\w+;/
6061

6162
/**
6263
* Convert a string template to a DocumentFragment.
@@ -75,16 +76,17 @@ function stringToFragment (templateString) {
7576
}
7677

7778
var frag = document.createDocumentFragment()
78-
var tagMatch = TAG_RE.exec(templateString)
79+
var tagMatch = templateString.match(tagRE)
80+
var entityMatch = entityRE.test(templateString)
7981

80-
if (!tagMatch) {
82+
if (!tagMatch && !entityMatch) {
8183
// text only, return a single text node.
8284
frag.appendChild(
8385
document.createTextNode(templateString)
8486
)
8587
} else {
8688

87-
var tag = tagMatch[1]
89+
var tag = tagMatch && tagMatch[1]
8890
var wrap = map[tag] || map._default
8991
var depth = wrap[0]
9092
var prefix = wrap[1]

test/unit/specs/parsers/template_spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ if (_.inBrowser) {
3737
expect(res.firstChild.nodeType).toBe(3) // Text node
3838
})
3939

40+
it('should handle string that contains html entities', function () {
41+
var res = parse('hi&lt;hi')
42+
expect(res instanceof DocumentFragment).toBeTruthy()
43+
expect(res.childNodes.length).toBe(1)
44+
expect(res.firstChild.nodeValue).toBe('hi<hi')
45+
})
46+
4047
it('should parse textContent if argument is a script node', function () {
4148
var node = document.createElement('script')
4249
node.textContent = testString

0 commit comments

Comments
 (0)