webpage_quality_analyzer 1.0.2

High-performance webpage quality analyzer with 115 comprehensive metrics - Rust library with WASM, C++, and Python bindings
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use crate::models::models::{AnalyzeError, Result};
use tl::VDom;

/// Trait for HTML parsing
pub trait Parser {
    /// Parse HTML into a DOM tree
    fn parse<'a>(&self, html: &'a str) -> Result<VDom<'a>>;
}

/// Default HTML parser implementation using the `tl` crate
#[derive(Debug, Default)]
pub struct HtmlParser {
    /// Parser options for tl
    parser_options: tl::ParserOptions,
}

impl HtmlParser {
    /// Create a new HTML parser with default options
    pub fn new() -> Self {
        Self {
            parser_options: tl::ParserOptions::default(),
        }
    }

    /// Create a new parser with custom options
    pub fn with_options(parser_options: tl::ParserOptions) -> Self {
        Self { parser_options }
    }

    /// Find all elements by tag name
    pub fn find_elements_by_tag<'a>(&self, dom: &'a VDom, tag_name: &str) -> Vec<&'a tl::Node<'a>> {
        utils::select_all(dom, tag_name)
    }

    /// Find all elements by class name
    pub fn find_elements_by_class<'a>(
        &self,
        dom: &'a VDom,
        class_name: &str,
    ) -> Vec<&'a tl::Node<'a>> {
        let selector = format!(".{}", class_name);
        utils::select_all(dom, &selector)
    }

    /// Find all elements with a specific attribute
    pub fn find_elements_with_attribute<'a>(
        &self,
        dom: &'a VDom,
        attr_name: &str,
    ) -> Vec<&'a tl::Node<'a>> {
        let selector = format!("[{}]", attr_name);
        utils::select_all(dom, &selector)
    }
}

impl Parser for HtmlParser {
    fn parse<'a>(&self, html: &'a str) -> Result<VDom<'a>> {
        // Use reference instead of cloning parser options
        tl::parse(html, self.parser_options)
            .map_err(|e| AnalyzeError::ParseError(format!("Failed to parse HTML: {:?}", e)))
    }
}

/// Parse HTML string into a DOM tree using the default parser
pub fn parse_html<'a>(html: &'a str) -> Result<VDom<'a>> {
    let parser = HtmlParser::new();
    parser.parse(html)
}

/// Utility functions for DOM traversal
pub mod utils {
    use tl::{Node, VDom};

