use core::fmt;
use query::{parse_query, QueryComponent, QueryElement, QueryParseError};
use tag::Tag;
mod query;
mod tag;
pub struct Html {
pub nodes: Vec,
}
impl Html {
pub fn parse>(raw: S) -> Self {
let mut raw = raw.as_ref();
let mut nodes = vec![];
loop {
let Consumed { node, remaining } = Self::parse_node(raw);
nodes.push(node);
match remaining {
None => break Self { nodes },
Some(rem) => raw = rem,
}
}
}
pub fn query>(&self, query: S) -> Result
";
let hh = Html::parse_node(nested);
assert_eq!(hh.node, tag!("p", [tag!("p", [text!("Hello!")])]))
}
#[test]
fn parse_multiple_toplevel() {
let nested = "Hello
World!
";
let hh = Html::parse(nested);
assert_eq!(
hh.nodes,
vec![tag!("p", [text!("Hello ")]), tag!("p", [text!("World!")])]
)
}
#[test]
fn parse_script() {
let raw = "\n\t\n";
let hh = Html::parse(raw);
assert_eq!(
hh.nodes,
vec![tag!(
"head",
[
text!("\n\t"),
tag!("script", [text!("let k=\"v\";\n\t")]),
text!("\n")
]
)]
)
}
#[test]
fn parse_external_script() {
let raw = "\n\t\n";
let hh = Html::parse(raw);
assert_eq!(
hh.nodes,
vec![tag!(
"head",
[
text!("\n\t"),
tag!("script", "src=\"script.js\""),
text!("\n")
]
)]
)
}
fn test_roundtrip(raw: &str) {
let html = Html::parse(raw);
let string = html.to_string();
for (raw, html) in raw.lines().zip(string.lines()) {
assert_eq!(raw, html)
}
}
#[test]
fn round_trip_simple() {
test_roundtrip("Hello!
")
}
#[test]
fn round_trip_complex() {
test_roundtrip(
r#"
Title!
Hello, World!
"#,
)
}
}