htmlnano 0.2.7 → 0.2.8
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.
- package/CHANGELOG.md +25 -0
- package/README.md +128 -0
- package/lib/helpers.js +4 -22
- package/lib/htmlnano.js +17 -47
- package/lib/modules/collapseAttributeWhitespace.js +6 -23
- package/lib/modules/collapseBooleanAttributes.js +5 -7
- package/lib/modules/collapseWhitespace.js +19 -24
- package/lib/modules/custom.js +2 -2
- package/lib/modules/deduplicateAttributeValues.js +8 -8
- package/lib/modules/mergeScripts.js +12 -17
- package/lib/modules/mergeStyles.js +8 -8
- package/lib/modules/minifyConditionalComments.js +52 -0
- package/lib/modules/minifyCss.js +13 -17
- package/lib/modules/minifyJs.js +19 -21
- package/lib/modules/minifyJson.js +3 -3
- package/lib/modules/minifySvg.js +9 -12
- package/lib/modules/minifyUrls.js +58 -55
- package/lib/modules/removeAttributeQuotes.js +1 -1
- package/lib/modules/removeComments.js +7 -9
- package/lib/modules/removeEmptyAttributes.js +5 -22
- package/lib/modules/removeOptionalTags.js +220 -0
- package/lib/modules/removeRedundantAttributes.js +33 -32
- package/lib/modules/removeUnusedCss.js +28 -52
- package/lib/modules/sortAttributes.js +120 -0
- package/lib/modules/sortAttributesWithLists.js +120 -7
- package/lib/presets/ampSafe.js +5 -55
- package/lib/presets/max.js +8 -56
- package/lib/presets/safe.js +7 -4
- package/package.json +16 -7
|
@@ -3,17 +3,17 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = mergeStyles;
|
|
7
7
|
|
|
8
8
|
var _helpers = require("../helpers");
|
|
9
9
|
|
|
10
10
|
/* Merge multiple <style> into one */
|
|
11
11
|
function mergeStyles(tree) {
|
|
12
|
-
|
|
12
|
+
const styleNodes = {};
|
|
13
13
|
tree.match({
|
|
14
14
|
tag: 'style'
|
|
15
|
-
},
|
|
16
|
-
|
|
15
|
+
}, node => {
|
|
16
|
+
const nodeAttrs = node.attrs || {}; // Skip <style scoped></style>
|
|
17
17
|
// https://developer.mozilla.org/en/docs/Web/HTML/Element/style
|
|
18
18
|
|
|
19
19
|
if (nodeAttrs.scoped !== undefined) {
|
|
@@ -24,12 +24,12 @@ function mergeStyles(tree) {
|
|
|
24
24
|
return node;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
const styleType = nodeAttrs.type || 'text/css';
|
|
28
|
+
const styleMedia = nodeAttrs.media || 'all';
|
|
29
|
+
const styleKey = styleType + '_' + styleMedia;
|
|
30
30
|
|
|
31
31
|
if (styleNodes[styleKey]) {
|
|
32
|
-
|
|
32
|
+
const styleContent = (node.content || []).join(' ');
|
|
33
33
|
styleNodes[styleKey].content.push(' ' + styleContent);
|
|
34
34
|
return '';
|
|
35
35
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
return match[0] + result.html + match[2];
|
|
51
|
+
}));
|
|
52
|
+
}
|
package/lib/modules/minifyCss.js
CHANGED
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = minifyCss;
|
|
7
7
|
|
|
8
8
|
var _helpers = require("../helpers");
|
|
9
9
|
|
|
10
10
|
var _cssnano = _interopRequireDefault(require("cssnano"));
|
|
11
11
|
|
|
12
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : {
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
const postcssOptions = {
|
|
15
15
|
// Prevent the following warning from being shown:
|
|
16
16
|
// > Without `from` option PostCSS could generate wrong source map and will not find Browserslist config.
|
|
17
17
|
// > Set it to CSS file path or to `undefined` to prevent this warning.
|
|
@@ -20,8 +20,8 @@ var postcssOptions = {
|
|
|
20
20
|
/** Minify CSS with cssnano */
|
|
21
21
|
|
|
22
22
|
function minifyCss(tree, options, cssnanoOptions) {
|
|
23
|
-
|
|
24
|
-
tree.walk(
|
|
23
|
+
let promises = [];
|
|
24
|
+
tree.walk(node => {
|
|
25
25
|
if ((0, _helpers.isStyleNode)(node)) {
|
|
26
26
|
promises.push(processStyleNode(node, cssnanoOptions));
|
|
27
27
|
} else if (node.attrs && node.attrs.style) {
|
|
@@ -30,26 +30,22 @@ function minifyCss(tree, options, cssnanoOptions) {
|
|
|
30
30
|
|
|
31
31
|
return node;
|
|
32
32
|
});
|
|
33
|
-
return Promise.all(promises).then(
|
|
34
|
-
return tree;
|
|
35
|
-
});
|
|
33
|
+
return Promise.all(promises).then(() => tree);
|
|
36
34
|
}
|
|
37
35
|
|
|
38
36
|
function processStyleNode(styleNode, cssnanoOptions) {
|
|
39
|
-
|
|
40
|
-
return _cssnano
|
|
41
|
-
return styleNode.content = [result.css];
|
|
42
|
-
});
|
|
37
|
+
const css = (0, _helpers.extractCssFromStyleNode)(styleNode);
|
|
38
|
+
return _cssnano.default.process(css, postcssOptions, cssnanoOptions).then(result => styleNode.content = [result.css]);
|
|
43
39
|
}
|
|
44
40
|
|
|
45
41
|
function processStyleAttr(node, cssnanoOptions) {
|
|
46
42
|
// CSS "color: red;" is invalid. Therefore it should be wrapped inside some selector:
|
|
47
43
|
// a{color: red;}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return _cssnano
|
|
52
|
-
|
|
44
|
+
const wrapperStart = 'a{';
|
|
45
|
+
const wrapperEnd = '}';
|
|
46
|
+
const wrappedStyle = wrapperStart + (node.attrs.style || '') + wrapperEnd;
|
|
47
|
+
return _cssnano.default.process(wrappedStyle, postcssOptions, cssnanoOptions).then(result => {
|
|
48
|
+
const minifiedCss = result.css; // Remove wrapperStart at the start and wrapperEnd at the end of minifiedCss
|
|
53
49
|
|
|
54
50
|
node.attrs.style = minifiedCss.substring(wrapperStart.length, minifiedCss.length - wrapperEnd.length);
|
|
55
51
|
});
|
package/lib/modules/minifyJs.js
CHANGED
|
@@ -3,19 +3,19 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = minifyJs;
|
|
7
7
|
|
|
8
8
|
var _terser = _interopRequireDefault(require("terser"));
|
|
9
9
|
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : {
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
11
|
|
|
12
12
|
/** Minify JS with Terser */
|
|
13
13
|
function minifyJs(tree, options, terserOptions) {
|
|
14
14
|
tree.match({
|
|
15
15
|
tag: 'script'
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
}, node => {
|
|
17
|
+
const nodeAttrs = node.attrs || {};
|
|
18
|
+
const mimeType = nodeAttrs.type || 'text/javascript';
|
|
19
19
|
|
|
20
20
|
if (mimeType === 'text/javascript' || mimeType === 'application/javascript') {
|
|
21
21
|
return processScriptNode(node, terserOptions);
|
|
@@ -25,35 +25,35 @@ function minifyJs(tree, options, terserOptions) {
|
|
|
25
25
|
});
|
|
26
26
|
tree.match({
|
|
27
27
|
attrs: true
|
|
28
|
-
},
|
|
28
|
+
}, node => {
|
|
29
29
|
return processNodeWithOnAttrs(node, terserOptions);
|
|
30
30
|
});
|
|
31
31
|
return tree;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
function stripCdata(js) {
|
|
35
|
-
|
|
35
|
+
const leftStrippedJs = js.replace(/\/\/\s*<!\[CDATA\[/, '');
|
|
36
36
|
|
|
37
37
|
if (leftStrippedJs === js) {
|
|
38
38
|
return js;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
const strippedJs = leftStrippedJs.replace(/\/\/\s*\]\]>/, '');
|
|
42
42
|
return leftStrippedJs === strippedJs ? js : strippedJs;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
function processScriptNode(scriptNode, terserOptions) {
|
|
46
|
-
|
|
46
|
+
let js = (scriptNode.content || []).join('').trim();
|
|
47
47
|
|
|
48
48
|
if (!js) {
|
|
49
49
|
return scriptNode;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
const strippedJs = stripCdata(js);
|
|
53
|
+
const isCdataWrapped = js !== strippedJs;
|
|
54
54
|
js = strippedJs;
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
const result = _terser.default.minify(js, terserOptions);
|
|
57
57
|
|
|
58
58
|
if (result.error) {
|
|
59
59
|
throw new Error(result.error);
|
|
@@ -63,7 +63,7 @@ function processScriptNode(scriptNode, terserOptions) {
|
|
|
63
63
|
return scriptNode;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
let content = result.code;
|
|
67
67
|
|
|
68
68
|
if (isCdataWrapped) {
|
|
69
69
|
content = '//<![CDATA[' + content + '//]]>';
|
|
@@ -74,12 +74,10 @@ function processScriptNode(scriptNode, terserOptions) {
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
function processNodeWithOnAttrs(node, terserOptions) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
for (var _i = 0, _Object$keys = Object.keys(node.attrs || {}); _i < _Object$keys.length; _i++) {
|
|
81
|
-
var attrName = _Object$keys[_i];
|
|
77
|
+
const jsWrapperStart = 'function _(){';
|
|
78
|
+
const jsWrapperEnd = '}';
|
|
82
79
|
|
|
80
|
+
for (const attrName of Object.keys(node.attrs || {})) {
|
|
83
81
|
if (attrName.search('on') !== 0) {
|
|
84
82
|
continue;
|
|
85
83
|
} // For example onclick="return false" is valid,
|
|
@@ -88,11 +86,11 @@ function processNodeWithOnAttrs(node, terserOptions) {
|
|
|
88
86
|
// "function _(){return false;}"
|
|
89
87
|
|
|
90
88
|
|
|
91
|
-
|
|
89
|
+
let wrappedJs = jsWrapperStart + node.attrs[attrName] + jsWrapperEnd;
|
|
92
90
|
|
|
93
|
-
|
|
91
|
+
let wrappedMinifiedJs = _terser.default.minify(wrappedJs, terserOptions).code;
|
|
94
92
|
|
|
95
|
-
|
|
93
|
+
let minifiedJs = wrappedMinifiedJs.substring(jsWrapperStart.length, wrappedMinifiedJs.length - jsWrapperEnd.length);
|
|
96
94
|
node.attrs[attrName] = minifiedJs;
|
|
97
95
|
}
|
|
98
96
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = minifyJson;
|
|
7
7
|
|
|
8
8
|
/* Minify JSON inside <script> tags */
|
|
9
9
|
function minifyJson(tree) {
|
|
@@ -13,8 +13,8 @@ function minifyJson(tree) {
|
|
|
13
13
|
attrs: {
|
|
14
14
|
type: /(\/|\+)json/
|
|
15
15
|
}
|
|
16
|
-
},
|
|
17
|
-
|
|
16
|
+
}, node => {
|
|
17
|
+
let content = (node.content || []).join('');
|
|
18
18
|
|
|
19
19
|
if (!content) {
|
|
20
20
|
return node;
|
package/lib/modules/minifySvg.js
CHANGED
|
@@ -3,24 +3,23 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = minifySvg;
|
|
7
7
|
|
|
8
8
|
var _svgo = _interopRequireDefault(require("svgo"));
|
|
9
9
|
|
|
10
10
|
var _posthtmlRender = _interopRequireDefault(require("posthtml-render"));
|
|
11
11
|
|
|
12
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : {
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
13
|
|
|
14
14
|
/** Minify SVG with SVGO */
|
|
15
|
-
function minifySvg(tree, options) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
var svgo = new _svgo["default"](svgoOptions);
|
|
15
|
+
function minifySvg(tree, options, svgoOptions = {}) {
|
|
16
|
+
let promises = [];
|
|
17
|
+
let svgo = new _svgo.default(svgoOptions);
|
|
19
18
|
tree.match({
|
|
20
19
|
tag: 'svg'
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
}, node => {
|
|
21
|
+
let svgStr = (0, _posthtmlRender.default)(node);
|
|
22
|
+
let promise = svgo.optimize(svgStr).then(result => {
|
|
24
23
|
node.tag = false;
|
|
25
24
|
node.attrs = {};
|
|
26
25
|
node.content = result.data;
|
|
@@ -28,7 +27,5 @@ function minifySvg(tree, options) {
|
|
|
28
27
|
promises.push(promise);
|
|
29
28
|
return node;
|
|
30
29
|
});
|
|
31
|
-
return Promise.all(promises).then(
|
|
32
|
-
return tree;
|
|
33
|
-
});
|
|
30
|
+
return Promise.all(promises).then(() => tree);
|
|
34
31
|
}
|
|
@@ -3,44 +3,37 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = minifyUrls;
|
|
7
7
|
|
|
8
8
|
var _relateurl = _interopRequireDefault(require("relateurl"));
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
var _srcset = _interopRequireDefault(require("srcset"));
|
|
11
11
|
|
|
12
|
-
function
|
|
13
|
-
|
|
14
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
15
|
-
|
|
16
|
-
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
17
|
-
|
|
18
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
19
|
-
|
|
20
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
21
|
-
|
|
22
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
23
|
-
|
|
24
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
25
|
-
|
|
26
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
|
|
27
|
-
|
|
28
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
29
|
-
|
|
30
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
31
13
|
|
|
32
14
|
// Adopts from https://github.com/kangax/html-minifier/blob/51ce10f4daedb1de483ffbcccecc41be1c873da2/src/htmlminifier.js#L209-L221
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return tagsHasHrefAttributes.has(tag) && attr === 'href' || tag === 'img' && attributesOfImgTagHasUriValues.has(attr) || tag === 'object' && attributesOfObjectTagHasUriValues.has(attr) || tag === 'q' && attr === 'cite' || tag === 'blockquote' && attr === 'cite' || (tag === 'ins' || tag === 'del') && attr === 'cite' || tag === 'form' && attr === 'action' || tag === 'input' && (attr === 'src' || attr === 'usemap') || tag === 'head' && attr === 'profile' || tag === 'script' && (attr === 'src' || attr === 'for')
|
|
15
|
+
const tagsHaveUriValuesForAttributes = new Set(['a', 'area', 'link', 'base', 'img', 'object', 'q', 'blockquote', 'ins', 'form', 'input', 'head', 'script']);
|
|
16
|
+
const REGEXP_TAGS_HAVE_URI_VSLUES_FOR_ATTRIBUTES = new RegExp('^(' + [...tagsHaveUriValuesForAttributes].join('|') + ')$');
|
|
17
|
+
const tagsHasHrefAttributes = new Set(['a', 'area', 'link', 'base']);
|
|
18
|
+
const attributesOfImgTagHasUriValues = new Set(['src', 'longdesc', 'usemap']);
|
|
19
|
+
const attributesOfObjectTagHasUriValues = new Set(['classid', 'codebase', 'data', 'usemap']);
|
|
20
|
+
|
|
21
|
+
const isUriTypeAttribute = (tag, attr) => {
|
|
22
|
+
return tagsHasHrefAttributes.has(tag) && attr === 'href' || tag === 'img' && attributesOfImgTagHasUriValues.has(attr) || tag === 'object' && attributesOfObjectTagHasUriValues.has(attr) || tag === 'q' && attr === 'cite' || tag === 'blockquote' && attr === 'cite' || (tag === 'ins' || tag === 'del') && attr === 'cite' || tag === 'form' && attr === 'action' || tag === 'input' && (attr === 'src' || attr === 'usemap') || tag === 'head' && attr === 'profile' || tag === 'script' && (attr === 'src' || attr === 'for') ||
|
|
23
|
+
/**
|
|
24
|
+
* https://html.spec.whatwg.org/#attr-source-src
|
|
25
|
+
*
|
|
26
|
+
* Although most of browsers recommend not to use "src" in <source>,
|
|
27
|
+
* but technically it does comply with HTML Standard.
|
|
28
|
+
*/
|
|
29
|
+
tag === 'source' && attr === 'src';
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const isSrcsetAttribute = (tag, attr) => {
|
|
33
|
+
return tag === 'source' && attr === 'srcset' || tag === 'img' && attr === 'srcset' || tag === 'link' && attr === 'imagesrcset';
|
|
41
34
|
};
|
|
42
35
|
|
|
43
|
-
|
|
36
|
+
const processModuleOptions = options => {
|
|
44
37
|
// FIXME!
|
|
45
38
|
// relateurl@1.0.0-alpha only supports URL while stable version (0.2.7) only supports string
|
|
46
39
|
// should convert input into URL instance after relateurl@1 is stable
|
|
@@ -49,29 +42,26 @@ var processModuleOptions = function processModuleOptions(options) {
|
|
|
49
42
|
return false;
|
|
50
43
|
};
|
|
51
44
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
45
|
+
const isLinkRelCanonical = ({
|
|
46
|
+
tag,
|
|
47
|
+
attrs
|
|
48
|
+
}) => {
|
|
55
49
|
// Return false early for non-"link" tag
|
|
56
50
|
if (tag !== 'link') return false;
|
|
57
51
|
|
|
58
|
-
for (
|
|
59
|
-
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
60
|
-
attrName = _Object$entries$_i[0],
|
|
61
|
-
attrValue = _Object$entries$_i[1];
|
|
62
|
-
|
|
52
|
+
for (const [attrName, attrValue] of Object.entries(attrs)) {
|
|
63
53
|
if (attrName.toLowerCase() === 'rel' && attrValue === 'canonical') return true;
|
|
64
54
|
}
|
|
65
55
|
|
|
66
56
|
return false;
|
|
67
57
|
};
|
|
68
58
|
|
|
69
|
-
|
|
70
|
-
|
|
59
|
+
let relateUrlInstance;
|
|
60
|
+
let STORED_URL_BASE;
|
|
71
61
|
/** Convert absolute url into relative url */
|
|
72
62
|
|
|
73
63
|
function minifyUrls(tree, options, moduleOptions) {
|
|
74
|
-
|
|
64
|
+
const urlBase = processModuleOptions(moduleOptions); // Invalid configuration, return tree directly
|
|
75
65
|
|
|
76
66
|
if (!urlBase) return tree;
|
|
77
67
|
/** Bring up a reusable RelateUrl instances (only once)
|
|
@@ -82,30 +72,43 @@ function minifyUrls(tree, options, moduleOptions) {
|
|
|
82
72
|
*/
|
|
83
73
|
|
|
84
74
|
if (!relateUrlInstance || STORED_URL_BASE !== urlBase) {
|
|
85
|
-
relateUrlInstance = new _relateurl
|
|
75
|
+
relateUrlInstance = new _relateurl.default(urlBase);
|
|
86
76
|
STORED_URL_BASE = urlBase;
|
|
87
77
|
}
|
|
88
78
|
|
|
89
79
|
tree.match({
|
|
90
80
|
tag: REGEXP_TAGS_HAVE_URI_VSLUES_FOR_ATTRIBUTES
|
|
91
|
-
},
|
|
81
|
+
}, node => {
|
|
92
82
|
if (!node.attrs) return node; // Prevent link[rel=canonical] being processed
|
|
93
83
|
// Can't be excluded by isUriTypeAttribute()
|
|
94
84
|
|
|
95
85
|
if (isLinkRelCanonical(node)) return node;
|
|
96
86
|
|
|
97
|
-
for (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
87
|
+
for (const [attrName, attrValue] of Object.entries(node.attrs)) {
|
|
88
|
+
const attrNameLower = attrName.toLowerCase();
|
|
89
|
+
|
|
90
|
+
if (isUriTypeAttribute(node.tag, attrNameLower)) {
|
|
91
|
+
// FIXME!
|
|
92
|
+
// relateurl@1.0.0-alpha only supports URL while stable version (0.2.7) only supports string
|
|
93
|
+
// the WHATWG URL API is very strict while attrValue might not be a valid URL
|
|
94
|
+
// new URL should be used, and relateUrl#relate should be wrapped in try...catch after relateurl@1 is stable
|
|
95
|
+
node.attrs[attrName] = relateUrlInstance.relate(attrValue);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (isSrcsetAttribute(node.tag, attrNameLower)) {
|
|
100
|
+
try {
|
|
101
|
+
const parsedSrcset = _srcset.default.parse(attrValue);
|
|
102
|
+
|
|
103
|
+
node.attrs[attrName] = _srcset.default.stringify(parsedSrcset.map(srcset => {
|
|
104
|
+
srcset.url = relateUrlInstance.relate(srcset.url);
|
|
105
|
+
return srcset;
|
|
106
|
+
}));
|
|
107
|
+
} catch (e) {// srcset will throw an Error for invalid srcset.
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
109
112
|
}
|
|
110
113
|
|
|
111
114
|
return node;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = removeAttributeQuotes;
|
|
7
7
|
|
|
8
8
|
// Specification: https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
|
|
9
9
|
// See also: https://github.com/posthtml/posthtml-render/pull/30
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = removeComments;
|
|
7
7
|
|
|
8
8
|
var _helpers = require("../helpers");
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
const MATCH_EXCERPT_REGEXP = /<!-- ?more ?-->/i;
|
|
11
11
|
/** Removes HTML comments */
|
|
12
12
|
|
|
13
13
|
function removeComments(tree, options, removeType) {
|
|
@@ -15,11 +15,9 @@ function removeComments(tree, options, removeType) {
|
|
|
15
15
|
removeType = 'safe';
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
tree.walk(
|
|
18
|
+
tree.walk(node => {
|
|
19
19
|
if (node.contents && node.contents.length) {
|
|
20
|
-
node.contents = node.contents.filter(
|
|
21
|
-
return !isCommentToRemove(content, removeType);
|
|
22
|
-
});
|
|
20
|
+
node.contents = node.contents.filter(content => !isCommentToRemove(content, removeType));
|
|
23
21
|
} else if (isCommentToRemove(node, removeType)) {
|
|
24
22
|
node = '';
|
|
25
23
|
}
|
|
@@ -40,14 +38,14 @@ function isCommentToRemove(text, removeType) {
|
|
|
40
38
|
}
|
|
41
39
|
|
|
42
40
|
if (removeType === 'safe') {
|
|
43
|
-
|
|
41
|
+
const isNoindex = text === '<!--noindex-->' || text === '<!--/noindex-->'; // Don't remove noindex comments.
|
|
44
42
|
// See: https://yandex.com/support/webmaster/controlling-robot/html.xml
|
|
45
43
|
|
|
46
44
|
if (isNoindex) {
|
|
47
45
|
return false;
|
|
48
46
|
}
|
|
49
47
|
|
|
50
|
-
|
|
48
|
+
const isServerSideExclude = text === '<!--sse-->' || text === '<!--/sse-->'; // Don't remove sse comments.
|
|
51
49
|
// See: https://support.cloudflare.com/hc/en-us/articles/200170036-What-does-Server-Side-Excludes-SSE-do-
|
|
52
50
|
|
|
53
51
|
if (isServerSideExclude) {
|
|
@@ -63,7 +61,7 @@ function isCommentToRemove(text, removeType) {
|
|
|
63
61
|
// Jekyll: https://jekyllrb.com/docs/posts/#post-excerpts
|
|
64
62
|
|
|
65
63
|
|
|
66
|
-
|
|
64
|
+
const isCMSExcerptComment = MATCH_EXCERPT_REGEXP.test(text);
|
|
67
65
|
|
|
68
66
|
if (isCMSExcerptComment) {
|
|
69
67
|
return false;
|
|
@@ -3,36 +3,19 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
9
|
-
|
|
10
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
11
|
-
|
|
12
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
13
|
-
|
|
14
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
15
|
-
|
|
16
|
-
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
17
|
-
|
|
18
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
19
|
-
|
|
6
|
+
exports.default = removeEmptyAttributes;
|
|
20
7
|
// Source: https://www.w3.org/TR/html4/sgml/dtd.html#events (Generic Attributes)
|
|
21
|
-
|
|
8
|
+
const safeToRemoveAttrs = new Set(['id', 'class', 'style', 'title', 'lang', 'dir', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup', 'onmouseover', 'onmousemove', 'onmouseout', 'onkeypress', 'onkeydown', 'onkeyup']);
|
|
22
9
|
/** Removes empty attributes */
|
|
23
10
|
|
|
24
11
|
function removeEmptyAttributes(tree) {
|
|
25
|
-
tree.walk(
|
|
12
|
+
tree.walk(node => {
|
|
26
13
|
if (!node.attrs) {
|
|
27
14
|
return node;
|
|
28
15
|
}
|
|
29
16
|
|
|
30
|
-
Object.entries(node.attrs).forEach(
|
|
31
|
-
|
|
32
|
-
attrName = _ref2[0],
|
|
33
|
-
attrValue = _ref2[1];
|
|
34
|
-
|
|
35
|
-
var attrNameLower = attrName.toLowerCase();
|
|
17
|
+
Object.entries(node.attrs).forEach(([attrName, attrValue]) => {
|
|
18
|
+
const attrNameLower = attrName.toLowerCase();
|
|
36
19
|
|
|
37
20
|
if (!safeToRemoveAttrs.has(attrNameLower)) {
|
|
38
21
|
return;
|