Sfoglia il codice sorgente

fix IE conditional comments (fix #4125)

Evan You 9 anni fa
parent
commit
5d36e8bc1b

+ 10 - 3
src/compiler/parser/html-parser.js

@@ -37,6 +37,8 @@ const startTagOpen = new RegExp('^<' + qnameCapture)
 const startTagClose = /^\s*(\/?)>/
 const endTag = new RegExp('^<\\/' + qnameCapture + '[^>]*>')
 const doctype = /^<!DOCTYPE [^>]+>/i
+const comment = /^<!--/
+const conditionalComment = /^<!\[/
 
 let IS_REGEX_CAPTURING_BROKEN = false
 'x'.replace(/x(.)?/g, function (m, g) {
@@ -94,7 +96,7 @@ export function parseHTML (html, options) {
       let textEnd = html.indexOf('<')
       if (textEnd === 0) {
         // Comment:
-        if (/^<!--/.test(html)) {
+        if (comment.test(html)) {
           const commentEnd = html.indexOf('-->')
 
           if (commentEnd >= 0) {
@@ -104,7 +106,7 @@ export function parseHTML (html, options) {
         }
 
         // http://en.wikipedia.org/wiki/Conditional_comment#Downlevel-revealed_conditional_comment
-        if (/^<!\[/.test(html)) {
+        if (conditionalComment.test(html)) {
           const conditionalEnd = html.indexOf(']>')
 
           if (conditionalEnd >= 0) {
@@ -140,7 +142,12 @@ export function parseHTML (html, options) {
       let text, rest
       if (textEnd > 0) {
         rest = html.slice(textEnd)
-        while (!startTagOpen.test(rest) && !endTag.test(rest)) {
+        while (
+          !endTag.test(rest) &&
+          !startTagOpen.test(rest) &&
+          !comment.test(rest) &&
+          !conditionalComment.test(rest)
+        ) {
           // < in plain text, be forgiving and treat it as text
           textEnd += rest.indexOf('<', 1)
           rest = html.slice(textEnd)

+ 13 - 0
test/unit/modules/compiler/parser.spec.js

@@ -384,4 +384,17 @@ describe('parser', () => {
     expect(ast.children[0].type).toBe(3)
     expect(ast.children[0].text).toBe('1 < 2 < 3')
   })
+
+  it('IE conditional comments', () => {
+    const options = extend({}, baseOptions)
+    const ast = parse(`
+      <div>
+        <!--[if lte IE 8]>
+          <p>Test 1</p>
+        <![endif]-->
+      </div>
+    `, options)
+    expect(ast.tag).toBe('div')
+    expect(ast.chilldren).toBeUndefined()
+  })
 })