htmlnano 0.2.5 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,30 +1,32 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
- value: true
4
+ value: true
5
5
  });
6
6
  exports.default = collapseAttributeWhitespace;
7
- var attributesWithLists = exports.attributesWithLists = new Set(['class', 'rel', 'ping']);
8
-
7
+ exports.attributesWithLists = void 0;
8
+ const attributesWithLists = new Set(['class', 'rel', 'ping']);
9
9
  /** Collapse whitespaces inside list-like attributes (e.g. class, rel) */
10
+
11
+ exports.attributesWithLists = attributesWithLists;
12
+
10
13
  function collapseAttributeWhitespace(tree) {
11
- tree.walk(function (node) {
12
- if (!node.attrs) {
13
- return node;
14
- }
14
+ tree.walk(node => {
15
+ if (!node.attrs) {
16
+ return node;
17
+ }
15
18
 
16
- Object.keys(node.attrs).forEach(function (attrName) {
17
- var attrNameLower = attrName.toLowerCase();
18
- if (!attributesWithLists.has(attrNameLower)) {
19
- return;
20
- }
19
+ Object.entries(node.attrs).forEach(([attrName, attrValue]) => {
20
+ const attrNameLower = attrName.toLowerCase();
21
21
 
22
- var attrValue = node.attrs[attrName].replace(/\s+/g, ' ').trim();
23
- node.attrs[attrName] = attrValue;
24
- });
22
+ if (!attributesWithLists.has(attrNameLower)) {
23
+ return;
24
+ }
25
25
 
26
- return node;
26
+ const newAttrValue = attrValue.replace(/\s+/g, ' ').trim();
27
+ node.attrs[attrName] = newAttrValue;
27
28
  });
28
-
29
- return tree;
29
+ return node;
30
+ });
31
+ return tree;
30
32
  }
@@ -1,56 +1,44 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
- value: true
4
+ value: true
5
5
  });
6
6
  exports.default = collapseBooleanAttributes;
7
7
  // Source: https://github.com/kangax/html-minifier/issues/63
8
- var htmlBooleanAttributes = new Set(['allowfullscreen', 'allowpaymentrequest', 'allowtransparency', 'async', 'autofocus', 'autoplay', 'checked', 'compact', 'controls', 'declare', 'default', 'defaultchecked', 'defaultmuted', 'defaultselected', 'defer', 'disabled', 'enabled', 'formnovalidate', 'hidden', 'indeterminate', 'inert', 'ismap', 'itemscope', 'loop', 'multiple', 'muted', 'nohref', 'noresize', 'noshade', 'novalidate', 'nowrap', 'open', 'pauseonexit', 'readonly', 'required', 'reversed', 'scoped', 'seamless', 'selected', 'sortable', 'truespeed', 'typemustmatch', 'visible']);
9
-
10
- var amphtmlBooleanAttributes = new Set(['⚡', 'amp', '⚡4ads', 'amp4ads', '⚡4email', 'amp4email', 'amp-custom', 'amp-boilerplate', 'amp4ads-boilerplate', 'amp4email-boilerplate', 'allow-blocked-ranges', 'amp-access-hide', 'amp-access-template', 'amp-keyframes', 'animate', 'arrows', 'data-block-on-consent', 'data-enable-refresh', 'data-multi-size', 'date-template', 'disable-double-tap', 'disable-session-states', 'disableremoteplayback', 'dots', 'expand-single-section', 'expanded', 'fallback', 'first', 'fullscreen', 'inline', 'lightbox', 'noaudio', 'noautoplay', 'noloading', 'once', 'open-after-clear', 'open-after-select', 'open-button', 'placeholder', 'preload', 'reset-on-refresh', 'reset-on-resize', 'resizable', 'rotate-to-fullscreen', 'second', 'standalone', 'stereo', 'submit-error', 'submit-success', 'submitting', 'subscriptions-actions', 'subscriptions-dialog']);
8
+ const htmlBooleanAttributes = new Set(['allowfullscreen', 'allowpaymentrequest', 'allowtransparency', 'async', 'autofocus', 'autoplay', 'checked', 'compact', 'controls', 'declare', 'default', 'defaultchecked', 'defaultmuted', 'defaultselected', 'defer', 'disabled', 'enabled', 'formnovalidate', 'hidden', 'indeterminate', 'inert', 'ismap', 'itemscope', 'loop', 'multiple', 'muted', 'nohref', 'noresize', 'noshade', 'novalidate', 'nowrap', 'open', 'pauseonexit', 'readonly', 'required', 'reversed', 'scoped', 'seamless', 'selected', 'sortable', 'truespeed', 'typemustmatch', 'visible']);
9
+ const amphtmlBooleanAttributes = new Set(['⚡', 'amp', '⚡4ads', 'amp4ads', '⚡4email', 'amp4email', 'amp-custom', 'amp-boilerplate', 'amp4ads-boilerplate', 'amp4email-boilerplate', 'allow-blocked-ranges', 'amp-access-hide', 'amp-access-template', 'amp-keyframes', 'animate', 'arrows', 'data-block-on-consent', 'data-enable-refresh', 'data-multi-size', 'date-template', 'disable-double-tap', 'disable-session-states', 'disableremoteplayback', 'dots', 'expand-single-section', 'expanded', 'fallback', 'first', 'fullscreen', 'inline', 'lightbox', 'noaudio', 'noautoplay', 'noloading', 'once', 'open-after-clear', 'open-after-select', 'open-button', 'placeholder', 'preload', 'reset-on-refresh', 'reset-on-resize', 'resizable', 'rotate-to-fullscreen', 'second', 'standalone', 'stereo', 'submit-error', 'submit-success', 'submitting', 'subscriptions-actions', 'subscriptions-dialog']);
11
10
 
