@satorijs/element 3.1.7 → 4.0.0-alpha.0

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/lib/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
- /// <reference types="node" />
2
- import { Awaitable, Dict } from 'cosmokit';
1
+ import { Binary, Dict } from 'cosmokit';
2
+ import Element from '@cordisjs/element';
3
+ export default Element;
4
+ export * from '@cordisjs/element';
3
5
  declare global {
4
6
  namespace JSX {
5
7
  interface IntrinsicElements {
6
- [key: string]: any;
7
8
  message: {
8
9
  id?: string;
9
10
  forward?: boolean;
@@ -43,68 +44,18 @@ declare global {
43
44
  }
44
45
  }
45
46
  }
46
- declare const kElement: unique symbol;
47
- declare interface Element {
48
- [kElement]: true;
49
- type: string;
50
- attrs: Dict;
51
- /** @deprecated use `attrs` instead */
52
- data: Dict;
53
- children: Element[];
54
- source?: string;
55
- toString(strip?: boolean): string;
56
- }
57
- type RenderFunction = Element.Render<Element.Fragment, any>;
58
- declare function Element(type: string | RenderFunction, ...children: Element.Fragment[]): Element;
59
- declare function Element(type: string | RenderFunction, attrs: Dict, ...children: Element.Fragment[]): Element;
60
- declare namespace Element {
61
- export const jsx: typeof Element;
62
- export const jsxs: typeof Element;
63
- export const jsxDEV: typeof Element;
64
- export const Fragment = "template";
65
- export type Fragment = string | Element | (string | Element)[];
66
- export type Visit<T, S> = (element: Element, session: S) => T;
67
- export type Render<T, S> = (attrs: Dict, children: Element[], session: S) => T;
68
- export type SyncTransformer<S = never> = boolean | Fragment | Render<boolean | Fragment, S>;
69
- export type Transformer<S = never> = boolean | Fragment | Render<Awaitable<boolean | Fragment>, S>;
70
- type SyncVisitor<S> = Dict<SyncTransformer<S>> | Visit<boolean | Fragment, S>;
71
- type Visitor<S> = Dict<Transformer<S>> | Visit<Awaitable<boolean | Fragment>, S>;
72
- export function isElement(source: any): source is Element;
73
- export function toElement(content: string | Element): Element;
74
- export function toElementArray(content: Element.Fragment): Element[];
75
- export function normalize(source: Fragment, context?: any): Element[];
76
- export function escape(source: string, inline?: boolean): string;
77
- export function unescape(source: string): string;
78
- export interface FindOptions {
79
- type?: string;
80
- caret?: boolean;
81
- }
82
- /** @deprecated use `Element.select()` instead */
83
- export function from(source: string, options?: FindOptions): Element;
84
- type Combinator = ' ' | '>' | '+' | '~';
85
- export interface Selector {
86
- type: string;
87
- combinator: Combinator;
47
+ declare module '@cordisjs/element' {
48
+ namespace Element {
49
+ function at(id: any, attrs?: Dict): Element;
50
+ function sharp(id: any, attrs?: Dict): Element;
51
+ function quote(id: any, attrs?: Dict): Element;
52
+ let image: AssetFactory;
53
+ let img: AssetFactory;
54
+ let video: AssetFactory;
55
+ let audio: AssetFactory;
56
+ let file: AssetFactory;
57
+ function i18n(path: string | Dict, children?: any[]): Element;
88
58
  }
89
- export function parseSelector(input: string): Selector[][];
90
- export function select(source: string | Element[], query: string | Selector[][]): Element[];
91
- export function interpolate(expr: string, context: any): any;
92
- export function parse(source: string, context?: any): Element[];
93
- export function transform<S = never>(source: string, rules: SyncVisitor<S>, session?: S): string;
94
- export function transform<S = never>(source: Element[], rules: SyncVisitor<S>, session?: S): Element[];
95
- export function transformAsync<S = never>(source: string, rules: Visitor<S>, session?: S): Promise<string>;
96
- export function transformAsync<S = never>(source: Element[], rules: Visitor<S>, session?: S): Promise<Element[]>;
97
- export type Factory<R extends any[]> = (...args: [...rest: R, attrs?: Dict]) => Element;
98
- export let warn: (message: string) => void;
99
- export const text: Factory<[content: any]>;
100
- export const at: Factory<[id: any]>;
101
- export const sharp: Factory<[id: any]>;
102
- export const quote: Factory<[id: any]>;
103
- export const image: Factory<[data: string] | [data: Buffer | ArrayBuffer | ArrayBufferView, type: string]>;
104
- export const img: Factory<[data: string] | [data: Buffer | ArrayBuffer | ArrayBufferView, type: string]>;
105
- export const video: Factory<[data: string] | [data: Buffer | ArrayBuffer | ArrayBufferView, type: string]>;
106
- export const audio: Factory<[data: string] | [data: Buffer | ArrayBuffer | ArrayBufferView, type: string]>;
107
- export const file: Factory<[data: string] | [data: Buffer | ArrayBuffer | ArrayBufferView, type: string]>;
108
- export function i18n(path: string | Dict, children?: any[]): Element;
109
59
  }
110
- export = Element;
60
+ type Factory<R extends any[]> = (...args: [...rest: R, attrs?: Dict]) => Element;
61
+ type AssetFactory = Factory<[data: string] | [data: Binary.Source, type: string]>;
@@ -6,8 +6,8 @@ var __commonJS = (cb, mod) => function __require() {
6
6
  };
7
7
 
8
8
  // src/index.ts
9
- import { arrayBufferToBase64, camelize, defineProperty, hyphenate, is, isNullable, makeArray } from "cosmokit";
10
- var require_src = __commonJS({
9
+ import { Binary, camelize, defineProperty, hyphenate, is, isNonNullable, isNullable, makeArray } from "cosmokit";
10
+ var require_index = __commonJS({
11
11
  "src/index.ts"(exports, module) {
12
12
  var kElement = Symbol.for("satori.element");
13
13
  var ElementConstructor = class {
@@ -26,13 +26,10 @@ var require_src = __commonJS({
26
26
  }
27
27
  toAttrString() {
28
28
  return Object.entries(this.attrs).map(([key, value]) => {
29
- if (isNullable(value))
30
- return "";
29
+ if (isNullable(value)) return "";
31
30
  key = hyphenate(key);
32
- if (value === true)
33
- return ` ${key}`;
34
- if (value === false)
35
- return ` no-${key}`;
31
+ if (value === true) return ` ${key}`;
32
+ if (value === false) return ` no-${key}`;
36
33
  return ` ${key}="${Element.escape("" + value, true)}"`;
37
34
  }).join("");
38
35
  }
@@ -41,12 +38,10 @@ var require_src = __commonJS({
41
38
  return strip ? this.attrs.content : Element.escape(this.attrs.content);
42
39
  }
43
40
  const inner = this.children.map((child) => child.toString(strip)).join("");
44
- if (strip)
45
- return inner;
41
+ if (strip) return inner;
46
42
  const attrs = this.toAttrString();
47
43
  const tag = this.getTagName();
48
- if (!this.children.length)
49
- return `<${tag}${attrs}/>`;
44
+ if (!this.children.length) return `<${tag}${attrs}/>`;
50
45
  return `<${tag}${attrs}>${inner}</${tag}>`;
51
46
  }
52
47
  };
@@ -58,8 +53,7 @@ var require_src = __commonJS({
58
53
  if (args[0] && typeof args[0] === "object" && !Element.isElement(args[0]) && !Array.isArray(args[0])) {
59
54
  const props = args.shift();
60
55
  for (const [key, value] of Object.entries(props)) {
61
- if (isNullable(value))
62
- continue;
56
+ if (isNullable(value)) continue;
63
57
  if (key === "children") {
64
58
  args.push(...makeArray(value));
65
59
  } else {
@@ -97,8 +91,7 @@ var require_src = __commonJS({
97
91
  function toElement(content) {
98
92
  if (typeof content === "string" || typeof content === "number" || typeof content === "boolean") {
99
93
  content = "" + content;
100
- if (content)
101
- return Element2("text", { content });
94
+ if (content) return Element2("text", { content });
102
95
  } else if (isElement(content)) {
103
96
  return content;
104
97
  } else if (!isNullable(content)) {
@@ -109,9 +102,9 @@ var require_src = __commonJS({
109
102
  __name(toElement, "toElement");
110
103
  function toElementArray(content) {
111
104
  if (Array.isArray(content)) {
112
- return content.map(toElement).filter((x) => x);
105
+ return content.map(toElement).filter(isNonNullable);
113
106
  } else {
114
- return [toElement(content)].filter((x) => x);
107
+ return [toElement(content)].filter(isNonNullable);
115
108
  }
116
109
  }
117
110
  Element2.toElementArray = toElementArray;
@@ -135,8 +128,7 @@ var require_src = __commonJS({
135
128
  function from(source, options = {}) {
136
129
  const elements = parse(source);
137
130
  if (options.caret) {
138
- if (options.type && elements[0]?.type !== options.type)
139
- return;
131
+ if (options.type && elements[0]?.type !== options.type) return;
140
132
  return elements[0];
141
133
  }
142
134
  return select(elements, options.type || "*")[0];
@@ -161,14 +153,10 @@ var require_src = __commonJS({
161
153
  Element2.parseSelector = parseSelector;
162
154
  __name(parseSelector, "parseSelector");
163
155
  function select(source, query) {
164
- if (!source || !query)
165
- return [];
166
- if (typeof source === "string")
167
- source = parse(source);
168
- if (typeof query === "string")
169
- query = parseSelector(query);
170
- if (!query.length)
171
- return [];
156
+ if (!source || !query) return [];
157
+ if (typeof source === "string") source = parse(source);
158
+ if (typeof query === "string") query = parseSelector(query);
159
+ if (!query.length) return [];
172
160
  let adjacent = [];
173
161
  const results = [];
174
162
  for (const [index, element] of source.entries()) {
@@ -193,8 +181,7 @@ var require_src = __commonJS({
193
181
  inner.push(group);
194
182
  }
195
183
  }
196
- if (matched)
197
- results.push(source[index]);
184
+ if (matched) results.push(source[index]);
198
185
  results.push(...select(element.children, inner));
199
186
  }
200
187
  return results;
@@ -209,8 +196,7 @@ var require_src = __commonJS({
209
196
  let value = context;
210
197
  for (const part of expr.split(".")) {
211
198
  value = value[part];
212
- if (isNullable(value))
213
- return "";
199
+ if (isNullable(value)) return "";
214
200
  }
215
201
  return value ?? "";
216
202
  }
@@ -230,38 +216,37 @@ var require_src = __commonJS({
230
216
  function parse(source, context) {
231
217
  const tokens = [];
232
218
  function pushText(content) {
233
- if (content)
234
- tokens.push(content);
219
+ if (content) tokens.push(content);
235
220
  }
236
221
  __name(pushText, "pushText");
237
222
  const tagRegExp = context ? tagRegExp2 : tagRegExp1;
238
223
  let tagCap;
239
224
  let trimStart = true;
240
225
  while (tagCap = tagRegExp.exec(source)) {
241
- const trimEnd = !tagCap.groups.curly;
226
+ const { curly, comment, derivative } = tagCap.groups;
227
+ const trimEnd = !curly;
242
228
  parseContent(source.slice(0, tagCap.index), trimStart, trimEnd);
243
229
  trimStart = trimEnd;
244
230
  source = source.slice(tagCap.index + tagCap[0].length);
245
231
  const [_, , , close, type, extra, empty] = tagCap;
246
- if (tagCap.groups.comment)
247
- continue;
248
- if (tagCap.groups.curly) {
232
+ if (comment) continue;
233
+ if (curly) {
249
234
  let name = "", position = 2 /* EMPTY */;
250
- if (tagCap.groups.derivative) {
251
- name = tagCap.groups.derivative.slice(1);
235
+ if (derivative) {
236
+ name = derivative.slice(1);
252
237
  position = {
253
238
  "@": 2 /* EMPTY */,
254
239
  "#": 0 /* OPEN */,
255
240
  "/": 1 /* CLOSE */,
256
241
  ":": 3 /* CONTINUE */
257
- }[tagCap.groups.derivative[0]];
242
+ }[derivative[0]];
258
243
  }
259
244
  tokens.push({
260
245
  type: "curly",
261
246
  name,
262
247
  position,
263
- source: tagCap.groups.curly,
264
- extra: tagCap.groups.curly.slice(1 + (tagCap.groups.derivative ?? "").length, -1)
248
+ source: curly,
249
+ extra: curly.slice(1 + (derivative ?? "").length, -1)
265
250
  });
266
251
  continue;
267
252
  }
@@ -276,10 +261,8 @@ var require_src = __commonJS({
276
261
  parseContent(source, trimStart, true);
277
262
  function parseContent(source2, trimStart2, trimEnd) {
278
263
  source2 = unescape(source2);
279
- if (trimStart2)
280
- source2 = source2.replace(/^\s*\n\s*/, "");
281
- if (trimEnd)
282
- source2 = source2.replace(/\s*\n\s*$/, "");
264
+ if (trimStart2) source2 = source2.replace(/^\s*\n\s*/, "");
265
+ if (trimEnd) source2 = source2.replace(/\s*\n\s*$/, "");
283
266
  pushText(source2);
284
267
  }
285
268
  __name(parseContent, "parseContent");
@@ -358,8 +341,7 @@ var require_src = __commonJS({
358
341
  } else if (token.name === "each") {
359
342
  const [expr, ident] = token.extra.split(/\s+as\s+/);
360
343
  const items = interpolate(expr, context);
361
- if (!items || !items[Symbol.iterator])
362
- continue;
344
+ if (!items || !items[Symbol.iterator]) continue;
363
345
  for (const item of items) {
364
346
  result.push(...parseTokens(token.children.default, { ...context, [ident]: item }));
365
347
  }
@@ -440,9 +422,9 @@ var require_src = __commonJS({
440
422
  if (is("Buffer", src)) {
441
423
  src = prefix + src.toString("base64");
442
424
  } else if (is("ArrayBuffer", src)) {
443
- src = prefix + arrayBufferToBase64(src);
425
+ src = prefix + Binary.toBase64(src);
444
426
  } else if (ArrayBuffer.isView(src)) {
445
- src = prefix + arrayBufferToBase64(src.buffer);
427
+ src = prefix + Binary.toBase64(src.buffer);
446
428
  }
447
429
  if (src.startsWith("base64://")) {
448
430
  (0, Element2.warn)(`protocol "base64:" is deprecated and will be removed in the future, please use "data:" instead`);
@@ -469,5 +451,4 @@ var require_src = __commonJS({
469
451
  module.exports = Element;
470
452
  }
471
453
  });
472
- export default require_src();
473
- //# sourceMappingURL=index.mjs.map
454
+ export default require_index();
package/package.json CHANGED
@@ -1,41 +1,33 @@
1
1
  {
2
2
  "name": "@satorijs/element",
3
3
  "description": "Element Manipulation",
4
- "version": "3.1.7",
5
- "main": "lib/index.cjs",
6
- "module": "lib/index.mjs",
7
- "types": "lib/index.d.ts",
4
+ "version": "4.0.0-alpha.0",
5
+ "type": "module",
8
6
  "exports": {
9
7
  ".": {
10
- "import": "./lib/index.mjs",
11
- "require": "./lib/index.cjs",
12
- "types": "./lib/index.d.ts"
8
+ "types": "./lib/index.d.ts",
9
+ "default": "./lib/index.js"
13
10
  },
14
11
  "./jsx-runtime": {
15
- "import": "./lib/index.mjs",
16
- "require": "./lib/index.cjs",
17
- "types": "./lib/index.d.ts"
12
+ "types": "./lib/index.d.ts",
13
+ "default": "./lib/index.js"
18
14
  },
19
15
  "./jsx-dev-runtime": {
20
- "import": "./lib/index.mjs",
21
- "require": "./lib/index.cjs",
22
- "types": "./lib/index.d.ts"
16
+ "types": "./lib/index.d.ts",
17
+ "default": "./lib/index.js"
23
18
  },
24
19
  "./src/*": "./src/*",
25
20
  "./package.json": "./package.json"
26
21
  },
27
22
  "files": [
28
- "lib",
29
- "src",
30
- "jsx-runtime.d.ts",
31
- "jsx-dev-runtime.d.ts"
23
+ "lib"
32
24
  ],
33
25
  "author": "Shigma <shigma10826@gmail.com>",
34
26
  "license": "MIT",
35
27
  "repository": {
36
28
  "type": "git",
37
29
  "url": "git+https://github.com/satorijs/satori.git",
38
- "directory": "packages/message"
30
+ "directory": "packages/element"
39
31
  },
40
32
  "bugs": {
41
33
  "url": "https://github.com/satorijs/satori/issues"
@@ -50,9 +42,12 @@
50
42
  "utilities"
51
43
  ],
52
44
  "devDependencies": {
53
- "chai-shape": "^1.0.0"
45
+ "@types/chai": "^5.2.0",
46
+ "chai": "^5.2.0",
47
+ "chai-shape": "^1.1.0"
54
48
  },
55
49
  "dependencies": {
56
- "cosmokit": "^1.6.2"
50
+ "@cordisjs/element": "^0.2.0",
51
+ "cosmokit": "^1.7.4"
57
52
  }
58
53
  }
@@ -1,3 +0,0 @@
1
- import segment from './lib'
2
-
3
- export = segment
package/jsx-runtime.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import segment from './lib'
2
-
3
- export = segment