Skip to content

Commit

Permalink
fix vuejs#618 parsing html string without tag but with enitities
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 4, 2014
1 parent 537fcf7 commit ef286ae
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/parsers/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ map.rect = [
'</svg>'
]

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

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

var frag = document.createDocumentFragment()
var tagMatch = TAG_RE.exec(templateString)
var tagMatch = templateString.match(tagRE)
var entityMatch = entityRE.test(templateString)

if (!tagMatch) {
if (!tagMatch && !entityMatch) {
// text only, return a single text node.
frag.appendChild(
document.createTextNode(templateString)
)
} else {

var tag = tagMatch[1]
var tag = tagMatch && tagMatch[1]
var wrap = map[tag] || map._default
var depth = wrap[0]
var prefix = wrap[1]
Expand Down
7 changes: 7 additions & 0 deletions test/unit/specs/parsers/template_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ if (_.inBrowser) {
expect(res.firstChild.nodeType).toBe(3) // Text node
})

it('should handle string that contains html entities', function () {
var res = parse('hi&lt;hi')
expect(res instanceof DocumentFragment).toBeTruthy()
expect(res.childNodes.length).toBe(1)
expect(res.firstChild.nodeValue).toBe('hi<hi')
})

it('should parse textContent if argument is a script node', function () {
var node = document.createElement('script')
node.textContent = testString
Expand Down

0 comments on commit ef286ae

Please sign in to comment.