12
11
  function collapseBooleanAttributes(tree, options, moduleOptions) {
13
- tree.match({ attrs: true }, function (node) {
14
- var _iteratorNormalCompletion = true;
15
- var _didIteratorError = false;
16
- var _iteratorError = undefined;
17
-
18
- try {
19
- for (var _iterator = Object.keys(node.attrs)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
20
- var attrName = _step.value;
21
-
22
- if (!node.tag) {
23
- continue;
24
- }
25
-
26
- if (node.tag.search('a-') === 0 && attrName === 'visible') {
27
- continue;
28
- }
29
-
30
- if (htmlBooleanAttributes.has(attrName)) {
31
- node.attrs[attrName] = true;
32
- }
33
- if (moduleOptions.amphtml && node.attrs[attrName] === '' && amphtmlBooleanAttributes.has(attrName)) {
34
- node.attrs[attrName] = true;
35
- }
36
- }
37
- } catch (err) {
38
- _didIteratorError = true;
39
- _iteratorError = err;
40
- } finally {
41
- try {
42
- if (!_iteratorNormalCompletion && _iterator.return) {
43
- _iterator.return();
44
- }
45
- } finally {
46
- if (_didIteratorError) {
47
- throw _iteratorError;
48
- }
49
- }
50
- }
51
-
52
- return node;
53
- });
54
-
55
- return tree;
12
+ tree.walk(node => {
13
+ if (!node.attrs) {
14
+ return node;
15
+ }
16
+
17
+ if (!node.tag) {
18
+ return node;
19
+ }
20
+
21
+ for (const attrName of Object.keys(node.attrs)) {
22
+ if (attrName === 'visible' && node.tag.startsWith('a-')) {
23
+ continue;
24
+ }
25
+
26
+ if (htmlBooleanAttributes.has(attrName)) {
27
+ node.attrs[attrName] = true;
28
+ }
29
+
30
+ if (moduleOptions.amphtml && amphtmlBooleanAttributes.has(attrName) && node.attrs[attrName] === '') {
31
+ node.attrs[attrName] = true;
32
+ } // collapse crossorigin attributes
33
+ // Specification: https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-settings-attributes
34
+
35
+
36
+ if (attrName.toLowerCase() === 'crossorigin' && (node.attrs[attrName] === 'anonymous' || node.attrs[attrName] === '')) {
37
+ node.attrs[attrName] = true;
38
+ }
39
+ }
40
+
41
+ return node;
42
+ });
43
+ return tree;
56
44
  }
@@ -1,50 +1,76 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
- value: true
4
+ value: true
5
5
  });
6
6
  exports.default = collapseWhitespace;
7
7
 
8
- var _normalizeHtmlWhitespace = require('normalize-html-whitespace');
9
-
10
- var _normalizeHtmlWhitespace2 = _interopRequireDefault(_normalizeHtmlWhitespace);
11
-
12
- var _helpers = require('../helpers');
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- var noWhitespaceCollapseElements = new Set(['script', 'style', 'pre', 'textarea']);
8
+ var _helpers = require("../helpers");
17
9
 
