-
Notifications
You must be signed in to change notification settings - Fork 85
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 end tag handler for elements #97
Conversation
What's the process for making this feature available via the JS API? Bug my account manager? 😂 |
@ZebraFlesh Yes, please ask your account manager to open a feature request and the Cloudflare worker's team will take it from there. |
@xtuc Now I'm mildly confused: I would have expected this PR to modify the functionality for the JS API in this repo: https://github.com/cloudflare/lol-html/blob/master/js-api/src/element.rs Otherwise, why does that code exist in this repo? (Seems like this PR creates a diverging set of functionality.) |
The JS api is meant to generate a Wasm module and be used directly from JS. However, Cloudflare workers uses the C API and uses v8 to expose to JS. |
Now I'm more confused: there's base functionality which is exposed via 2 different APIs. Doesn't this PR add base functionality which is not exposed to those APIs? Are the APIs not required to be kept in sync with the base Rust functionality? |
Hi @ZebraFlesh. I'm the author of this change. Note that I'm not affiliated with Cloudflare. I'm a user of the Looking into the code I was able to work out how to add this capability to the core library. This solved my specific problem, and it's great if other people can benefit from my change, so I submitted it as a PR. I don't use the API layers, and, although they appear to be quite slim wrappers around the core library, I'm not so familiar with the translation mechanisms they use. If my change had been rejected due to lacking this code, rather than add them, I would likely just have kept using my personal fork of This is why there is a split in supported capabilities between the layers. Hopefully someone who does understand the API layers better than me can bring them up to the same capability. |
@ZebraFlesh I've opened #121 to expose this in the C API; there's a little more work required in the Workers runtime to expose this, but I've started it already and I hope to have it available by the new year. |
@jongiddy I realized while writing some tests for this that it doesn't allow access to
The fix is to calculate the info ahead of time, but that's non-obvious and seems kinda unnecessary ... maybe it would be better to pass element!("div", |el| {
eprintln!("saw element {}", el.tag_name());
let is_first_div = el.has_attribute("first-div");
el.on_end_tag(move |end_tag| {
if is_first_div {
eprintln!("saw first div");
}
eprintln!("saw end tag {}", end_tag.name());
Ok(())
})?;
Ok(())
}) |
Hmm, actually I realized that would prevent streaming the element contents, since |
As you've realized, at the time of the I wouldn't call this a footgun. We pass an |
@jyn514 any luck with exposing this on Workers runtime? |
@methyl yes, it's implemented today. It looks like the documentation is missing on https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/#element, I'll try and get that fixed. |
@jyn514 in the meantime, what should be the method name? EDIT: this is the API (found it quite surprising and inconsistent with the Document Handler, where you specify class ElementHandler {
element(el) {
el.onEndTag(() => console.log("end tag"))
}
}
new HTMLRewriter().on("a", new ElementHandler()) |
Provide the ability to run a handler for the end tag. Rather then provide a selector for the end tag, this uses the existing selector on the start tag, and adds an
on_end_tag
method toElement
to provide a handler that gets triggered when the end tag is reached.This should be adequate to resolve the issues described in #85, #93, and #96. I'll add an example to #93 of that code using this PR.
This PR also updates the
TextChunk
docs to emphasize thatlast_in_text_node
can be true multiple times inside an element when there are sub-elements, and point to the newElement::on_end_tag
method as an alternative.