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
//! Core HTML cleaning functionality.

use crate::options::CleaningOptions;
use dom_query::Document;
use std::collections::HashSet;

/// HTML cleaning utility.
///
/// Provides methods for removing, stripping, and normalizing HTML elements.
///
/// # Example
///
/// ```
/// use html_cleaning::{HtmlCleaner, CleaningOptions};
/// use dom_query::Document;
///
/// let options = CleaningOptions {
///     tags_to_remove: vec!["script".into(), "style".into()],
///     prune_empty: true,
///     ..Default::default()
/// };
///
/// let cleaner = HtmlCleaner::with_options(options);
/// let doc = Document::from("<div><script>x</script><p>Hello</p></div>");
/// cleaner.clean(&doc);
/// assert!(doc.select("script").is_empty());
/// ```
#[derive(Debug, Clone)]
pub struct HtmlCleaner {
    options: CleaningOptions,
}

impl Default for HtmlCleaner {
    fn default() -> Self {
        Self::new()
    }
}

impl HtmlCleaner {
    /// Create a cleaner with default options.
    #[must_use]
    pub fn new() -> Self {
        Self {
            options: CleaningOptions::default(),
        }
    }

    /// Create a cleaner with custom options.
    #[must_use]
    pub fn with_options(options: CleaningOptions) -> Self {
        Self { options }
    }

    /// Get a reference to the current options.
    #[must_use]
    pub fn options(&self) -> &CleaningOptions {
        &self.options
    }

    /// Apply all configured cleaning operations to the document.
    ///
    /// Operations are applied in this order:
    /// 1. Remove tags (with children)
    /// 2. Strip tags (keep children)
    /// 3. Remove by CSS selector
    /// 4. Remove HTML comments
    /// 5. Prune empty elements
    /// 6. Normalize whitespace
    /// 7. Clean attributes
    pub fn clean(&self, doc: &Document) {
        // 1. Remove tags
        if !self.options.tags_to_remove.is_empty() {
            let tags: Vec<&str> = self.options.tags_to_remove.iter().map(String::as_str).collect();
            self.remove_tags(doc, &tags);
        }

        // 2. Strip tags
        if !self.options.tags_to_strip.is_empty() {
            let tags: Vec<&str> = self.options.tags_to_strip.iter().map(String::as_str).collect();
            self.strip_tags(doc, &tags);
        }

        // 3. Remove by selector
        for selector in &self.options.selectors_to_remove {
            self.remove_by_selector(doc, selector);
        }

        // 4. Remove HTML comments
        if self.options.remove_comments {
            self.remove_comments(doc);
        }

        // 5. Prune empty
        if self.options.prune_empty {
            self.prune_empty(doc);
        }

        // 6. Normalize whitespace
        if self.options.normalize_whitespace {
            self.normalize_text(doc);
        }

        // 7. Clean attributes
        if self.options.strip_attributes {
            self.clean_attributes(doc);
        }
    }

    /// Remove HTML comment nodes from the document.
    ///
    /// Walks the entire DOM tree and removes all comment nodes (`<!-- ... -->`).
    ///
    /// # Example
    ///
    /// ```
    /// use html_cleaning::HtmlCleaner;
    /// use dom_query::Document;
    ///
    /// let cleaner = HtmlCleaner::new();
    /// let doc = Document::from("<div><!-- comment --><p>Content</p></div>");
    /// cleaner.remove_comments(&doc);
    /// ```
    pub fn remove_comments(&self, doc: &Document) {
        // Walk all nodes and collect comment nodes
        let body = doc.select("*");
        for node in body.nodes() {
            for child in node.children() {
                if child.is_comment() {
                    child.remove_from_parent();
                }
            }
        }
    }