10
+ const noWhitespaceCollapseElements = new Set(['script', 'style', 'pre', 'textarea']);
11
+ const noTrimWhitespacesArroundElements = new Set([// non-empty tags that will maintain whitespace around them
12
+ 'a', 'abbr', 'acronym', 'b', 'bdi', 'bdo', 'big', 'button', 'cite', 'code', 'del', 'dfn', 'em', 'font', 'i', 'ins', 'kbd', 'label', 'mark', 'math', 'nobr', 'object', 'q', 'rp', 'rt', 'rtc', 'ruby', 's', 'samp', 'select', 'small', 'span', 'strike', 'strong', 'sub', 'sup', 'svg', 'textarea', 'time', 'tt', 'u', 'var', // self-closing tags that will maintain whitespace around them
13
+ 'comment', 'img', 'input', 'wbr']);
14
+ const noTrimWhitespacesInsideElements = new Set([// non-empty tags that will maintain whitespace within them
15
+ 'a', 'abbr', 'acronym', 'b', 'big', 'del', 'em', 'font', 'i', 'ins', 'kbd', 'mark', 'nobr', 'rp', 's', 'samp', 'small', 'span', 'strike', 'strong', 'sub', 'sup', 'time', 'tt', 'u', 'var']);
16
+ const whitespacePattern = /\s+/g;
17
+ const NONE = '';
18
+ const SINGLE_SPACE = ' ';
19
+ const validOptions = ['all', 'aggressive', 'conservative'];
18
20
  /** Collapses redundant whitespaces */
21
+
19
22
  function collapseWhitespace(tree, options, collapseType, tag) {
20
- if (collapseType !== 'all') {
21
- collapseType = 'conservative';
23
+ collapseType = validOptions.includes(collapseType) ? collapseType : 'conservative';
24
+ tree.forEach((node, index) => {
25
+ if (typeof node === 'string') {
26
+ const prevNode = tree[index - 1];
27
+ const nextNode = tree[index + 1];
28
+ const prevNodeTag = prevNode && prevNode.tag;
29
+ const nextNodeTag = nextNode && nextNode.tag;
30
+ const isTopLevel = !tag || tag === 'html' || tag === 'head';
31
+ const shouldTrim = collapseType === 'all' || isTopLevel ||
32
+ /*
33
+ * When collapseType is set to 'aggressive', and the tag is not inside 'noTrimWhitespacesInsideElements'.
34
+ * the first & last space inside the tag will be trimmed
35
+ */
36
+ collapseType === 'aggressive' && !noTrimWhitespacesInsideElements.has(tag);
37
+ node = collapseRedundantWhitespaces(node, collapseType, shouldTrim, tag, prevNodeTag, nextNodeTag);
22
38
  }
23
39
 
24
- tree.forEach(function (node, index) {
25
- if (typeof node === 'string' && !(0, _helpers.isComment)(node)) {
26
- var isTopLevel = !tag || tag === 'html' || tag === 'head';
27
- node = collapseRedundantWhitespaces(node, collapseType, isTopLevel);
28
- }
40
+ const isAllowCollapseWhitespace = !noWhitespaceCollapseElements.has(node.tag);
29
41
 
30
- var isAllowCollapseWhitespace = !noWhitespaceCollapseElements.has(node.tag);
31
- if (node.content && node.content.length && isAllowCollapseWhitespace) {
32
- node.content = collapseWhitespace(node.content, options, collapseType, node.tag);
33
- }
34
-
35
- tree[index] = node;
36
- });
42
+ if (node.content && node.content.length && isAllowCollapseWhitespace) {
43
+ node.content = collapseWhitespace(node.content, options, collapseType, node.tag);
44
+ }
37
45
 
38
- return tree;
46
+ tree[index] = node;
47
+ });
48
+ return tree;
39
49
  }
40
50
 
41
- function collapseRedundantWhitespaces(text, collapseType) {
42
- var isTopLevel = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
51
+ function collapseRedundantWhitespaces(text, collapseType, shouldTrim = false, currentTag, prevNodeTag, nextNodeTag) {
52
+ if (!text || text.length === 0) {
53
+ return NONE;
54
+ }
55
+
56
+ if (!(0, _helpers.isComment)(text)) {
57
+ text = text.replace(whitespacePattern, SINGLE_SPACE);
58
+ }
59
+
60
+ if (shouldTrim) {
61
+ if (collapseType === 'aggressive') {
62
+ if (!noTrimWhitespacesArroundElements.has(prevNodeTag)) {
63
+ text = text.trimStart();
64
+ }
43
65
 
44
- text = text && text.length > 0 ? (0, _normalizeHtmlWhitespace2.default)(text) : '';
45
- if (collapseType === 'all' || isTopLevel) {
46
- text = text.trim();
66
+ if (!noTrimWhitespacesArroundElements.has(nextNodeTag)) {
67
+ text = text.trimEnd();
68
+ }
69
+ } else {
70
+ // collapseType is 'all', trim spaces
71
+ text = text.trim();
47
72
  }
73
+ }
48
74
 
49
- return text;
75
+ return text;
50
76
  }
@@ -1,22 +1,22 @@
1
1
  "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
- value: true
4
+ value: true
5
5
  });
6
6
  exports.default = custom;
7
+
7
8
  /** Meta-module that runs custom modules */
8
9
  function custom(tree, options, customModules) {
9
- if (!customModules) {
10
- return tree;
11
- }
12
-
13
- if (!Array.isArray(customModules)) {
14
- customModules = [customModules];
15
- }
10
+ if (!customModules) {
11
+ return tree;
12
+ }
16
13
 
17
- customModules.forEach(function (customModule) {
18
- tree = customModule(tree, options);
19
- });
14
+ if (!Array.isArray(customModules)) {
15
+ customModules = [customModules];
16
+ }
20
17
 
21
- return tree;
18
+ customModules.forEach(customModule => {
19
+ tree = customModule(tree, options);
20
+ });
21
+ return tree;
22
22
  }
@@ -1,48 +1,46 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
- value: true
4
+ value: true
5
5
  });
6
6
  exports.default = collapseAttributeWhitespace;
7
7
 
8
- var _collapseAttributeWhitespace = require('./collapseAttributeWhitespace');
8
+ var _collapseAttributeWhitespace = require("./collapseAttributeWhitespace");
9
9
 
10
10
  /** Deduplicate values inside list-like attributes (e.g. class, rel) */
11
11
  function collapseAttributeWhitespace(tree) {
12
- tree.walk(function (node) {
13
- if (!node.attrs) {
14
- return node;
12
+ tree.walk(node => {
13
+ if (!node.attrs) {
14
+ return node;
15
+ }
16
+
17
+ Object.keys(node.attrs).forEach(attrName => {
18
+ const attrNameLower = attrName.toLowerCase();
19
+
20
+ if (!_collapseAttributeWhitespace.attributesWithLists.has(attrNameLower)) {
21
+ return;
22
+ }
23
+
24
+ const attrValues = node.attrs[attrName].split(/\s/);
25
+ const uniqeAttrValues = new Set();
26
+ const deduplicatedAttrValues = [];
27
+ attrValues.forEach(attrValue => {
28
+ if (!attrValue) {
29
+ // Keep whitespaces
30
+ deduplicatedAttrValues.push('');
31
+ return;
15
32
  }
16
33
 
17
- Object.keys(node.attrs).forEach(function (attrName) {
18
- var attrNameLower = attrName.toLowerCase();
19
- if (!_collapseAttributeWhitespace.attributesWithLists.has(attrNameLower)) {
20
- return;
21
- }
22
-
23
- var attrValues = node.attrs[attrName].split(/\s/);
24
- var uniqeAttrValues = new Set();
25
- var deduplicatedAttrValues = [];
26
- attrValues.forEach(function (attrValue) {
27
- if (!attrValue) {
28
- // Keep whitespaces
29
- deduplicatedAttrValues.push('');
30
- return;
31
- }
32
-
33
- if (uniqeAttrValues.has(attrValue)) {
34
- return;
35
- }
36
-
37
- deduplicatedAttrValues.push(attrValue);
38
- uniqeAttrValues.add(attrValue);
39
- });
40
-
41
- node.attrs[attrName] = deduplicatedAttrValues.join(' ');
42
- });
43
-
44
- return node;
45
- });
34
+ if (uniqeAttrValues.has(attrValue)) {
35
+ return;
36
+ }
46
37
 
47
- return tree;
38
+ deduplicatedAttrValues.push(attrValue);
39
+ uniqeAttrValues.add(attrValue);
40
+ });
41
+ node.attrs[attrName] = deduplicatedAttrValues.join(' ');
42
+ });
43
+ return node;
44
+ });
45
+ return tree;
48
46
  }
@@ -1,83 +1,63 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
- value: true
4
+ value: true
5
5
  });
6
6
  exports.default = mergeScripts;
7
+
7
8
  /* Merge multiple <script> into one */
