html-cleaning 0.3.0

HTML cleaning, sanitization, and text processing utilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! DOM helper utilities.
//!
//! Convenience functions for common DOM operations.

use dom_query::Selection;
pub use dom_query::Document;

/// Get text content of element (recursive).
///
/// # Example
///
/// ```
/// use html_cleaning::dom;
///
/// let doc = dom::parse("<div>Hello <span>World</span></div>");
/// assert_eq!(dom::text_content(&doc.select("div")), "Hello World");
/// ```
#[must_use]
pub fn text_content(sel: &Selection) -> String {
    sel.text().to_string()
}

/// Get direct text of element (non-recursive, excludes nested element text).
///
/// # Example
///
/// ```
/// use html_cleaning::dom;
///
/// let doc = dom::parse("<div>Direct <span>Nested</span> text</div>");
/// let direct = dom::direct_text(&doc.select("div"));
/// assert!(direct.contains("Direct"));
/// assert!(!direct.contains("Nested"));
/// ```
#[must_use]
pub fn direct_text(sel: &Selection) -> String {
    sel.nodes()
        .first()
        .map(|node| {
            node.children()
                .into_iter()
                .filter(dom_query::NodeRef::is_text)
                .map(|text_node| text_node.text().to_string())
                .collect::<String>()
        })
        .unwrap_or_default()
}

/// Get tag name (lowercase).
///
/// # Example
///
/// ```
/// use html_cleaning::dom;
///
/// let doc = dom::parse("<ARTICLE>Content</ARTICLE>");
/// assert_eq!(dom::tag_name(&doc.select("article")), Some("article".to_string()));
/// ```
#[must_use]
pub fn tag_name(sel: &Selection) -> Option<String> {
    sel.nodes()
        .first()
        .and_then(dom_query::NodeRef::node_name)
        .map(|t| t.to_string())
}

/// Get attribute value.
///
/// # Example
///
/// ```
/// use html_cleaning::dom;
///
/// let doc = dom::parse(r#"<a href="https://example.com">Link</a>"#);
/// assert_eq!(dom::get_attribute(&doc.select("a"), "href"), Some("https://example.com".to_string()));
/// ```
#[must_use]
pub fn get_attribute(sel: &Selection, name: &str) -> Option<String> {
    sel.attr(name).map(|s| s.to_string())
}

/// Set attribute value.
pub fn set_attribute(sel: &Selection, name: &str, value: &str) {
    sel.set_attr(name, value);
}

/// Remove attribute.
pub fn remove_attribute(sel: &Selection, name: &str) {
    sel.remove_attr(name);
}

/// Check if attribute exists.
#[must_use]
pub fn has_attribute(sel: &Selection, name: &str) -> bool {
    sel.has_attr(name)
}

/// Get all attributes as key-value pairs.
#[must_use]
pub fn get_all_attributes(sel: &Selection) -> Vec<(String, String)> {
    sel.nodes()
        .first()
        .map(|node| {
            node.attrs()
                .iter()
                .map(|attr| (attr.name.local.to_string(), attr.value.to_string()))
                .collect()
        })
        .unwrap_or_default()
}

/// Get direct element children.
#[must_use]
pub fn children<'a>(sel: &Selection<'a>) -> Selection<'a> {
    sel.children()
}

/// Get parent element.
#[must_use]
pub fn parent<'a>(sel: &Selection<'a>) -> Selection<'a> {
    sel.parent()
}

/// Get next element sibling (skipping text nodes).
#[must_use]
pub fn next_element_sibling<'a>(sel: &Selection<'a>) -> Option<Selection<'a>> {
    sel.nodes().first().and_then(|node| {
        let mut sibling = node.next_sibling();
        while let Some(s) = sibling {
            if s.is_element() {
                return Some(Selection::from(s));
            }
            sibling = s.next_sibling();
        }
        None
    })
}

/// Get previous element sibling (skipping text nodes).
#[must_use]
pub fn previous_element_sibling<'a>(sel: &Selection<'a>) -> Option<Selection<'a>> {
    sel.nodes().first().and_then(|node| {
        let mut sibling = node.prev_sibling();
        while let Some(s) = sibling {
            if s.is_element() {
                return Some(Selection::from(s));
            }
            sibling = s.prev_sibling();
        }
        None
    })
}

/// Check if element is a void element (self-closing).
#[must_use]
pub fn is_void_element(sel: &Selection) -> bool {
    const VOID_ELEMENTS: &[&str] = &[
        "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param",
        "source", "track", "wbr",
    ];

    tag_name(sel).is_some_and(|t| VOID_ELEMENTS.contains(&t.as_str()))
}

/// Check if element has specified class.
#[must_use]
pub fn has_class(sel: &Selection, class: &str) -> bool {
    sel.attr("class")
        .is_some_and(|c| c.split_whitespace().any(|c| c == class))
}

/// Add a class to the element.
///
/// # Example
///
/// ```
/// use html_cleaning::dom;
///
/// let doc = dom::parse(r#"<div class="foo">Content</div>"#);
/// let div = doc.select("div");
/// dom::add_class(&div, "bar");
/// assert!(dom::has_class(&div, "foo"));
/// assert!(dom::has_class(&div, "bar"));
/// ```
pub fn add_class(sel: &Selection, class: &str) {
    if class.is_empty() {
        return;
    }

    match sel.attr("class") {
        Some(existing) => {
            // Check if class already exists
            if !existing.split_whitespace().any(|c| c == class) {
                let new_class = format!("{existing} {class}");
                sel.set_attr("class", &new_class);
            }
        }
        None => {
            sel.set_attr("class", class);
        }
    }
}

