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

Add element.onEndTag js binding #263

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js-api/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "lol-html"
description = "Streaming HTML rewriter/parser with CSS selector-based API"
license = "BSD-3-Clause"
version = "2.1.1"
version = "2.2.0"
authors = ["Ivan Nikulin <[email protected]>", "Gus Caplan <[email protected]>"]
repository = "https://github.com/cloudflare/lol-html"
edition = "2021"
Expand Down
4 changes: 4 additions & 0 deletions js-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ rewriter.on('a[href]', {
.getAttribute('href')
.replace('http:', 'https:');
el.setAttribute('href', href);

el.onEndTag((tag)=> {
console.log(`Tag ended: ${tag.name}`);
});
},
});

Expand Down
34 changes: 34 additions & 0 deletions js-api/src/element.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
use super::end_tag::EndTag;
use super::*;
use js_sys::Function as JsFunction;
use lol_html::html_content::{Attribute as NativeAttribute, Element as NativeElement};
use serde::Serialize;
use serde_wasm_bindgen::to_value as to_js_value;
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, $typehint:ty) => {{
fn type_hint(h: $typehint) -> $typehint {
h
}
type_hint(Box::new(move |arg: &mut _| {
$JsArgType::with_native(arg, |js_value| $handler.call1(&JsValue::NULL, &js_value))
.map_err(|e| HandlerJsErrorWrap(e))?;

Ok(())
}))
}};
}

#[derive(Serialize)]
pub struct Attribute {
Expand Down Expand Up @@ -112,4 +137,13 @@ impl Element {
pub fn remove_and_keep_content(&mut self) -> Result<(), JsValue> {
self.0.get_mut().map(|e| e.remove_and_keep_content())
}

#[wasm_bindgen(method, js_name=onEndTag)]
pub fn on_end_tag(&mut self, handler: JsFunction) -> JsResult<()> {
if let Some(handlers) = self.0.get_mut()?.end_tag_handlers() {
handlers.push(make_handler!(handler, EndTag, lol_html::EndTagHandler));
}

Ok(())
}
}
16 changes: 16 additions & 0 deletions js-api/src/end_tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use super::*;
use lol_html::html_content::EndTag as NativeEndTag;

#[wasm_bindgen]
pub struct EndTag(NativeRefWrap<NativeEndTag<'static>>);

impl_from_native!(NativeEndTag => EndTag);
impl_mutations!(EndTag);

#[wasm_bindgen]
impl EndTag {
#[wasm_bindgen(getter)]
pub fn name(&self) -> JsResult<String> {
self.0.get().map(|d| d.name())
}
}
1 change: 1 addition & 0 deletions js-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ mod comment;
mod doctype;
mod document_end;
mod element;
mod end_tag;
mod handlers;
mod html_rewriter;
mod text_chunk;
9 changes: 9 additions & 0 deletions js-api/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ const rewriter = new HTMLRewriter('utf8', (chunk) => {
chunks.push(chunk);
});

const endTags = [];
rewriter.on('a[href]', {
element(el) {
const href = el
.getAttribute('href')
.replace('http:', 'https:');
el.setAttribute('href', href);

el.onEndTag((tag) => {
endTags.push(tag.name);
});
},
});

Expand All @@ -30,3 +35,7 @@ const output = Buffer.concat(chunks).toString('utf8');
if (output != '<div><a href="https://example.com"></a></div>') {
throw "fail";
}

if (endTags.length != 1 || endTags[0] != 'a') {
throw "onEndTag fail";
}
7 changes: 7 additions & 0 deletions src/rewritable_units/tokens/end_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ impl<'i> EndTag<'i> {
self.mutations.mutate().remove();
}

/// Returns `true` if the comment has been replaced or removed.
#[inline]
#[must_use]
pub fn removed(&self) -> bool {
self.mutations.removed()
}

#[inline]
fn serialize_self(&self, output_handler: &mut dyn FnMut(&[u8])) -> Result<(), RewritingError> {
if let Some(raw) = &self.raw {
Expand Down