Skip to content

Commit

Permalink
Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
qwtel committed Apr 13, 2022
1 parent 7c496b3 commit 9cdb9c5
Show file tree
Hide file tree
Showing 20 changed files with 494 additions and 5,961 deletions.
File renamed without changes.
3 changes: 0 additions & 3 deletions .prettierrc

This file was deleted.

23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
wasm-pack-build:
@echo "---> Building custom wasm-pack with asyncify enabled..."
cd ./wasm-pack; cargo build --release

wasm-pack: wasm-pack-build
@echo "---> Building WebAssembly with wasm-pack..."
./wasm-pack/target/release/wasm-pack build --target web

# Wraps write/end with asyncify magic and adds this returns for chaining
# diff -uN pkg/html_rewriter.js pkg2/html_rewriter.js > html_rewriter.js.patch
patch: wasm-pack
@echo "---> Patching JavaScript glue code..."
patch -uN pkg/html_rewriter.js < html_rewriter.js.patch

dist: patch
@echo "---> Copying required files to root..."
cp pkg/html_rewriter.js .
cp pkg/html_rewriter_bg.wasm .
cp src/asyncify.js .
cp src/html_rewriter.d.ts .

test:
deno test ./test
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
106 changes: 106 additions & 0 deletions __test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { TextEncoder, TextDecoder } from "util";
import { Macro } from "ava";
import {
Comment,
DocumentHandlers,
Element,
ElementHandlers,
HTMLRewriter as RawHTMLRewriter,
HTMLRewriterOptions as RawHTMLRewriterOptions,
TextChunk,
} from "..";

const encoder = new TextEncoder();
const decoder = new TextDecoder();

export class HTMLRewriter {
private elementHandlers: [selector: string, handlers: ElementHandlers][] = [];
private documentHandlers: DocumentHandlers[] = [];

constructor(private readonly options?: RawHTMLRewriterOptions) {}

on(selector: string, handlers: ElementHandlers): this {
this.elementHandlers.push([selector, handlers]);
return this;
}

onDocument(handlers: DocumentHandlers): this {
this.documentHandlers.push(handlers);
return this;
}

async transform(input: string): Promise<string> {
let output = "";
const rewriter = new RawHTMLRewriter((chunk) => {
output += decoder.decode(chunk);
}, this.options);
for (const [selector, handlers] of this.elementHandlers) {
rewriter.on(selector, handlers);
}
for (const handlers of this.documentHandlers) {
rewriter.onDocument(handlers);
}
try {
await rewriter.write(encoder.encode(input));
await rewriter.end();
return output;
} finally {
rewriter.free();
}
}
}

export function wait(t: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, t));
}

