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
devsnek
wants to merge
1
commit into
cloudflare:master
Choose a base branch
from
devsnek:feature/js-api
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# LOL HTML JavaScript API | ||
|
||
## Example | ||
|
||
```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); | ||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use super::comment::Comment; | ||
use super::doctype::Doctype; | ||
use super::document_end::DocumentEnd; | ||
use super::element::Element; | ||
use super::text_chunk::TextChunk; | ||
use super::*; | ||
use js_sys::Function as JsFunction; | ||
use lol_html::{ | ||
DocumentContentHandlers as NativeDocumentContentHandlers, | ||
ElementContentHandlers as NativeElementContentHandlers, | ||
}; | ||
use std::mem; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
#[error("JS handler error")] | ||
pub struct HandlerJsErrorWrap(pub JsValue); | ||
|
||
// SAFETY: The exposed js-api only supports single-threaded usage. | ||
unsafe impl Send for HandlerJsErrorWrap {} | ||
unsafe impl Sync for HandlerJsErrorWrap {} | ||
|
||
macro_rules! make_handler { | ||
($handler:ident, $JsArgType:ident) => { | ||
move |arg: &mut _| { | ||
let (js_arg, anchor) = $JsArgType::from_native(arg); | ||
|
||
let res = match $handler.call1(&JsValue::NULL, &JsValue::from(js_arg)) { | ||
Ok(_) => Ok(()), | ||
Err(e) => Err(HandlerJsErrorWrap(e).into()), | ||
}; | ||
|
||
mem::drop(anchor); | ||
|
||
res | ||
} | ||
}; | ||
} | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
pub type ElementContentHandlers; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
fn element(this: &ElementContentHandlers) -> Option<JsFunction>; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
fn comments(this: &ElementContentHandlers) -> Option<JsFunction>; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
fn text(this: &ElementContentHandlers) -> Option<JsFunction>; | ||
} | ||
|
||
impl IntoNative<NativeElementContentHandlers<'static>> for ElementContentHandlers { | ||
fn into_native(self) -> NativeElementContentHandlers<'static> { | ||
let mut native = NativeElementContentHandlers::default(); | ||
|
||
if let Some(handler) = self.element() { | ||
native = native.element(make_handler!(handler, Element)); | ||
} | ||
|
||
if let Some(handler) = self.comments() { | ||
native = native.comments(make_handler!(handler, Comment)); | ||
} | ||
|
||
if let Some(handler) = self.text() { | ||
native = native.text(make_handler!(handler, TextChunk)); | ||
} | ||
|
||
native | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
pub type DocumentContentHandlers; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
fn doctype(this: &DocumentContentHandlers) -> Option<JsFunction>; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
fn comments(this: &DocumentContentHandlers) -> Option<JsFunction>; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
fn text(this: &DocumentContentHandlers) -> Option<JsFunction>; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
fn end(this: &DocumentContentHandlers) -> Option<JsFunction>; | ||
} | ||
|
||
impl IntoNative<NativeDocumentContentHandlers<'static>> for DocumentContentHandlers { | ||
fn into_native(self) -> NativeDocumentContentHandlers<'static> { | ||
let mut native = NativeDocumentContentHandlers::default(); | ||
|
||
if let Some(handler) = self.doctype() { | ||
native = native.doctype(make_handler!(handler, Doctype)); | ||
} | ||
|
||
if let Some(handler) = self.comments() { | ||
native = native.comments(make_handler!(handler, Comment)); | ||
} | ||
|
||
if let Some(handler) = self.text() { | ||
native = native.text(make_handler!(handler, TextChunk)); | ||
} | ||
|
||
if let Some(handler) = self.end() { | ||
native = native.end(make_handler!(handler, DocumentEnd)); | ||
} | ||
|
||
native | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,5 +171,6 @@ mod comment; | |
mod doctype; | ||
mod document_end; | ||
mod element; | ||
mod handlers; | ||
mod html_rewriter; | ||
mod text_chunk; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 useinclude_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