Skip to main content

HtmlSaxParser

Struct HtmlSaxParser 

Source
pub struct HtmlSaxParser<H>
where H: HtmlSaxHandler + 'static,
{ /* private fields */ }
Expand description

Push-based HTML parser. Caller drives input chunks via feed; the parser dispatches events synchronously into the registered handler as they’re produced.

libxml2 htmlCreatePushParserCtxt + htmlParseChunk analogue, or lxml’s HTMLParser(target=…) + parser.feed(bytes).

§Example

use sup_xml_core::html::{HtmlAttrs, HtmlSaxHandler, HtmlSaxParser};

#[derive(Default)]
struct LinkCollector {
    hrefs: Vec<String>,
}

impl HtmlSaxHandler for LinkCollector {
    fn start_element(&mut self, name: &str, attrs: HtmlAttrs<'_>) {
        if name == "a" {
            if let Some(href) = attrs.get("href") {
                self.hrefs.push(href.to_string());
            }
        }
    }
}

let html = r#"<a href="/x">x</a><a href="/y">y</a>"#;
let mut p = HtmlSaxParser::new(LinkCollector::default());
p.feed(html).unwrap();
let collector = p.finish().unwrap();
assert_eq!(collector.hrefs, vec!["/x", "/y"]);

Implementations§

Source§

impl<H> HtmlSaxParser<H>
where H: HtmlSaxHandler + 'static,

Source

pub fn new(handler: H) -> HtmlSaxParser<H>

Create a push parser with default options.

Source

pub fn with_opts(handler: H, opts: HtmlParseOptions) -> HtmlSaxParser<H>

Create a push parser with explicit options.

Source

pub fn feed(&mut self, chunk: &str) -> Result<(), XmlError>

Feed a chunk of UTF-8 text. Drives the tokenizer and dispatches any emitted events synchronously into the handler before returning.

Errors only when an internal limit (e.g. max_text_bytes) is exceeded.

Source

pub fn finish(self) -> Result<H, XmlError>

Signal end-of-input. Flushes any pending text, dispatches EndElement events for any still-open elements, then calls end_document. Returns the handler.

Auto Trait Implementations§

§

impl<H> !Freeze for HtmlSaxParser<H>

§

impl<H> !RefUnwindSafe for HtmlSaxParser<H>

§

impl<H> !Send for HtmlSaxParser<H>

§

impl<H> !Sync for HtmlSaxParser<H>

§

impl<H> Unpin for HtmlSaxParser<H>
where H: Unpin,

§

impl<H> UnsafeUnpin for HtmlSaxParser<H>
where H: UnsafeUnpin,

§

impl<H> UnwindSafe for HtmlSaxParser<H>
where H: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.