export const mutationsMacro: Macro<
[
(
rw: HTMLRewriter,
handler: (token: Element | TextChunk | Comment) => void
) => HTMLRewriter,
string,
{
beforeAfter: string;
replace: string;
replaceHtml: string;
remove: string;
}
]
> = async (t, func, input, expected) => {
// In all these tests, only process text chunks containing text. All test
// inputs for text handlers will be single characters, so we'll only process
// text nodes once.

// before/after
let res = await func(new HTMLRewriter(), (token) => {
if ("text" in token && !token.text) return;
token.before("<span>before</span>");
token.before("<span>before html</span>", { html: true });
token.after("<span>after</span>");
token.after("<span>after html</span>", { html: true });
}).transform(input);
t.is(res, expected.beforeAfter);

// replace
res = await func(new HTMLRewriter(), (token) => {
if ("text" in token && !token.text) return;
token.replace("<span>replace</span>");
}).transform(input);
t.is(res, expected.replace);
res = await func(new HTMLRewriter(), (token) => {
if ("text" in token && !token.text) return;
token.replace("<span>replace</span>", { html: true });
}).transform(input);
t.is(res, expected.replaceHtml);

// remove
res = await func(new HTMLRewriter(), (token) => {
if ("text" in token && !token.text) return;
t.false(token.removed);
token.remove();
t.true(token.removed);
}).transform(input);
t.is(res, expected.remove);
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 0 additions & 5 deletions ava.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if [[ ! $WASM_PACK_VERSION =~ -asyncify$ ]]; then
fi

echo "---> Building WebAssembly with wasm-pack..."
wasm-pack build --target nodejs
wasm-pack build --target web

echo "---> Patching JavaScript glue code..."
# Wraps write/end with asyncify magic and adds this returns for chaining
Expand Down
94 changes: 46 additions & 48 deletions html_rewriter.js.patch
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
--- pkg/html_rewriter.js 2022-01-18 17:37:39.000000000 +0000
+++ pkg2/html_rewriter.js 2022-01-18 17:37:19.000000000 +0000
@@ -1,7 +1,7 @@
let imports = {};
imports['__wbindgen_placeholder__'] = module.exports;
--- html_rewriter.js 2022-04-12 11:25:20.000000000 +0700
+++ html_rewriter__x.js 2022-04-12 11:41:16.000000000 +0700
@@ -1,4 +1,4 @@
-import { awaitPromise } from './asyncify.js';
+import { awaitPromise, setWasmExports, wrap } from './asyncify.js';

let wasm;
-const { awaitPromise } = require(String.raw`./asyncify.js`);
+const { awaitPromise, setWasmExports, wrap } = require(String.raw`./asyncify.js`);
const { TextDecoder, TextEncoder } = require(String.raw`util`);

const heap = new Array(32).fill(undefined);
@@ -233,6 +233,7 @@
@@ -231,6 +231,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.comment_before(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -242,6 +243,7 @@
@@ -240,6 +241,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.comment_after(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -251,11 +253,13 @@
@@ -249,11 +251,13 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.comment_replace(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
Expand All @@ -39,31 +36,31 @@
}
/**
* @returns {boolean}
@@ -364,6 +368,7 @@
@@ -360,6 +364,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.documentend_append(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
}
module.exports.DocumentEnd = DocumentEnd;
@@ -397,6 +402,7 @@
/**
@@ -392,6 +397,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_before(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -406,6 +412,7 @@
@@ -401,6 +407,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_after(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -415,11 +422,13 @@
@@ -410,11 +417,13 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_replace(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
Expand All @@ -77,7 +74,7 @@
}
/**
* @returns {boolean}
@@ -463,7 +472,7 @@
@@ -458,7 +467,7 @@
*/
get attributes() {
var ret = wasm.element_attributes(this.ptr);
Expand All @@ -86,39 +83,39 @@
}
/**
* @param {string} name
@@ -495,6 +504,7 @@
@@ -490,6 +499,7 @@
var ptr1 = passStringToWasm0(value, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len1 = WASM_VECTOR_LEN;
wasm.element_setAttribute(this.ptr, ptr0, len0, ptr1, len1);
+ return this;
}
/**
* @param {string} name
@@ -503,6 +513,7 @@
@@ -498,6 +508,7 @@
var ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_removeAttribute(this.ptr, ptr0, len0);
+ return this;
}
/**
* @param {string} content
@@ -512,6 +523,7 @@
@@ -507,6 +518,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_prepend(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -521,6 +533,7 @@
@@ -516,6 +528,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_append(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -530,17 +543,19 @@
@@ -525,17 +538,19 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.element_setInnerContent(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
Expand All @@ -138,16 +135,16 @@
+ wasm.element_onEndTag(this.ptr, addHeapObject(handler.bind(this)));
}
}
module.exports.Element = Element;
@@ -597,6 +612,7 @@
/**
@@ -591,6 +606,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.endtag_before(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -606,11 +622,13 @@
@@ -600,11 +616,13 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.endtag_after(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
Expand All @@ -160,8 +157,8 @@
+ return this;
}
}
module.exports.EndTag = EndTag;
@@ -656,25 +674,27 @@
/**
@@ -649,25 +667,27 @@
var ptr0 = passStringToWasm0(selector, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.htmlrewriter_on(this.ptr, ptr0, len0, addHeapObject(handlers));
Expand All @@ -182,34 +179,34 @@
var ptr0 = passArray8ToWasm0(chunk, wasm.__wbindgen_malloc);
var len0 = WASM_VECTOR_LEN;
- wasm.htmlrewriter_write(this.ptr, ptr0, len0);
+ await wrap(this, wasm.htmlrewriter_write, this.ptr, ptr0, len0);
+ await wrap(this, wasm.htmlrewriter_write, this.ptr, ptr0, len0)
}
/**
*/
- end() {
- wasm.htmlrewriter_end(this.ptr);
+ async end() {
+ await wrap(this, wasm.htmlrewriter_end, this.ptr);
+ await wrap(this, wasm.htmlrewriter_end, this.ptr)
}
/**
* @returns {number}
@@ -715,6 +735,7 @@
@@ -707,6 +727,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.textchunk_before(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -724,6 +745,7 @@
@@ -716,6 +737,7 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.textchunk_after(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
+ return this;
}
/**
* @param {string} content
@@ -733,11 +755,13 @@
@@ -725,11 +747,13 @@
var ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.textchunk_replace(this.ptr, ptr0, len0, isLikeNone(content_type) ? 0 : addHeapObject(content_type));
Expand All @@ -223,20 +220,21 @@
}
/**
* @returns {boolean}
@@ -893,7 +917,8 @@
};

module.exports.__wbg_instanceof_Promise_c6535fc791fcc4d2 = function(arg0) {
- var ret = getObject(arg0) instanceof Promise;
+ var obj = getObject(arg0);
+ var ret = (obj instanceof Promise) || (Object.prototype.toString.call(obj) === '[object Promise]');
return ret;
};
@@ -897,7 +921,8 @@
return addHeapObject(ret);
};
imports.wbg.__wbg_instanceof_Promise_c6535fc791fcc4d2 = function(arg0) {
- var ret = getObject(arg0) instanceof Promise;
+ var obj = getObject(arg0);
+ var ret = (obj instanceof Promise) || (Object.prototype.toString.call(obj) === '[object Promise]');
return ret;
};
imports.wbg.__wbg_buffer_89a8560ab6a3d9c6 = function(arg0) {
@@ -939,6 +964,7 @@
const { instance, module } = await load(await input, imports);

@@ -939,5 +964,6 @@
const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
+setWasmExports(wasm);
module.exports.__wasm = wasm;
wasm = instance.exports;
+ setWasmExports(wasm)
init.__wbindgen_wasm_module = module;

return wasm;
Loading

0 comments on commit 9cdb9c5

Please sign in to comment.