8
9
  function mergeScripts(tree) {
9
- var scriptNodesIndex = {};
10
- var scriptSrcIndex = 1;
11
-
12
- tree.match({ tag: 'script' }, function (node) {
13
- var nodeAttrs = node.attrs || {};
14
- if (nodeAttrs.src) {
15
- scriptSrcIndex++;
16
- return node;
17
- }
10
+ let scriptNodesIndex = {};
11
+ let scriptSrcIndex = 1;
12
+ tree.match({
13
+ tag: 'script'
14
+ }, node => {
15
+ const nodeAttrs = node.attrs || {};
16
+
17
+ if (nodeAttrs.src) {
18
+ scriptSrcIndex++;
19
+ return node;
20
+ }
18
21
 
19
- var scriptType = nodeAttrs.type || 'text/javascript';
20
- if (scriptType !== 'text/javascript' && scriptType !== 'application/javascript') {
21
- return node;
22
- }
22
+ const scriptType = nodeAttrs.type || 'text/javascript';
23
23
 
24
- var scriptKey = JSON.stringify({
25
- id: nodeAttrs.id,
26
- class: nodeAttrs.class,
27
- type: scriptType,
28
- defer: nodeAttrs.defer !== undefined,
29
- async: nodeAttrs.async !== undefined,
30
- index: scriptSrcIndex
31
- });
32
- if (!scriptNodesIndex[scriptKey]) {
33
- scriptNodesIndex[scriptKey] = [];
34
- }
24
+ if (scriptType !== 'text/javascript' && scriptType !== 'application/javascript') {
25
+ return node;
26
+ }
35
27
 
36
- scriptNodesIndex[scriptKey].push(node);
37
- return node;
28
+ const scriptKey = JSON.stringify({
29
+ id: nodeAttrs.id,
30
+ class: nodeAttrs.class,
31
+ type: scriptType,
32
+ defer: nodeAttrs.defer !== undefined,
33
+ async: nodeAttrs.async !== undefined,
34
+ index: scriptSrcIndex
38
35
  });
39
36
 
40
- var _loop = function _loop(scriptKey) {
41
- var scriptNodes = scriptNodesIndex[scriptKey];
42
- var lastScriptNode = scriptNodes.pop();
43
- scriptNodes.reverse().forEach(function (scriptNode) {
44
- var scriptContent = (scriptNode.content || []).join(' ');
45
- scriptContent = scriptContent.trim();
46
- if (scriptContent.slice(-1) !== ';') {
47
- scriptContent += ';';
48
- }
49
-
50
- lastScriptNode.content.unshift(scriptContent);
37
+ if (!scriptNodesIndex[scriptKey]) {
38
+ scriptNodesIndex[scriptKey] = [];
39
+ }
51
40
 
52
- scriptNode.tag = false;
53
- scriptNode.content = [];
54
- });
55
- };
41
+ scriptNodesIndex[scriptKey].push(node);
42
+ return node;
43
+ });
56
44
 
57
- var _iteratorNormalCompletion = true;
58
- var _didIteratorError = false;
59
- var _iteratorError = undefined;
45
+ for (const scriptNodes of Object.values(scriptNodesIndex)) {
46
+ let lastScriptNode = scriptNodes.pop();
47
+ scriptNodes.reverse().forEach(scriptNode => {
48
+ let scriptContent = (scriptNode.content || []).join(' ');
49
+ scriptContent = scriptContent.trim();
60
50
 
61
- try {
62
- for (var _iterator = Object.keys(scriptNodesIndex)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
63
- var scriptKey = _step.value;
51
+ if (scriptContent.slice(-1) !== ';') {
52
+ scriptContent += ';';
53
+ }
64
54
 
65
- _loop(scriptKey);
66
- }
67
- } catch (err) {
68
- _didIteratorError = true;
69
- _iteratorError = err;
70
- } finally {
71
- try {
72
- if (!_iteratorNormalCompletion && _iterator.return) {
73
- _iterator.return();
74
- }
75
- } finally {
76
- if (_didIteratorError) {
77
- throw _iteratorError;
78
- }
79
- }
80
- }
55
+ lastScriptNode.content = lastScriptNode.content || [];
56
+ lastScriptNode.content.unshift(scriptContent);
57
+ scriptNode.tag = false;
58
+ scriptNode.content = [];
59
+ });
60
+ }
81
61
 
82
- return tree;
62
+ return tree;
83
63
  }
@@ -1,41 +1,42 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
- value: true
4
+ value: true
5
5
  });
6
6
  exports.default = mergeStyles;
7
7
 
8
- var _helpers = require('../helpers');
8
+ var _helpers = require("../helpers");
9
9
 
10
10
  /* Merge multiple <style> into one */
11
11
  function mergeStyles(tree) {
12
- var styleNodes = {};
13
-
14
- tree.match({ tag: 'style' }, function (node) {
15
- var nodeAttrs = node.attrs || {};
16
- // Skip <style scoped></style>
17
- // https://developer.mozilla.org/en/docs/Web/HTML/Element/style
18
- if (nodeAttrs.scoped !== undefined) {
19
- return node;
20
- }
21
-
22
- if ((0, _helpers.isAmpBoilerplate)(node)) {
23
- return node;
24
- }
25
-
26
- var styleType = nodeAttrs.type || 'text/css';
27
- var styleMedia = nodeAttrs.media || 'all';
28
- var styleKey = styleType + '_' + styleMedia;
29
- if (styleNodes[styleKey]) {
30
- var styleContent = (node.content || []).join(' ');
31
- styleNodes[styleKey].content.push(' ' + styleContent);
32
- return '';
33
- }
34
-
35
- node.content = node.content || [];
36
- styleNodes[styleKey] = node;
37
- return node;
38
- });
39
-
40
- return tree;
12
+ const styleNodes = {};
13
+ tree.match({
14
+ tag: 'style'
15
+ }, node => {
16
+ const nodeAttrs = node.attrs || {}; // Skip <style scoped></style>
17
+ // https://developer.mozilla.org/en/docs/Web/HTML/Element/style
18
+
19
+ if (nodeAttrs.scoped !== undefined) {
20
+ return node;
21
+ }
22
+
23
+ if ((0, _helpers.isAmpBoilerplate)(node)) {
24
+ return node;
25
+ }
26
+
27
+ const styleType = nodeAttrs.type || 'text/css';
28
+ const styleMedia = nodeAttrs.media || 'all';
29
+ const styleKey = styleType + '_' + styleMedia;
30
+
31
+ if (styleNodes[styleKey]) {
32
+ const styleContent = (node.content || []).join(' ');
33
+ styleNodes[styleKey].content.push(' ' + styleContent);
34
+ return '';
35
+ }
36
+
37
+ node.content = node.content || [];
38
+ styleNodes[styleKey] = node;
39
+ return node;
40
+ });
41
+ return tree;
41
42
  }
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = minifyConditionalComments;
7
+
8
+ var _htmlnano = _interopRequireDefault(require("../htmlnano"));
9
+
10
+ var _helpers = require("../helpers");
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ // Spec: https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/ms537512(v=vs.85)
15
+ const CONDITIONAL_COMMENT_REGEXP = /(<!--\[if\s+?[^<>[\]]+?]>)([\s\S]+?)(<!\[endif\]-->)/gm;
16
+ /** Minify content inside conditional comments */
17
+
18
+ async function minifyConditionalComments(tree, htmlnanoOptions) {
19
+ // forEach, tree.walk, tree.match just don't support Promise.
20
+ for (let i = 0, len = tree.length; i < len; i++) {
21
+ const node = tree[i];
22
+
23
+ if (typeof node === 'string' && (0, _helpers.isConditionalComment)(node)) {
24
+ tree[i] = await minifycontentInsideConditionalComments(node, htmlnanoOptions);
25
+ }
26
+
27
+ if (node.content && node.content.length) {
28
+ tree[i].content = await minifyConditionalComments(node.content, htmlnanoOptions);
29
+ }
30
+ }
31
+
32
+ return tree;
33
+ }
34
+
35
+ async function minifycontentInsideConditionalComments(text, htmlnanoOptions) {
36
+ let match;
37
+ const matches = []; // FIXME!
38
+ // String#matchAll is supported since Node.js 12
39
+
40
+ while ((match = CONDITIONAL_COMMENT_REGEXP.exec(text)) !== null) {
41
+ matches.push([match[1], match[2], match[3]]);
42
+ }
43
+
44
+ if (!matches.length) {
45
+ return Promise.resolve(text);
46
+ }
47
+
48
+ return Promise.all(matches.map(async match => {
49
+ const result = await _htmlnano.default.process(match[1], htmlnanoOptions, {}, {});
50
+ let minified = result.html;
51
+
52
+ if (match[1].includes('<html') && minified.includes('</html>')) {
53
+ minified = minified.replace('</html>', '');
54
+ }
55
+
56
+ return match[0] + minified + match[2];
57
+ }));
58
+ }