Skip to content

Commit 5d36e8b

Browse files
committed
fix IE conditional comments (fix vuejs#4125)
1 parent 0973342 commit 5d36e8b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/compiler/parser/html-parser.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const startTagOpen = new RegExp('^<' + qnameCapture)
3737
const startTagClose = /^\s*(\/?)>/
3838
const endTag = new RegExp('^<\\/' + qnameCapture + '[^>]*>')
3939
const doctype = /^<!DOCTYPE [^>]+>/i
40+
const comment = /^<!--/
41+
const conditionalComment = /^<!\[/
4042

4143
let IS_REGEX_CAPTURING_BROKEN = false
4244
'x'.replace(/x(.)?/g, function (m, g) {
@@ -94,7 +96,7 @@ export function parseHTML (html, options) {
9496
let textEnd = html.indexOf('<')
9597
if (textEnd === 0) {
9698
// Comment:
97-
if (/^<!--/.test(html)) {
99+
if (comment.test(html)) {
98100
const commentEnd = html.indexOf('-->')
99101

100102
if (commentEnd >= 0) {
@@ -104,7 +106,7 @@ export function parseHTML (html, options) {
104106
}
105107

106108
// http://en.wikipedia.org/wiki/Conditional_comment#Downlevel-revealed_conditional_comment
107-
if (/^<!\[/.test(html)) {
109+
if (conditionalComment.test(html)) {
108110
const conditionalEnd = html.indexOf(']>')
109111

110112
if (conditionalEnd >= 0) {
@@ -140,7 +142,12 @@ export function parseHTML (html, options) {
140142
let text, rest
141143
if (textEnd > 0) {
142144
rest = html.slice(textEnd)
143-
while (!startTagOpen.test(rest) && !endTag.test(rest)) {
145+
while (
146+
!endTag.test(rest) &&
147+
!startTagOpen.test(rest) &&
148+
!comment.test(rest) &&
149+
!conditionalComment.test(rest)
150+
) {
144151
// < in plain text, be forgiving and treat it as text
145152
textEnd += rest.indexOf('<', 1)
146153
rest = html.slice(textEnd)

test/unit/modules/compiler/parser.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,17 @@ describe('parser', () => {
384384
expect(ast.children[0].type).toBe(3)
385385
expect(ast.children[0].text).toBe('1 < 2 < 3')
386386
})
387+
388+
it('IE conditional comments', () => {
389+
const options = extend({}, baseOptions)
390+
const ast = parse(`
391+
<div>
392+
<!--[if lte IE 8]>
393+
<p>Test 1</p>
394+
<![endif]-->
395+
</div>
396+
`, options)
397+
expect(ast.tag).toBe('div')
398+
expect(ast.chilldren).toBeUndefined()
399+
})
387400
})

0 commit comments

Comments
 (0)