@iimm/shared
Version:
shared utils on browser and react env
238 lines (236 loc) • 7.37 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/cjs/html/htmlJsonConvert/index.ts
var htmlJsonConvert_exports = {};
__export(htmlJsonConvert_exports, {
htmlToJson: () => htmlToJson,
inlineStyleToObjectStyle: () => inlineStyleToObjectStyle,
jsonToHtml: () => jsonToHtml
});
module.exports = __toCommonJS(htmlJsonConvert_exports);
var import_htmlparser2 = require("htmlparser2");
var import_string = require("../string");
var numberStyleValueRegexp = /^\-?((\d+(\.\d*)?)|(\.\d+))$/;
var scriptRegexp = /^script$/i;
var styleRegexp = /^style$/i;
var selfCloseTagRegexp = /^(meta|base|br|img|input|col|frame|link|area|param|embed|keygen|source)$/i;
var inlineStyleToObjectStyle = (style = "", options) => {
const { styleCamelCase, objectStyleConvertFn } = options || {};
const objStyle = {};
if (typeof style === "string" && style) {
const styles = style.split(";");
styles.forEach((item) => {
item = item.trim();
if (item) {
let [prop, value] = item.split(":");
prop = prop == null ? void 0 : prop.trim();
value = value == null ? void 0 : value.trim();
if (prop && value) {
objStyle[styleCamelCase ? (0, import_string.camelCase)(prop) : prop] = numberStyleValueRegexp.test(value) ? Number(value) : value;
}
}
});
}
return typeof objectStyleConvertFn === "function" ? objectStyleConvertFn(objStyle) : objStyle;
};
var htmlToJson = (html, options) => {
const {
skipStyle,
skipComment,
skipScript,
skipAttributes,
skipClass,
attributeNameFilter,
keepInlineStyle,
styleCamelCase,
attributesCamelCase,
objectStyleConvertFn,
ATTRIBUTES = "attributes",
NODENAME = "name",
NODETAG = "element",
TEXTTAG = "text",
TEXTVALUE = "text",
COMMENTTAG = "comment",
COMMENTVALUE = "comment",
CHILDREN = "elements",
CLASSLIST = "classList",
STYLE = "style",
INLINESTYLE = "inlineStyle"
} = options || {};
const json = [];
let levelNodes = [];
const parser = new import_htmlparser2.Parser({
onopentag(name, { style, class: classNames, ...attrs }) {
let node = {};
if (scriptRegexp.test(name) && skipScript || styleRegexp.test(name) && skipStyle) {
node = null;
} else {
node = {
type: NODETAG,
[NODENAME]: name,
[CHILDREN]: []
};
if (!skipClass) {
node[CLASSLIST] = (classNames || "").split(/\s+/).filter(Boolean);
}
if (!skipAttributes) {
let keys = Object.keys(attrs);
if (typeof attributeNameFilter === "function" && keys.length) {
keys = keys.filter(attributeNameFilter);
}
node[ATTRIBUTES] = {};
keys.forEach((ele) => {
node[ATTRIBUTES][attributesCamelCase ? (0, import_string.camelCase)(ele) : ele] = attrs[ele];
});
}
if (!skipStyle) {
node[STYLE] = inlineStyleToObjectStyle(style, { styleCamelCase, objectStyleConvertFn });
if (style && keepInlineStyle) {
node[INLINESTYLE] = style.replace(/"/g, "");
}
}
}
if (levelNodes[0]) {
if (node) {
const parent = levelNodes[0];
parent[CHILDREN].push(node);
}
levelNodes.unshift(node);
} else {
if (node) {
json.push(node);
}
levelNodes.push(node);
}
},
ontext(text) {
const parent = levelNodes[0];
if (parent === null)
return;
const node = { type: TEXTTAG, [TEXTVALUE]: text };
if (parent === void 0) {
json.push(node);
} else {
if (!parent[CHILDREN]) {
parent[CHILDREN] = [];
}
parent[CHILDREN].push(node);
}
},
oncomment(commonts) {
if (skipComment)
return;
const parent = levelNodes[0];
if (parent === null)
return;
const node = { type: COMMENTTAG, [COMMENTVALUE]: commonts };
if (!parent) {
json.push(node);
} else {
if (!parent[CHILDREN]) {
parent[CHILDREN] = [];
}
parent[CHILDREN].push(node);
}
},
onclosetag() {
levelNodes.shift();
},
onend() {
levelNodes = null;
}
});
parser.end(html);
return json;
};
var setAttrs = (attrs, results) => {
Object.keys(attrs || {}).forEach((k) => {
if (attrs[k] === void 0) {
results.push((0, import_string.separatorCase)(k));
} else {
results.push(" ", (0, import_string.separatorCase)(k), "=", '"', attrs[k], '"');
}
});
};
var toElement = (elementInfo, results, options) => {
var _a;
const {
ATTRIBUTES = "attributes",
NODENAME = "name",
NODETAG = "element",
TEXTTAG = "text",
TEXTVALUE = "text",
COMMENTTAG = "comment",
COMMENTVALUE = "comment",
CHILDREN = "elements",
CLASSLIST = "classList",
STYLE = "style",
INLINESTYLE = "inlineStyle"
} = options || {};
const tagName = elementInfo[NODENAME];
let inlineStyle = elementInfo[INLINESTYLE];
switch (elementInfo.type) {
case NODETAG:
results.push("<", tagName);
if (!inlineStyle && elementInfo[STYLE]) {
const styleKeys = Object.keys(elementInfo[STYLE]);
inlineStyle = "";
for (let i = 0; i < styleKeys.length; i++) {
const k = styleKeys[i];
inlineStyle += `${k}:${elementInfo[STYLE][k]};`;
}
}
if (inlineStyle) {
results.push(' style="', inlineStyle, '"');
}
if ((_a = elementInfo[CLASSLIST]) == null ? void 0 : _a.length) {
results.push(' class="', elementInfo[CLASSLIST].join(" "), '"');
}
setAttrs(elementInfo[ATTRIBUTES], results);
if (selfCloseTagRegexp.test(tagName)) {
results.push(" />");
} else {
results.push(">");
if (Array.isArray(elementInfo[CHILDREN])) {
elementInfo[CHILDREN].forEach((item) => toElement(item, results, options));
}
results.push("</", tagName, ">");
}
break;
case TEXTTAG:
results.push(elementInfo[TEXTVALUE]);
break;
case COMMENTTAG:
results.push("<!-- ", elementInfo[COMMENTVALUE], " -->");
break;
default:
}
};
var jsonToHtml = (json, options) => {
const result = [];
if (json) {
(Array.isArray(json) ? json : [json]).forEach((item) => toElement(item, result, options));
}
return result.join("");
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
htmlToJson,
inlineStyleToObjectStyle,
jsonToHtml
});