@whitesev/domutils 1.5.0 → 1.5.2

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.
@@ -58,6 +58,22 @@ var DOMUtils = (function () {
58
58
  isShow(element) {
59
59
  return Boolean(element.getClientRects().length);
60
60
  },
61
+ /**
62
+ * 获取安全的html
63
+ */
64
+ getSafeHTML(text) {
65
+ // @ts-ignore
66
+ if (globalThis.trustedTypes) {
67
+ // @ts-ignore
68
+ const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
69
+ createHTML: (html) => html,
70
+ });
71
+ return policy.createHTML(text);
72
+ }
73
+ else {
74
+ return text;
75
+ }
76
+ },
61
77
  /**
62
78
  * 在CSP策略下设置innerHTML
63
79
  * @param $el 元素
@@ -65,22 +81,7 @@ var DOMUtils = (function () {
65
81
  */
66
82
  setSafeHTML($el, text) {
67
83
  // 创建 TrustedHTML 策略(需 CSP 允许)
68
- try {
69
- $el.innerHTML = text;
70
- }
71
- catch (error) {
72
- // @ts-ignore
73
- if (globalThis.trustedTypes) {
74
- // @ts-ignore
75
- const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
76
- createHTML: (html) => html,
77
- });
78
- $el.innerHTML = policy.createHTML(text);
79
- }
80
- else {
81
- throw new Error("trustedTypes is not defined");
82
- }
83
- }
84
+ $el.innerHTML = this.getSafeHTML(text);
84
85
  },
85
86
  /**
86
87
  * 用于显示元素并获取它的高度宽度等其它属性
@@ -1060,7 +1061,7 @@ var DOMUtils = (function () {
1060
1061
  super(option);
1061
1062
  }
1062
1063
  /** 版本号 */
1063
- version = "2025.3.2";
1064
+ version = "2025.4.8";
1064
1065
  attr(element, attrName, attrValue) {
1065
1066
  let DOMUtilsContext = this;
1066
1067
  if (typeof element === "string") {
@@ -1203,6 +1204,19 @@ var DOMUtils = (function () {
1203
1204
  else ;
1204
1205
  return;
1205
1206
  }
1207
+ let setStyleProperty = (propertyName, propertyValue) => {
1208
+ if (propertyValue === "string" && propertyValue.includes("!important")) {
1209
+ propertyValue = propertyValue
1210
+ .trim()
1211
+ .replace(/!important$/gi, "")
1212
+ .trim();
1213
+ element.style.setProperty(propertyName, propertyValue, "important");
1214
+ }
1215
+ else {
1216
+ propertyValue = handlePixe(propertyName, propertyValue);
1217
+ element.style.setProperty(propertyName, propertyValue);
1218
+ }
1219
+ };
1206
1220
  if (typeof property === "string") {
1207
1221
  if (value == null) {
1208
1222
  return DOMUtilsContext.windowApi.globalThis
@@ -1210,28 +1224,19 @@ var DOMUtils = (function () {
1210
1224
  .getPropertyValue(property);
1211
1225
  }
1212
1226
  else {
1213
- if (value === "string" && value.includes("!important")) {
1214
- element.style.setProperty(property, value, "important");
1215
- }
1216
- else {
1217
- value = handlePixe(property, value);
1218
- element.style.setProperty(property, value);
1219
- }
1227
+ setStyleProperty(property, value);
1220
1228
  }
1221
1229
  }
1222
1230
  else if (typeof property === "object") {
1223
1231
  for (let prop in property) {
1224
- if (typeof property[prop] === "string" &&
1225
- property[prop].includes("!important")) {
1226
- element.style.setProperty(prop, property[prop], "important");
1227
- }
1228
- else {
1229
- property[prop] = handlePixe(prop, property[prop]);
1230
- element.style.setProperty(prop, property[prop]);
1231
- }
1232
+ let value = property[prop];
1233
+ setStyleProperty(prop, value);
1232
1234
  }
1233
1235
  }
1234
- else ;
1236
+ else {
1237
+ // 其他情况
1238
+ throw new TypeError("property must be string or object");
1239
+ }
1235
1240
  }
1236
1241
  text(element, text) {
1237
1242
  let DOMUtilsContext = this;
@@ -1627,7 +1632,7 @@ var DOMUtils = (function () {
1627
1632
  }
1628
1633
  function elementAppendChild(ele, text) {
1629
1634
  if (typeof content === "string") {
1630
- ele.insertAdjacentHTML("beforeend", text);
1635
+ ele.insertAdjacentHTML("beforeend", DOMUtilsCommonUtils.getSafeHTML(text));
1631
1636
  }
1632
1637
  else {
1633
1638
  ele.appendChild(text);
@@ -1673,7 +1678,7 @@ var DOMUtils = (function () {
1673
1678
  return;
1674
1679
  }
1675
1680
  if (typeof content === "string") {
1676
- element.insertAdjacentHTML("afterbegin", content);
1681
+ element.insertAdjacentHTML("afterbegin", DOMUtilsCommonUtils.getSafeHTML(content));
1677
1682
  }
1678
1683
  else {
1679
1684
  let $firstChild = element.firstChild;
@@ -1710,7 +1715,7 @@ var DOMUtils = (function () {
1710
1715
  return;
1711
1716
  }
1712
1717
  if (typeof content === "string") {
1713
- element.insertAdjacentHTML("afterend", content);
1718
+ element.insertAdjacentHTML("afterend", DOMUtilsCommonUtils.getSafeHTML(content));
1714
1719
  }
1715
1720
  else {
1716
1721
  let $parent = element.parentElement;
@@ -1749,7 +1754,7 @@ var DOMUtils = (function () {
1749
1754
  return;
1750
1755
  }
1751
1756
  if (typeof content === "string") {
1752
- element.insertAdjacentHTML("beforebegin", content);
1757
+ element.insertAdjacentHTML("beforebegin", DOMUtilsCommonUtils.getSafeHTML(content));
1753
1758
  }
1754
1759
  else {
1755
1760
  let $parent = element.parentElement;