@wordpress/dom
Version:
DOM utilities module for WordPress.
139 lines (138 loc) • 5.37 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var clean_node_list_exports = {};
__export(clean_node_list_exports, {
default: () => cleanNodeList
});
module.exports = __toCommonJS(clean_node_list_exports);
var import_is_empty = __toESM(require("./is-empty"));
var import_remove = __toESM(require("./remove"));
var import_unwrap = __toESM(require("./unwrap"));
var import_phrasing_content = require("../phrasing-content");
var import_insert_after = __toESM(require("./insert-after"));
var import_is_element = __toESM(require("./is-element"));
const noop = () => {
};
function cleanNodeList(nodeList, doc, schema, inline) {
Array.from(nodeList).forEach(
(node) => {
const tag = node.nodeName.toLowerCase();
if (schema.hasOwnProperty(tag) && (!schema[tag].isMatch || schema[tag].isMatch?.(node))) {
if ((0, import_is_element.default)(node)) {
const {
attributes = [],
classes = [],
children,
require: require2 = [],
allowEmpty
} = schema[tag];
if (children && !allowEmpty && (0, import_is_empty.default)(node)) {
(0, import_remove.default)(node);
return;
}
if (node.hasAttributes()) {
Array.from(node.attributes).forEach(({ name }) => {
if (name !== "class" && !attributes.includes(name)) {
node.removeAttribute(name);
}
});
if (node.classList && node.classList.length) {
const mattchers = classes.map((item) => {
if (item === "*") {
return () => true;
} else if (typeof item === "string") {
return (className) => className === item;
} else if (item instanceof RegExp) {
return (className) => item.test(className);
}
return noop;
});
Array.from(node.classList).forEach((name) => {
if (!mattchers.some(
(isMatch) => isMatch(name)
)) {
node.classList.remove(name);
}
});
if (!node.classList.length) {
node.removeAttribute("class");
}
}
}
if (node.hasChildNodes()) {
if (children === "*") {
return;
}
if (children) {
if (require2.length && !node.querySelector(require2.join(","))) {
cleanNodeList(
node.childNodes,
doc,
schema,
inline
);
(0, import_unwrap.default)(node);
} else if (node.parentNode && node.parentNode.nodeName === "BODY" && (0, import_phrasing_content.isPhrasingContent)(node)) {
cleanNodeList(
node.childNodes,
doc,
schema,
inline
);
if (Array.from(node.childNodes).some(
(child) => !(0, import_phrasing_content.isPhrasingContent)(child)
)) {
(0, import_unwrap.default)(node);
}
} else {
cleanNodeList(
node.childNodes,
doc,
children,
inline
);
}
} else {
while (node.firstChild) {
(0, import_remove.default)(node.firstChild);
}
}
}
}
} else {
cleanNodeList(node.childNodes, doc, schema, inline);
if (inline && !(0, import_phrasing_content.isPhrasingContent)(node) && node.nextElementSibling) {
(0, import_insert_after.default)(doc.createElement("br"), node);
}
(0, import_unwrap.default)(node);
}
}
);
}
//# sourceMappingURL=clean-node-list.js.map