    /// Remove elements matching tags (including all children).
    ///
    /// # Example
    ///
    /// ```
    /// use html_cleaning::HtmlCleaner;
    /// use dom_query::Document;
    ///
    /// let cleaner = HtmlCleaner::new();
    /// let doc = Document::from("<div><script>bad</script><p>good</p></div>");
    /// cleaner.remove_tags(&doc, &["script"]);
    /// assert!(doc.select("script").is_empty());
    /// ```
    pub fn remove_tags(&self, doc: &Document, tags: &[&str]) {
        if tags.is_empty() {
            return;
        }
        let selector = tags.join(", ");
        doc.select(&selector).remove();
    }

    /// Strip tags but preserve their children.
    ///
    /// The tag wrapper is removed but inner content (text and child elements)
    /// is moved to the parent.
    ///
    /// # Example
    ///
    /// ```
    /// use html_cleaning::HtmlCleaner;
    /// use dom_query::Document;
    ///
    /// let cleaner = HtmlCleaner::new();
    /// let doc = Document::from("<div><span>text</span></div>");
    /// cleaner.strip_tags(&doc, &["span"]);
    /// assert!(doc.select("span").is_empty());
    /// ```
    pub fn strip_tags(&self, doc: &Document, tags: &[&str]) {
        if tags.is_empty() {
            return;
        }
        let root = doc.select("*").first();
        if root.exists() {
            root.strip_elements(tags);
        }
    }

    /// Remove elements matching a CSS selector.
    ///
    /// # Example
    ///
    /// ```
    /// use html_cleaning::HtmlCleaner;
    /// use dom_query::Document;
    ///
    /// let cleaner = HtmlCleaner::new();
    /// let doc = Document::from(r#"<div class="ad">Ad</div><p>Content</p>"#);
    /// cleaner.remove_by_selector(&doc, ".ad");
    /// assert!(doc.select(".ad").is_empty());
    /// ```
    pub fn remove_by_selector(&self, doc: &Document, selector: &str) {
        doc.select(selector).remove();
    }

    /// Remove empty elements.
    ///
    /// Elements are considered empty if they:
    /// - Have no child elements
    /// - Have no text content (or only whitespace)
    ///
    /// Processes in reverse document order (children before parents).
    pub fn prune_empty(&self, doc: &Document) {
        let empty_tags: Vec<&str> = if self.options.empty_tags.is_empty() {
            vec!["div", "span", "p", "section", "article"]
        } else {
            self.options.empty_tags.iter().map(String::as_str).collect()
        };

        // Loop until no more empty elements found
        loop {
            let mut removed = false;
            for tag in &empty_tags {
                let nodes: Vec<_> = doc.select(tag).nodes().to_vec();
                for node in nodes.into_iter().rev() {
                    let sel = dom_query::Selection::from(node);
                    let children = sel.children();
                    let text = sel.text().to_string();

                    if children.is_empty() && text.trim().is_empty() {
                        sel.remove();
                        removed = true;
                    }
                }
            }
            if !removed {
                break;
            }
        }
    }

    /// Normalize text nodes (trim, collapse whitespace).
    ///
    /// Walks all text nodes and collapses multiple whitespace to single space.
    pub fn normalize_text(&self, doc: &Document) {
        // Process all elements and normalize their text content
        for node in doc.select("*").nodes() {
            let sel = dom_query::Selection::from(*node);

            // Get direct text children and normalize them
            if let Some(n) = sel.nodes().first() {
                for child in n.children() {
                    if child.is_text() {
                        let text = child.text();
                        let text_str = text.to_string();
                        let normalized = crate::text::normalize(&text_str);
                        if text_str != normalized {
                            // Replace text node content by updating via the node
                            child.set_text(normalized);
                        }
                    }
                }
            }
        }
    }

