@mtkruto/node 0.0.825 → 0.0.827
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/esm/client/client.d.ts +20 -0
- package/esm/client/client.js +52 -1
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/DomHandler.d.ts +83 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/DomHandler.js +203 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/DomSerializer.d.ts +50 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/DomSerializer.js +274 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/ElementType.d.ts +47 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/ElementType.js +51 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/FeedHandler.d.ts +66 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/FeedHandler.js +191 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Node.d.ts +168 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Node.js +385 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Parser.d.ts +159 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Parser.js +431 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Tokenizer.d.ts +181 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Tokenizer.js +1046 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/mod.d.ts +42 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/mod.js +52 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode.d.ts +11 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode.js +122 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode_codepoint.d.ts +1 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode_codepoint.js +24 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/encode.d.ts +46 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/encode.js +121 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/decode.d.ts +31 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/decode.js +30 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/entities.d.ts +2128 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/entities.js +2127 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/legacy.d.ts +109 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/legacy.js +108 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/xml.d.ts +8 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/xml.js +1 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/mod.d.ts +90 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/mod.js +95 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/helpers.d.ts +50 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/helpers.js +128 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/legacy.d.ts +46 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/legacy.js +110 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/manipulation.d.ts +42 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/manipulation.js +120 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/mod.d.ts +6 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/mod.js +6 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/querying.d.ts +54 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/querying.js +110 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/stringify.d.ts +40 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/stringify.js +75 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/traversal.d.ts +58 -0
- package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/traversal.js +101 -0
- package/esm/deps.d.ts +1 -0
- package/esm/deps.js +1 -0
- package/esm/storage/storage.d.ts +1 -1
- package/esm/types/0_message_entity.d.ts +90 -0
- package/esm/types/0_message_entity.js +128 -0
- package/esm/utilities/0_html.d.ts +2 -0
- package/esm/utilities/0_html.js +74 -0
- package/package.json +1 -1
- package/script/client/client.d.ts +20 -0
- package/script/client/client.js +52 -1
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/DomHandler.d.ts +83 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/DomHandler.js +207 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/DomSerializer.d.ts +50 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/DomSerializer.js +301 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/ElementType.d.ts +47 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/ElementType.js +55 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/FeedHandler.d.ts +66 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/FeedHandler.js +222 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/Node.d.ts +168 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/Node.js +404 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/Parser.d.ts +159 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/Parser.js +438 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/Tokenizer.d.ts +181 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/Tokenizer.js +1052 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/mod.d.ts +42 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/mod.js +88 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode.d.ts +11 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode.js +128 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode_codepoint.d.ts +1 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode_codepoint.js +30 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/encode.d.ts +46 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/encode.js +129 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/decode.d.ts +31 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/decode.js +32 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/entities.d.ts +2128 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/entities.js +2129 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/legacy.d.ts +109 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/legacy.js +110 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/xml.d.ts +8 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/xml.js +3 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/mod.d.ts +90 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/mod.js +114 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/helpers.d.ts +50 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/helpers.js +134 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/legacy.d.ts +46 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/legacy.js +118 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/manipulation.d.ts +42 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/manipulation.js +129 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/mod.d.ts +6 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/mod.js +22 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/querying.d.ts +54 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/querying.js +119 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/stringify.d.ts +40 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/stringify.js +86 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/traversal.d.ts +58 -0
- package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/traversal.js +112 -0
- package/script/deps.d.ts +1 -0
- package/script/deps.js +3 -1
- package/script/storage/storage.d.ts +1 -1
- package/script/types/0_message_entity.d.ts +90 -0
- package/script/types/0_message_entity.js +156 -0
- package/script/utilities/0_html.d.ts +2 -0
- package/script/utilities/0_html.js +78 -0
package/esm/client/client.d.ts
CHANGED
|
@@ -4,7 +4,12 @@ import * as functions from "../tl/3_functions.js";
|
|
|
4
4
|
import { ClientAbstract } from "./client_abstract.js";
|
|
5
5
|
import { Storage } from "../storage/storage.js";
|
|
6
6
|
import { DC, TransportProvider } from "../transport/transport_provider.js";
|
|
7
|
+
import { MessageEntity } from "../types/0_message_entity.js";
|
|
7
8
|
export declare const restartAuth: unique symbol;
|
|
9
|
+
export declare enum ParseMode {
|
|
10
|
+
None = "none",
|
|
11
|
+
HTML = "html"
|
|
12
|
+
}
|
|
8
13
|
export interface AuthorizeUserParams<S = string> {
|
|
9
14
|
phone: S | (() => MaybePromise<S>);
|
|
10
15
|
code: S | (() => MaybePromise<S>);
|
|
@@ -12,6 +17,10 @@ export interface AuthorizeUserParams<S = string> {
|
|
|
12
17
|
}
|
|
13
18
|
export type UpdatesHandler = null | ((client: Client, update: types.Updates) => MaybePromise<void>);
|
|
14
19
|
export interface ClientParams {
|
|
20
|
+
/**
|
|
21
|
+
* Default parse mode. Defauls to `ParseMode.None`.
|
|
22
|
+
*/
|
|
23
|
+
parseMode?: ParseMode;
|
|
15
24
|
/**
|
|
16
25
|
* The transport provider to use. Defaults to `defaultTransportProvider`.
|
|
17
26
|
*/
|
|
@@ -51,6 +60,7 @@ export declare class Client extends ClientAbstract {
|
|
|
51
60
|
private promises;
|
|
52
61
|
private toAcknowledge;
|
|
53
62
|
updatesHandler: UpdatesHandler;
|
|
63
|
+
readonly parseMode: ParseMode;
|
|
54
64
|
readonly appVersion: string;
|
|
55
65
|
readonly deviceModel: string;
|
|
56
66
|
readonly langCode: string;
|
|
@@ -117,4 +127,14 @@ export declare class Client extends ClientAbstract {
|
|
|
117
127
|
private processUsers;
|
|
118
128
|
private processUpdates;
|
|
119
129
|
getInputPeer(id: string | number): Promise<types.InputPeerChat | types.InputPeerUser | types.InputPeerChannel>;
|
|
130
|
+
sendMessage(chatId: number | string, text: string, params?: {
|
|
131
|
+
parseMode?: ParseMode;
|
|
132
|
+
entities?: MessageEntity[];
|
|
133
|
+
disableWebPagePreview?: boolean;
|
|
134
|
+
disableNotification?: boolean;
|
|
135
|
+
protectContent?: boolean;
|
|
136
|
+
replyToMessageId?: number;
|
|
137
|
+
messageThreadId?: number;
|
|
138
|
+
sendAs?: number | string;
|
|
139
|
+
}): Promise<types.TypeUpdates>;
|
|
120
140
|
}
|
package/esm/client/client.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { gunzip } from "../deps.js";
|
|
2
2
|
import { ackThreshold, DEFAULT_APP_VERSION, DEFAULT_DEVICE_MODEL, DEFAULT_INITIAL_DC, DEFAULT_LANG_CODE, DEFAULT_LANG_PACK, DEFAULT_SYSTEM_LANG_CODE, DEFAULT_SYSTEM_VERSION, LAYER, MAX_CHANNEL_ID, MAX_CHAT_ID, USERNAME_TTL, ZERO_CHANNEL_ID } from "../constants.js";
|
|
3
|
-
import { bigIntFromBuffer, getRandomBigInt } from "../utilities/0_bigint.js";
|
|
3
|
+
import { bigIntFromBuffer, getRandomBigInt, getRandomId } from "../utilities/0_bigint.js";
|
|
4
|
+
import { parseHtml } from "../utilities/0_html.js";
|
|
4
5
|
import { decryptMessage, encryptMessage, getMessageId } from "../utilities/1_message.js";
|
|
5
6
|
import { checkPassword } from "../utilities/1_password.js";
|
|
6
7
|
import { as } from "../tl/1_tl_object.js";
|
|
@@ -14,7 +15,13 @@ import { ClientAbstract } from "./client_abstract.js";
|
|
|
14
15
|
import { ClientPlain } from "./client_plain.js";
|
|
15
16
|
import { StorageMemory } from "../storage/storage_memory.js";
|
|
16
17
|
import { sha1 } from "../utilities/0_hash.js";
|
|
18
|
+
import { toTlObject } from "../types/0_message_entity.js";
|
|
17
19
|
export const restartAuth = Symbol();
|
|
20
|
+
export var ParseMode;
|
|
21
|
+
(function (ParseMode) {
|
|
22
|
+
ParseMode["None"] = "none";
|
|
23
|
+
ParseMode["HTML"] = "html";
|
|
24
|
+
})(ParseMode || (ParseMode = {}));
|
|
18
25
|
export class Client extends ClientAbstract {
|
|
19
26
|
/**
|
|
20
27
|
* Constructs the client.
|
|
@@ -80,6 +87,12 @@ export class Client extends ClientAbstract {
|
|
|
80
87
|
writable: true,
|
|
81
88
|
value: null
|
|
82
89
|
});
|
|
90
|
+
Object.defineProperty(this, "parseMode", {
|
|
91
|
+
enumerable: true,
|
|
92
|
+
configurable: true,
|
|
93
|
+
writable: true,
|
|
94
|
+
value: void 0
|
|
95
|
+
});
|
|
83
96
|
Object.defineProperty(this, "appVersion", {
|
|
84
97
|
enumerable: true,
|
|
85
98
|
configurable: true,
|
|
@@ -122,6 +135,7 @@ export class Client extends ClientAbstract {
|
|
|
122
135
|
writable: true,
|
|
123
136
|
value: false
|
|
124
137
|
});
|
|
138
|
+
this.parseMode = params?.parseMode ?? ParseMode.None;
|
|
125
139
|
this.appVersion = params?.appVersion ?? DEFAULT_APP_VERSION;
|
|
126
140
|
this.deviceModel = params?.deviceModel ?? DEFAULT_DEVICE_MODEL;
|
|
127
141
|
this.langCode = params?.langCode ?? DEFAULT_LANG_CODE;
|
|
@@ -584,4 +598,41 @@ export class Client extends ClientAbstract {
|
|
|
584
598
|
throw new Error("ID format unknown or not implemented");
|
|
585
599
|
}
|
|
586
600
|
}
|
|
601
|
+
async sendMessage(chatId, text, params) {
|
|
602
|
+
const entities_ = params?.entities ?? [];
|
|
603
|
+
const parseMode = params?.parseMode ?? this.parseMode;
|
|
604
|
+
switch (parseMode) {
|
|
605
|
+
case ParseMode.None:
|
|
606
|
+
break;
|
|
607
|
+
case ParseMode.HTML: {
|
|
608
|
+
const [newText, entitiesToPush] = parseHtml(text);
|
|
609
|
+
text = newText;
|
|
610
|
+
for (const entity of entitiesToPush) {
|
|
611
|
+
entities_.push(entity);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
const peer = await this.getInputPeer(chatId);
|
|
616
|
+
const randomId = getRandomId();
|
|
617
|
+
const message = text;
|
|
618
|
+
const noWebpage = params?.disableWebPagePreview ? true : undefined;
|
|
619
|
+
const silent = params?.disableNotification ? true : undefined;
|
|
620
|
+
const noforwards = params?.protectContent ? true : undefined;
|
|
621
|
+
const replyToMsgId = params?.replyToMessageId;
|
|
622
|
+
const topMsgId = params?.messageThreadId;
|
|
623
|
+
const sendAs = params?.sendAs ? await this.getInputPeer(params.sendAs) : undefined;
|
|
624
|
+
const entities = entities_?.length > 0 ? entities_.map((v) => toTlObject(v)) : undefined;
|
|
625
|
+
return await this.invoke(new functions.MessagesSendMessage({
|
|
626
|
+
peer,
|
|
627
|
+
randomId,
|
|
628
|
+
message,
|
|
629
|
+
noWebpage,
|
|
630
|
+
silent,
|
|
631
|
+
noforwards,
|
|
632
|
+
replyToMsgId,
|
|
633
|
+
topMsgId,
|
|
634
|
+
sendAs,
|
|
635
|
+
entities,
|
|
636
|
+
}));
|
|
637
|
+
}
|
|
587
638
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Node, Element, Document, NodeWithChildren, DataNode } from './Node.js';
|
|
2
|
+
export interface DomHandlerOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Add a `startIndex` property to nodes.
|
|
5
|
+
* When the parser is used in a non-streaming fashion, `startIndex` is an integer
|
|
6
|
+
* indicating the position of the start of the node in the document.
|
|
7
|
+
*
|
|
8
|
+
* @default false
|
|
9
|
+
*/
|
|
10
|
+
withStartIndices?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Add an `endIndex` property to nodes.
|
|
13
|
+
* When the parser is used in a non-streaming fashion, `endIndex` is an integer
|
|
14
|
+
* indicating the position of the end of the node in the document.
|
|
15
|
+
*
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
18
|
+
withEndIndices?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Replace all whitespace with single spaces.
|
|
21
|
+
*
|
|
22
|
+
* **Note:** Enabling this might break your markup.
|
|
23
|
+
*
|
|
24
|
+
* @default false
|
|
25
|
+
* @deprecated
|
|
26
|
+
*/
|
|
27
|
+
normalizeWhitespace?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Treat the markup as XML.
|
|
30
|
+
*
|
|
31
|
+
* @default false
|
|
32
|
+
*/
|
|
33
|
+
xmlMode?: boolean;
|
|
34
|
+
}
|
|
35
|
+
interface ParserInterface {
|
|
36
|
+
startIndex: number | null;
|
|
37
|
+
endIndex: number | null;
|
|
38
|
+
}
|
|
39
|
+
type Callback = (error: Error | null, dom: Node[]) => void;
|
|
40
|
+
type ElementCallback = (element: Element) => void;
|
|
41
|
+
export declare class DomHandler {
|
|
42
|
+
/** The elements of the DOM */
|
|
43
|
+
dom: Node[];
|
|
44
|
+
/** The root element for the DOM */
|
|
45
|
+
root: Document;
|
|
46
|
+
/** Called once parsing has completed. */
|
|
47
|
+
private readonly callback;
|
|
48
|
+
/** Settings for the handler. */
|
|
49
|
+
private readonly options;
|
|
50
|
+
/** Callback whenever a tag is closed. */
|
|
51
|
+
private readonly elementCB;
|
|
52
|
+
/** Indicated whether parsing has been completed. */
|
|
53
|
+
private done;
|
|
54
|
+
/** Stack of open tags. */
|
|
55
|
+
protected tagStack: NodeWithChildren[];
|
|
56
|
+
/** A data node that is still being written to. */
|
|
57
|
+
protected lastNode: DataNode | null;
|
|
58
|
+
/** Reference to the parser instance. Used for location information. */
|
|
59
|
+
private parser;
|
|
60
|
+
/**
|
|
61
|
+
* @param callback Called once parsing has completed.
|
|
62
|
+
* @param options Settings for the handler.
|
|
63
|
+
* @param elementCB Callback whenever a tag is closed.
|
|
64
|
+
*/
|
|
65
|
+
constructor(callback?: Callback | null, options?: DomHandlerOptions | null, elementCB?: ElementCallback);
|
|
66
|
+
onparserinit(parser: ParserInterface): void;
|
|
67
|
+
onreset(): void;
|
|
68
|
+
onend(): void;
|
|
69
|
+
onerror(error: Error): void;
|
|
70
|
+
onclosetag(): void;
|
|
71
|
+
onopentag(name: string, attribs: {
|
|
72
|
+
[key: string]: string;
|
|
73
|
+
}): void;
|
|
74
|
+
ontext(data: string): void;
|
|
75
|
+
oncomment(data: string): void;
|
|
76
|
+
oncommentend(): void;
|
|
77
|
+
oncdatastart(): void;
|
|
78
|
+
oncdataend(): void;
|
|
79
|
+
onprocessinginstruction(name: string, data: string): void;
|
|
80
|
+
protected handleCallback(error: Error | null): void;
|
|
81
|
+
protected addNode(node: Node): void;
|
|
82
|
+
}
|
|
83
|
+
export default DomHandler;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { Element, Document, NodeWithChildren, Comment, Text, ProcessingInstruction, } from './Node.js';
|
|
2
|
+
import { ElementType } from './ElementType.js';
|
|
3
|
+
const reWhitespace = /\s+/g;
|
|
4
|
+
// Default options
|
|
5
|
+
const defaultOpts = {
|
|
6
|
+
normalizeWhitespace: false,
|
|
7
|
+
withStartIndices: false,
|
|
8
|
+
withEndIndices: false,
|
|
9
|
+
};
|
|
10
|
+
export class DomHandler {
|
|
11
|
+
/**
|
|
12
|
+
* @param callback Called once parsing has completed.
|
|
13
|
+
* @param options Settings for the handler.
|
|
14
|
+
* @param elementCB Callback whenever a tag is closed.
|
|
15
|
+
*/
|
|
16
|
+
constructor(callback, options, elementCB) {
|
|
17
|
+
/** The elements of the DOM */
|
|
18
|
+
Object.defineProperty(this, "dom", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: []
|
|
23
|
+
});
|
|
24
|
+
/** The root element for the DOM */
|
|
25
|
+
Object.defineProperty(this, "root", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: new Document(this.dom)
|
|
30
|
+
});
|
|
31
|
+
/** Called once parsing has completed. */
|
|
32
|
+
Object.defineProperty(this, "callback", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: void 0
|
|
37
|
+
});
|
|
38
|
+
/** Settings for the handler. */
|
|
39
|
+
Object.defineProperty(this, "options", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
45
|
+
/** Callback whenever a tag is closed. */
|
|
46
|
+
Object.defineProperty(this, "elementCB", {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
configurable: true,
|
|
49
|
+
writable: true,
|
|
50
|
+
value: void 0
|
|
51
|
+
});
|
|
52
|
+
/** Indicated whether parsing has been completed. */
|
|
53
|
+
Object.defineProperty(this, "done", {
|
|
54
|
+
enumerable: true,
|
|
55
|
+
configurable: true,
|
|
56
|
+
writable: true,
|
|
57
|
+
value: false
|
|
58
|
+
});
|
|
59
|
+
/** Stack of open tags. */
|
|
60
|
+
Object.defineProperty(this, "tagStack", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
configurable: true,
|
|
63
|
+
writable: true,
|
|
64
|
+
value: [this.root]
|
|
65
|
+
});
|
|
66
|
+
/** A data node that is still being written to. */
|
|
67
|
+
Object.defineProperty(this, "lastNode", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: null
|
|
72
|
+
});
|
|
73
|
+
/** Reference to the parser instance. Used for location information. */
|
|
74
|
+
Object.defineProperty(this, "parser", {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
configurable: true,
|
|
77
|
+
writable: true,
|
|
78
|
+
value: null
|
|
79
|
+
});
|
|
80
|
+
// Make it possible to skip arguments, for backwards-compatibility
|
|
81
|
+
if (typeof options === 'function') {
|
|
82
|
+
elementCB = options;
|
|
83
|
+
options = defaultOpts;
|
|
84
|
+
}
|
|
85
|
+
if (typeof callback === 'object') {
|
|
86
|
+
options = callback;
|
|
87
|
+
callback = undefined;
|
|
88
|
+
}
|
|
89
|
+
this.callback = callback ?? null;
|
|
90
|
+
this.options = options ?? defaultOpts;
|
|
91
|
+
this.elementCB = elementCB ?? null;
|
|
92
|
+
}
|
|
93
|
+
onparserinit(parser) {
|
|
94
|
+
this.parser = parser;
|
|
95
|
+
}
|
|
96
|
+
// Resets the handler back to starting state
|
|
97
|
+
onreset() {
|
|
98
|
+
this.dom = [];
|
|
99
|
+
this.root = new Document(this.dom);
|
|
100
|
+
this.done = false;
|
|
101
|
+
this.tagStack = [this.root];
|
|
102
|
+
this.lastNode = null;
|
|
103
|
+
this.parser = this.parser ?? null;
|
|
104
|
+
}
|
|
105
|
+
// Signals the handler that parsing is done
|
|
106
|
+
onend() {
|
|
107
|
+
if (this.done)
|
|
108
|
+
return;
|
|
109
|
+
this.done = true;
|
|
110
|
+
this.parser = null;
|
|
111
|
+
this.handleCallback(null);
|
|
112
|
+
}
|
|
113
|
+
onerror(error) {
|
|
114
|
+
this.handleCallback(error);
|
|
115
|
+
}
|
|
116
|
+
onclosetag() {
|
|
117
|
+
this.lastNode = null;
|
|
118
|
+
const elem = this.tagStack.pop();
|
|
119
|
+
if (this.options.withEndIndices) {
|
|
120
|
+
elem.endIndex = this.parser.endIndex;
|
|
121
|
+
}
|
|
122
|
+
if (this.elementCB)
|
|
123
|
+
this.elementCB(elem);
|
|
124
|
+
}
|
|
125
|
+
onopentag(name, attribs) {
|
|
126
|
+
const type = this.options.xmlMode ? ElementType.Tag : undefined;
|
|
127
|
+
const element = new Element(name, attribs, undefined, type);
|
|
128
|
+
this.addNode(element);
|
|
129
|
+
this.tagStack.push(element);
|
|
130
|
+
}
|
|
131
|
+
ontext(data) {
|
|
132
|
+
const { normalizeWhitespace } = this.options;
|
|
133
|
+
const { lastNode } = this;
|
|
134
|
+
if (lastNode && lastNode.type === ElementType.Text) {
|
|
135
|
+
if (normalizeWhitespace) {
|
|
136
|
+
lastNode.data = (lastNode.data + data).replace(reWhitespace, ' ');
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
lastNode.data += data;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
if (normalizeWhitespace) {
|
|
144
|
+
data = data.replace(reWhitespace, ' ');
|
|
145
|
+
}
|
|
146
|
+
const node = new Text(data);
|
|
147
|
+
this.addNode(node);
|
|
148
|
+
this.lastNode = node;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
oncomment(data) {
|
|
152
|
+
if (this.lastNode && this.lastNode.type === ElementType.Comment) {
|
|
153
|
+
this.lastNode.data += data;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const node = new Comment(data);
|
|
157
|
+
this.addNode(node);
|
|
158
|
+
this.lastNode = node;
|
|
159
|
+
}
|
|
160
|
+
oncommentend() {
|
|
161
|
+
this.lastNode = null;
|
|
162
|
+
}
|
|
163
|
+
oncdatastart() {
|
|
164
|
+
const text = new Text('');
|
|
165
|
+
const node = new NodeWithChildren(ElementType.CDATA, [text]);
|
|
166
|
+
this.addNode(node);
|
|
167
|
+
text.parent = node;
|
|
168
|
+
this.lastNode = text;
|
|
169
|
+
}
|
|
170
|
+
oncdataend() {
|
|
171
|
+
this.lastNode = null;
|
|
172
|
+
}
|
|
173
|
+
onprocessinginstruction(name, data) {
|
|
174
|
+
const node = new ProcessingInstruction(name, data);
|
|
175
|
+
this.addNode(node);
|
|
176
|
+
}
|
|
177
|
+
handleCallback(error) {
|
|
178
|
+
if (typeof this.callback === 'function') {
|
|
179
|
+
this.callback(error, this.dom);
|
|
180
|
+
}
|
|
181
|
+
else if (error) {
|
|
182
|
+
throw error;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
addNode(node) {
|
|
186
|
+
const parent = this.tagStack[this.tagStack.length - 1];
|
|
187
|
+
const previousSibling = parent.children[parent.children.length - 1];
|
|
188
|
+
if (this.options.withStartIndices) {
|
|
189
|
+
node.startIndex = this.parser.startIndex;
|
|
190
|
+
}
|
|
191
|
+
if (this.options.withEndIndices) {
|
|
192
|
+
node.endIndex = this.parser.endIndex;
|
|
193
|
+
}
|
|
194
|
+
parent.children.push(node);
|
|
195
|
+
if (previousSibling) {
|
|
196
|
+
node.prev = previousSibling;
|
|
197
|
+
previousSibling.next = node;
|
|
198
|
+
}
|
|
199
|
+
node.parent = parent;
|
|
200
|
+
this.lastNode = null;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
export default DomHandler;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Node } from './Node.js';
|
|
2
|
+
/**
|
|
3
|
+
* Mixed-case SVG and MathML tags & attributes
|
|
4
|
+
* recognized by the HTML parser.
|
|
5
|
+
*
|
|
6
|
+
* @see https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inforeign
|
|
7
|
+
*/
|
|
8
|
+
export declare const elementNames: Map<string, string>;
|
|
9
|
+
export declare const attributeNames: Map<string, string>;
|
|
10
|
+
export interface DomSerializerOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Print an empty attribute's value.
|
|
13
|
+
*
|
|
14
|
+
* @default xmlMode
|
|
15
|
+
* @example With <code>emptyAttrs: false</code>: <code><input checked></code>
|
|
16
|
+
* @example With <code>emptyAttrs: true</code>: <code><input checked=""></code>
|
|
17
|
+
*/
|
|
18
|
+
emptyAttrs?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Print self-closing tags for tags without contents.
|
|
21
|
+
*
|
|
22
|
+
* @default xmlMode
|
|
23
|
+
* @example With <code>selfClosingTags: false</code>: <code><foo></foo></code>
|
|
24
|
+
* @example With <code>selfClosingTags: true</code>: <code><foo /></code>
|
|
25
|
+
*/
|
|
26
|
+
selfClosingTags?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Treat the input as an XML document; enables the `emptyAttrs` and `selfClosingTags` options.
|
|
29
|
+
*
|
|
30
|
+
* If the value is `"foreign"`, it will try to correct mixed-case attribute names.
|
|
31
|
+
*
|
|
32
|
+
* @default false
|
|
33
|
+
*/
|
|
34
|
+
xmlMode?: boolean | 'foreign';
|
|
35
|
+
/**
|
|
36
|
+
* Encode characters that are either reserved in HTML or XML, or are outside of the ASCII range.
|
|
37
|
+
*
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
40
|
+
decodeEntities?: boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Renders a DOM node or an array of DOM nodes to a string.
|
|
44
|
+
*
|
|
45
|
+
* Can be thought of as the equivalent of the `outerHTML` of the passed node(s).
|
|
46
|
+
*
|
|
47
|
+
* @param node Node to be rendered.
|
|
48
|
+
* @param options Changes serialization behavior
|
|
49
|
+
*/
|
|
50
|
+
export default function render(node: Node | ArrayLike<Node>, options?: DomSerializerOptions): string;
|