Skip to content

Commit

Permalink
adjust doc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
niklak committed Nov 1, 2024
1 parent 1849da7 commit 02b715e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
24 changes: 17 additions & 7 deletions Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,22 @@ use dom_query::Document;
let html = include_str!("../test-pages/rustwiki_2024.html");
let doc = Document::from(html);

// searching list items inside a `tr` element which has a `a` element with title="Programming paradigm"
let paradigm_selection = doc.select(r#"table tr:has(a[title="Programming paradigm"]) td.infobox-data ul > li"#);
// searching list items inside a `tr` element which has a `a` element
// with title="Programming paradigm"
let paradigm_selection =
doc.select(
r#"table tr:has(a[title="Programming paradigm"]) td.infobox-data ul > li"#
);

println!("Rust programming paradigms:");
for item in paradigm_selection.iter() {
println!(" {}", item.text());
}
println!("{:-<50}", "");

//since `th` contains text "Influenced by" without sibling tags, we can use `:has-text` pseudo class
let influenced_by_selection = doc.select(r#"table tr:has-text("Influenced by") + tr td ul > li > a"#);
//since `th` contains text "Paradigms" without sibling tags, we can use `:has-text` pseudo class
let influenced_by_selection =
doc.select(r#"table tr:has-text("Influenced by") + tr td ul > li > a"#);

println!("Rust influenced by:");
for item in influenced_by_selection.iter() {
Expand All @@ -310,17 +315,22 @@ println!("{:-<50}", "");
// Extract all links from the block that contains certain text.
// Since `foreign function interface` located in its own tag,
// we have to use `:contains` pseudo class
let links_selection = doc.select(r#"p:contains("Rust has a foreign function interface") a[href^="/"]"#);
let links_selection =
doc.select(
r#"p:contains("Rust has a foreign function interface") a[href^="/"]"#
);

println!("Links in the FFI block:");
for item in links_selection.iter() {
println!(" {}", item.attr("href").unwrap());
}
println!("{:-<50}", "");

// :only-text selects an element that contains only a single text node, with no child elements.
// :only-text selects an element that contains only a single text node,
// with no child elements.
// It can be combined with other pseudo-classes to achieve more specific selections.
// For example, to select a <div> inside an <a> that has no siblings and no child elements other than text.
// For example, to select a <div> inside an <a>
//that has no siblings and no child elements other than text.
println!("Single <div> inside an <a> with text only:");
for el in doc.select("a div:only-text:only-child").iter() {
println!("{}", el.text().trim());
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,12 @@ use dom_query::Document;
let html = include_str!("../test-pages/rustwiki_2024.html");
let doc = Document::from(html);

// searching list items inside a `tr` element which has a `a` element with title="Programming paradigm"
// searching list items inside a `tr` element which has a `a` element
// with title="Programming paradigm"
let paradigm_selection =
doc.select(r#"table tr:has(a[title="Programming paradigm"]) td.infobox-data ul > li"#);
doc.select(
r#"table tr:has(a[title="Programming paradigm"]) td.infobox-data ul > li"#
);

println!("Rust programming paradigms:");
for item in paradigm_selection.iter() {
Expand All @@ -346,17 +349,21 @@ println!("{:-<50}", "");
// Since `foreign function interface` located in its own tag,
// we have to use `:contains` pseudo class
let links_selection =
doc.select(r#"p:contains("Rust has a foreign function interface") a[href^="/"]"#);
doc.select(
r#"p:contains("Rust has a foreign function interface") a[href^="/"]"#
);

println!("Links in the FFI block:");
for item in links_selection.iter() {
println!(" {}", item.attr("href").unwrap());
}
println!("{:-<50}", "");

// :only-text selects an element that contains only a single text node, with no child elements.
// :only-text selects an element that contains only a single text node,
// with no child elements.
// It can be combined with other pseudo-classes to achieve more specific selections.
// For example, to select a <div> inside an <a> that has no siblings and no child elements other than text.
// For example, to select a <div> inside an <a>
//that has no siblings and no child elements other than text.
println!("Single <div> inside an <a> with text only:");
for el in doc.select("a div:only-text:only-child").iter() {
println!("{}", el.text().trim());
Expand Down
17 changes: 12 additions & 5 deletions examples/pseudo_classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ fn main() {
let html = include_str!("../test-pages/rustwiki_2024.html");
let doc = Document::from(html);

// searching list items inside a `tr` element which has a `a` element with title="Programming paradigm"
// searching list items inside a `tr` element which has a `a` element
// with title="Programming paradigm"
let paradigm_selection =
doc.select(r#"table tr:has(a[title="Programming paradigm"]) td.infobox-data ul > li"#);
doc.select(
r#"table tr:has(a[title="Programming paradigm"]) td.infobox-data ul > li"#
);

println!("Rust programming paradigms:");
for item in paradigm_selection.iter() {
Expand All @@ -28,17 +31,21 @@ fn main() {
// Since `foreign function interface` located in its own tag,
// we have to use `:contains` pseudo class
let links_selection =
doc.select(r#"p:contains("Rust has a foreign function interface") a[href^="/"]"#);
doc.select(
r#"p:contains("Rust has a foreign function interface") a[href^="/"]"#
);

println!("Links in the FFI block:");
for item in links_selection.iter() {
println!(" {}", item.attr("href").unwrap());
}
println!("{:-<50}", "");

// :only-text selects an element that contains only a single text node, with no child elements.
// :only-text selects an element that contains only a single text node,
// with no child elements.
// It can be combined with other pseudo-classes to achieve more specific selections.
// For example, to select a <div> inside an <a> that has no siblings and no child elements other than text.
// For example, to select a <div> inside an <a>
//that has no siblings and no child elements other than text.
println!("Single <div> inside an <a> with text only:");
for el in doc.select("a div:only-text:only-child").iter() {
println!("{}", el.text().trim());
Expand Down

0 comments on commit 02b715e

Please sign in to comment.