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: minifyConditionalComment with <html> support #125

Merged
merged 1 commit into from Nov 21, 2020
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
22 changes: 1 addition & 21 deletions README.md
Expand Up @@ -550,27 +550,7 @@ Source:
Minified:

```html
<!--[if lte IE 7]><style>.title {color:red}</style><![endif]-->
```

##### Notice

Due to [the limitation of PostHTML](https://github.com/posthtml/posthtml-parser/issues/9) (which is actually a issue from upstream [htmlparser2](https://github.com/fb55/htmlparser2/pull/146)), following html snippet is not supported:

```html
<!--[if lt IE 7]><html class="no-js ie6"><![endif]-->
<!--[if IE 7]><html class="no-js ie7"><![endif]-->
<!--[if IE 8]><html class="no-js ie8"><![endif]-->
<!--[if gt IE 8]><!--><html class="no-js"><!--<![endif]-->
```

Which will result in:

```html
<!--[if lt IE 7]><html class="no-js ie6"></html><![endif]-->
<!--[if IE 7]><html class="no-js ie7"></html><![endif]-->
<!--[if IE 8]><html class="no-js ie8"></html><![endif]-->
<!--[if gt IE 8]><!--><html class="no-js"></html><!--<![endif]-->
<!--[if lte IE 7]><style>.title{color:red}</style><![endif]-->
```

### removeRedundantAttributes
Expand Down
7 changes: 6 additions & 1 deletion lib/modules/minifyConditionalComments.es6
Expand Up @@ -38,7 +38,12 @@ async function minifycontentInsideConditionalComments(text, htmlnanoOptions) {

return Promise.all(matches.map(async match => {
const result = await htmlnano.process(match[1], htmlnanoOptions, {}, {});
let minified = result.html;

return match[0] + result.html + match[2];
if (match[1].includes('<html') && minified.includes('</html>')) {
minified = minified.replace('</html>', '');
}

return match[0] + minified + match[2];
}));
}
20 changes: 20 additions & 0 deletions test/modules/minifyConditionalComments.js
Expand Up @@ -44,6 +44,16 @@ describe('minifyConditionalComments', () => {
multipleConditionalCommentMinified: '<!--[if lt IE 7 ]><div class="ie6"> </div><![endif]--><!--[if IE 7 ]><div class="ie7"> </div><![endif]--><!--[if IE 8 ]><div class="ie8"> </div><![endif]--><!--[if IE 9 ]><div class="ie9"> </div><![endif]--><!--[if (gt IE 9)|!(IE)]><!--><div class="w3c"> </div>',
singleLineMultipleConditionalComment: `
<!--[if lt IE 7 ]><div class="ie6"></div><![endif]--><!--[if IE 7 ]><div class="ie7"></div><![endif]--><!--[if IE 8 ]><div class="ie8"></div><![endif]--><!--[if IE 9 ]><div class="ie9"></div><![endif]--><!--[if (gt IE 9)|!(IE)]><!--><div class="w3c"></div><!--<![endif]-->`,
htmlTagIncludedConditionalComment: `
<!--[if lt IE 7]><html class="no-js ie6"><![endif]-->
<!--[if IE 7]><html class="no-js ie7"><![endif]-->
<!--[if IE 8]><html class="no-js ie8"><![endif]-->
<!--[if gt IE 8]><!--><html class="no-js"><!--<![endif]-->`,
htmlTagIncludedConditionalCommentMinified: `
<!--[if lt IE 7]><html class="no-js ie6"><![endif]-->
<!--[if IE 7]><html class="no-js ie7"><![endif]-->
<!--[if IE 8]><html class="no-js ie8"><![endif]-->
<!--[if gt IE 8]><!--><html class="no-js"><!--<![endif]--></html>`
};

it('common html', () => {
Expand Down Expand Up @@ -77,4 +87,14 @@ describe('minifyConditionalComments', () => {
}
);
});

it('<html> in conditional comment', () => {
return init(
fixture.htmlTagIncludedConditionalComment,
fixture.htmlTagIncludedConditionalCommentMinified,
{
minifyConditionalComments: true
}
);
});
});