Skip to content

Commit 7500e7f

Browse files
xmakroteymour-aldridge
authored andcommitted
Add example on how to manipulate the DOM
1 parent 71a588c commit 7500e7f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ let text = h1.text().collect::<Vec<_>>();
129129
assert_eq!(vec!["Hello, ", "world!"], text);
130130
```
131131

132+
### Manipulating the DOM
133+
134+
```rust
135+
use html5ever::tree_builder::TreeSink;
136+
use scraper::{Html, Selector};
137+
138+
let html = "<html><body>hello<p class=\"hello\">REMOVE ME</p></body></html>";
139+
let selector = Selector::parse(".hello").unwrap();
140+
let mut document = Html::parse_document(html);
141+
let node_ids: Vec<_> = document.select(&selector).map(|x| x.id()).collect();
142+
for id in node_ids {
143+
document.remove_from_parent(&id);
144+
}
145+
assert_eq!(document.html(), "<html><head></head><body>hello</body></html>");
146+
```
147+
132148
## Contributing
133149

134150
Please feel free to open pull requests. If you're planning on implementing

0 commit comments

Comments
 (0)