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
base: master
Are you sure you want to change the base?
Conversation
```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); | ||
``` |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
abac865
to
d8f27c7
Compare
this is mostly code from @inikulin