    /// Get all nodes matching a selector
    pub fn select_all<'a>(dom: &'a VDom, selector: &str) -> Vec<&'a Node<'a>> {
        if let Some(nodes) = dom.query_selector(selector) {
            // Pre-size the vector based on the iterator's size hint
            let (lower, upper) = nodes.size_hint();
            let capacity = upper.unwrap_or(lower.max(16)); // Default to at least 16 or the lower bound
            let mut results = Vec::with_capacity(capacity);

            for node_handle in nodes {
                if let Some(node) = node_handle.get(dom.parser()) {
                    results.push(node);
                }
            }
            results
        } else {
            Vec::new()
        }
    }

    /// Get the first node matching a selector
    pub fn select_first<'a>(dom: &'a VDom, selector: &str) -> Option<&'a Node<'a>> {
        if let Some(mut nodes) = dom.query_selector(selector) {
            if let Some(node_handle) = nodes.next() {
                return node_handle.get(dom.parser());
            }
        }
        None
    }
    /// Get all descendant nodes matching a selector, relative to a Node
    pub fn select_all_within<'a>(
        node: &'a Node<'a>,
        selector: &str,
        dom: &'a VDom,
    ) -> Vec<&'a Node<'a>> {
        let mut results = Vec::new();

        fn recurse<'a>(
            node: &'a Node<'a>,
            selector: &str,
            dom: &'a VDom,
            results: &mut Vec<&'a Node<'a>>,
        ) {
            // Check if node matches the selector (simplest: tag name match)
            if let Node::Tag(tag) = node {
                if tag.name().as_utf8_str().eq_ignore_ascii_case(selector) {
                    results.push(node);
                }
                // Recurse into children
                for child_id in tag.children().top().iter() {
                    if let Some(child) = child_id.get(dom.parser()) {
                        recurse(child, selector, dom, results);
                    }
                }
            }
        }

        recurse(node, selector, dom, &mut results);
        results
    }

    /// Extract text content from a node
    pub fn get_text_content(node: &Node, dom: &VDom) -> String {
        let mut buffer = String::new();
        get_text_content_into_buffer(node, dom, &mut buffer);
        buffer
    }

    /// Get text content length without allocating string (performance optimized)
    pub fn get_text_content_length(node: &Node, dom: &VDom) -> usize {
        get_text_content_length_recursive(node, dom)
    }

    /// Check if node has any text content (without allocation) - ITERATIVE
    pub fn has_text_content(node: &Node, dom: &VDom) -> bool {
        // Use iterative traversal to prevent stack overflow
        let mut stack = vec![node];

        while let Some(current_node) = stack.pop() {
            match current_node {
                Node::Raw(bytes) => {
                    if !bytes.as_bytes().is_empty() {
                        return true;
                    }
                }
                Node::Tag(tag) => {
                    for child_id in tag.children().top().iter() {
                        if let Some(child) = child_id.get(dom.parser()) {
                            stack.push(child);
                        }
                    }
                }
                Node::Comment(_) => {} // Skip comments
            }
        }
        false
    }

    /// Count text content length without string allocation - ITERATIVE
    fn get_text_content_length_recursive(node: &Node, dom: &VDom) -> usize {
        // Use iterative traversal to prevent stack overflow
        let mut stack = vec![node];
        let mut total = 0;

        while let Some(current_node) = stack.pop() {
            match current_node {
                Node::Raw(bytes) => {
                    total += bytes.as_bytes().len();
                }
                Node::Tag(tag) => {
                    for child_id in tag.children().top().iter() {
                        if let Some(child) = child_id.get(dom.parser()) {
                            stack.push(child);
                        }
                    }
                }
                Node::Comment(_) => {} // Comments don't count
            }
        }
        total
    }

    /// Extract text content into a pre-allocated buffer - ITERATIVE
    fn get_text_content_into_buffer(node: &Node, dom: &VDom, buffer: &mut String) {
        // Use iterative traversal to prevent stack overflow
        let mut stack = vec![node];

        while let Some(current_node) = stack.pop() {
            match current_node {
                Node::Raw(bytes) => {
                    buffer.push_str(&String::from_utf8_lossy(bytes.as_bytes()));
                }
                Node::Tag(tag) => {
                    // Skip tags that contain non-content data
                    let tag_name = tag.name().as_utf8_str().to_lowercase();
                    const EXCLUDED_TAGS: &[&str] = &[
                        "script", "style", "noscript", "iframe", "object", "embed", "svg", "canvas",
                    ];

                    if EXCLUDED_TAGS.contains(&tag_name.as_str()) {
                        // Skip this tag and all its children
                        continue;
                    }

                    for child_id in tag.children().top().iter() {
                        if let Some(child) = child_id.get(dom.parser()) {
                            stack.push(child);
                        }
                    }
                }
                Node::Comment(_) => {} // Comments don't contribute to text content
            }
        }
    }

    /// Extract attribute value from a tag node
    pub fn get_attribute(node: &Node, attr_name: &str) -> Option<String> {
        match node {
            Node::Tag(tag) => tag
                .attributes()
                .get(attr_name)?
                .map(|v| v.as_utf8_str().to_string()),
            _ => None,
        }
    }

    /// Get attribute value as string slice (zero-copy when possible)
    pub fn get_attribute_str(node: &Node, attr_name: &str) -> Option<String> {
        match node {
            Node::Tag(tag) => tag
                .attributes()
                .get(attr_name)?
                .as_ref()
                .map(|v| v.as_utf8_str().to_string()),
            _ => None,
        }
    }

    /// Check attribute value without allocating string
    pub fn attribute_equals(node: &Node, attr_name: &str, expected: &str) -> bool {
        match node {
            Node::Tag(tag) => {
                if let Some(attr) = tag.attributes().get(attr_name) {
                    if let Some(value) = attr {
                        value.as_utf8_str().as_ref() == expected
                    } else {
                        false
                    }
                } else {
                    false
                }
            }
            _ => false,
        }
    }

    /// Check if an attribute exists on a tag node (for boolean attributes)
    pub fn has_attribute(node: &Node, attr_name: &str) -> bool {
        match node {
            Node::Tag(tag) => tag.attributes().get(attr_name).is_some(),
            _ => false,
        }
    }

    /// Check if a node matches a tag name
    pub fn is_tag(node: &Node, tag_name: &str) -> bool {
        match node {
            Node::Tag(tag) => tag.name().as_utf8_str().eq_ignore_ascii_case(tag_name),
            _ => false,
        }
    }

    /// Get tag name from a node
    pub fn get_tag_name(node: &Node) -> Option<String> {
        match node {
            Node::Tag(tag) => Some(tag.name().as_utf8_str().to_string()),
            _ => None,
        }
    }

    /// Calculate the byte size of HTML content
    pub fn calculate_html_size(html: &str) -> usize {
        html.len()
    }
}

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

    const SAMPLE_HTML: &str = r#"<!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
    <meta charset="utf-8">
    <style>body { margin: 0; }</style>
    <script>console.log('header script');</script>
