Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement JavaScript Rewriter API #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

devsnek
Copy link

@devsnek devsnek commented Jul 10, 2021

this is mostly code from @inikulin

js-api/src/handlers.rs Outdated Show resolved Hide resolved
Comment on lines +5 to +36
```js
'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');
console.log(output);
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this tested somewhere? I'm worried it will regress otherwise - maybe you could put it in examples/ and use include_str! from a doctest?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think ideally we should get some tests going with node but idk how to set that up with travis

js-api/src/handlers.rs Outdated Show resolved Hide resolved
@devsnek devsnek force-pushed the feature/js-api branch 2 times, most recently from abac865 to d8f27c7 Compare July 31, 2021 04:18
@devsnek devsnek marked this pull request as ready for review July 31, 2021 04:26
@devsnek devsnek requested a review from a team as a code owner July 31, 2021 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants