@pixui-dev/pixui-richtext-helper 0.2.6-test.1 → 0.2.6

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.
@@ -148,7 +148,10 @@ var RichTextCore = /** @class */ (function () {
148
148
  // 重置全局状态
149
149
  RichTextCore.linkNodes = [];
150
150
  RichTextCore.hrefIdCnt = 0;
151
- var $1 = cheerio.load(str, null, false);
151
+ var preEncodedStr = str.replace(/href="([^"]*?)"/g, function (_match, hrefVal) {
152
+ return "href=\"".concat(encodeURIComponent(hrefVal), "\"");
153
+ });
154
+ var $1 = cheerio.load(preEncodedStr, null, false);
152
155
  //检查html,如果有text节点,说明是转换过的,直接返回
153
156
  if ($1("text").length > 0) {
154
157
  return str;
@@ -171,9 +174,38 @@ var RichTextCore = /** @class */ (function () {
171
174
  // 直接查询统一的 class
172
175
  setTimeout(function () {
173
176
  var elements = Array.from(document.querySelectorAll(".".concat(RichTextCore.LINK_NODE_CLASS)));
177
+ // HTML 实体解码工具函数(纯算法实现,避免依赖 textarea 节点)
178
+ var decodeHtmlEntities = function (str) {
179
+ if (!str)
180
+ return str;
181
+ var named = {
182
+ quot: '"',
183
+ amp: "&",
184
+ lt: "<",
185
+ gt: ">",
186
+ apos: "'",
187
+ };
188
+ return str.replace(/&(#(?:x[0-9a-fA-F]+|\d+)|[a-zA-Z]+);/g, function (_, ent) {
189
+ if (ent[0] === "#") {
190
+ // 数字实体
191
+ var isHex = ent[1].toLowerCase() === "x";
192
+ var numStr = isHex ? ent.slice(2) : ent.slice(1);
193
+ var codePoint = parseInt(numStr, isHex ? 16 : 10);
194
+ if (!isNaN(codePoint)) {
195
+ return String.fromCodePoint(codePoint);
196
+ }
197
+ return _; // 解析失败,原样返回
198
+ }
199
+ // 命名实体
200
+ if (named.hasOwnProperty(ent))
201
+ return named[ent];
202
+ return _; // 未知实体,原样返回
203
+ });
204
+ };
174
205
  elements.forEach(function (el) {
175
206
  var hrefEncoded = el.getAttribute("data-link-href") || "";
176
- var href = decodeURIComponent(hrefEncoded);
207
+ // URI 解码,再做 HTML 实体解码
208
+ var href = decodeHtmlEntities(decodeURIComponent(hrefEncoded));
177
209
  var typeStr = el.getAttribute("data-link-type");
178
210
  var type = typeStr === LinkNodeType.IMG ? LinkNodeType.IMG : LinkNodeType.DIV;
179
211
  // 避免重复绑定
@@ -257,8 +289,12 @@ var RichTextCore = /** @class */ (function () {
257
289
  if (tagName === "div" || tagName === "text") {
258
290
  return []; // 丢弃空 div / text
259
291
  }
292
+ else if (tagName === "img") {
293
+ // img标签直接返回,不包装在div中
294
+ return [$.html(node)];
295
+ }
260
296
  else {
261
- // 其它空标签保留(img...)
297
+ // 其它空标签保留
262
298
  return [wrapNode($elem, "")];
263
299
  }
264
300
  }
@@ -523,7 +559,7 @@ var RichTextCore = /** @class */ (function () {
523
559
  mergedStyles.push(innerStyle);
524
560
  });
525
561
  var finalStyle = mergeStyles.apply(void 0, __spreadArray([originalStyle], mergedStyles, false));
526
- var encodedHref = href ? encodeURIComponent(href) : "";
562
+ var encodedHref = href || ""; // 已在预处理阶段编码,避免二次编码
527
563
  if (href) {
528
564
  self.linkNodes.push({ type: LinkNodeType.DIV, href: href, id: id });
529
565
  }
@@ -610,7 +646,7 @@ var RichTextCore = /** @class */ (function () {
610
646
  self.linkNodes.push({ type: LinkNodeType.IMG, href: href, id: id });
611
647
  }
612
648
  // 将链接信息以转义形式保存在 data- 属性中,避免默认跳转
613
- var encodedHref = href ? encodeURIComponent(href) : "";
649
+ var encodedHref = href || ""; // 已在预处理阶段编码,避免二次编码
614
650
  $(this).attr("data-link-type", "img");
615
651
  $(this).attr("data-link-href", encodedHref);
616
652
  $(this).attr("href", "");
@@ -698,7 +734,8 @@ var RichTextCore = /** @class */ (function () {
698
734
  var attribs = ($(node).get(0) || {}).attribs || {};
699
735
  for (var _i = 0, _a = Object.entries(attribs); _i < _a.length; _i++) {
700
736
  var _b = _a[_i], k = _b[0], v = _b[1];
701
- if (k !== "style" && k !== "id") {
737
+ // 跳过styleid和href属性,避免重复处理或错误处理
738
+ if (k !== "style" && k !== "id" && k !== "href") {
702
739
  customAttrs += " ".concat(k, "=\"").concat(v, "\"");
703
740
  }
704
741
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pixui-dev/pixui-richtext-helper",
3
- "version": "0.2.6-test.1",
3
+ "version": "0.2.6",
4
4
  "description": "pixui richtext helper",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",