-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,128 additions
and
723 deletions.
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 |
---|---|---|
@@ -1,42 +1,24 @@ | ||
use std::cell::Cell; | ||
use crate::preprocess; | ||
|
||
use html5ever::{interface::TreeSink, namespace_url, ns, LocalName, QualName}; | ||
pub type Parser<'book> = html5ever::Parser<preprocess::tree::HtmlTreeSink<'book>>; | ||
|
||
pub type NodeId = <scraper::HtmlTreeSink as TreeSink>::Handle; | ||
pub type Parser = html5ever::Parser<scraper::HtmlTreeSink>; | ||
|
||
pub fn name(local: LocalName) -> QualName { | ||
QualName::new(None, ns!(), local) | ||
} | ||
|
||
/// Determines the HTML element to which child events should be appended based on the state of the parser. | ||
pub fn most_recently_created_open_element(parser: &Parser) -> NodeId { | ||
struct Tracer<'a> { | ||
html: &'a scraper::Html, | ||
prev: Cell<Option<NodeId>>, | ||
next: Cell<Option<NodeId>>, | ||
} | ||
|
||
impl html5ever::interface::Tracer for Tracer<'_> { | ||
type Handle = NodeId; | ||
|
||
fn trace_handle(&self, handle: &Self::Handle) { | ||
if let Some(node) = self.html.tree.get(*handle) { | ||
if node.value().is_element() { | ||
self.prev.swap(&self.next); | ||
self.next.set(Some(*handle)) | ||
} | ||
} | ||
#[macro_export] | ||
macro_rules! html_name { | ||
(html $name:tt) => {{ | ||
use html5ever::namespace_url; | ||
html5ever::QualName { | ||
prefix: None, | ||
ns: html5ever::ns!(html), | ||
local: html5ever::local_name!($name), | ||
} | ||
} | ||
|
||
let sink = &parser.tokenizer.sink; | ||
let html = sink.sink.0.borrow(); | ||
let tracer = Tracer { | ||
html: &html, | ||
prev: Default::default(), | ||
next: Default::default(), | ||
}; | ||
sink.trace_handles(&tracer); | ||
tracer.prev.into_inner().unwrap() | ||
}}; | ||
($name:tt) => {{ | ||
use html5ever::namespace_url; | ||
html5ever::QualName { | ||
prefix: None, | ||
ns: html5ever::ns!(), | ||
local: html5ever::local_name!($name), | ||
} | ||
}}; | ||
} | ||
pub use crate::html_name as name; |
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
Oops, something went wrong.