|
|
@@ -121,7 +121,7 @@ this.htmlnano = this.htmlnano || (() => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- const modules = [collapseBooleanAttributes, collapseWhitespace, removeComments, removeEmptyAttributes, removeRedundantAttributes];
|
|
|
+ const modules = [collapseBooleanAttributes, mergeTextNodes, collapseWhitespace, removeComments, removeEmptyAttributes, removeRedundantAttributes];
|
|
|
|
|
|
return {
|
|
|
process: doc => {
|
|
|
@@ -148,13 +148,17 @@ this.htmlnano = this.htmlnano || (() => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /** Collapses redundant whitespaces */
|
|
|
- function collapseWhitespace(node) {
|
|
|
+ function mergeTextNodes(node) {
|
|
|
if (node.nodeType == Node.TEXT_NODE) {
|
|
|
if (node.previousSibling && node.previousSibling.nodeType == Node.TEXT_NODE) {
|
|
|
node.textContent = node.previousSibling.textContent + node.textContent;
|
|
|
node.previousSibling.remove();
|
|
|
}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function collapseWhitespace(node) {
|
|
|
+ if (node.nodeType == Node.TEXT_NODE) {
|
|
|
let element = node.parentElement;
|
|
|
let textContent = node.textContent;
|
|
|
while (noWhitespaceCollapse(element)) {
|
|
|
@@ -175,14 +179,12 @@ this.htmlnano = this.htmlnano || (() => {
|
|
|
return element && !noWhitespaceCollapseElements.includes(element.tagName.toLowerCase());
|
|
|
}
|
|
|
|
|
|
- /** Removes HTML comments */
|
|
|
function removeComments(node) {
|
|
|
if (node.nodeType == Node.COMMENT_NODE) {
|
|
|
return !node.textContent.toLowerCase().trim().startsWith("[if");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /** Removes empty attributes */
|
|
|
function removeEmptyAttributes(node) {
|
|
|
if (node.nodeType == Node.ELEMENT_NODE) {
|
|
|
node.getAttributeNames().forEach(attributeName => {
|
|
|
@@ -196,7 +198,6 @@ this.htmlnano = this.htmlnano || (() => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /** Removes redundant attributes */
|
|
|
function removeRedundantAttributes(node) {
|
|
|
if (node.nodeType == Node.ELEMENT_NODE) {
|
|
|
const tagRedundantAttributes = redundantAttributes[node.tagName.toLowerCase()];
|