    /// Remove or filter attributes from all elements.
    ///
    /// If `strip_attributes` is true in options:
    /// - Removes all attributes except those in `preserved_attributes`
    pub fn clean_attributes(&self, doc: &Document) {
        let preserved: HashSet<&str> = self
            .options
            .preserved_attributes
            .iter()
            .map(String::as_str)
            .collect();

        for node in doc.select("*").nodes() {
            let sel = dom_query::Selection::from(*node);

            // Get all attribute names first
            let attrs: Vec<String> = sel
                .nodes()
                .first()
                .map(|n| {
                    n.attrs()
                        .iter()
                        .map(|a| a.name.local.to_string())
                        .collect()
                })
                .unwrap_or_default();

            // Remove non-preserved attributes
            for attr in attrs {
                if !preserved.contains(attr.as_str()) {
                    sel.remove_attr(&attr);
                }
            }
        }
    }
}

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

    #[test]
    fn test_new_cleaner() {
        let cleaner = HtmlCleaner::new();
        assert!(cleaner.options().tags_to_remove.is_empty());
    }

    #[test]
    fn test_remove_tags() {
        let cleaner = HtmlCleaner::new();
        let doc = Document::from("<div><script>bad</script><p>good</p></div>");
        cleaner.remove_tags(&doc, &["script"]);
        assert!(doc.select("script").is_empty());
        assert!(doc.select("p").exists());
    }

    #[test]
    fn test_remove_by_selector() {
        let cleaner = HtmlCleaner::new();
        let doc = Document::from(r#"<div class="ad">Ad</div><p>Content</p>"#);
        cleaner.remove_by_selector(&doc, ".ad");
        assert!(doc.select(".ad").is_empty());
        assert!(doc.select("p").exists());
    }

    #[test]
    fn test_prune_empty() {
        let options = CleaningOptions {
            prune_empty: true,
            ..Default::default()
        };
        let cleaner = HtmlCleaner::with_options(options);
        let doc = Document::from("<div><p></p><p>Content</p></div>");
        cleaner.prune_empty(&doc);
        assert_eq!(doc.select("p").length(), 1);
    }

    #[test]
    fn test_clean_attributes() {
        let options = CleaningOptions {
            strip_attributes: true,
            preserved_attributes: vec!["href".into()],
            ..Default::default()
        };
        let cleaner = HtmlCleaner::with_options(options);
        let doc = Document::from(r#"<a href="url" class="link" id="x">Link</a>"#);
        cleaner.clean_attributes(&doc);

        let a = doc.select("a");
        assert!(a.attr("href").is_some());
        assert!(a.attr("class").is_none());
        assert!(a.attr("id").is_none());
    }

    #[test]
    fn test_strip_tags_preserves_text() {
        let cleaner = HtmlCleaner::new();
        let doc = Document::from("<div><span>Hello</span> <b>World</b></div>");
        cleaner.strip_tags(&doc, &["span", "b"]);

        assert!(doc.select("span").is_empty());
        assert!(doc.select("b").is_empty());
        let text = doc.select("div").text();
        assert!(text.contains("Hello"), "Text 'Hello' should be preserved");
        assert!(text.contains("World"), "Text 'World' should be preserved");
    }

    #[test]
    fn test_remove_comments() {
        let cleaner = HtmlCleaner::new();
        let doc = Document::from("<div><!-- This is a comment --><p>Content</p><!-- Another --></div>");
        cleaner.remove_comments(&doc);
        let html = doc.select("div").html().to_string();
        assert!(!html.contains("comment"), "Comments should be removed: {html}");
        assert!(html.contains("Content"), "Content should be preserved");
    }

    #[test]
    fn test_clean_with_comments_option() {
        let options = CleaningOptions {
            remove_comments: true,
            ..Default::default()
        };
        let cleaner = HtmlCleaner::with_options(options);
        let doc = Document::from("<div><!-- comment --><p>Text</p></div>");
        cleaner.clean(&doc);
        let html = doc.select("div").html().to_string();
        assert!(!html.contains("comment"));
    }

    #[test]
    fn test_normalize_text() {
        let options = CleaningOptions {
            normalize_whitespace: true,
            ..Default::default()
        };
        let cleaner = HtmlCleaner::with_options(options);
        let doc = Document::from("<p>  Multiple   spaces   here  </p>");
        cleaner.normalize_text(&doc);

        let text = doc.select("p").text();
        // Text should have collapsed whitespace
        assert!(!text.contains("  "), "Multiple spaces should be collapsed");
    }
}