/// Remove a class from the element.
pub fn remove_class(sel: &Selection, class: &str) {
    if let Some(existing) = sel.attr("class") {
        let new_class: Vec<&str> = existing
            .split_whitespace()
            .filter(|c| *c != class)
            .collect();

        if new_class.is_empty() {
            sel.remove_attr("class");
        } else {
            sel.set_attr("class", &new_class.join(" "));
        }
    }
}

/// Check if element matches a CSS selector.
///
/// # Example
///
/// ```
/// use html_cleaning::dom;
///
/// let doc = dom::parse(r#"<div class="content" id="main">Text</div>"#);
/// let div = doc.select("div");
/// assert!(dom::matches(&div, ".content"));
/// assert!(dom::matches(&div, "#main"));
/// assert!(!dom::matches(&div, ".sidebar"));
/// ```
#[must_use]
pub fn matches(sel: &Selection, selector: &str) -> bool {
    sel.is(selector)
}

/// Get inner HTML content.
#[must_use]
pub fn inner_html(sel: &Selection) -> String {
    sel.inner_html().to_string()
}

/// Get outer HTML content.
#[must_use]
pub fn outer_html(sel: &Selection) -> String {
    sel.html().to_string()
}

/// Parse HTML string into Document.
#[must_use]
pub fn parse(html: &str) -> Document {
    Document::from(html)
}

/// Clone a document.
#[must_use]
pub fn clone_document(doc: &Document) -> Document {
    Document::from(doc.html().to_string())
}

/// Rename element tag.
pub fn rename(sel: &Selection, new_tag: &str) {
    sel.rename(new_tag);
}

/// Remove element from DOM.
pub fn remove(sel: &Selection) {
    sel.remove();
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_text_content() {
        let doc = parse("<div>Hello <span>World</span></div>");
        let div = doc.select("div");
        assert_eq!(text_content(&div), "Hello World");
    }

    #[test]
    fn test_tag_name() {
        let doc = parse("<article>Content</article>");
        let article = doc.select("article");
        assert_eq!(tag_name(&article), Some("article".to_string()));
    }

    #[test]
    fn test_attributes() {
        let doc = parse(r#"<a href="url" class="link">Link</a>"#);
        let a = doc.select("a");

        assert_eq!(get_attribute(&a, "href"), Some("url".to_string()));
        assert!(has_attribute(&a, "class"));
        assert!(!has_attribute(&a, "id"));

        let attrs = get_all_attributes(&a);
        assert_eq!(attrs.len(), 2);
    }

    #[test]
    fn test_is_void_element() {
        let doc = parse("<div><br><img src='x'><p>text</p></div>");

        assert!(is_void_element(&doc.select("br")));
        assert!(is_void_element(&doc.select("img")));
        assert!(!is_void_element(&doc.select("p")));
        assert!(!is_void_element(&doc.select("div")));
    }

    #[test]
    fn test_has_class() {
        let doc = parse(r#"<div class="foo bar baz">Content</div>"#);
        let div = doc.select("div");

        assert!(has_class(&div, "foo"));
        assert!(has_class(&div, "bar"));
        assert!(!has_class(&div, "qux"));
    }

    #[test]
    fn test_navigation() {
        let doc = parse("<div><p>1</p><span>2</span><p>3</p></div>");

        let span = doc.select("span");
        let prev = previous_element_sibling(&span);
        let next = next_element_sibling(&span);

        assert!(prev.is_some());
        assert_eq!(tag_name(&prev.unwrap()), Some("p".to_string()));
        assert!(next.is_some());
        assert_eq!(tag_name(&next.unwrap()), Some("p".to_string()));
    }

    #[test]
    fn test_direct_text() {
        let doc = parse("<div>Direct text<span>Nested</span> more direct</div>");
        let div = doc.select("div");

        let direct = direct_text(&div);
        assert!(direct.contains("Direct text"));
        assert!(direct.contains("more direct"));
        assert!(!direct.contains("Nested"));
    }

    #[test]
    fn test_matches() {
        let doc = parse(r#"<div class="foo" id="bar">Content</div>"#);
        let div = doc.select("div");

        assert!(matches(&div, "div"));
        assert!(matches(&div, ".foo"));
        assert!(matches(&div, "#bar"));
        assert!(matches(&div, "div.foo"));
        assert!(!matches(&div, "span"));
        assert!(!matches(&div, ".baz"));
    }

    #[test]
    fn test_add_class() {
        let doc = parse(r#"<div class="existing">Content</div>"#);
        let div = doc.select("div");

        add_class(&div, "new");
        assert!(has_class(&div, "existing"));
        assert!(has_class(&div, "new"));

        // Adding same class again shouldn't duplicate
        add_class(&div, "new");
        let class_attr = get_attribute(&div, "class").unwrap();
        assert_eq!(class_attr.matches("new").count(), 1);
    }

    #[test]
    fn test_add_class_to_element_without_class() {
        let doc = parse("<div>Content</div>");
        let div = doc.select("div");

        add_class(&div, "new");
        assert!(has_class(&div, "new"));
    }

    #[test]
    fn test_remove_class() {
        let doc = parse(r#"<div class="foo bar baz">Content</div>"#);
        let div = doc.select("div");

        remove_class(&div, "bar");
        assert!(has_class(&div, "foo"));
        assert!(!has_class(&div, "bar"));
        assert!(has_class(&div, "baz"));
    }

    #[test]
    fn test_remove_last_class() {
        let doc = parse(r#"<div class="only">Content</div>"#);
        let div = doc.select("div");

        remove_class(&div, "only");
        assert!(!has_class(&div, "only"));
        assert!(!has_attribute(&div, "class"));
    }
}