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
432
433
434
435
436
437
438
439
440
441
442
//! Unified Element Processing Interface
//!
//! This module provides a high-performance, unified interface for element counting,
//! text processing, and DOM analysis to eliminate code duplication and improve
//! efficiency across all modules.

use crate::parser::utils::{self as parser_utils};
use crate::utils::dom_cache::{DomCache, TextMetricsCache};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use tl::{Node, VDom};

/// High-performance element processor with caching and batching capabilities
pub struct ElementProcessor<'a> {
    dom: &'a VDom<'a>,
    dom_cache: Option<&'a DomCache<'a>>,
    text_cache: TextMetricsCache,
    results_cache: Arc<RwLock<HashMap<String, usize>>>,
}

/// Shared results cache for high-throughput batch processing
pub type SharedResultsCache = Arc<RwLock<HashMap<String, usize>>>;

impl<'a> ElementProcessor<'a> {
    /// Create a new element processor with optional DOM cache
    pub fn new(dom: &'a VDom<'a>) -> Self {
        Self {
            dom,
            dom_cache: None,
            text_cache: TextMetricsCache::new(),
            results_cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Create processor with existing DOM cache for maximum performance
    pub fn with_cache(dom: &'a VDom<'a>, dom_cache: &'a DomCache<'a>) -> Self {
        Self {
            dom,
            dom_cache: Some(dom_cache),
            text_cache: TextMetricsCache::new(),
            results_cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Create processor with shared cache for batch processing
    pub fn with_shared_cache(
        dom: &'a VDom<'a>,
        dom_cache: Option<&'a DomCache<'a>>,
        shared_cache: SharedResultsCache,
    ) -> Self {
        Self {
            dom,
            dom_cache,
            text_cache: TextMetricsCache::new(),
            results_cache: shared_cache,
        }
    }

    /// Count elements matching a selector (cached)
    pub fn count_elements(&mut self, selector: &str) -> usize {
        // Check cache first
        if let Ok(cache) = self.results_cache.read() {
            if let Some(&cached_result) = cache.get(selector) {
                return cached_result;
            }
        }

        let count = if let Some(cache) = &self.dom_cache {
            // Use DOM cache's immutable methods by passing the selector
            match selector {
                "img" => cache.get_images().len(),
                "a[href]" | "a" => cache.get_links().len(),
                "h1, h2, h3, h4, h5, h6" => cache.get_headings().len(),
                "form" => cache.get_forms().len(),
                "input, textarea, select" => cache.get_inputs().len(),
                "script" => cache.get_scripts().len(),
                "style" => cache.get_styles().len(),
                "meta" => cache.get_meta_elements().len(),
                "img, video, audio, picture, svg" => cache.get_media_elements().len(),
                "button, input, select, textarea, a[href]" => {
                    cache.get_interactive_elements().len()
                }
                _ => parser_utils::select_all(self.dom, selector).len(),
            }
        } else {
            parser_utils::select_all(self.dom, selector).len()
        };

        // Cache the result
        if let Ok(mut cache) = self.results_cache.write() {
            cache.insert(selector.to_string(), count);
        }
        count
    }

    /// Check if elements matching selector exist (more efficient than counting)
    pub fn has_elements(&mut self, selector: &str) -> bool {
        if let Some(cache) = &self.dom_cache {
            match selector {
                "img" => !cache.get_images().is_empty(),
                "a[href]" | "a" => !cache.get_links().is_empty(),
                "h1, h2, h3, h4, h5, h6" => !cache.get_headings().is_empty(),
                "form" => !cache.get_forms().is_empty(),
                "input, textarea, select" => !cache.get_inputs().is_empty(),
                "script" => !cache.get_scripts().is_empty(),
                "style" => !cache.get_styles().is_empty(),
                "meta" => !cache.get_meta_elements().is_empty(),
                "img, video, audio, picture, svg" => !cache.get_media_elements().is_empty(),
                "button, input, select, textarea, a[href]" => {
                    !cache.get_interactive_elements().is_empty()
                }
                _ => parser_utils::select_first(self.dom, selector).is_some(),
            }
        } else {
            parser_utils::select_first(self.dom, selector).is_some()
        }
    }

    /// Count elements matching multiple selectors efficiently with optimized batching
    pub fn count_elements_batch(&mut self, selectors: &[&str]) -> Vec<usize> {
        // Pre-allocate result vector
        let mut results = Vec::with_capacity(selectors.len());

        // Process selectors that can use DOM cache first
        for &selector in selectors {
            results.push(self.count_elements(selector));
        }

        results
    }

    /// Optimized batch processing for high-throughput scenarios
    pub fn count_elements_batch_optimized(&mut self, selectors: &[&str]) -> Vec<usize> {
        // For high-throughput, prioritize cache hits and minimize DOM traversals
        let mut results = Vec::with_capacity(selectors.len());
        let mut uncached_selectors = Vec::new();

        // First pass: collect all cached results
        for (idx, &selector) in selectors.iter().enumerate() {
            if let Ok(cache) = self.results_cache.read() {
                if let Some(&cached_result) = cache.get(selector) {
                    results.push((idx, cached_result));
                    continue;
                }
            }
            uncached_selectors.push((idx, selector));
        }

        // Second pass: process uncached selectors
        for (idx, selector) in uncached_selectors {
            let count = self.count_elements(selector);
            results.push((idx, count));
        }

        // Sort by original index and extract values
        results.sort_by_key(|(idx, _)| *idx);
        results.into_iter().map(|(_, count)| count).collect()
    }

    /// Count total elements across multiple selectors
    pub fn count_elements_sum(&mut self, selectors: &[&str]) -> usize {
        selectors
            .iter()
            .map(|selector| self.count_elements(selector))
            .sum()
    }

    /// Get elements with optional filtering
    pub fn get_elements(&self, selector: &str) -> Vec<&Node<'_>> {
        if let Some(cache) = &self.dom_cache {
            match selector {
                "img" => cache.get_images().to_vec(),
                "a[href]" | "a" => cache.get_links().to_vec(),
                "h1, h2, h3, h4, h5, h6" => cache.get_headings().to_vec(),
                "form" => cache.get_forms().to_vec(),
                "input, textarea, select" => cache.get_inputs().to_vec(),
                "script" => cache.get_scripts().to_vec(),
                "style" => cache.get_styles().to_vec(),
                "meta" => cache.get_meta_elements().to_vec(),
                _ => parser_utils::select_all(self.dom, selector),
            }
        } else {
            parser_utils::select_all(self.dom, selector)
        }
    }

    /// Calculate total text length from elements matching selector (cached)
    pub fn calculate_text_length(&mut self, selector: &str) -> usize {
        let cache_key = format!("text_len:{}", selector);

        // Check cache first
        if let Ok(cache) = self.results_cache.read() {
            if let Some(&cached_result) = cache.get(&cache_key) {
                return cached_result;
            }
        }

        // Get elements directly to avoid borrowing conflicts
        let elements = if let Some(cache) = &self.dom_cache {
            match selector {
                "img" => cache.get_images().to_vec(),
                "a[href]" | "a" => cache.get_links().to_vec(),
                "h1, h2, h3, h4, h5, h6" => cache.get_headings().to_vec(),
                "form" => cache.get_forms().to_vec(),
                "input, textarea, select" => cache.get_inputs().to_vec(),
                "script" => cache.get_scripts().to_vec(),
                "style" => cache.get_styles().to_vec(),
                "meta" => cache.get_meta_elements().to_vec(),
                _ => parser_utils::select_all(self.dom, selector),
            }
        } else {
            parser_utils::select_all(self.dom, selector)
        };

        let mut total = 0;
        for node in elements {
            total += self.text_cache.get_text_length(node, self.dom);
        }

        // Cache the result
        if let Ok(mut cache) = self.results_cache.write() {
            cache.insert(cache_key, total);
        }
        total
    }

    /// Check if any elements have text content (more efficient than extracting text)
    pub fn has_text_content(&mut self, selector: &str) -> bool {
        // Get elements directly to avoid borrowing conflicts
        let elements = if let Some(cache) = &self.dom_cache {
            match selector {
                "img" => cache.get_images().to_vec(),
                "a[href]" | "a" => cache.get_links().to_vec(),
                "h1, h2, h3, h4, h5, h6" => cache.get_headings().to_vec(),
                "form" => cache.get_forms().to_vec(),
                "input, textarea, select" => cache.get_inputs().to_vec(),
                "script" => cache.get_scripts().to_vec(),
                "style" => cache.get_styles().to_vec(),
                "meta" => cache.get_meta_elements().to_vec(),
                _ => parser_utils::select_all(self.dom, selector),
            }
        } else {
            parser_utils::select_all(self.dom, selector)
        };

        for node in elements {
            if self.text_cache.has_text_content(node, self.dom) {
                return true;
            }
        }
        false
    }

    /// Extract attribute values efficiently with caching
    pub fn extract_attributes(&self, selector: &str, attribute: &str) -> Vec<String> {
        self.get_elements(selector)
            .into_iter()
            .filter_map(|element| parser_utils::get_attribute(element, attribute))
            .collect()
    }

    /// Count elements with specific attribute
    pub fn count_with_attribute(&mut self, selector: &str, attribute: &str) -> usize {
        let cache_key = format!("attr:{}:{}", selector, attribute);

        // Check cache first
        if let Ok(cache) = self.results_cache.read() {
            if let Some(&cached_result) = cache.get(&cache_key) {
                return cached_result;
            }
        }

        let count = self
            .get_elements(selector)
            .into_iter()
            .filter(|element| parser_utils::has_attribute(element, attribute))
            .count();

        // Cache the result
        if let Ok(mut cache) = self.results_cache.write() {
            cache.insert(cache_key, count);
        }
        count
    }

    /// Count elements with specific attribute value
    pub fn count_with_attribute_value(
        &mut self,
        selector: &str,
        attribute: &str,
        value: &str,
    ) -> usize {
        let cache_key = format!("attr_val:{}:{}:{}", selector, attribute, value);

        // Check cache first
        if let Ok(cache) = self.results_cache.read() {
            if let Some(&cached_result) = cache.get(&cache_key) {
                return cached_result;
            }
        }

        let count = self
            .get_elements(selector)
            .into_iter()
            .filter(|element| {
                if let Some(attr_val) = parser_utils::get_attribute(element, attribute) {
                    attr_val == value
                } else {
                    false
                }
            })
            .count();

        // Cache the result
        if let Ok(mut cache) = self.results_cache.write() {
            cache.insert(cache_key, count);
        }
        count
    }

    /// Get attribute value counts
    pub fn get_attribute_counts(&self, selector: &str, attribute: &str) -> HashMap<String, usize> {
        let mut counts = HashMap::new();

        for element in self.get_elements(selector) {
            if let Some(attr_value) = parser_utils::get_attribute(element, attribute) {
                *counts.entry(attr_value).or_insert(0) += 1;
            }
        }

        counts
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::parse_html;
    use crate::utils::dom_cache::DomCache;

    const TEST_HTML: &str = r#"
        <html>
            <head>
                <title>Test</title>
                <meta name="description" content="Test page">
            </head>
            <body>
                <h1>Heading 1</h1>
                <h2>Heading 2</h2>
                <p>Some text with <a href="https://example.com">a link</a></p>
                <img src="test.jpg" alt="Test image">
                <img src="test2.jpg">
                <form>
                    <input type="text" name="test" required>
                    <button type="submit">Submit</button>
                </form>
                <script src="test.js"></script>
                <script>console.log('inline');</script>
            </body>
        </html>
    "#;

    #[test]
    fn test_element_processor_basic() {
        let dom = parse_html(TEST_HTML).unwrap();
        let mut processor = ElementProcessor::new(&dom);

        assert_eq!(processor.count_elements("img"), 2);
        assert_eq!(processor.count_elements("h1"), 1);
        assert_eq!(processor.count_elements("h2"), 1);
        assert!(processor.has_elements("form"));
        assert!(!processor.has_elements("video"));
    }

    #[test]
    fn test_element_processor_with_cache() {
        let dom = parse_html(TEST_HTML).unwrap();
        let dom_cache = DomCache::new(&dom);
        let mut processor = ElementProcessor::with_cache(&dom, &dom_cache);

        assert_eq!(processor.count_elements("img"), 2);
        assert_eq!(processor.count_elements("script"), 2);
        assert!(processor.has_elements("input"));
    }

    #[test]
    fn test_element_counting() {
        let dom = parse_html(TEST_HTML).unwrap();
        let mut processor = ElementProcessor::new(&dom);

        // Direct counting using ElementProcessor methods
        let headings = &["h1", "h2", "h3", "h4", "h5", "h6"];
        let media = &["img", "video", "audio", "picture", "svg"];
        let form_elements = &["input", "textarea", "select", "button"];
        let interactive = &["a[href]", "button", "input", "textarea", "select"];

        assert_eq!(processor.count_elements_sum(headings), 2);
        assert_eq!(processor.count_elements_sum(media), 2); // 2 images
        assert_eq!(processor.count_elements_sum(form_elements), 2); // 1 input + 1 button
        assert!(processor.count_elements_sum(interactive) > 0);
    }

    #[test]
    fn test_batch_counting() {
        let dom = parse_html(TEST_HTML).unwrap();
        let mut processor = ElementProcessor::new(&dom);

        let selectors = &["h1", "h2", "img", "form"];
        let counts = processor.count_elements_batch(selectors);
        assert_eq!(counts, vec![1, 1, 2, 1]);

        let total = processor.count_elements_sum(selectors);
        assert_eq!(total, 5);
    }

    #[test]
    fn test_attribute_processing() {
        let dom = parse_html(TEST_HTML).unwrap();
        let mut processor = ElementProcessor::new(&dom);

        // Count images with alt attribute
        assert_eq!(processor.count_with_attribute("img", "alt"), 1);

        // Count required inputs (boolean attribute - just check for presence)
        assert_eq!(processor.count_with_attribute("input", "required"), 1);

        // Extract href values
        let hrefs = processor.extract_attributes("a", "href");
        assert_eq!(hrefs.len(), 1);
        assert!(hrefs[0].contains("example.com"));
    }

    #[test]
    fn test_text_processing() {
        let dom = parse_html(TEST_HTML).unwrap();
        let mut processor = ElementProcessor::new(&dom);

        assert!(processor.has_text_content("h1"));
        assert!(processor.calculate_text_length("p") > 0);
        assert!(!processor.has_text_content("img")); // Images don't have text content
    }
}