Skip to content
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

Fix handling comments when template has no tags #3008

Merged
merged 1 commit into from
Jun 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/parsers/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function isRealTemplate (node) {

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

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

if (!tagMatch && !entityMatch) {
if (!tagMatch && !entityMatch && !commentMatch) {
// text only, return a single text node.
frag.appendChild(
document.createTextNode(templateString)
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 @@ -23,6 +23,13 @@ describe('Template Parser', function () {
expect(res.firstChild.nodeType).toBe(3) // Text node
})

it('should work if the template string doesn\'t contain tags but contains comments', function () {
var res = parse('<!-- yo -->hello<!-- yo -->')
expect(res.childNodes.length).toBe(1)
expect(res.firstChild.nodeType).toBe(3) // text node
expect(res.firstChild.nodeValue).toBe('hello')
})

it('should handle string that contains html entities', function () {
var res = parse('foo&lt;bar')
expect(res.nodeType).toBe(11)
Expand Down