-
Notifications
You must be signed in to change notification settings - Fork 29
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
Feat: minifyConditionalComments #119
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e2f8e07
Feat: add minifyConditionalComments
SukkaW 9ed1496
Docs: add minifyConditionalComments
SukkaW bb6f4d4
Docs: add a FIXME message
SukkaW ac53c2a
Docs: add notice for minifyConditionalComments
SukkaW 1b53ded
Fix: remove ESLint rules
SukkaW File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import htmlnano from '../htmlnano'; | ||
import { isConditionalComment } from '../helpers'; | ||
|
||
// Spec: https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/ms537512(v=vs.85) | ||
const CONDITIONAL_COMMENT_REGEXP = /(<!--\[if\s+?[^<>[\]]+?]>)([\s\S]+?)(<!\[endif\]-->)/gm; | ||
|
||
/** Minify content inside conditional comments */ | ||
export default async function minifyConditionalComments(tree, htmlnanoOptions) { | ||
// forEach, tree.walk, tree.match just don't support Promise. | ||
for (let i = 0, len = tree.length; i < len; i++) { | ||
const node = tree[i]; | ||
|
||
if (typeof node === 'string' && isConditionalComment(node)) { | ||
tree[i] = await minifycontentInsideConditionalComments(node, htmlnanoOptions); | ||
} | ||
|
||
if (node.content && node.content.length) { | ||
tree[i].content = await minifyConditionalComments(node.content, htmlnanoOptions); | ||
} | ||
} | ||
|
||
return tree; | ||
} | ||
|
||
async function minifycontentInsideConditionalComments(text, htmlnanoOptions) { | ||
let match; | ||
const matches = []; | ||
|
||
// FIXME! | ||
// String#matchAll is supported since Node.js 12 | ||
while ((match = CONDITIONAL_COMMENT_REGEXP.exec(text)) !== null) { | ||
matches.push([match[1], match[2], match[3]]); | ||
} | ||
|
||
if (!matches.length) { | ||
return Promise.resolve(text); | ||
} | ||
|
||
return Promise.all(matches.map(async match => { | ||
const result = await htmlnano.process(match[1], htmlnanoOptions, {}, {}); | ||
|
||
return match[0] + result.html + match[2]; | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { init } from '../htmlnano'; | ||
import safePreset from '../../lib/presets/safe'; | ||
|
||
|
||
describe('minifyConditionalComments', () => { | ||
const fixture = { | ||
fullHtml: ` | ||
<!DOCTYPE html> | ||
<html class="no-js"> | ||
<head> | ||
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"> | ||
<meta charset="utf-8"> | ||
<!--[if lte IE 7]> | ||
<style type="text/css"> | ||
.title { | ||
color: red; | ||
} | ||
</style> | ||
<![endif]--> | ||
</head> | ||
</html>`, | ||
fullHtmlMinified: '<!DOCTYPE html><html class="no-js"><head><meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"><meta charset="utf-8"><!--[if lte IE 7]><style type="text/css">.title{color:red}</style><![endif]--></head></html>', | ||
multipleConditionalComment: ` | ||
<!--[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]-->`, | ||
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]-->`, | ||
}; | ||
|
||
it('common html', () => { | ||
return init( | ||
fixture.fullHtml, | ||
fixture.fullHtmlMinified, | ||
{ | ||
...safePreset, | ||
minifyConditionalComments: true | ||
} | ||
); | ||
}); | ||
|
||
it('multiple conditional comment', () => { | ||
return init( | ||
fixture.multipleConditionalComment, | ||
fixture.multipleConditionalCommentMinified, | ||
{ | ||
...safePreset, | ||
minifyConditionalComments: true | ||
} | ||
); | ||
}); | ||
|
||
it('multiple conditional comment in single line', () => { | ||
return init( | ||
fixture.singleLineMultipleConditionalComment, | ||
fixture.singleLineMultipleConditionalComment, | ||
{ | ||
minifyConditionalComments: true | ||
} | ||
); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned in #16 (comment),
tree.walk
&tree.match
is not designed to be work with Promise in the first place. Thus I use for loop to traverse throughtree
.