Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Fix IE Conditional Comment processing #566

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ var getBlocks = function (content) {
var indent = (text.match(/^\s*/) || [])[0];
var endbuild = regend.test(text);
var build = text.match(regbuild);
var rawText = content.slice(location.start, location.end);

// discard empty lines
if (build) {
Expand All @@ -131,7 +132,7 @@ var getBlocks = function (content) {
indent: indent,
searchPath: [],
src: [],
raw: [content.substring(location.start, location.end)]
raw: [rawText]
};

if (build[2]) {
Expand All @@ -141,18 +142,30 @@ var getBlocks = function (content) {
}

// Check IE conditionals
var isConditionalStart = text.match(/(\[if.*\]>)(<!-->)?( -->)?/g);
var isConditionalEnd = text.match(/(<!--\s?)?(<!\[endif\])/g);
var isConditionalStart = /(<!--\[if.*\]>)(\s?<!.*-->)?( -->)?/g.exec(rawText);
var isConditionalEnd = /(<!--.*?)?(<!\[endif\]-->)/g.exec(rawText);

if (block && isConditionalStart) {
last.conditionalStart = isConditionalStart;
last.conditionalStart = isConditionalStart[0];
}
if (block && isConditionalEnd) {
last.conditionalEnd = isConditionalEnd;
last.conditionalEnd = isConditionalEnd[0];
}

// Process the contents of the conditional comment
if(isConditionalStart){
var start = isConditionalStart.index + isConditionalStart[0].length;
var end = isConditionalEnd ? isConditionalEnd.index : null;
var conditionalContent = rawText.slice(start, end);
if(conditionalContent.trim().length){
var fragmentParser = new parse5.SimpleApiParser(parser.handlers);
fragmentParser.parse(conditionalContent);
}
}

// switch back block flag when endbuild
if (block && endbuild) {
last.raw.push(content.substring(location.start, location.end));
last.raw.push(rawText);
sections.push(last);
block = false;
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/block_with_IEconditionals_inline_exposed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- build:js javascripts/vendor/selectivizr.js --><!--[if (lt IE 9) & (!IEmobile)]><!-->
<script src="javascripts/vendor/selectivizr/selectivizr.js"></script>
<!--<![endif]--><!-- endbuild -->
5 changes: 5 additions & 0 deletions test/fixtures/block_with_IEconditionals_within_exposed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- build:js javascripts/vendor/selectivizr.js -->
<!--[if (lt IE 9) & (!IEmobile)]><!-->
<script src="javascripts/vendor/selectivizr/selectivizr.js"></script>
<!--<![endif]-->
<!-- endbuild -->
33 changes: 29 additions & 4 deletions test/test-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,45 @@ describe('File', function () {
var filename = path.join(__dirname, '/fixtures/block_with_IEconditionals_inline.html');
var file = new File(filename);
assert.equal(1, file.blocks.length);
assert.equal(1,file.blocks[0].src.length);
assert.ok(file.blocks[0].conditionalStart);
assert.ok(file.blocks[0].conditionalEnd);
assert.equal('[if (lt IE 9) & (!IEmobile)]>', file.blocks[0].conditionalStart);
assert.equal('<![endif]', file.blocks[0].conditionalEnd);
assert.equal('<!--[if (lt IE 9) & (!IEmobile)]>', file.blocks[0].conditionalStart);
assert.equal('<![endif]-->', file.blocks[0].conditionalEnd);
});

it('should also detect block that has exposed IE conditionals on same line', function () {
var filename = path.join(__dirname, '/fixtures/block_with_IEconditionals_inline_exposed.html');
var file = new File(filename);
assert.equal(1, file.blocks.length);
assert.equal(1,file.blocks[0].src.length);
assert.ok(file.blocks[0].conditionalStart);
assert.ok(file.blocks[0].conditionalEnd);
assert.equal('<!--[if (lt IE 9) & (!IEmobile)]><!-->', file.blocks[0].conditionalStart);
assert.equal('<!--<![endif]-->', file.blocks[0].conditionalEnd);
});

it('should also detect block that has IE conditionals within block', function () {
var filename = path.join(__dirname, '/fixtures/block_with_IEconditionals_within.html');
var file = new File(filename);
assert.equal(1, file.blocks.length);
assert.equal(1,file.blocks[0].src.length);
assert.ok(file.blocks[0].conditionalStart);
assert.ok(file.blocks[0].conditionalEnd);
assert.equal('<!--[if (lt IE 9) & (!IEmobile)]>', file.blocks[0].conditionalStart);
assert.equal('<![endif]-->', file.blocks[0].conditionalEnd);

});

it('should also detect block that has uncommented IE conditionals within block', function () {
var filename = path.join(__dirname, '/fixtures/block_with_IEconditionals_within_exposed.html');
var file = new File(filename);
assert.equal(1, file.blocks.length);
assert.equal(1,file.blocks[0].src.length);
assert.ok(file.blocks[0].conditionalStart);
assert.ok(file.blocks[0].conditionalEnd);
assert.equal('[if (lt IE 9) & (!IEmobile)]>', file.blocks[0].conditionalStart);
assert.equal('<![endif]', file.blocks[0].conditionalEnd);
assert.equal('<!--[if (lt IE 9) & (!IEmobile)]><!-->', file.blocks[0].conditionalStart);
assert.equal('<!--<![endif]-->', file.blocks[0].conditionalEnd);
});

it('should throw an exception if it finds RequireJS blocks', function () {
Expand Down
30 changes: 30 additions & 0 deletions test/test-fileprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,21 @@ describe('FileProcessor', function () {
assert.equal(result, ' <!--[if (lt IE 9) & (!IEmobile)]>\n <script src="foo.js"><\/script>\n <![endif]-->');
});

it('should preserve exposed IE conditionals for js blocks', function () {
var fp = new FileProcessor('html', [], {});
var block = {
dest: 'foo.js',
type: 'js',
src: ['bar.js'],
conditionalStart: '<!--[if (lt IE 9) & (!IEmobile)]><!-->',
conditionalEnd: '<!--<![endif]-->',
indent: ' '
};

var result = fp.replaceWith(block);
assert.equal(result, ' <!--[if (lt IE 9) & (!IEmobile)]><!-->\n <script src="foo.js"><\/script>\n <!--<![endif]-->');
});

it('should preserve IE conditionals for css blocks', function () {
var fp = new FileProcessor('html', [], {});
var block = {
Expand All @@ -195,6 +210,21 @@ describe('FileProcessor', function () {
var result = fp.replaceWith(block);
assert.equal(result, ' <!--[if (lt IE 9) & (!IEmobile)]>\n <link rel="stylesheet" href="foo.css">\n <![endif]-->');
});

it('should preserve exposed IE conditionals for css blocks', function () {
var fp = new FileProcessor('html', [], {});
var block = {
dest: 'foo.css',
type: 'css',
src: ['bar.css'],
conditionalStart: '<!--[if (lt IE 9) & (!IEmobile)]> <!-->',
conditionalEnd: '<!--<![endif]-->',
indent: ' '
};

var result = fp.replaceWith(block);
assert.equal(result, ' <!--[if (lt IE 9) & (!IEmobile)]> <!-->\n <link rel="stylesheet" href="foo.css">\n <!--<![endif]-->');
});
});

describe('replaceWithRevved', function () {
Expand Down