Skip to content

Commit 7059c15

Browse files
simplesmileryyx990803
authored andcommitted
Fix handling comments when template has no tags (#3008)
1 parent 2feb413 commit 7059c15

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/parsers/template.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function isRealTemplate (node) {
7676

7777
const tagRE = /<([\w:-]+)/
7878
const entityRE = /&#?\w+?;/
79+
const commentRE = /<!--/
7980

8081
/**
8182
* Convert a string template to a DocumentFragment.
@@ -100,8 +101,9 @@ function stringToFragment (templateString, raw) {
100101
var frag = document.createDocumentFragment()
101102
var tagMatch = templateString.match(tagRE)
102103
var entityMatch = entityRE.test(templateString)
104+
var commentMatch = commentRE.test(templateString)
103105

104-
if (!tagMatch && !entityMatch) {
106+
if (!tagMatch && !entityMatch && !commentMatch) {
105107
// text only, return a single text node.
106108
frag.appendChild(
107109
document.createTextNode(templateString)

test/unit/specs/parsers/template_spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ describe('Template Parser', function () {
2323
expect(res.firstChild.nodeType).toBe(3) // Text node
2424
})
2525

26+
it('should work if the template string doesn\'t contain tags but contains comments', function () {
27+
var res = parse('<!-- yo -->hello<!-- yo -->')
28+
expect(res.childNodes.length).toBe(1)
29+
expect(res.firstChild.nodeType).toBe(3) // text node
30+
expect(res.firstChild.nodeValue).toBe('hello')
31+
})
32+
2633
it('should handle string that contains html entities', function () {
2734
var res = parse('foo&lt;bar')
2835
expect(res.nodeType).toBe(11)

0 commit comments

Comments
 (0)