Skip to content

Commit

Permalink
Basic JS test
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Dec 7, 2024
1 parent 937043d commit b6305b9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions js-api/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test:
wasm-pack build --target nodejs --dev --out-dir node_modules/lol-html
node test.js
11 changes: 10 additions & 1 deletion js-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
```
32 changes: 32 additions & 0 deletions js-api/test.js
Original file line number Diff line number Diff line change
@@ -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);
},
});

[
'<div><a href=',
'http://example.com>',
'</a></div>',
].forEach((part) => {
rewriter.write(Buffer.from(part));
});

rewriter.end();

const output = Buffer.concat(chunks).toString('utf8');
if (output != '<div><a href="https://example.com"></a></div>') {
throw "fail";
}

0 comments on commit b6305b9

Please sign in to comment.