@weapp-core/shared 3.5.1-dev.20261117.1 → 3.5.2-dev.20261119.1

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.
package/README.md CHANGED
@@ -44,7 +44,7 @@ import { normalizeRichTextNodes } from '@weapp-core/shared/rich-text'
44
44
  const nodes = normalizeRichTextNodes('<p>Hello <strong>world</strong></p>')
45
45
  ```
46
46
 
47
- 该子入口接受 HTML 字符串或结构化节点,返回命名的 `SafeRichTextNode` 文本/元素联合类型。Web 原生组件与 headless 模拟器共用同一份标签、属性、URL 和样式规则;输出是惰性的内容树,不执行脚本或模板。宿主保留自己的元数据边界,例如模拟器另外拒绝外部 `data-sim-*` 属性。
47
+ 该子入口接受 HTML 字符串或结构化节点,返回命名的 `SafeRichTextNode` 文本/元素联合类型。`normalizeRichTextNodes` 保留 Web 的安全规范化策略;`normalizeNativeRichTextNodes` 和 `sanitizeNativeRichTextAttributes` 使用微信原生富文本规则:未知元素及子树被移除,数组文本实体仅解码一次,属性仅保留官方逐标签允许项及安全 class/style,不保留 id、href、事件或 `data-sim-*`。两个策略共用节点类型、HTML 解析器和安全样式检查,但具有独立的规范化函数,Web bundle 无需保留原生策略及其额外解码逻辑。输出均为惰性内容树,不执行脚本或模板。
48
48
 
49
49
  ## 配置
50
50
 
@@ -0,0 +1,3 @@
1
+ import { SafeRichTextNode } from "./rich-text-shared.js";
2
+ export declare function sanitizeNativeRichTextAttributes(value: unknown, tagName: string): Record<string, string>;
3
+ export declare function normalizeNativeRichTextNodes(value: unknown): SafeRichTextNode[];
@@ -0,0 +1,99 @@
1
+ import { ALLOWED_TAGS, isRecord, isSafeUrl, sanitizeRichTextStyle } from "./rich-text-shared.js";
2
+ import { parseDocument } from "htmlparser2";
3
+ import { decodeHTML } from "entities";
4
+ // src/rich-text-native.ts
5
+ const NATIVE_ALLOWED_TAGS = /* @__PURE__ */ new Set([
6
+ ...ALLOWED_TAGS,
7
+ "footer",
8
+ "nav"
9
+ ]);
10
+ const NATIVE_TAG_ATTRIBUTES = {
11
+ bdo: ["dir"],
12
+ col: ["span", "width"],
13
+ colgroup: ["span", "width"],
14
+ img: [
15
+ "alt",
16
+ "src",
17
+ "height",
18
+ "width"
19
+ ],
20
+ ol: ["start", "type"],
21
+ table: ["width"],
22
+ td: [
23
+ "colspan",
24
+ "height",
25
+ "rowspan",
26
+ "width"
27
+ ],
28
+ th: [
29
+ "colspan",
30
+ "height",
31
+ "rowspan",
32
+ "width"
33
+ ],
34
+ tr: [
35
+ "colspan",
36
+ "height",
37
+ "rowspan",
38
+ "width"
39
+ ]
40
+ };
41
+ function sanitizeNativeRichTextAttributes(value, tagName) {
42
+ if (!isRecord(value)) return {};
43
+ const attributes = {};
44
+ const nativeAttributes = Object.prototype.hasOwnProperty.call(NATIVE_TAG_ATTRIBUTES, tagName) ? NATIVE_TAG_ATTRIBUTES[tagName] : void 0;
45
+ for (const [rawName, rawValue] of Object.entries(value)) {
46
+ const name = rawName.trim().toLowerCase();
47
+ if (name.startsWith("on") || name === "srcdoc") continue;
48
+ if (name !== "class" && name !== "style" && !nativeAttributes?.includes(name)) continue;
49
+ if (name === "src" && tagName !== "img") continue;
50
+ const serialized = name === "style" ? sanitizeRichTextStyle(rawValue) : String(rawValue ?? "");
51
+ if (!serialized || name === "src" && !isSafeUrl(serialized, name)) continue;
52
+ if (name === "src" && !/^(?:https?:)?\/\//i.test(serialized.trim())) continue;
53
+ attributes[name] = serialized;
54
+ }
55
+ return attributes;
56
+ }
57
+ function convertSourceNode(value) {
58
+ if (!isRecord(value)) return [];
59
+ if (value.type === "text") return typeof value.text === "string" ? [{
60
+ type: "text",
61
+ text: decodeHTML(value.text)
62
+ }] : [];
63
+ if (value.type !== void 0 && value.type !== "node" || typeof value.name !== "string") return [];
64
+ const name = value.name.toLowerCase();
65
+ if (!NATIVE_ALLOWED_TAGS.has(name)) return [];
66
+ const children = Array.isArray(value.children) ? value.children.flatMap(convertSourceNode) : [];
67
+ return [{
68
+ type: "element",
69
+ name,
70
+ attrs: sanitizeNativeRichTextAttributes(value.attrs, name),
71
+ children
72
+ }];
73
+ }
74
+ function convertParsedNode(node) {
75
+ if (node.type === "text") return [{
76
+ type: "text",
77
+ text: node.data
78
+ }];
79
+ if (node.type !== "tag") return [];
80
+ const element = node;
81
+ const name = element.name.toLowerCase();
82
+ if (!NATIVE_ALLOWED_TAGS.has(name)) return [];
83
+ const children = element.children.flatMap(convertParsedNode);
84
+ return [{
85
+ type: "element",
86
+ name,
87
+ attrs: sanitizeNativeRichTextAttributes(element.attribs, name),
88
+ children
89
+ }];
90
+ }
91
+ function normalizeNativeRichTextNodes(value) {
92
+ if (typeof value === "string") return parseDocument(value, {
93
+ decodeEntities: true,
94
+ lowerCaseAttributeNames: true,
95
+ lowerCaseTags: true
96
+ }).children.flatMap(convertParsedNode);
97
+ return (Array.isArray(value) ? value : [value]).flatMap(convertSourceNode);
98
+ }
99
+ export { normalizeNativeRichTextNodes, sanitizeNativeRichTextAttributes };
@@ -0,0 +1,17 @@
1
+ export interface RichTextSourceNode {
2
+ type?: string;
3
+ name?: string;
4
+ attrs?: Record<string, unknown>;
5
+ children?: RichTextSourceNode[];
6
+ text?: string;
7
+ }
8
+ export type SafeRichTextNode = {
9
+ type: 'text';
10
+ text: string;
11
+ } | {
12
+ type: 'element';
13
+ name: string;
14
+ attrs: Record<string, string>;
15
+ children: SafeRichTextNode[];
16
+ };
17
+ export declare function sanitizeRichTextStyle(value: unknown): string;
@@ -0,0 +1,140 @@
1
+ // src/rich-text-shared.ts
2
+ const ALLOWED_TAGS = /* @__PURE__ */ new Set([
3
+ "a",
4
+ "abbr",
5
+ "address",
6
+ "article",
7
+ "aside",
8
+ "b",
9
+ "bdi",
10
+ "bdo",
11
+ "big",
12
+ "blockquote",
13
+ "br",
14
+ "caption",
15
+ "center",
16
+ "cite",
17
+ "code",
18
+ "col",
19
+ "colgroup",
20
+ "dd",
21
+ "del",
22
+ "div",
23
+ "dl",
24
+ "dt",
25
+ "em",
26
+ "fieldset",
27
+ "font",
28
+ "h1",
29
+ "h2",
30
+ "h3",
31
+ "h4",
32
+ "h5",
33
+ "h6",
34
+ "header",
35
+ "hr",
36
+ "i",
37
+ "img",
38
+ "ins",
39
+ "label",
40
+ "legend",
41
+ "li",
42
+ "mark",
43
+ "ol",
44
+ "p",
45
+ "pre",
46
+ "q",
47
+ "rt",
48
+ "ruby",
49
+ "s",
50
+ "section",
51
+ "small",
52
+ "span",
53
+ "strong",
54
+ "sub",
55
+ "sup",
56
+ "table",
57
+ "tbody",
58
+ "td",
59
+ "tfoot",
60
+ "th",
61
+ "thead",
62
+ "tr",
63
+ "tt",
64
+ "u",
65
+ "ul"
66
+ ]);
67
+ const ALLOWED_STYLE_PROPERTIES = /* @__PURE__ */ new Set([
68
+ "background",
69
+ "background-color",
70
+ "border",
71
+ "border-bottom",
72
+ "border-color",
73
+ "border-left",
74
+ "border-radius",
75
+ "border-right",
76
+ "border-style",
77
+ "border-top",
78
+ "border-width",
79
+ "color",
80
+ "display",
81
+ "font-family",
82
+ "font-size",
83
+ "font-style",
84
+ "font-weight",
85
+ "height",
86
+ "letter-spacing",
87
+ "line-height",
88
+ "margin",
89
+ "margin-bottom",
90
+ "margin-left",
91
+ "margin-right",
92
+ "margin-top",
93
+ "max-height",
94
+ "max-width",
95
+ "min-height",
96
+ "min-width",
97
+ "opacity",
98
+ "overflow",
99
+ "padding",
100
+ "padding-bottom",
101
+ "padding-left",
102
+ "padding-right",
103
+ "padding-top",
104
+ "text-align",
105
+ "text-decoration",
106
+ "text-indent",
107
+ "text-overflow",
108
+ "text-transform",
109
+ "vertical-align",
110
+ "white-space",
111
+ "width",
112
+ "word-break",
113
+ "word-wrap"
114
+ ]);
115
+ function isRecord(value) {
116
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
117
+ }
118
+ function isSafeUrl(value, attribute) {
119
+ const normalized = Array.from(value).filter((character) => character.charCodeAt(0) > 32).join("").toLowerCase();
120
+ if (!normalized || normalized.startsWith("#") || normalized.startsWith("/") || normalized.startsWith("./") || normalized.startsWith("../")) return true;
121
+ if (/^(?:https?:|mailto:|tel:)/.test(normalized)) return true;
122
+ return attribute === "src" && /^data:image\/(?:gif|jpe?g|png|webp);base64,/.test(normalized);
123
+ }
124
+ function isSafeStyleValue(value) {
125
+ const normalized = value.toLowerCase().replace(/\s+/g, "");
126
+ return !normalized.includes("url(") && !normalized.includes("expression(") && !normalized.includes("javascript:") && !normalized.includes("behavior:") && !normalized.includes("-moz-binding");
127
+ }
128
+ function sanitizeRichTextStyle(value) {
129
+ const source = isRecord(value) ? Object.entries(value).map(([property, propertyValue]) => `${property}:${String(propertyValue)}`).join(";") : String(value ?? "");
130
+ const declarations = [];
131
+ for (const declaration of source.split(";")) {
132
+ const separator = declaration.indexOf(":");
133
+ if (separator <= 0) continue;
134
+ const property = declaration.slice(0, separator).trim().toLowerCase();
135
+ const propertyValue = declaration.slice(separator + 1).trim();
136
+ if (ALLOWED_STYLE_PROPERTIES.has(property) && propertyValue && isSafeStyleValue(propertyValue)) declarations.push(`${property}: ${propertyValue}`);
137
+ }
138
+ return declarations.join("; ");
139
+ }
140
+ export { ALLOWED_TAGS, isRecord, isSafeUrl, sanitizeRichTextStyle };
@@ -1,19 +1,5 @@
1
- export interface RichTextSourceNode {
2
- type?: string;
3
- name?: string;
4
- attrs?: Record<string, unknown>;
5
- children?: RichTextSourceNode[];
6
- text?: string;
7
- }
8
- export type SafeRichTextNode = {
9
- type: 'text';
10
- text: string;
11
- } | {
12
- type: 'element';
13
- name: string;
14
- attrs: Record<string, string>;
15
- children: SafeRichTextNode[];
16
- };
17
- export declare function sanitizeRichTextStyle(value: unknown): string;
1
+ import { RichTextSourceNode, SafeRichTextNode, sanitizeRichTextStyle } from "./rich-text-shared.js";
2
+ import { normalizeNativeRichTextNodes, sanitizeNativeRichTextAttributes } from "./rich-text-native.js";
18
3
  export declare function sanitizeRichTextAttributes(value: unknown, tagName: string): Record<string, string>;
19
- export declare function normalizeRichTextNodes(value: unknown): SafeRichTextNode[];
4
+ export declare function normalizeRichTextNodes(value: unknown): SafeRichTextNode[];
5
+ export { type RichTextSourceNode, type SafeRichTextNode, normalizeNativeRichTextNodes, sanitizeNativeRichTextAttributes, sanitizeRichTextStyle };
package/dist/rich-text.js CHANGED
@@ -1,70 +1,7 @@
1
+ import { ALLOWED_TAGS, isRecord, isSafeUrl, sanitizeRichTextStyle } from "./rich-text-shared.js";
2
+ import { normalizeNativeRichTextNodes, sanitizeNativeRichTextAttributes } from "./rich-text-native.js";
1
3
  import { parseDocument } from "htmlparser2";
2
4
  // src/rich-text.ts
3
- const ALLOWED_TAGS = /* @__PURE__ */ new Set([
4
- "a",
5
- "abbr",
6
- "address",
7
- "article",
8
- "aside",
9
- "b",
10
- "bdi",
11
- "bdo",
12
- "big",
13
- "blockquote",
14
- "br",
15
- "caption",
16
- "center",
17
- "cite",
18
- "code",
19
- "col",
20
- "colgroup",
21
- "dd",
22
- "del",
23
- "div",
24
- "dl",
25
- "dt",
26
- "em",
27
- "fieldset",
28
- "font",
29
- "h1",
30
- "h2",
31
- "h3",
32
- "h4",
33
- "h5",
34
- "h6",
35
- "header",
36
- "hr",
37
- "i",
38
- "img",
39
- "ins",
40
- "label",
41
- "legend",
42
- "li",
43
- "mark",
44
- "ol",
45
- "p",
46
- "pre",
47
- "q",
48
- "rt",
49
- "ruby",
50
- "s",
51
- "section",
52
- "small",
53
- "span",
54
- "strong",
55
- "sub",
56
- "sup",
57
- "table",
58
- "tbody",
59
- "td",
60
- "tfoot",
61
- "th",
62
- "thead",
63
- "tr",
64
- "tt",
65
- "u",
66
- "ul"
67
- ]);
68
5
  const ALLOWED_ATTRIBUTES = /* @__PURE__ */ new Set([
69
6
  "abbr",
70
7
  "align",
@@ -94,79 +31,6 @@ const ALLOWED_ATTRIBUTES = /* @__PURE__ */ new Set([
94
31
  "width"
95
32
  ]);
96
33
  const URL_ATTRIBUTES = /* @__PURE__ */ new Set(["href", "src"]);
97
- const ALLOWED_STYLE_PROPERTIES = /* @__PURE__ */ new Set([
98
- "background",
99
- "background-color",
100
- "border",
101
- "border-bottom",
102
- "border-color",
103
- "border-left",
104
- "border-radius",
105
- "border-right",
106
- "border-style",
107
- "border-top",
108
- "border-width",
109
- "color",
110
- "display",
111
- "font-family",
112
- "font-size",
113
- "font-style",
114
- "font-weight",
115
- "height",
116
- "letter-spacing",
117
- "line-height",
118
- "margin",
119
- "margin-bottom",
120
- "margin-left",
121
- "margin-right",
122
- "margin-top",
123
- "max-height",
124
- "max-width",
125
- "min-height",
126
- "min-width",
127
- "opacity",
128
- "overflow",
129
- "padding",
130
- "padding-bottom",
131
- "padding-left",
132
- "padding-right",
133
- "padding-top",
134
- "text-align",
135
- "text-decoration",
136
- "text-indent",
137
- "text-overflow",
138
- "text-transform",
139
- "vertical-align",
140
- "white-space",
141
- "width",
142
- "word-break",
143
- "word-wrap"
144
- ]);
145
- function isRecord(value) {
146
- return Boolean(value) && typeof value === "object" && !Array.isArray(value);
147
- }
148
- function isSafeUrl(value, attribute) {
149
- const normalized = Array.from(value).filter((character) => character.charCodeAt(0) > 32).join("").toLowerCase();
150
- if (!normalized || normalized.startsWith("#") || normalized.startsWith("/") || normalized.startsWith("./") || normalized.startsWith("../")) return true;
151
- if (/^(?:https?:|mailto:|tel:)/.test(normalized)) return true;
152
- return attribute === "src" && /^data:image\/(?:gif|jpe?g|png|webp);base64,/.test(normalized);
153
- }
154
- function isSafeStyleValue(value) {
155
- const normalized = value.toLowerCase().replace(/\s+/g, "");
156
- return !normalized.includes("url(") && !normalized.includes("expression(") && !normalized.includes("javascript:") && !normalized.includes("behavior:") && !normalized.includes("-moz-binding");
157
- }
158
- function sanitizeRichTextStyle(value) {
159
- const source = isRecord(value) ? Object.entries(value).map(([property, propertyValue]) => `${property}:${String(propertyValue)}`).join(";") : String(value ?? "");
160
- const declarations = [];
161
- for (const declaration of source.split(";")) {
162
- const separator = declaration.indexOf(":");
163
- if (separator <= 0) continue;
164
- const property = declaration.slice(0, separator).trim().toLowerCase();
165
- const propertyValue = declaration.slice(separator + 1).trim();
166
- if (ALLOWED_STYLE_PROPERTIES.has(property) && propertyValue && isSafeStyleValue(propertyValue)) declarations.push(`${property}: ${propertyValue}`);
167
- }
168
- return declarations.join("; ");
169
- }
170
34
  function sanitizeRichTextAttributes(value, tagName) {
171
35
  if (!isRecord(value)) return {};
172
36
  const attributes = {};
@@ -188,8 +52,8 @@ function convertSourceNode(value) {
188
52
  type: "text",
189
53
  text: String(value.text ?? "")
190
54
  }];
191
- const children = Array.isArray(value.children) ? value.children.flatMap(convertSourceNode) : [];
192
55
  const name = String(value.name ?? "").toLowerCase();
56
+ const children = Array.isArray(value.children) ? value.children.flatMap(convertSourceNode) : [];
193
57
  if (!ALLOWED_TAGS.has(name)) return children;
194
58
  return [{
195
59
  type: "element",
@@ -205,8 +69,8 @@ function convertParsedNode(node) {
205
69
  }];
206
70
  if (node.type !== "tag") return [];
207
71
  const element = node;
208
- const children = element.children.flatMap(convertParsedNode);
209
72
  const name = element.name.toLowerCase();
73
+ const children = element.children.flatMap(convertParsedNode);
210
74
  if (!ALLOWED_TAGS.has(name)) return children;
211
75
  return [{
212
76
  type: "element",
@@ -223,4 +87,4 @@ function normalizeRichTextNodes(value) {
223
87
  }).children.flatMap(convertParsedNode);
224
88
  return (Array.isArray(value) ? value : [value]).flatMap(convertSourceNode);
225
89
  }
226
- export { normalizeRichTextNodes, sanitizeRichTextAttributes, sanitizeRichTextStyle };
90
+ export { normalizeNativeRichTextNodes, normalizeRichTextNodes, sanitizeNativeRichTextAttributes, sanitizeRichTextAttributes, sanitizeRichTextStyle };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@weapp-core/shared",
3
3
  "type": "module",
4
- "version": "3.5.1-dev.20261117.1",
4
+ "version": "3.5.2-dev.20261119.1",
5
5
  "description": "Shared runtime-safe utilities and platform contracts for the weapp-vite ecosystem.",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -106,8 +106,9 @@
106
106
  },
107
107
  "dependencies": {
108
108
  "@types/set-value": "^4.0.3",
109
- "@weapp-vite/wxml": "0.0.3-dev.20261117.1",
109
+ "@weapp-vite/wxml": "0.0.4-dev.20261119.1",
110
110
  "domhandler": "^6.0.1",
111
+ "entities": "^8.1.0",
111
112
  "get-value": "^4.1.0",
112
113
  "htmlparser2": "^12.0.0"
113
114
  },
@@ -118,9 +119,9 @@
118
119
  },
119
120
  "repoctlSnapshot": {
120
121
  "kind": "nightly",
121
- "commit": "1218da39b57ef783727d51ad86b2eecde20c375a",
122
- "buildId": "44017370760-1",
123
- "identityKey": "308bcda13e3bd3f6b91c0a3c717e48c6f2dcd2caee9b39cdf00c077ef771f58a",
122
+ "commit": "29a77f1e58e5686dbbfceefa78673afa59d2a1ef",
123
+ "buildId": "44450884003-1",
124
+ "identityKey": "0c330c88d6a9536155ce29b9033403e7ca04e00c3d490aa29ff68859371758e7",
124
125
  "tag": "snapshot-nightly"
125
126
  },
126
127
  "scripts": {