-
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
Acting after on_end_tag #108
Comments
I see you've proposed #109 to add this capability. Curious as to why this didn't seem to work using the existing I don't object to your proposed change. I just wanted to understand why it was a problem. Have I understood it correctly, or have I missed an aspect of the problem you're describing? use lol_html::{element, HtmlRewriter, Settings};
use std::{cell::RefCell, error::Error, rc::Rc};
const PAGE: &str = "
<html>
This <a href=\"http://example.com\">link</a> is an example.
</html>
";
struct OutputHandler {
can_write: bool,
extra: String,
}
impl OutputHandler {
fn on(&mut self) {
self.can_write = true;
}
fn off(&mut self) {
self.can_write = false;
}
fn push(&mut self, extra: &str) {
self.extra.push_str(extra)
}
}
fn main() -> Result<(), Box<dyn Error>> {
let output = Rc::new(RefCell::new(OutputHandler {
can_write: false,
extra: String::new(),
}));
let element_content_handlers = vec![element!("a", |a| {
output.borrow_mut().on();
let output = output.clone();
a.on_end_tag(move |tag| {
let mut handler = output.borrow_mut();
handler.push(&format!("</{}>", tag.name()));
handler.off();
Ok(())
})?;
Ok(())
})];
let output = output.clone();
let mut rewriter = HtmlRewriter::new(
Settings {
element_content_handlers,
..Settings::default()
},
|chunk: &[u8]| {
let mut handler = output.borrow_mut();
if !handler.extra.is_empty() {
print!("{}", handler.extra);
handler.extra.clear();
}
if handler.can_write {
print!("{}", String::from_utf8_lossy(chunk))
}
},
);
rewriter.write(PAGE.as_ref())?;
rewriter.end()?;
Ok(())
} |
You're right in that it can be somewhat emulated but it's quite inconvenient. This solution now also always inserts a closing tag, even if that did not exist in the original document. For me the biggest issue was actually that I attempted to maintain a somewhat accurate tag stack to make more meaningful decisions and having the However right now all of this is entirely blocked on #110 anyways. A solution to that might change the situation somewhat. |
Would this be easier if, rather than having the |
Potentially. The current nice aspect of this |
I'm not sure if this is a feature request but I have tried using
on_end_tag
to do something after a tag has been closed. Unfortunately the handler is invoked before the tag is being written into the sink. This is intentional clearly as this lets the handler modify things like the tag name or append stuff behind the tag, but it also means that you cannot communicate into the sink easily.My idea was to instruct the sink to output or not output content outside of an element of interest (eg: to "select" a certain element exclusively). I am thus flipping a flag on enter/leave. The result now is that my closing tag is no longer emitted.
I believe there are use cases where one wants to have code run after the tag has been closed and emitted tot he sink and I'm not sure if this is at all possible at the moment.
The text was updated successfully, but these errors were encountered: