ruchy 4.2.0

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Native HTML parsing stdlib (HTTP-002-C, STD-011)
//!
//! Provides HTML parsing and querying using Mozilla's html5ever parser.
//! Avoids deprecated `scraper` crate by implementing native solution.
//!
//! # Design Philosophy
//!
//! - **Zero Deprecated Dependencies**: Uses maintained html5ever from Mozilla Servo
//! - **Thin Wrapper**: Minimal complexity, maximum reliability
//! - **Ruchy-Friendly**: Clean API matching Ruby/JavaScript patterns
//! - **Toyota Way**: ≤10 complexity per function, comprehensive tests
//!
//! # Examples
//!
//! ```ruchy
//! html = Html.parse("<div class='test'>Hello</div>")
//! elements = html.select(".test")
//! puts elements[0].text()  # "Hello"
//! ```

use html5ever::parse_document;
use html5ever::tendril::TendrilSink;
use markup5ever_rcdom::{Handle, NodeData, RcDom};
use std::fmt;
use std::sync::Arc;

/// HTML document type for parsing and querying
///
/// Wraps html5ever's `RcDom` for Ruchy-friendly API
#[derive(Clone)]
pub struct HtmlDocument {
    dom: Arc<RcDom>,
}

impl fmt::Debug for HtmlDocument {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "HtmlDocument")
    }
}

/// HTML element wrapper
///
/// References a node in the HTML DOM tree
#[derive(Clone)]
pub struct HtmlElement {
    handle: Handle,
}

impl fmt::Debug for HtmlElement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "HtmlElement")
    }
}

impl HtmlDocument {
    /// Parse HTML from string
    ///
    /// Uses html5ever's parser for standards-compliant HTML5 parsing.
    /// Handles malformed HTML gracefully.
    ///
    /// # Examples
    ///
    /// ```
    /// use ruchy::stdlib::html::HtmlDocument;
    ///
    /// let html = HtmlDocument::parse("<div>Test</div>");
    /// ```
    pub fn parse(content: &str) -> Self {
        let dom = parse_document(RcDom::default(), Default::default())
            .from_utf8()
            .read_from(&mut content.as_bytes())
            .expect("HTML content should be valid UTF-8");

        Self { dom: Arc::new(dom) }
    }

    /// Select elements matching CSS selector
    ///
    /// Returns all elements matching the given CSS selector.
    /// Uses simple selector matching (tag, class, id, attribute).
    ///
    /// # Errors
    ///
    /// Returns error if selector is invalid.
    ///
    /// # Examples
    ///
    /// ```
    /// let html = HtmlDocument::parse("<p class='text'>Hello</p>");
    /// let elements = html.select(".text").unwrap();
    /// assert_eq!(elements.len(), 1);
    /// ```
    pub fn select(&self, selector: &str) -> Result<Vec<HtmlElement>, String> {
        // Validate selector syntax before attempting to match
        self.validate_selector(selector)?;

        let elements = Self::select_nodes(&self.dom.document, selector);
        Ok(elements
            .into_iter()
            .map(|handle| HtmlElement { handle })
            .collect())
    }

    /// Query selector (returns first match)
    ///
    /// Returns the first element matching the selector, or None.
    ///
    /// # Examples
    ///
    /// ```
    /// let html = HtmlDocument::parse("<p>First</p><p>Second</p>");
    /// let element = html.query_selector("p").unwrap();
    /// assert!(element.is_some());
    /// ```
    pub fn query_selector(&self, selector: &str) -> Result<Option<HtmlElement>, String> {
        let elements = self.select(selector)?;
        Ok(elements.into_iter().next())
    }

    /// Query selector all (alias for select)
    pub fn query_selector_all(&self, selector: &str) -> Result<Vec<HtmlElement>, String> {
        self.select(selector)
    }

    /// Recursively select nodes matching selector
    ///
    /// Internal helper for traversing DOM tree.
    /// Complexity: 8 (within Toyota Way limits)
    fn select_nodes(node: &Handle, selector: &str) -> Vec<Handle> {
        let mut results = Vec::new();

        // Check if current node matches
        if Self::matches_selector(node, selector) {
            results.push(node.clone());
        }

        // Recursively check children
        for child in node.children.borrow().iter() {
            results.extend(Self::select_nodes(child, selector));
        }

        results
    }

    /// Check if node matches CSS selector
    ///
    /// Supports: tag, .class, #id, [attr], [attr=value]
    /// Complexity: 10 (at Toyota Way limit)
    fn matches_selector(node: &Handle, selector: &str) -> bool {
        let selector = selector.trim();

        match &node.data {
            NodeData::Element { name, attrs, .. } => {
                let tag_name = name.local.as_ref();
                let attrs_borrowed = attrs.borrow();

                // Class selector: ".className"
                if let Some(class_name) = selector.strip_prefix('.') {
                    return attrs_borrowed.iter().any(|attr| {
                        attr.name.local.as_ref() == "class"
                            && attr
                                .value
                                .as_ref()
                                .split_whitespace()
                                .any(|c| c == class_name)
                    });
                }

                // ID selector: "#idName"
                if let Some(id_name) = selector.strip_prefix('#') {
                    return attrs_borrowed.iter().any(|attr| {
                        attr.name.local.as_ref() == "id" && attr.value.as_ref() == id_name
                    });
                }

                // Attribute selector: "[attr]" or "[attr=value]"
                if selector.starts_with('[') && selector.ends_with(']') {
                    let inner = &selector[1..selector.len() - 1];
                    if let Some((attr_name, attr_value)) = inner.split_once('=') {
                        let attr_value = attr_value.trim_matches('\'').trim_matches('"');
                        return attrs_borrowed.iter().any(|attr| {
                            attr.name.local.as_ref() == attr_name
                                && attr.value.as_ref() == attr_value
                        });
                    }
                    return attrs_borrowed
                        .iter()
                        .any(|attr| attr.name.local.as_ref() == inner);
                }

                // Descendant selector: "div p" - match last element only (simplified)
                if selector.contains(' ') {
                    let parts: Vec<&str> = selector.split_whitespace().collect();
                    if let Some(&last) = parts.last() {
                        return Self::matches_selector(node, last);
                    }
                }

                // Tag selector: "div"
                tag_name == selector
            }
            _ => false,
        }
    }

    /// Validate CSS selector syntax
    ///
    /// Checks for common CSS selector syntax errors.
    /// Complexity: 5 (within Toyota Way limits)
    ///
    /// # Errors
    ///
    /// Returns error if selector has invalid syntax
    fn validate_selector(&self, selector: &str) -> Result<(), String> {
        let selector = selector.trim();

        // Empty selector
        if selector.is_empty() {
            return Err("Selector cannot be empty".to_string());
        }

        // Check for unmatched brackets in attribute selectors
        let open_brackets = selector.matches('[').count();
        let close_brackets = selector.matches(']').count();
        if open_brackets != close_brackets {
            return Err(format!(
                "Invalid selector syntax: unmatched brackets in '{selector}'"
            ));
        }

        // Check for invalid bracket nesting (e.g., "[[" or "[[attr]")
        if selector.contains("[[") || selector.contains("]]") {
            return Err(format!(
                "Invalid selector syntax: nested brackets in '{selector}'"
            ));
        }

        Ok(())
    }
}

impl HtmlElement {
    /// Get text content of element and its descendants
    ///
    /// Recursively collects all text nodes.
    ///
    /// # Examples
    ///
    /// ```
    /// let html = HtmlDocument::parse("<p>Hello <span>World</span></p>");
    /// let p = html.query_selector("p").unwrap().unwrap();
    /// assert_eq!(p.text(), "Hello World");
    /// ```
    pub fn text(&self) -> String {
        Self::collect_text(&self.handle)
    }

    /// Get attribute value
    ///
    /// Returns attribute value or None if attribute doesn't exist.
    ///
    /// # Examples
    ///
    /// ```
    /// let html = HtmlDocument::parse("<a href='test.html'>Link</a>");
    /// let link = html.query_selector("a").unwrap().unwrap();
    /// assert_eq!(link.attr("href"), Some("test.html".to_string()));
    /// ```
    pub fn attr(&self, name: &str) -> Option<String> {
        match &self.handle.data {
            NodeData::Element { attrs, .. } => attrs.borrow().iter().find_map(|attr| {
                if attr.name.local.as_ref() == name {
                    Some(attr.value.to_string())
                } else {
                    None
                }
            }),
            _ => None,
        }
    }

    /// Get inner HTML
    ///
    /// Returns HTML content of element's children.
    ///
    /// # Examples
    ///
    /// ```
    /// let html = HtmlDocument::parse("<div><p>Test</p></div>");
    /// let div = html.query_selector("div").unwrap().unwrap();
    /// assert!(div.html().contains("<p>Test</p>"));
    /// ```
    pub fn html(&self) -> String {
        Self::serialize_node(&self.handle)
    }

    /// Recursively collect text from node and children
    ///
    /// Complexity: 5 (well within Toyota Way limits)
    fn collect_text(node: &Handle) -> String {
        let mut text = String::new();

        match &node.data {
            NodeData::Text { contents } => {
                text.push_str(&contents.borrow());
            }
            _ => {
                for child in node.children.borrow().iter() {
                    text.push_str(&Self::collect_text(child));
                }
            }
        }

        text
    }

    /// Serialize node to HTML string
    ///
    /// Simplified HTML serialization.
    /// Complexity: 8 (within Toyota Way limits)
    fn serialize_node(node: &Handle) -> String {
        let mut html = String::new();

        match &node.data {
            NodeData::Element { name, attrs, .. } => {
                let tag_name = name.local.as_ref();
                html.push('<');
                html.push_str(tag_name);

                // Add attributes
                for attr in attrs.borrow().iter() {
                    html.push(' ');
                    html.push_str(attr.name.local.as_ref());
                    html.push_str("=\"");
                    html.push_str(&attr.value);
                    html.push('"');
                }

                html.push('>');

                // Add children
                for child in node.children.borrow().iter() {
                    html.push_str(&Self::serialize_node(child));
                }

                html.push_str("</");
                html.push_str(tag_name);
                html.push('>');
            }
            NodeData::Text { contents } => {
                html.push_str(&contents.borrow());
            }
            _ => {
                // Serialize children for other node types
                for child in node.children.borrow().iter() {
                    html.push_str(&Self::serialize_node(child));
                }
            }
        }

        html
    }
}

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

    #[test]
    fn test_parse_simple_html() {
        let html = HtmlDocument::parse("<div>Test</div>");
        assert!(!html.dom.document.children.borrow().is_empty());
    }

    #[test]
    fn test_select_by_tag() {
        let html = HtmlDocument::parse("<div><p>Test</p></div>");
        let elements = html.select("p").expect("operation should succeed in test");
        assert_eq!(elements.len(), 1);
    }

    #[test]
    fn test_select_by_class() {
        let html = HtmlDocument::parse("<div class='test'>Hello</div>");
        let elements = html
            .select(".test")
            .expect("operation should succeed in test");
        assert_eq!(elements.len(), 1);
    }

    #[test]
    fn test_element_text() {
        let html = HtmlDocument::parse("<p>Hello World</p>");
        let p = html
            .query_selector("p")
            .expect("operation should succeed in test")
            .expect("operation should succeed in test");
        assert_eq!(p.text().trim(), "Hello World");
    }

    #[test]
    fn test_element_attr() {
        let html = HtmlDocument::parse("<a href='test.html'>Link</a>");
        let link = html
            .query_selector("a")
            .expect("operation should succeed in test")
            .expect("operation should succeed in test");
        assert_eq!(link.attr("href"), Some("test.html".to_string()));
    }

    #[test]
    fn test_element_attr_missing() {
        let html = HtmlDocument::parse("<a>Link</a>");
        let link = html
            .query_selector("a")
            .expect("operation should succeed in test")
            .expect("operation should succeed in test");
        assert_eq!(link.attr("href"), None);
    }

    #[test]
    fn test_multiple_elements() {
        let html = HtmlDocument::parse("<p>1</p><p>2</p><p>3</p>");
        let elements = html.select("p").expect("operation should succeed in test");
        assert_eq!(elements.len(), 3);
    }

    #[test]
    fn test_query_selector_none() {
        let html = HtmlDocument::parse("<div>Test</div>");
        let element = html
            .query_selector("p")
            .expect("operation should succeed in test");
        assert!(element.is_none());
    }

    #[test]
    fn test_malformed_html() {
        let html = HtmlDocument::parse("<div><p>Unclosed");
        let elements = html.select("p").expect("operation should succeed in test");
        assert_eq!(elements.len(), 1);
    }

    #[test]
    fn test_method_chaining_simulation() {
        // Simulate the exact pattern from test_http002d_11
        let html = HtmlDocument::parse("<div class='content'>Hello World</div>");
        let elements = html
            .select(".content")
            .expect("operation should succeed in test");
        assert_eq!(elements.len(), 1, "Should have 1 element");

        let element = &elements[0];
        let text = element.text();
        assert_eq!(text.trim(), "Hello World", "Text extraction should work");
    }

    #[test]
    fn test_empty_html() {
        let html = HtmlDocument::parse("");
        let elements = html.select("*").expect("operation should succeed in test");
        assert_eq!(elements.len(), 0);
    }

    /// Property test: Parsing never panics
    #[test]
    #[ignore = "Property test - run with: cargo test -- --ignored"]
    fn prop_parse_never_panics() {
        use proptest::prelude::*;

        proptest!(|(html_str in ".*")| {
            let _ = HtmlDocument::parse(&html_str);
        });
    }

    // COVERAGE-95: Additional tests

    #[test]
    fn test_select_by_id() {
        let html = HtmlDocument::parse("<div id='main'>Content</div>");
        let elements = html.select("#main").expect("should parse");
        assert_eq!(elements.len(), 1);
    }

    #[test]
    fn test_select_by_attribute() {
        let html = HtmlDocument::parse("<input type='text' name='field'>");
        let elements = html.select("[type]").expect("should parse");
        assert_eq!(elements.len(), 1);
    }

    #[test]
    fn test_select_by_attribute_value() {
        let html = HtmlDocument::parse("<input type='text'><input type='checkbox'>");
        let elements = html.select("[type=text]").expect("should parse");
        assert_eq!(elements.len(), 1);
    }

    #[test]
    fn test_select_by_attribute_value_quoted() {
        let html = HtmlDocument::parse("<input data-test='value'>");
        let elements = html.select("[data-test='value']").expect("should parse");
        assert_eq!(elements.len(), 1);
    }

    #[test]
    fn test_query_selector_all() {
        let html = HtmlDocument::parse("<p>1</p><p>2</p>");
        let elements = html.query_selector_all("p").expect("should parse");
        assert_eq!(elements.len(), 2);
    }

    #[test]
    fn test_descendant_selector() {
        let html = HtmlDocument::parse("<div><span><p>Nested</p></span></div>");
        let elements = html.select("div p").expect("should parse");
        assert_eq!(elements.len(), 1);
    }

    #[test]
    fn test_select_article_tag() {
        let html = HtmlDocument::parse("<article>Content</article>");
        let elem = html
            .query_selector("article")
            .expect("should parse")
            .unwrap();
        assert!(elem.text().contains("Content"));
    }

    #[test]
    fn test_element_html() {
        let html = HtmlDocument::parse("<div><span>Inner</span></div>");
        let elem = html.query_selector("div").expect("should parse").unwrap();
        let inner = elem.html();
        assert!(inner.contains("span"));
        assert!(inner.contains("Inner"));
    }

    #[test]
    fn test_element_multiple_classes() {
        let html = HtmlDocument::parse("<div class='a b c'>Test</div>");
        let elements = html.select(".b").expect("should parse");
        assert_eq!(elements.len(), 1);
    }

    #[test]
    fn test_debug_impl_document() {
        let html = HtmlDocument::parse("<div>Test</div>");
        let debug_str = format!("{:?}", html);
        assert_eq!(debug_str, "HtmlDocument");
    }

    #[test]
    fn test_debug_impl_element() {
        let html = HtmlDocument::parse("<div>Test</div>");
        let elem = html.query_selector("div").expect("should parse").unwrap();
        let debug_str = format!("{:?}", elem);
        assert_eq!(debug_str, "HtmlElement");
    }

    #[test]
    fn test_nested_text_extraction() {
        let html = HtmlDocument::parse("<div>Hello <b>World</b>!</div>");
        let elem = html.query_selector("div").expect("should parse").unwrap();
        let text = elem.text();
        assert!(text.contains("Hello"));
        assert!(text.contains("World"));
    }

    #[test]
    fn test_select_no_match() {
        let html = HtmlDocument::parse("<div>Test</div>");
        let elements = html.select("nonexistent").expect("should parse");
        assert!(elements.is_empty());
    }

    #[test]
    fn test_select_whitespace_in_selector() {
        let html = HtmlDocument::parse("<div><p>Test</p></div>");
        let elements = html.select(" p ").expect("should parse");
        assert_eq!(elements.len(), 1);
    }

    #[test]
    fn test_html_clone() {
        let html = HtmlDocument::parse("<div>Test</div>");
        let _cloned = html.clone();
        // Should compile and work without panicking
    }

    #[test]
    fn test_element_clone() {
        let html = HtmlDocument::parse("<div>Test</div>");
        let elem = html.query_selector("div").expect("should parse").unwrap();
        let _cloned = elem.clone();
        // Should compile and work without panicking
    }

    #[test]
    fn test_attr_missing_on_text_node() {
        let html = HtmlDocument::parse("<div>Just text</div>");
        let elem = html.query_selector("div").expect("should parse").unwrap();
        // Getting non-existent attribute returns None
        assert!(elem.attr("data-nonexistent").is_none());
    }
}