</head>
<body>
    <h1 class="main-title">Main Title</h1>
    <div class="content" data-test="value">
        <p>Paragraph content</p>
        <p style="color: red;">Styled paragraph</p>
    </div>
    <script>console.log('body script');</script>
    <style>.footer { padding: 10px; }</style>
</body>
</html>"#;

    #[test]
    fn test_html_parser_creation() {
        let _parser = HtmlParser::new();
        // Should create without error
        assert!(true);
    }

    #[test]
    fn test_html_parser_with_options() {
        let options = tl::ParserOptions::default();
        let _parser = HtmlParser::with_options(options);
        // Should create without error
        assert!(true);
    }

    #[test]
    fn test_parse_valid_html() {
        let parser = HtmlParser::new();
        let result = parser.parse(SAMPLE_HTML);

        assert!(result.is_ok());
        let dom = result.unwrap();
        assert!(dom.nodes().len() > 0);
    }

    #[test]
    fn test_parse_empty_html() {
        let parser = HtmlParser::new();
        let result = parser.parse("");

        assert!(result.is_ok());
        let _dom = result.unwrap();
        // Should parse successfully - just verify we got a DOM structure
        // Empty string should still create a valid (though empty) DOM
        assert!(true); // Test passes if we reach this point without panic
    }

    #[test]
    fn test_parse_malformed_html() {
        let parser = HtmlParser::new();
        let malformed_html = "<html><body><p>unclosed paragraph<div>unclosed div</body></html>";
        let result = parser.parse(malformed_html);

        // tl is forgiving with malformed HTML
        assert!(result.is_ok());
    }

    #[test]
    fn test_find_elements_by_tag() {
        let parser = HtmlParser::new();
        let dom = parser.parse(SAMPLE_HTML).unwrap();

        let p_elements = parser.find_elements_by_tag(&dom, "p");
        assert_eq!(p_elements.len(), 2);

        let script_elements = parser.find_elements_by_tag(&dom, "script");
        assert_eq!(script_elements.len(), 2);

        let nonexistent_elements = parser.find_elements_by_tag(&dom, "article");
        assert_eq!(nonexistent_elements.len(), 0);
    }

    #[test]
    fn test_find_elements_by_class() {
        let parser = HtmlParser::new();
        let dom = parser.parse(SAMPLE_HTML).unwrap();

        let main_title_elements = parser.find_elements_by_class(&dom, "main-title");
        assert_eq!(main_title_elements.len(), 1);

        let content_elements = parser.find_elements_by_class(&dom, "content");
        assert_eq!(content_elements.len(), 1);

        let nonexistent_elements = parser.find_elements_by_class(&dom, "nonexistent");
        assert_eq!(nonexistent_elements.len(), 0);
    }

    #[test]
    fn test_find_elements_with_attribute() {
        let parser = HtmlParser::new();
        let dom = parser.parse(SAMPLE_HTML).unwrap();

        let data_test_elements = parser.find_elements_with_attribute(&dom, "data-test");
        assert_eq!(data_test_elements.len(), 1);

        let style_elements = parser.find_elements_with_attribute(&dom, "style");
        assert_eq!(style_elements.len(), 1);

        let nonexistent_elements = parser.find_elements_with_attribute(&dom, "nonexistent");
        assert_eq!(nonexistent_elements.len(), 0);
    }

    // Note: calculate_bytes function tests removed due to function not being available

    #[test]
    fn test_parser_trait_implementation() {
        let parser = HtmlParser::new();

        // Test that HtmlParser properly implements Parser trait
        let result: Result<VDom> = parser.parse("<html><body>Test</body></html>");
        assert!(result.is_ok());
    }

    #[test]
    fn test_parser_default_implementation() {
        let parser = HtmlParser::default();
        let result = parser.parse("<html><body>Default Test</body></html>");
        assert!(result.is_ok());
    }
}