@mtkruto/browser 0.1.702 → 0.1.704
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 +78 -1
- package/esm/client/5_client.d.ts +1 -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/package.json +1 -1
- package/script/client/5_client.d.ts +1 -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/README.md
CHANGED
|
@@ -1 +1,78 @@
|
|
|
1
|
-
<
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# MTKruto
|
|
4
|
+
|
|
5
|
+
Cross-runtime JavaScript library for building Telegram clients
|
|
6
|
+
|
|
7
|
+
###### [Documentation](https://mtkruto.deno.dev) / [API Reference](https://deno.land/x/mtkruto/mod.ts) / [Discussion Chat](https://t.me/MTKrutoChat) / [License](#license)
|
|
8
|
+
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
### Key Features
|
|
12
|
+
|
|
13
|
+
- **Easy-to-use.** Provides high-level [methods](https://mtkruto.deno.dev/methods) and [types](https://mtkruto.deno.dev/types) for convenience.
|
|
14
|
+
- **Cross-runtime.** Supports Node.js, Deno, browsers, and Bun.
|
|
15
|
+
- **Type-safe.** Written in TypeScript with accurate typings.
|
|
16
|
+
- **Made for the Web.** Leverages Web APIs.
|
|
17
|
+
- **Unopinionated.** No hidden behaviors.
|
|
18
|
+
- **Extensible.** Highly customizable.
|
|
19
|
+
|
|
20
|
+
> Note: MTKruto has not reached version 1.0.0 yet. While it can run in production, we currently do not recommend depending on it for critical projects.
|
|
21
|
+
|
|
22
|
+
## Get Started
|
|
23
|
+
|
|
24
|
+
### Node.js
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
const { Client, getRandomId } = require("@mtkruto/node"); // npm install @mtkruto/node
|
|
28
|
+
|
|
29
|
+
const client = new Client();
|
|
30
|
+
await client.connect();
|
|
31
|
+
|
|
32
|
+
const pong = await client.api.ping({ ping_id: getRandomId() });
|
|
33
|
+
console.debug(pong);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Deno
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { Client, getRandomId } from "https://deno.land/x/mtkruto/mod.ts";
|
|
40
|
+
|
|
41
|
+
const client = new Client();
|
|
42
|
+
await client.connect();
|
|
43
|
+
|
|
44
|
+
const pong = await client.api.ping({ ping_id: getRandomId() });
|
|
45
|
+
console.debug(pong);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Browsers
|
|
49
|
+
|
|
50
|
+
```html
|
|
51
|
+
<script type="module">
|
|
52
|
+
import { Client, getRandomId } from "https://esm.sh/@mtkruto/browser";
|
|
53
|
+
|
|
54
|
+
const client = new Client();
|
|
55
|
+
await client.connect();
|
|
56
|
+
|
|
57
|
+
const pong = await client.api.ping({ ping_id: getRandomId() });
|
|
58
|
+
console.debug(pong);
|
|
59
|
+
</script>
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
> The [@mtkruto/browser](https://npm.im/@mtkruto/browser) package can also be used with front end frameworks and bundlers.
|
|
63
|
+
|
|
64
|
+
### Bun
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { Client, getRandomId } from "@mtkruto/mtkruto"; // bunx jsr i @mtkruto/mtkruto
|
|
68
|
+
|
|
69
|
+
const client = new Client();
|
|
70
|
+
await client.connect();
|
|
71
|
+
|
|
72
|
+
const pong = await client.api.ping({ ping_id: getRandomId() });
|
|
73
|
+
console.debug(pong);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
MTKruto is made open-source under the GNU Lesser General Public License version 3, or at your option, any later version. Refer to [COPYING](./COPYING) and [COPYING.LESSER](./COPYING.LESSER) for more.
|
package/esm/client/5_client.d.ts
CHANGED
|
@@ -17,13 +17,13 @@
|
|
|
17
17
|
* You should have received a copy of the GNU Lesser General Public License
|
|
18
18
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
19
19
|
*/
|
|
20
|
+
import { AddReactionParams, AnswerCallbackQueryParams, AnswerInlineQueryParams, BanChatMemberParams, CreateInviteLinkParams, CreateStoryParams, DeleteMessageParams, DeleteMessagesParams, DownloadLiveStreamChunkParams, DownloadParams, EditMessageLiveLocationParams, EditMessageMediaParams, EditMessageParams, EditMessageReplyMarkupParams, ForwardMessagesParams, GetChatsParams, GetCreatedInviteLinksParams, GetHistoryParams, GetMyCommandsParams, JoinVideoChatParams, PinMessageParams, ReplyParams, ScheduleVideoChatParams, SearchMessagesParams, SendAnimationParams, SendAudioParams, SendContactParams, SendDiceParams, SendDocumentParams, SendInlineQueryParams, SendLocationParams, SendMessageParams, SendPhotoParams, SendPollParams, SendStickerParams, SendVenueParams, SendVideoNoteParams, SendVideoParams, SendVoiceParams, SetChatMemberRightsParams, SetChatPhotoParams, SetMyCommandsParams, SetReactionsParams, SignInParams, StartVideoChatParams, StopPollParams } from "./0_params.js";
|
|
20
21
|
import { MaybePromise } from "../1_utilities.js";
|
|
21
22
|
import { enums, functions, types } from "../2_tl.js";
|
|
22
23
|
import { Storage } from "../2_storage.js";
|
|
23
24
|
import { DC } from "../3_transport.js";
|
|
24
25
|
import { BotCommand, BusinessConnection, CallbackQueryAnswer, CallbackQueryQuestion, Chat, ChatAction, ChatListItem, ChatMember, ChatP, FileSource, ID, InactiveChat, InlineQueryAnswer, InlineQueryResult, InputMedia, InputStoryContent, InviteLink, LiveStreamChannel, Message, MessageAnimation, MessageAudio, MessageContact, MessageDice, MessageDocument, MessageLocation, MessagePhoto, MessagePoll, MessageSticker, MessageText, MessageVenue, MessageVideo, MessageVideoNote, MessageVoice, NetworkStatistics, ParseMode, Poll, Reaction, Sticker, Story, Update, User, VideoChat, VideoChatActive, VideoChatScheduled } from "../3_types.js";
|
|
25
26
|
import { Migrate } from "../4_errors.js";
|
|
26
|
-
import { AddReactionParams, AnswerCallbackQueryParams, AnswerInlineQueryParams, BanChatMemberParams, CreateInviteLinkParams, CreateStoryParams, DeleteMessageParams, DeleteMessagesParams, DownloadLiveStreamChunkParams, DownloadParams, EditMessageLiveLocationParams, EditMessageMediaParams, EditMessageParams, EditMessageReplyMarkupParams, ForwardMessagesParams, GetChatsParams, GetCreatedInviteLinksParams, GetHistoryParams, GetMyCommandsParams, JoinVideoChatParams, PinMessageParams, ReplyParams, ScheduleVideoChatParams, SearchMessagesParams, SendAnimationParams, SendAudioParams, SendContactParams, SendDiceParams, SendDocumentParams, SendInlineQueryParams, SendLocationParams, SendMessageParams, SendPhotoParams, SendPollParams, SendStickerParams, SendVenueParams, SendVideoNoteParams, SendVideoParams, SendVoiceParams, SetChatMemberRightsParams, SetChatPhotoParams, SetMyCommandsParams, SetReactionsParams, SignInParams, StartVideoChatParams, StopPollParams } from "./0_params.js";
|
|
27
27
|
import { Api } from "./1_types.js";
|
|
28
28
|
import { ClientPlainParams } from "./1_client_plain.js";
|
|
29
29
|
import { Composer as Composer_, NextFunction } from "./1_composer.js";
|
|
@@ -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;
|