From b6305b9803d721f74d35dc1a406fd448765bafa1 Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 5 Dec 2024 13:02:41 +0000 Subject: [PATCH] Basic JS test --- .github/workflows/ci.yml | 5 +++++ js-api/Makefile | 3 +++ js-api/README.md | 11 ++++++++++- js-api/test.js | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 js-api/Makefile create mode 100644 js-api/test.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 149f0d6b..b1a96784 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -72,3 +72,8 @@ jobs: path: target - name: Run Rust tests run: scripts/test.sh + - uses: actions/setup-node@v4 + - name: wasm-pack install + run: npm i -g wasm-pack + - name: Run JS tests + run: make -C js-api test diff --git a/js-api/Makefile b/js-api/Makefile new file mode 100644 index 00000000..37e3b290 --- /dev/null +++ b/js-api/Makefile @@ -0,0 +1,3 @@ +test: + wasm-pack build --target nodejs --dev --out-dir node_modules/lol-html + node test.js diff --git a/js-api/README.md b/js-api/README.md index 7e80412e..dbe97281 100644 --- a/js-api/README.md +++ b/js-api/README.md @@ -5,7 +5,7 @@ ```js 'use strict'; -const { HTMLRewriter } = require('lol-html'); +const { HTMLRewriter } = require('lol-html'); // path/to/lol-html.js const chunks = []; const rewriter = new HTMLRewriter('utf8', (chunk) => { @@ -34,3 +34,12 @@ rewriter.end(); const output = Buffer.concat(chunks).toString('utf8'); console.log(output); ``` + +## Building + +```bash +rustup update # https://rustup.rs +cargo install wasm-pack + +wasm-pack build --target nodejs --release +``` diff --git a/js-api/test.js b/js-api/test.js new file mode 100644 index 00000000..1be868d8 --- /dev/null +++ b/js-api/test.js @@ -0,0 +1,32 @@ +'use strict'; + +const { HTMLRewriter } = require('lol-html'); + +const chunks = []; +const rewriter = new HTMLRewriter('utf8', (chunk) => { + chunks.push(chunk); +}); + +rewriter.on('a[href]', { + element(el) { + const href = el + .getAttribute('href') + .replace('http:', 'https:'); + el.setAttribute('href', href); + }, +}); + +[ + '
', + '
', +].forEach((part) => { + rewriter.write(Buffer.from(part)); +}); + +rewriter.end(); + +const output = Buffer.concat(chunks).toString('utf8'); +if (output != '
') { + throw "fail"; +}