domq-wasm 0.0.0-stage → 0.4.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/LICENSE_MIT +25 -0
- package/README.md +251 -2
- package/domq_wasm.d.ts +221 -0
- package/domq_wasm.js +652 -0
- package/domq_wasm_bg.wasm +0 -0
- package/node.js +13 -0
- package/package.json +51 -4
package/LICENSE_MIT
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Copyright (c) 2018 iamsaquib8 <saquibulhassan6@gmail.com>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any
|
|
4
|
+
person obtaining a copy of this software and associated
|
|
5
|
+
documentation files (the "Software"), to deal in the
|
|
6
|
+
Software without restriction, including without
|
|
7
|
+
limitation the rights to use, copy, modify, merge,
|
|
8
|
+
publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software
|
|
10
|
+
is furnished to do so, subject to the following
|
|
11
|
+
conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice
|
|
14
|
+
shall be included in all copies or substantial portions
|
|
15
|
+
of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
|
18
|
+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
20
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
|
21
|
+
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
22
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
23
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
|
24
|
+
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
25
|
+
DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,252 @@
|
|
|
1
|
-
#
|
|
1
|
+
# domq for JavaScript
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Messy HTML in. Clean tree, text, Markdown, or JSON out.** A forgiving HTML
|
|
4
|
+
parser with CSS selectors for JavaScript: browsers, Node, Deno, Bun and edge
|
|
5
|
+
workers. It is the Rust crate [`domq`](https://github.com/iamsaquib8/domq)
|
|
6
|
+
compiled to one **~41 KB (gzipped)** WebAssembly module, with zero JS
|
|
7
|
+
dependencies.
|
|
8
|
+
|
|
9
|
+
**[Try it in the playground](https://iamsaquib8.github.io/domq/)**: paste
|
|
10
|
+
any HTML and query it live.
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
import { select, toMarkdown } from "domq-wasm";
|
|
14
|
+
|
|
15
|
+
const html = `<ul id=links><li><a href="/a">One</a><li><a href="/b">Two</a></ul>`;
|
|
16
|
+
|
|
17
|
+
select(html, "#links a[href]").map((a) => a.attrs.href); // ["/a", "/b"]
|
|
18
|
+
toMarkdown(html); // "- [One](/a)\n- [Two](/b)"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
- **Never throws on bad HTML.** Unclosed tags, mis-nesting and stray end tags
|
|
22
|
+
are repaired, and `errors()` tells you where.
|
|
23
|
+
- **Real CSS selectors.** Combinators, attribute operators, `:nth-child()`,
|
|
24
|
+
`:not()`, `:is()`, `:has()`, plus jQuery-style `:contains("text")`.
|
|
25
|
+
- **Content out.** Readable text (no scripts or styles), CommonMark/GFM Markdown
|
|
26
|
+
for LLM prompts and RAG, a JSON-ready tree, or repaired HTML.
|
|
27
|
+
- **Small.** One `.wasm` (~41 KB gzipped) plus ~4 KB of generated glue JS.
|
|
28
|
+
|
|
29
|
+
It is not a spec-compliant WHATWG parser (no implicit `<html>`/`<body>`, no
|
|
30
|
+
adoption agency) and not a sanitizer. Use DOMPurify for untrusted HTML that you
|
|
31
|
+
render.
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
npm install domq-wasm
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Use it
|
|
40
|
+
|
|
41
|
+
The package ships one ES module build. How it loads the `.wasm` depends on the
|
|
42
|
+
runtime:
|
|
43
|
+
|
|
44
|
+
| Runtime | Setup | Status |
|
|
45
|
+
|---|---|---|
|
|
46
|
+
| Node.js 22+ (supported LTS lines) | nothing: `import { select } from "domq-wasm"` | tested in CI on the current LTS |
|
|
47
|
+
| Browsers, bundlers (Vite, webpack 5, …) | `await init()` once | tested (the playground uses this build) |
|
|
48
|
+
| Bun | nothing (uses the Node entry point) | expected to work, not tested |
|
|
49
|
+
| Deno | nothing with `npm:domq-wasm` (Node entry point) | expected to work, not tested |
|
|
50
|
+
| Cloudflare Workers | `initSync({ module })` with the imported `.wasm` | expected to work, not tested |
|
|
51
|
+
|
|
52
|
+
### Node.js
|
|
53
|
+
|
|
54
|
+
Import and call. The Node entry point loads the module synchronously at import
|
|
55
|
+
time, so there is no `init()` to await (calling it anyway is harmless).
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
import { select, toText } from "domq-wasm";
|
|
59
|
+
|
|
60
|
+
const res = await fetch("https://news.ycombinator.com");
|
|
61
|
+
const html = await res.text();
|
|
62
|
+
for (const link of select(html, ".titleline > a")) {
|
|
63
|
+
console.log(link.text, link.attrs.href);
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
From CommonJS, `require("domq-wasm")` works on Node versions that can
|
|
68
|
+
`require()` ES modules (20.19+ and 22.12+). On older versions, use
|
|
69
|
+
`const { select } = await import("domq-wasm")`.
|
|
70
|
+
|
|
71
|
+
### Browsers and bundlers
|
|
72
|
+
|
|
73
|
+
Call the default export once before anything else. It fetches
|
|
74
|
+
`domq_wasm_bg.wasm` from next to the JS file, which bundlers such as Vite
|
|
75
|
+
and webpack 5 handle as an asset.
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
import init, { select, toMarkdown } from "domq-wasm";
|
|
79
|
+
|
|
80
|
+
await init();
|
|
81
|
+
const md = toMarkdown(document.documentElement.outerHTML, "article");
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Without a bundler, import it from a CDN:
|
|
85
|
+
|
|
86
|
+
```html
|
|
87
|
+
<script type="module">
|
|
88
|
+
import init, { toText } from "https://cdn.jsdelivr.net/npm/domq@0.3/domq_wasm.js";
|
|
89
|
+
await init();
|
|
90
|
+
console.log(toText("<p>Hello <b>world</b>"));
|
|
91
|
+
</script>
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Deno and Bun
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
import { select } from "npm:domq-wasm@0.4"; // Deno; in Bun: "domq-wasm"
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Both resolve the package's Node entry point, which reads the `.wasm` with
|
|
101
|
+
`node:fs`. Deno may ask for read permission (`--allow-read`).
|
|
102
|
+
|
|
103
|
+
### Cloudflare Workers
|
|
104
|
+
|
|
105
|
+
Wrangler imports `.wasm` files as compiled `WebAssembly.Module`s; hand it to
|
|
106
|
+
`initSync`:
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
import { initSync, toMarkdown } from "domq-wasm";
|
|
110
|
+
import wasm from "domq-wasm/domq_wasm_bg.wasm";
|
|
111
|
+
|
|
112
|
+
initSync({ module: wasm });
|
|
113
|
+
|
|
114
|
+
export default {
|
|
115
|
+
async fetch(request) {
|
|
116
|
+
const page = await fetch("https://blog.rust-lang.org");
|
|
117
|
+
return new Response(toMarkdown(await page.text(), "main"));
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## API
|
|
123
|
+
|
|
124
|
+
Every function takes the HTML as a string. Functions that take a `selector`
|
|
125
|
+
throw a `SelectorError` if it is invalid; nothing else throws.
|
|
126
|
+
|
|
127
|
+
| Function | Returns |
|
|
128
|
+
|---|---|
|
|
129
|
+
| `parse(html)` | `DomDocument`: the tree as plain objects |
|
|
130
|
+
| `parseJson(html)` | `string`: the same tree as JSON text |
|
|
131
|
+
| `select(html, selector)` | `Match[]`: every matching element, in document order |
|
|
132
|
+
| `selectFirst(html, selector)` | `Match \| undefined`: the first match |
|
|
133
|
+
| `toText(html, selector?)` | `string`: readable text of the document, or of every match separated by a blank line |
|
|
134
|
+
| `toMarkdown(html, selector?)` | `string`: Markdown of the document, or of every match separated by a blank line |
|
|
135
|
+
| `toHtml(html)` | `string`: the repaired HTML (every element closed and properly nested) |
|
|
136
|
+
| `errors(html)` | `ParseError[]`: what the parser had to repair, and where |
|
|
137
|
+
| `init(input?)`, `initSync({ module })` | load the `.wasm` (browsers and Workers only; see above) |
|
|
138
|
+
|
|
139
|
+
### Parse once, query many times
|
|
140
|
+
|
|
141
|
+
Each function above parses the HTML again. To run several queries on one
|
|
142
|
+
document, parse it once:
|
|
143
|
+
|
|
144
|
+
```js
|
|
145
|
+
import { Document } from "domq-wasm";
|
|
146
|
+
|
|
147
|
+
const doc = new Document(html); // or Document.parse(html)
|
|
148
|
+
const title = doc.selectFirst("h1")?.text;
|
|
149
|
+
const prices = doc.select("tr:has(td.in-stock) .price").map((m) => m.text);
|
|
150
|
+
const body = doc.toMarkdown("article");
|
|
151
|
+
doc.free(); // release its wasm memory now instead of waiting for the GC
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
`Document` has the same operations as methods: `select(selector)`,
|
|
155
|
+
`selectFirst(selector)`, `toText(selector?)`, `toMarkdown(selector?)`,
|
|
156
|
+
`toHtml()`, `tree()`, `errors()` and `free()`. `toJSON()` returns the same as
|
|
157
|
+
`tree()`, so `JSON.stringify(doc)` gives the JSON tree. Where explicit resource
|
|
158
|
+
management is available, `using doc = new Document(html)` frees it
|
|
159
|
+
automatically.
|
|
160
|
+
|
|
161
|
+
If you also use the DOM's `Document` type in the same file, rename the import:
|
|
162
|
+
`import { Document as HtmlDocument } from "domq-wasm"`.
|
|
163
|
+
|
|
164
|
+
### Types
|
|
165
|
+
|
|
166
|
+
The package includes TypeScript declarations; none of the API is typed `any`.
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
interface Match {
|
|
170
|
+
tag: string; // lower-cased tag name
|
|
171
|
+
attrs: Record<string, string>; // attributes; boolean attributes are ""
|
|
172
|
+
text: string; // readable text, as toText() gives it
|
|
173
|
+
html: string; // outer HTML, re-serialized
|
|
174
|
+
nodeIndex: number; // position in a depth-first walk of tree()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
interface DomDocument {
|
|
178
|
+
type: "document";
|
|
179
|
+
errors: number; // how many repairs (0 = clean input)
|
|
180
|
+
children: DomNode[];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
type DomNode =
|
|
184
|
+
| { type: "element"; tag: string; attrs: Record<string, string>; children: DomNode[] }
|
|
185
|
+
| { type: "text"; value: string }
|
|
186
|
+
| { type: "comment"; value: string }
|
|
187
|
+
| { type: "doctype"; value: string };
|
|
188
|
+
|
|
189
|
+
interface ParseError {
|
|
190
|
+
position: number; // index into the html string
|
|
191
|
+
message: string; // e.g. "element implicitly closed"
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
interface SelectorError extends Error {
|
|
195
|
+
name: "SelectorError";
|
|
196
|
+
position: number; // index into the selector string
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Invalid selectors
|
|
201
|
+
|
|
202
|
+
```js
|
|
203
|
+
try {
|
|
204
|
+
select(html, "a:hover");
|
|
205
|
+
} catch (e) {
|
|
206
|
+
e.name; // "SelectorError"
|
|
207
|
+
e.message; // "unsupported pseudo-class ':hover' at byte 1"
|
|
208
|
+
e.position; // 1
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Unsupported selectors are errors rather than silently matching nothing:
|
|
213
|
+
pseudo-elements, `:hover` and other interactive states, namespaces, and
|
|
214
|
+
`:has()` inside `:has()`. The full grammar is in the
|
|
215
|
+
[`Selector` docs](https://docs.rs/domq/latest/domq/struct.Selector.html).
|
|
216
|
+
|
|
217
|
+
## Size
|
|
218
|
+
|
|
219
|
+
| File | Raw | Gzipped |
|
|
220
|
+
|---|---:|---:|
|
|
221
|
+
| `domq_wasm_bg.wasm` | ~88 KB | ~41 KB |
|
|
222
|
+
| `domq_wasm.js` (glue) | ~22 KB | ~4 KB |
|
|
223
|
+
|
|
224
|
+
The module contains the parser, the selector engine, and the text, Markdown,
|
|
225
|
+
JSON and HTML serializers. CI fails if the gzipped `.wasm` grows past 45 KiB.
|
|
226
|
+
|
|
227
|
+
## Upgrading from 0.1 or 0.2
|
|
228
|
+
|
|
229
|
+
0.3 is a new API. `parse()` now returns objects, so drop the `JSON.parse`, or
|
|
230
|
+
call `parseJson()` for the string. `parseErrorCount(html)` is now
|
|
231
|
+
`errors(html).length`. `query(html, selector)` is now `select(html, selector)`,
|
|
232
|
+
which returns `Match` objects instead of a JSON string of subtrees.
|
|
233
|
+
|
|
234
|
+
## Building from source
|
|
235
|
+
|
|
236
|
+
```sh
|
|
237
|
+
rustup target add wasm32-unknown-unknown
|
|
238
|
+
cargo install wasm-pack
|
|
239
|
+
|
|
240
|
+
cd wasm
|
|
241
|
+
wasm-pack build --target web --release # -> pkg/
|
|
242
|
+
node npm/finish.mjs # add the Node entry point + "exports"
|
|
243
|
+
wasm-pack test --node # Rust-side tests of the bindings
|
|
244
|
+
sh npm/smoke-test.sh # pack pkg/ and run tests/node.mjs against it
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
For the playground: `wasm-pack build --target web --release --no-pack --out-dir ../playground/pkg`,
|
|
248
|
+
then serve `playground/` over HTTP (for example `python3 -m http.server -d ../playground`).
|
|
249
|
+
|
|
250
|
+
## License
|
|
251
|
+
|
|
252
|
+
MIT
|
package/domq_wasm.d.ts
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/** An element in the parsed tree. */
|
|
5
|
+
export interface DomElement {
|
|
6
|
+
type: "element";
|
|
7
|
+
/** Lower-cased tag name, e.g. `"div"`. */
|
|
8
|
+
tag: string;
|
|
9
|
+
/** Attributes (names lower-cased, values entity-decoded; boolean attributes are `""`). */
|
|
10
|
+
attrs: Record<string, string>;
|
|
11
|
+
children: DomNode[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Any node in the parsed tree. Text is already entity-decoded. */
|
|
15
|
+
export type DomNode =
|
|
16
|
+
| DomElement
|
|
17
|
+
| { type: "text"; value: string }
|
|
18
|
+
| { type: "comment"; value: string }
|
|
19
|
+
| { type: "doctype"; value: string };
|
|
20
|
+
|
|
21
|
+
/** The parsed document, as returned by `parse()` and `Document#tree()`. */
|
|
22
|
+
export interface DomDocument {
|
|
23
|
+
type: "document";
|
|
24
|
+
/** How many problems the parser recovered from (0 = clean input). See `errors()`. */
|
|
25
|
+
errors: number;
|
|
26
|
+
/** Top-level nodes, in document order. */
|
|
27
|
+
children: DomNode[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** An element matched by a CSS selector. */
|
|
31
|
+
export interface Match {
|
|
32
|
+
/** Lower-cased tag name. */
|
|
33
|
+
tag: string;
|
|
34
|
+
/** The element's attributes. */
|
|
35
|
+
attrs: Record<string, string>;
|
|
36
|
+
/** Readable text of the element (what `toText` gives: no scripts, collapsed whitespace). */
|
|
37
|
+
text: string;
|
|
38
|
+
/** The element's outer HTML (re-serialized, so it is well-formed). */
|
|
39
|
+
html: string;
|
|
40
|
+
/**
|
|
41
|
+
* The element's position in a depth-first walk of the whole tree (0-based,
|
|
42
|
+
* counting text, comment and doctype nodes too) — the order in which
|
|
43
|
+
* `tree()` lists nodes. Use it to find a match in the tree.
|
|
44
|
+
*/
|
|
45
|
+
nodeIndex: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A problem the parser recovered from. Parsing itself never fails. */
|
|
49
|
+
export interface ParseError {
|
|
50
|
+
/** Index into the input string (a normal JS string index) where it happened. */
|
|
51
|
+
position: number;
|
|
52
|
+
/** Short description, e.g. `"element implicitly closed"`. */
|
|
53
|
+
message: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Thrown (as an `Error` with `name === "SelectorError"`) when a CSS selector is
|
|
58
|
+
* invalid or unsupported, e.g. `"a:hover"` or `"div >"`.
|
|
59
|
+
*/
|
|
60
|
+
export interface SelectorError extends Error {
|
|
61
|
+
name: "SelectorError";
|
|
62
|
+
/** Index into the selector string where the problem is. */
|
|
63
|
+
position: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A parsed HTML document. Parse once, then query it as often as you like.
|
|
70
|
+
*
|
|
71
|
+
* Free it with `free()` (or a `using` declaration) when you are done, or let
|
|
72
|
+
* the garbage collector do it.
|
|
73
|
+
*/
|
|
74
|
+
export class Document {
|
|
75
|
+
free(): void;
|
|
76
|
+
[Symbol.dispose](): void;
|
|
77
|
+
/**
|
|
78
|
+
* The problems the parser recovered from (empty for clean input).
|
|
79
|
+
*/
|
|
80
|
+
errors(): ParseError[];
|
|
81
|
+
/**
|
|
82
|
+
* Parse `html` (a full page or a fragment). Never throws: broken markup is
|
|
83
|
+
* repaired and the repairs are listed by `errors()`.
|
|
84
|
+
*/
|
|
85
|
+
constructor(html: string);
|
|
86
|
+
/**
|
|
87
|
+
* Same as `new Document(html)`.
|
|
88
|
+
*/
|
|
89
|
+
static parse(html: string): Document;
|
|
90
|
+
/**
|
|
91
|
+
* The first element matching a CSS `selector`, or `undefined`. Throws a
|
|
92
|
+
* `SelectorError` if the selector is invalid.
|
|
93
|
+
*/
|
|
94
|
+
selectFirst(selector: string): Match | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* Every element matching a CSS `selector`, in document order. Throws a
|
|
97
|
+
* `SelectorError` if the selector is invalid.
|
|
98
|
+
*/
|
|
99
|
+
select(selector: string): Match[];
|
|
100
|
+
/**
|
|
101
|
+
* The document re-serialized as HTML: every tag closed and nested
|
|
102
|
+
* properly, attributes quoted, text escaped.
|
|
103
|
+
*/
|
|
104
|
+
toHtml(): string;
|
|
105
|
+
/**
|
|
106
|
+
* Same as `tree()`, so `JSON.stringify(doc)` gives the JSON tree.
|
|
107
|
+
*/
|
|
108
|
+
toJSON(): DomDocument;
|
|
109
|
+
/**
|
|
110
|
+
* Markdown (CommonMark + GFM tables): headings, lists, links, images,
|
|
111
|
+
* code blocks, tables, quotes. With a `selector`, every match converted
|
|
112
|
+
* and separated by a blank line.
|
|
113
|
+
*/
|
|
114
|
+
toMarkdown(selector?: string | null): string;
|
|
115
|
+
/**
|
|
116
|
+
* Readable plain text: no `<script>`/`<style>`/`<head>`, whitespace
|
|
117
|
+
* collapsed, a line per block, a blank line between paragraphs. With a
|
|
118
|
+
* `selector`, the text of every match, separated by a blank line.
|
|
119
|
+
*/
|
|
120
|
+
toText(selector?: string | null): string;
|
|
121
|
+
/**
|
|
122
|
+
* The whole tree as plain JS objects.
|
|
123
|
+
*/
|
|
124
|
+
tree(): DomDocument;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The problems the parser recovered from while parsing `html`.
|
|
129
|
+
*/
|
|
130
|
+
export function errors(html: string): ParseError[];
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Parse `html` into a tree of plain JS objects (see `DomDocument`).
|
|
134
|
+
*/
|
|
135
|
+
export function parse(html: string): DomDocument;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Parse `html` and return the tree as a JSON string (the same shape as
|
|
139
|
+
* `parse()`), e.g. to send it over the network without re-encoding.
|
|
140
|
+
*/
|
|
141
|
+
export function parseJson(html: string): string;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Every element of `html` matching a CSS `selector`. Throws a
|
|
145
|
+
* `SelectorError` if the selector is invalid.
|
|
146
|
+
*/
|
|
147
|
+
export function select(html: string, selector: string): Match[];
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The first element of `html` matching a CSS `selector`, or `undefined`.
|
|
151
|
+
* Throws a `SelectorError` if the selector is invalid.
|
|
152
|
+
*/
|
|
153
|
+
export function selectFirst(html: string, selector: string): Match | undefined;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* `html` parsed and re-serialized: the repaired, well-formed version.
|
|
157
|
+
*/
|
|
158
|
+
export function toHtml(html: string): string;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* `html` as Markdown — or, with a `selector`, every match converted and
|
|
162
|
+
* separated by a blank line.
|
|
163
|
+
*/
|
|
164
|
+
export function toMarkdown(html: string, selector?: string | null): string;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* `html` as readable plain text — or, with a `selector`, the text of every
|
|
168
|
+
* match separated by a blank line.
|
|
169
|
+
*/
|
|
170
|
+
export function toText(html: string, selector?: string | null): string;
|
|
171
|
+
|
|
172
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
173
|
+
|
|
174
|
+
export interface InitOutput {
|
|
175
|
+
readonly memory: WebAssembly.Memory;
|
|
176
|
+
readonly __wbg_document_free: (a: number, b: number) => void;
|
|
177
|
+
readonly document_errors: (a: number) => number;
|
|
178
|
+
readonly document_new: (a: number, b: number) => number;
|
|
179
|
+
readonly document_parse: (a: number, b: number) => number;
|
|
180
|
+
readonly document_select: (a: number, b: number, c: number, d: number) => void;
|
|
181
|
+
readonly document_selectFirst: (a: number, b: number, c: number, d: number) => void;
|
|
182
|
+
readonly document_toHtml: (a: number, b: number) => void;
|
|
183
|
+
readonly document_toJSON: (a: number) => number;
|
|
184
|
+
readonly document_toMarkdown: (a: number, b: number, c: number, d: number) => void;
|
|
185
|
+
readonly document_toText: (a: number, b: number, c: number, d: number) => void;
|
|
186
|
+
readonly document_tree: (a: number) => number;
|
|
187
|
+
readonly errors: (a: number, b: number) => number;
|
|
188
|
+
readonly parse: (a: number, b: number) => number;
|
|
189
|
+
readonly parseJson: (a: number, b: number, c: number) => void;
|
|
190
|
+
readonly select: (a: number, b: number, c: number, d: number, e: number) => void;
|
|
191
|
+
readonly selectFirst: (a: number, b: number, c: number, d: number, e: number) => void;
|
|
192
|
+
readonly toHtml: (a: number, b: number, c: number) => void;
|
|
193
|
+
readonly toMarkdown: (a: number, b: number, c: number, d: number, e: number) => void;
|
|
194
|
+
readonly toText: (a: number, b: number, c: number, d: number, e: number) => void;
|
|
195
|
+
readonly __wbindgen_export: (a: number, b: number) => number;
|
|
196
|
+
readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
|
|
197
|
+
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
|
|
198
|
+
readonly __wbindgen_export3: (a: number, b: number, c: number) => void;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
205
|
+
* a precompiled `WebAssembly.Module`.
|
|
206
|
+
*
|
|
207
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
208
|
+
*
|
|
209
|
+
* @returns {InitOutput}
|
|
210
|
+
*/
|
|
211
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
215
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
216
|
+
*
|
|
217
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
218
|
+
*
|
|
219
|
+
* @returns {Promise<InitOutput>}
|
|
220
|
+
*/
|
|
221
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
package/domq_wasm.js
ADDED
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
/* @ts-self-types="./domq_wasm.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A parsed HTML document. Parse once, then query it as often as you like.
|
|
5
|
+
*
|
|
6
|
+
* Free it with `free()` (or a `using` declaration) when you are done, or let
|
|
7
|
+
* the garbage collector do it.
|
|
8
|
+
*/
|
|
9
|
+
export class Document {
|
|
10
|
+
static __wrap(ptr) {
|
|
11
|
+
const obj = Object.create(Document.prototype);
|
|
12
|
+
obj.__wbg_ptr = ptr;
|
|
13
|
+
DocumentFinalization.register(obj, obj.__wbg_ptr, obj);
|
|
14
|
+
return obj;
|
|
15
|
+
}
|
|
16
|
+
__destroy_into_raw() {
|
|
17
|
+
const ptr = this.__wbg_ptr;
|
|
18
|
+
this.__wbg_ptr = 0;
|
|
19
|
+
DocumentFinalization.unregister(this);
|
|
20
|
+
return ptr;
|
|
21
|
+
}
|
|
22
|
+
free() {
|
|
23
|
+
const ptr = this.__destroy_into_raw();
|
|
24
|
+
wasm.__wbg_document_free(ptr, 0);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The problems the parser recovered from (empty for clean input).
|
|
28
|
+
* @returns {ParseError[]}
|
|
29
|
+
*/
|
|
30
|
+
errors() {
|
|
31
|
+
const ret = wasm.document_errors(this.__wbg_ptr);
|
|
32
|
+
return takeObject(ret);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Parse `html` (a full page or a fragment). Never throws: broken markup is
|
|
36
|
+
* repaired and the repairs are listed by `errors()`.
|
|
37
|
+
* @param {string} html
|
|
38
|
+
*/
|
|
39
|
+
constructor(html) {
|
|
40
|
+
const ptr0 = passStringToWasm0(html, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
41
|
+
const len0 = WASM_VECTOR_LEN;
|
|
42
|
+
const ret = wasm.document_new(ptr0, len0);
|
|
43
|
+
this.__wbg_ptr = ret;
|
|
44
|
+
DocumentFinalization.register(this, this.__wbg_ptr, this);
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Same as `new Document(html)`.
|
|
49
|
+
* @param {string} html
|
|
50
|
+
* @returns {Document}
|
|
51
|
+
*/
|
|
52
|
+
static parse(html) {
|
|
53
|
+
const ptr0 = passStringToWasm0(html, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
54
|
+
const len0 = WASM_VECTOR_LEN;
|
|
55
|
+
const ret = wasm.document_parse(ptr0, len0);
|
|
56
|
+
return Document.__wrap(ret);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The first element matching a CSS `selector`, or `undefined`. Throws a
|
|
60
|
+
* `SelectorError` if the selector is invalid.
|
|
61
|
+
* @param {string} selector
|
|
62
|
+
* @returns {Match | undefined}
|
|
63
|
+
*/
|
|
64
|
+
selectFirst(selector) {
|
|
65
|
+
try {
|
|
66
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
67
|
+
const ptr0 = passStringToWasm0(selector, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
68
|
+
const len0 = WASM_VECTOR_LEN;
|
|
69
|
+
wasm.document_selectFirst(retptr, this.__wbg_ptr, ptr0, len0);
|
|
70
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
71
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
72
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
73
|
+
if (r2) {
|
|
74
|
+
throw takeObject(r1);
|
|
75
|
+
}
|
|
76
|
+
return takeObject(r0);
|
|
77
|
+
} finally {
|
|
78
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Every element matching a CSS `selector`, in document order. Throws a
|
|
83
|
+
* `SelectorError` if the selector is invalid.
|
|
84
|
+
* @param {string} selector
|
|
85
|
+
* @returns {Match[]}
|
|
86
|
+
*/
|
|
87
|
+
select(selector) {
|
|
88
|
+
try {
|
|
89
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
90
|
+
const ptr0 = passStringToWasm0(selector, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
91
|
+
const len0 = WASM_VECTOR_LEN;
|
|
92
|
+
wasm.document_select(retptr, this.__wbg_ptr, ptr0, len0);
|
|
93
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
94
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
95
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
96
|
+
if (r2) {
|
|
97
|
+
throw takeObject(r1);
|
|
98
|
+
}
|
|
99
|
+
return takeObject(r0);
|
|
100
|
+
} finally {
|
|
101
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The document re-serialized as HTML: every tag closed and nested
|
|
106
|
+
* properly, attributes quoted, text escaped.
|
|
107
|
+
* @returns {string}
|
|
108
|
+
*/
|
|
109
|
+
toHtml() {
|
|
110
|
+
let deferred1_0;
|
|
111
|
+
let deferred1_1;
|
|
112
|
+
try {
|
|
113
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
114
|
+
wasm.document_toHtml(retptr, this.__wbg_ptr);
|
|
115
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
116
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
117
|
+
deferred1_0 = r0;
|
|
118
|
+
deferred1_1 = r1;
|
|
119
|
+
return getStringFromWasm0(r0, r1);
|
|
120
|
+
} finally {
|
|
121
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
122
|
+
wasm.__wbindgen_export3(deferred1_0, deferred1_1, 1);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Same as `tree()`, so `JSON.stringify(doc)` gives the JSON tree.
|
|
127
|
+
* @returns {DomDocument}
|
|
128
|
+
*/
|
|
129
|
+
toJSON() {
|
|
130
|
+
const ret = wasm.document_toJSON(this.__wbg_ptr);
|
|
131
|
+
return takeObject(ret);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Markdown (CommonMark + GFM tables): headings, lists, links, images,
|
|
135
|
+
* code blocks, tables, quotes. With a `selector`, every match converted
|
|
136
|
+
* and separated by a blank line.
|
|
137
|
+
* @param {string | null} [selector]
|
|
138
|
+
* @returns {string}
|
|
139
|
+
*/
|
|
140
|
+
toMarkdown(selector) {
|
|
141
|
+
let deferred3_0;
|
|
142
|
+
let deferred3_1;
|
|
143
|
+
try {
|
|
144
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
145
|
+
var ptr0 = isLikeNone(selector) ? 0 : passStringToWasm0(selector, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
146
|
+
var len0 = WASM_VECTOR_LEN;
|
|
147
|
+
wasm.document_toMarkdown(retptr, this.__wbg_ptr, ptr0, len0);
|
|
148
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
149
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
150
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
151
|
+
var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
|
|
152
|
+
var ptr2 = r0;
|
|
153
|
+
var len2 = r1;
|
|
154
|
+
if (r3) {
|
|
155
|
+
ptr2 = 0; len2 = 0;
|
|
156
|
+
throw takeObject(r2);
|
|
157
|
+
}
|
|
158
|
+
deferred3_0 = ptr2;
|
|
159
|
+
deferred3_1 = len2;
|
|
160
|
+
return getStringFromWasm0(ptr2, len2);
|
|
161
|
+
} finally {
|
|
162
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
163
|
+
wasm.__wbindgen_export3(deferred3_0, deferred3_1, 1);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Readable plain text: no `<script>`/`<style>`/`<head>`, whitespace
|
|
168
|
+
* collapsed, a line per block, a blank line between paragraphs. With a
|
|
169
|
+
* `selector`, the text of every match, separated by a blank line.
|
|
170
|
+
* @param {string | null} [selector]
|
|
171
|
+
* @returns {string}
|
|
172
|
+
*/
|
|
173
|
+
toText(selector) {
|
|
174
|
+
let deferred3_0;
|
|
175
|
+
let deferred3_1;
|
|
176
|
+
try {
|
|
177
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
178
|
+
var ptr0 = isLikeNone(selector) ? 0 : passStringToWasm0(selector, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
179
|
+
var len0 = WASM_VECTOR_LEN;
|
|
180
|
+
wasm.document_toText(retptr, this.__wbg_ptr, ptr0, len0);
|
|
181
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
182
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
183
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
184
|
+
var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
|
|
185
|
+
var ptr2 = r0;
|
|
186
|
+
var len2 = r1;
|
|
187
|
+
if (r3) {
|
|
188
|
+
ptr2 = 0; len2 = 0;
|
|
189
|
+
throw takeObject(r2);
|
|
190
|
+
}
|
|
191
|
+
deferred3_0 = ptr2;
|
|
192
|
+
deferred3_1 = len2;
|
|
193
|
+
return getStringFromWasm0(ptr2, len2);
|
|
194
|
+
} finally {
|
|
195
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
196
|
+
wasm.__wbindgen_export3(deferred3_0, deferred3_1, 1);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* The whole tree as plain JS objects.
|
|
201
|
+
* @returns {DomDocument}
|
|
202
|
+
*/
|
|
203
|
+
tree() {
|
|
204
|
+
const ret = wasm.document_tree(this.__wbg_ptr);
|
|
205
|
+
return takeObject(ret);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (Symbol.dispose) Document.prototype[Symbol.dispose] = Document.prototype.free;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The problems the parser recovered from while parsing `html`.
|
|
212
|
+
* @param {string} html
|
|
213
|
+
* @returns {ParseError[]}
|
|
214
|
+
*/
|
|
215
|
+
export function errors(html) {
|
|
216
|
+
const ptr0 = passStringToWasm0(html, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
217
|
+
const len0 = WASM_VECTOR_LEN;
|
|
218
|
+
const ret = wasm.errors(ptr0, len0);
|
|
219
|
+
return takeObject(ret);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Parse `html` into a tree of plain JS objects (see `DomDocument`).
|
|
224
|
+
* @param {string} html
|
|
225
|
+
* @returns {DomDocument}
|
|
226
|
+
*/
|
|
227
|
+
export function parse(html) {
|
|
228
|
+
const ptr0 = passStringToWasm0(html, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
229
|
+
const len0 = WASM_VECTOR_LEN;
|
|
230
|
+
const ret = wasm.parse(ptr0, len0);
|
|
231
|
+
return takeObject(ret);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Parse `html` and return the tree as a JSON string (the same shape as
|
|
236
|
+
* `parse()`), e.g. to send it over the network without re-encoding.
|
|
237
|
+
* @param {string} html
|
|
238
|
+
* @returns {string}
|
|
239
|
+
*/
|
|
240
|
+
export function parseJson(html) {
|
|
241
|
+
let deferred2_0;
|
|
242
|
+
let deferred2_1;
|
|
243
|
+
try {
|
|
244
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
245
|
+
const ptr0 = passStringToWasm0(html, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
246
|
+
const len0 = WASM_VECTOR_LEN;
|
|
247
|
+
wasm.parseJson(retptr, ptr0, len0);
|
|
248
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
249
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
250
|
+
deferred2_0 = r0;
|
|
251
|
+
deferred2_1 = r1;
|
|
252
|
+
return getStringFromWasm0(r0, r1);
|
|
253
|
+
} finally {
|
|
254
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
255
|
+
wasm.__wbindgen_export3(deferred2_0, deferred2_1, 1);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Every element of `html` matching a CSS `selector`. Throws a
|
|
261
|
+
* `SelectorError` if the selector is invalid.
|
|
262
|
+
* @param {string} html
|
|
263
|
+
* @param {string} selector
|
|
264
|
+
* @returns {Match[]}
|
|
265
|
+
*/
|
|
266
|
+
export function select(html, selector) {
|
|
267
|
+
try {
|
|
268
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
269
|
+
const ptr0 = passStringToWasm0(html, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
270
|
+
const len0 = WASM_VECTOR_LEN;
|
|
271
|
+
const ptr1 = passStringToWasm0(selector, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
272
|
+
const len1 = WASM_VECTOR_LEN;
|
|
273
|
+
wasm.select(retptr, ptr0, len0, ptr1, len1);
|
|
274
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
275
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
276
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
277
|
+
if (r2) {
|
|
278
|
+
throw takeObject(r1);
|
|
279
|
+
}
|
|
280
|
+
return takeObject(r0);
|
|
281
|
+
} finally {
|
|
282
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* The first element of `html` matching a CSS `selector`, or `undefined`.
|
|
288
|
+
* Throws a `SelectorError` if the selector is invalid.
|
|
289
|
+
* @param {string} html
|
|
290
|
+
* @param {string} selector
|
|
291
|
+
* @returns {Match | undefined}
|
|
292
|
+
*/
|
|
293
|
+
export function selectFirst(html, selector) {
|
|
294
|
+
try {
|
|
295
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
296
|
+
const ptr0 = passStringToWasm0(html, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
297
|
+
const len0 = WASM_VECTOR_LEN;
|
|
298
|
+
const ptr1 = passStringToWasm0(selector, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
299
|
+
const len1 = WASM_VECTOR_LEN;
|
|
300
|
+
wasm.selectFirst(retptr, ptr0, len0, ptr1, len1);
|
|
301
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
302
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
303
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
304
|
+
if (r2) {
|
|
305
|
+
throw takeObject(r1);
|
|
306
|
+
}
|
|
307
|
+
return takeObject(r0);
|
|
308
|
+
} finally {
|
|
309
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* `html` parsed and re-serialized: the repaired, well-formed version.
|
|
315
|
+
* @param {string} html
|
|
316
|
+
* @returns {string}
|
|
317
|
+
*/
|
|
318
|
+
export function toHtml(html) {
|
|
319
|
+
let deferred2_0;
|
|
320
|
+
let deferred2_1;
|
|
321
|
+
try {
|
|
322
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
323
|
+
const ptr0 = passStringToWasm0(html, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
324
|
+
const len0 = WASM_VECTOR_LEN;
|
|
325
|
+
wasm.toHtml(retptr, ptr0, len0);
|
|
326
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
327
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
328
|
+
deferred2_0 = r0;
|
|
329
|
+
deferred2_1 = r1;
|
|
330
|
+
return getStringFromWasm0(r0, r1);
|
|
331
|
+
} finally {
|
|
332
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
333
|
+
wasm.__wbindgen_export3(deferred2_0, deferred2_1, 1);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* `html` as Markdown — or, with a `selector`, every match converted and
|
|
339
|
+
* separated by a blank line.
|
|
340
|
+
* @param {string} html
|
|
341
|
+
* @param {string | null} [selector]
|
|
342
|
+
* @returns {string}
|
|
343
|
+
*/
|
|
344
|
+
export function toMarkdown(html, selector) {
|
|
345
|
+
let deferred4_0;
|
|
346
|
+
let deferred4_1;
|
|
347
|
+
try {
|
|
348
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
349
|
+
const ptr0 = passStringToWasm0(html, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
350
|
+
const len0 = WASM_VECTOR_LEN;
|
|
351
|
+
var ptr1 = isLikeNone(selector) ? 0 : passStringToWasm0(selector, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
352
|
+
var len1 = WASM_VECTOR_LEN;
|
|
353
|
+
wasm.toMarkdown(retptr, ptr0, len0, ptr1, len1);
|
|
354
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
355
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
356
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
357
|
+
var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
|
|
358
|
+
var ptr3 = r0;
|
|
359
|
+
var len3 = r1;
|
|
360
|
+
if (r3) {
|
|
361
|
+
ptr3 = 0; len3 = 0;
|
|
362
|
+
throw takeObject(r2);
|
|
363
|
+
}
|
|
364
|
+
deferred4_0 = ptr3;
|
|
365
|
+
deferred4_1 = len3;
|
|
366
|
+
return getStringFromWasm0(ptr3, len3);
|
|
367
|
+
} finally {
|
|
368
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
369
|
+
wasm.__wbindgen_export3(deferred4_0, deferred4_1, 1);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* `html` as readable plain text — or, with a `selector`, the text of every
|
|
375
|
+
* match separated by a blank line.
|
|
376
|
+
* @param {string} html
|
|
377
|
+
* @param {string | null} [selector]
|
|
378
|
+
* @returns {string}
|
|
379
|
+
*/
|
|
380
|
+
export function toText(html, selector) {
|
|
381
|
+
let deferred4_0;
|
|
382
|
+
let deferred4_1;
|
|
383
|
+
try {
|
|
384
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
385
|
+
const ptr0 = passStringToWasm0(html, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
386
|
+
const len0 = WASM_VECTOR_LEN;
|
|
387
|
+
var ptr1 = isLikeNone(selector) ? 0 : passStringToWasm0(selector, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
388
|
+
var len1 = WASM_VECTOR_LEN;
|
|
389
|
+
wasm.toText(retptr, ptr0, len0, ptr1, len1);
|
|
390
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
391
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
392
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
393
|
+
var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
|
|
394
|
+
var ptr3 = r0;
|
|
395
|
+
var len3 = r1;
|
|
396
|
+
if (r3) {
|
|
397
|
+
ptr3 = 0; len3 = 0;
|
|
398
|
+
throw takeObject(r2);
|
|
399
|
+
}
|
|
400
|
+
deferred4_0 = ptr3;
|
|
401
|
+
deferred4_1 = len3;
|
|
402
|
+
return getStringFromWasm0(ptr3, len3);
|
|
403
|
+
} finally {
|
|
404
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
405
|
+
wasm.__wbindgen_export3(deferred4_0, deferred4_1, 1);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
function __wbg_get_imports() {
|
|
409
|
+
const import0 = {
|
|
410
|
+
__proto__: null,
|
|
411
|
+
__wbg___wbindgen_throw_41e9ee4f547fc59a: function(arg0, arg1) {
|
|
412
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
413
|
+
},
|
|
414
|
+
__wbg_new_debb421d9db3ecaf: function(arg0, arg1) {
|
|
415
|
+
const ret = new Error(getStringFromWasm0(arg0, arg1));
|
|
416
|
+
return addHeapObject(ret);
|
|
417
|
+
},
|
|
418
|
+
__wbg_parse_727b9ce5f066bfca: function(arg0, arg1) {
|
|
419
|
+
const ret = JSON.parse(getStringFromWasm0(arg0, arg1));
|
|
420
|
+
return addHeapObject(ret);
|
|
421
|
+
},
|
|
422
|
+
__wbg_set_name_85a0a4c54f07ab5c: function(arg0, arg1, arg2) {
|
|
423
|
+
getObject(arg0).name = getStringFromWasm0(arg1, arg2);
|
|
424
|
+
},
|
|
425
|
+
__wbg_set_position_f011541ab3447656: function(arg0, arg1) {
|
|
426
|
+
getObject(arg0).position = arg1 >>> 0;
|
|
427
|
+
},
|
|
428
|
+
};
|
|
429
|
+
return {
|
|
430
|
+
__proto__: null,
|
|
431
|
+
"./domq_wasm_bg.js": import0,
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const DocumentFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
436
|
+
? { register: () => {}, unregister: () => {} }
|
|
437
|
+
: new FinalizationRegistry(ptr => wasm.__wbg_document_free(ptr, 1));
|
|
438
|
+
|
|
439
|
+
function addHeapObject(obj) {
|
|
440
|
+
if (heap_next === heap.length) heap.push(heap.length + 1);
|
|
441
|
+
const idx = heap_next;
|
|
442
|
+
heap_next = heap[idx];
|
|
443
|
+
|
|
444
|
+
heap[idx] = obj;
|
|
445
|
+
return idx;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function dropObject(idx) {
|
|
449
|
+
if (idx < 1028) return;
|
|
450
|
+
heap[idx] = heap_next;
|
|
451
|
+
heap_next = idx;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
let cachedDataViewMemory0 = null;
|
|
455
|
+
function getDataViewMemory0() {
|
|
456
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
457
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
458
|
+
}
|
|
459
|
+
return cachedDataViewMemory0;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function getStringFromWasm0(ptr, len) {
|
|
463
|
+
return decodeText(ptr >>> 0, len);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
let cachedUint8ArrayMemory0 = null;
|
|
467
|
+
function getUint8ArrayMemory0() {
|
|
468
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
469
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
470
|
+
}
|
|
471
|
+
return cachedUint8ArrayMemory0;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
function getObject(idx) { return heap[idx]; }
|
|
475
|
+
|
|
476
|
+
let heap = new Array(1024).fill(undefined);
|
|
477
|
+
heap.push(undefined, null, true, false);
|
|
478
|
+
|
|
479
|
+
let heap_next = heap.length;
|
|
480
|
+
|
|
481
|
+
function isLikeNone(x) {
|
|
482
|
+
return x === undefined || x === null;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
486
|
+
if (realloc === undefined) {
|
|
487
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
488
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
489
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
490
|
+
WASM_VECTOR_LEN = buf.length;
|
|
491
|
+
return ptr;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
let len = arg.length;
|
|
495
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
496
|
+
|
|
497
|
+
const mem = getUint8ArrayMemory0();
|
|
498
|
+
|
|
499
|
+
let offset = 0;
|
|
500
|
+
|
|
501
|
+
for (; offset < len; offset++) {
|
|
502
|
+
const code = arg.charCodeAt(offset);
|
|
503
|
+
if (code > 0x7F) break;
|
|
504
|
+
mem[ptr + offset] = code;
|
|
505
|
+
}
|
|
506
|
+
if (offset !== len) {
|
|
507
|
+
if (offset !== 0) {
|
|
508
|
+
arg = arg.slice(offset);
|
|
509
|
+
}
|
|
510
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
511
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
512
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
513
|
+
|
|
514
|
+
offset += ret.written;
|
|
515
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
WASM_VECTOR_LEN = offset;
|
|
519
|
+
return ptr;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function takeObject(idx) {
|
|
523
|
+
const ret = getObject(idx);
|
|
524
|
+
dropObject(idx);
|
|
525
|
+
return ret;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
529
|
+
cachedTextDecoder.decode();
|
|
530
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
531
|
+
let numBytesDecoded = 0;
|
|
532
|
+
function decodeText(ptr, len) {
|
|
533
|
+
numBytesDecoded += len;
|
|
534
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
535
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
536
|
+
cachedTextDecoder.decode();
|
|
537
|
+
numBytesDecoded = len;
|
|
538
|
+
}
|
|
539
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const cachedTextEncoder = new TextEncoder();
|
|
543
|
+
|
|
544
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
545
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
546
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
547
|
+
view.set(buf);
|
|
548
|
+
return {
|
|
549
|
+
read: arg.length,
|
|
550
|
+
written: buf.length
|
|
551
|
+
};
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
let WASM_VECTOR_LEN = 0;
|
|
556
|
+
|
|
557
|
+
let wasmModule, wasmInstance, wasm;
|
|
558
|
+
function __wbg_finalize_init(instance, module) {
|
|
559
|
+
wasmInstance = instance;
|
|
560
|
+
wasm = instance.exports;
|
|
561
|
+
wasmModule = module;
|
|
562
|
+
cachedDataViewMemory0 = null;
|
|
563
|
+
cachedUint8ArrayMemory0 = null;
|
|
564
|
+
return wasm;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
async function __wbg_load(module, imports) {
|
|
568
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
569
|
+
if (!module.ok) {
|
|
570
|
+
throw new Error(`failed to fetch Wasm: ${module.status} ${module.statusText} fetching '${module.url}'`);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
574
|
+
try {
|
|
575
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
576
|
+
} catch (e) {
|
|
577
|
+
const validResponse = expectedResponseType(module.type);
|
|
578
|
+
|
|
579
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
580
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
581
|
+
|
|
582
|
+
} else { throw e; }
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const bytes = await module.arrayBuffer();
|
|
587
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
588
|
+
} else {
|
|
589
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
590
|
+
|
|
591
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
592
|
+
return { instance, module };
|
|
593
|
+
} else {
|
|
594
|
+
return instance;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function expectedResponseType(type) {
|
|
599
|
+
switch (type) {
|
|
600
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
601
|
+
}
|
|
602
|
+
return false;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
function initSync(module) {
|
|
607
|
+
if (wasm !== undefined) return wasm;
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
if (module !== undefined) {
|
|
611
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
612
|
+
({module} = module)
|
|
613
|
+
} else {
|
|
614
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
const imports = __wbg_get_imports();
|
|
619
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
620
|
+
module = new WebAssembly.Module(module);
|
|
621
|
+
}
|
|
622
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
623
|
+
return __wbg_finalize_init(instance, module);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
async function __wbg_init(module_or_path) {
|
|
627
|
+
if (wasm !== undefined) return wasm;
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
if (module_or_path !== undefined) {
|
|
631
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
632
|
+
({module_or_path} = module_or_path)
|
|
633
|
+
} else {
|
|
634
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
if (module_or_path === undefined) {
|
|
639
|
+
module_or_path = new URL('domq_wasm_bg.wasm', import.meta.url);
|
|
640
|
+
}
|
|
641
|
+
const imports = __wbg_get_imports();
|
|
642
|
+
|
|
643
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
644
|
+
module_or_path = fetch(module_or_path);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
648
|
+
|
|
649
|
+
return __wbg_finalize_init(instance, module);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
export { initSync, __wbg_init as default };
|
|
Binary file
|
package/node.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Node.js entry point of the `domq` npm package (selected through the
|
|
2
|
+
// "node" export condition; Bun picks it too).
|
|
3
|
+
//
|
|
4
|
+
// It loads the .wasm from disk synchronously, so the API is ready as soon as
|
|
5
|
+
// the import resolves: no `await init()` needed. Calling `init()` anyway (as
|
|
6
|
+
// code shared with the browser does) is harmless: it returns immediately.
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import init, { initSync } from "./domq_wasm.js";
|
|
9
|
+
|
|
10
|
+
initSync({ module: readFileSync(new URL("./domq_wasm_bg.wasm", import.meta.url)) });
|
|
11
|
+
|
|
12
|
+
export * from "./domq_wasm.js";
|
|
13
|
+
export default init;
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "domq-wasm",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
"type": "module",
|
|
4
|
+
"collaborators": [
|
|
5
|
+
"iamsaquib8 <saquibulhassan6@gmail.com>"
|
|
6
|
+
],
|
|
7
|
+
"description": "Tiny, forgiving HTML parser for JavaScript (WebAssembly): CSS selectors, readable text, Markdown and JSON from messy HTML. Zero JS dependencies.",
|
|
8
|
+
"version": "0.4.0",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/iamsaquib8/domq.git"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"domq_wasm_bg.wasm",
|
|
16
|
+
"domq_wasm.js",
|
|
17
|
+
"domq_wasm.d.ts",
|
|
18
|
+
"LICENSE_MIT",
|
|
19
|
+
"node.js"
|
|
20
|
+
],
|
|
21
|
+
"main": "domq_wasm.js",
|
|
22
|
+
"homepage": "https://iamsaquib8.github.io/domq/",
|
|
23
|
+
"types": "domq_wasm.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./domq_wasm.d.ts",
|
|
27
|
+
"node": "./node.js",
|
|
28
|
+
"default": "./domq_wasm.js"
|
|
29
|
+
},
|
|
30
|
+
"./domq_wasm.js": {
|
|
31
|
+
"types": "./domq_wasm.d.ts",
|
|
32
|
+
"default": "./domq_wasm.js"
|
|
33
|
+
},
|
|
34
|
+
"./domq_wasm_bg.wasm": "./domq_wasm_bg.wasm",
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"sideEffects": [
|
|
38
|
+
"./snippets/*",
|
|
39
|
+
"./node.js"
|
|
40
|
+
],
|
|
41
|
+
"keywords": [
|
|
42
|
+
"html",
|
|
43
|
+
"parser",
|
|
44
|
+
"css-selector",
|
|
45
|
+
"markdown",
|
|
46
|
+
"wasm",
|
|
47
|
+
"scraper",
|
|
48
|
+
"html-to-markdown",
|
|
49
|
+
"html-to-text",
|
|
50
|
+
"dom",
|
|
51
|
+
"cloudflare-workers"
|
|
52
|
+
]
|
|
53
|
+
}
|