we (web engine): Experimental web browser project to understand the limits of Claude
1//! Block layout engine: box generation, block/inline layout, and text wrapping.
2//!
3//! Builds a layout tree from a styled tree (DOM + computed styles) and positions
4//! block-level elements vertically with proper inline formatting context.
5
6use std::collections::HashMap;
7
8use we_css::values::Color;
9use we_dom::{Document, NodeData, NodeId};
10use we_style::computed::{
11 BorderStyle, ComputedStyle, Display, LengthOrAuto, StyledNode, TextAlign, TextDecoration,
12};
13use we_text::font::Font;
14
15/// Edge sizes for box model (margin, padding, border).
16#[derive(Debug, Clone, Copy, Default, PartialEq)]
17pub struct EdgeSizes {
18 pub top: f32,
19 pub right: f32,
20 pub bottom: f32,
21 pub left: f32,
22}
23
24/// A positioned rectangle with content area dimensions.
25#[derive(Debug, Clone, Copy, Default, PartialEq)]
26pub struct Rect {
27 pub x: f32,
28 pub y: f32,
29 pub width: f32,
30 pub height: f32,
31}
32
33/// The type of layout box.
34#[derive(Debug)]
35pub enum BoxType {
36 /// Block-level box from an element.
37 Block(NodeId),
38 /// Inline-level box from an element.
39 Inline(NodeId),
40 /// A run of text from a text node.
41 TextRun { node: NodeId, text: String },
42 /// Anonymous block wrapping inline content within a block container.
43 Anonymous,
44}
45
46/// A single positioned text fragment with its own styling.
47///
48/// Multiple fragments can share the same y-coordinate when they are
49/// on the same visual line (e.g. `<p>Hello <em>world</em></p>` produces
50/// two fragments at the same y).
51#[derive(Debug, Clone, PartialEq)]
52pub struct TextLine {
53 pub text: String,
54 pub x: f32,
55 pub y: f32,
56 pub width: f32,
57 pub font_size: f32,
58 pub color: Color,
59 pub text_decoration: TextDecoration,
60 pub background_color: Color,
61}
62
63/// A box in the layout tree with dimensions and child boxes.
64#[derive(Debug)]
65pub struct LayoutBox {
66 pub box_type: BoxType,
67 pub rect: Rect,
68 pub margin: EdgeSizes,
69 pub padding: EdgeSizes,
70 pub border: EdgeSizes,
71 pub children: Vec<LayoutBox>,
72 pub font_size: f32,
73 /// Positioned text fragments (populated for boxes with inline content).
74 pub lines: Vec<TextLine>,
75 /// Text color.
76 pub color: Color,
77 /// Background color.
78 pub background_color: Color,
79 /// Text decoration (underline, etc.).
80 pub text_decoration: TextDecoration,
81 /// Border styles (top, right, bottom, left).
82 pub border_styles: [BorderStyle; 4],
83 /// Border colors (top, right, bottom, left).
84 pub border_colors: [Color; 4],
85 /// Text alignment for this box's inline content.
86 pub text_align: TextAlign,
87 /// Computed line height in px.
88 pub line_height: f32,
89 /// For replaced elements (e.g., `<img>`): content dimensions (width, height).
90 pub replaced_size: Option<(f32, f32)>,
91}
92
93impl LayoutBox {
94 fn new(box_type: BoxType, style: &ComputedStyle) -> Self {
95 LayoutBox {
96 box_type,
97 rect: Rect::default(),
98 margin: EdgeSizes::default(),
99 padding: EdgeSizes::default(),
100 border: EdgeSizes::default(),
101 children: Vec::new(),
102 font_size: style.font_size,
103 lines: Vec::new(),
104 color: style.color,
105 background_color: style.background_color,
106 text_decoration: style.text_decoration,
107 border_styles: [
108 style.border_top_style,
109 style.border_right_style,
110 style.border_bottom_style,
111 style.border_left_style,
112 ],
113 border_colors: [
114 style.border_top_color,
115 style.border_right_color,
116 style.border_bottom_color,
117 style.border_left_color,
118 ],
119 text_align: style.text_align,
120 line_height: style.line_height,
121 replaced_size: None,
122 }
123 }
124
125 /// Total height including margin, border, and padding.
126 pub fn margin_box_height(&self) -> f32 {
127 self.margin.top
128 + self.border.top
129 + self.padding.top
130 + self.rect.height
131 + self.padding.bottom
132 + self.border.bottom
133 + self.margin.bottom
134 }
135
136 /// Iterate over all boxes in depth-first pre-order.
137 pub fn iter(&self) -> LayoutBoxIter<'_> {
138 LayoutBoxIter { stack: vec![self] }
139 }
140}
141
142/// Depth-first pre-order iterator over layout boxes.
143pub struct LayoutBoxIter<'a> {
144 stack: Vec<&'a LayoutBox>,
145}
146
147impl<'a> Iterator for LayoutBoxIter<'a> {
148 type Item = &'a LayoutBox;
149
150 fn next(&mut self) -> Option<&'a LayoutBox> {
151 let node = self.stack.pop()?;
152 for child in node.children.iter().rev() {
153 self.stack.push(child);
154 }
155 Some(node)
156 }
157}
158
159/// The result of laying out a document.
160#[derive(Debug)]
161pub struct LayoutTree {
162 pub root: LayoutBox,
163 pub width: f32,
164 pub height: f32,
165}
166
167impl LayoutTree {
168 /// Iterate over all layout boxes in depth-first pre-order.
169 pub fn iter(&self) -> LayoutBoxIter<'_> {
170 self.root.iter()
171 }
172}
173
174// ---------------------------------------------------------------------------
175// Resolve LengthOrAuto to f32
176// ---------------------------------------------------------------------------
177
178fn resolve_length(value: LengthOrAuto) -> f32 {
179 match value {
180 LengthOrAuto::Length(px) => px,
181 LengthOrAuto::Auto => 0.0,
182 }
183}
184
185// ---------------------------------------------------------------------------
186// Build layout tree from styled tree
187// ---------------------------------------------------------------------------
188
189fn build_box(
190 styled: &StyledNode,
191 doc: &Document,
192 image_sizes: &HashMap<NodeId, (f32, f32)>,
193) -> Option<LayoutBox> {
194 let node = styled.node;
195 let style = &styled.style;
196
197 match doc.node_data(node) {
198 NodeData::Document => {
199 let mut children = Vec::new();
200 for child in &styled.children {
201 if let Some(child_box) = build_box(child, doc, image_sizes) {
202 children.push(child_box);
203 }
204 }
205 if children.len() == 1 {
206 children.into_iter().next()
207 } else if children.is_empty() {
208 None
209 } else {
210 let mut b = LayoutBox::new(BoxType::Anonymous, style);
211 b.children = children;
212 Some(b)
213 }
214 }
215 NodeData::Element { .. } => {
216 if style.display == Display::None {
217 return None;
218 }
219
220 let margin = EdgeSizes {
221 top: resolve_length(style.margin_top),
222 right: resolve_length(style.margin_right),
223 bottom: resolve_length(style.margin_bottom),
224 left: resolve_length(style.margin_left),
225 };
226 let padding = EdgeSizes {
227 top: style.padding_top,
228 right: style.padding_right,
229 bottom: style.padding_bottom,
230 left: style.padding_left,
231 };
232 let border = EdgeSizes {
233 top: if style.border_top_style != BorderStyle::None {
234 style.border_top_width
235 } else {
236 0.0
237 },
238 right: if style.border_right_style != BorderStyle::None {
239 style.border_right_width
240 } else {
241 0.0
242 },
243 bottom: if style.border_bottom_style != BorderStyle::None {
244 style.border_bottom_width
245 } else {
246 0.0
247 },
248 left: if style.border_left_style != BorderStyle::None {
249 style.border_left_width
250 } else {
251 0.0
252 },
253 };
254
255 let mut children = Vec::new();
256 for child in &styled.children {
257 if let Some(child_box) = build_box(child, doc, image_sizes) {
258 children.push(child_box);
259 }
260 }
261
262 let box_type = match style.display {
263 Display::Block => BoxType::Block(node),
264 Display::Inline => BoxType::Inline(node),
265 Display::None => unreachable!(),
266 };
267
268 if style.display == Display::Block {
269 children = normalize_children(children, style);
270 }
271
272 let mut b = LayoutBox::new(box_type, style);
273 b.margin = margin;
274 b.padding = padding;
275 b.border = border;
276 b.children = children;
277
278 // Check for replaced element (e.g., <img>).
279 if let Some(&(w, h)) = image_sizes.get(&node) {
280 b.replaced_size = Some((w, h));
281 }
282
283 Some(b)
284 }
285 NodeData::Text { data } => {
286 let collapsed = collapse_whitespace(data);
287 if collapsed.is_empty() {
288 return None;
289 }
290 Some(LayoutBox::new(
291 BoxType::TextRun {
292 node,
293 text: collapsed,
294 },
295 style,
296 ))
297 }
298 NodeData::Comment { .. } => None,
299 }
300}
301
302/// Collapse runs of whitespace to a single space.
303fn collapse_whitespace(s: &str) -> String {
304 let mut result = String::new();
305 let mut in_ws = false;
306 for ch in s.chars() {
307 if ch.is_whitespace() {
308 if !in_ws {
309 result.push(' ');
310 }
311 in_ws = true;
312 } else {
313 in_ws = false;
314 result.push(ch);
315 }
316 }
317 result
318}
319
320/// If a block container has a mix of block-level and inline-level children,
321/// wrap consecutive inline runs in anonymous block boxes.
322fn normalize_children(children: Vec<LayoutBox>, parent_style: &ComputedStyle) -> Vec<LayoutBox> {
323 if children.is_empty() {
324 return children;
325 }
326
327 let has_block = children.iter().any(is_block_level);
328 if !has_block {
329 return children;
330 }
331
332 let has_inline = children.iter().any(|c| !is_block_level(c));
333 if !has_inline {
334 return children;
335 }
336
337 let mut result = Vec::new();
338 let mut inline_group: Vec<LayoutBox> = Vec::new();
339
340 for child in children {
341 if is_block_level(&child) {
342 if !inline_group.is_empty() {
343 let mut anon = LayoutBox::new(BoxType::Anonymous, parent_style);
344 anon.children = std::mem::take(&mut inline_group);
345 result.push(anon);
346 }
347 result.push(child);
348 } else {
349 inline_group.push(child);
350 }
351 }
352
353 if !inline_group.is_empty() {
354 let mut anon = LayoutBox::new(BoxType::Anonymous, parent_style);
355 anon.children = inline_group;
356 result.push(anon);
357 }
358
359 result
360}
361
362fn is_block_level(b: &LayoutBox) -> bool {
363 matches!(b.box_type, BoxType::Block(_) | BoxType::Anonymous)
364}
365
366// ---------------------------------------------------------------------------
367// Layout algorithm
368// ---------------------------------------------------------------------------
369
370/// Position and size a layout box within `available_width` at position (`x`, `y`).
371fn compute_layout(
372 b: &mut LayoutBox,
373 x: f32,
374 y: f32,
375 available_width: f32,
376 font: &Font,
377 doc: &Document,
378) {
379 let content_x = x + b.margin.left + b.border.left + b.padding.left;
380 let content_y = y + b.margin.top + b.border.top + b.padding.top;
381 let content_width = (available_width
382 - b.margin.left
383 - b.margin.right
384 - b.border.left
385 - b.border.right
386 - b.padding.left
387 - b.padding.right)
388 .max(0.0);
389
390 b.rect.x = content_x;
391 b.rect.y = content_y;
392 b.rect.width = content_width;
393
394 // Replaced elements (e.g., <img>) have intrinsic dimensions.
395 if let Some((rw, rh)) = b.replaced_size {
396 // Use CSS width/height if specified, otherwise use replaced dimensions.
397 // Content width is the minimum of replaced width and available width.
398 b.rect.width = rw.min(content_width);
399 b.rect.height = rh;
400 return;
401 }
402
403 match &b.box_type {
404 BoxType::Block(_) | BoxType::Anonymous => {
405 if has_block_children(b) {
406 layout_block_children(b, font, doc);
407 } else {
408 layout_inline_children(b, font, doc);
409 }
410 }
411 BoxType::TextRun { .. } | BoxType::Inline(_) => {
412 // Handled by the parent's inline layout.
413 }
414 }
415}
416
417fn has_block_children(b: &LayoutBox) -> bool {
418 b.children.iter().any(is_block_level)
419}
420
421/// Lay out block-level children: stack them vertically.
422fn layout_block_children(parent: &mut LayoutBox, font: &Font, doc: &Document) {
423 let content_x = parent.rect.x;
424 let content_width = parent.rect.width;
425 let mut cursor_y = parent.rect.y;
426
427 for child in &mut parent.children {
428 compute_layout(child, content_x, cursor_y, content_width, font, doc);
429 cursor_y += child.margin_box_height();
430 }
431
432 parent.rect.height = cursor_y - parent.rect.y;
433}
434
435// ---------------------------------------------------------------------------
436// Inline formatting context
437// ---------------------------------------------------------------------------
438
439/// An inline item produced by flattening the inline tree.
440enum InlineItemKind {
441 /// A word of text with associated styling.
442 Word {
443 text: String,
444 font_size: f32,
445 color: Color,
446 text_decoration: TextDecoration,
447 background_color: Color,
448 },
449 /// Whitespace between words.
450 Space { font_size: f32 },
451 /// Forced line break (`<br>`).
452 ForcedBreak,
453 /// Start of an inline box (for margin/padding/border tracking).
454 InlineStart {
455 margin_left: f32,
456 padding_left: f32,
457 border_left: f32,
458 },
459 /// End of an inline box.
460 InlineEnd {
461 margin_right: f32,
462 padding_right: f32,
463 border_right: f32,
464 },
465}
466
467/// A pending fragment on the current line.
468struct PendingFragment {
469 text: String,
470 x: f32,
471 width: f32,
472 font_size: f32,
473 color: Color,
474 text_decoration: TextDecoration,
475 background_color: Color,
476}
477
478/// Flatten the inline children tree into a sequence of items.
479fn flatten_inline_tree(children: &[LayoutBox], doc: &Document, items: &mut Vec<InlineItemKind>) {
480 for child in children {
481 match &child.box_type {
482 BoxType::TextRun { text, .. } => {
483 let words = split_into_words(text);
484 for segment in words {
485 match segment {
486 WordSegment::Word(w) => {
487 items.push(InlineItemKind::Word {
488 text: w,
489 font_size: child.font_size,
490 color: child.color,
491 text_decoration: child.text_decoration,
492 background_color: child.background_color,
493 });
494 }
495 WordSegment::Space => {
496 items.push(InlineItemKind::Space {
497 font_size: child.font_size,
498 });
499 }
500 }
501 }
502 }
503 BoxType::Inline(node_id) => {
504 if let NodeData::Element { tag_name, .. } = doc.node_data(*node_id) {
505 if tag_name == "br" {
506 items.push(InlineItemKind::ForcedBreak);
507 continue;
508 }
509 }
510
511 items.push(InlineItemKind::InlineStart {
512 margin_left: child.margin.left,
513 padding_left: child.padding.left,
514 border_left: child.border.left,
515 });
516
517 flatten_inline_tree(&child.children, doc, items);
518
519 items.push(InlineItemKind::InlineEnd {
520 margin_right: child.margin.right,
521 padding_right: child.padding.right,
522 border_right: child.border.right,
523 });
524 }
525 _ => {}
526 }
527 }
528}
529
530enum WordSegment {
531 Word(String),
532 Space,
533}
534
535/// Split text into alternating words and spaces.
536fn split_into_words(text: &str) -> Vec<WordSegment> {
537 let mut segments = Vec::new();
538 let mut current_word = String::new();
539
540 for ch in text.chars() {
541 if ch == ' ' {
542 if !current_word.is_empty() {
543 segments.push(WordSegment::Word(std::mem::take(&mut current_word)));
544 }
545 segments.push(WordSegment::Space);
546 } else {
547 current_word.push(ch);
548 }
549 }
550
551 if !current_word.is_empty() {
552 segments.push(WordSegment::Word(current_word));
553 }
554
555 segments
556}
557
558/// Lay out inline children using a proper inline formatting context.
559fn layout_inline_children(parent: &mut LayoutBox, font: &Font, doc: &Document) {
560 let available_width = parent.rect.width;
561 let text_align = parent.text_align;
562 let line_height = parent.line_height;
563
564 let mut items = Vec::new();
565 flatten_inline_tree(&parent.children, doc, &mut items);
566
567 if items.is_empty() {
568 parent.rect.height = 0.0;
569 return;
570 }
571
572 // Process items into line boxes.
573 let mut all_lines: Vec<Vec<PendingFragment>> = Vec::new();
574 let mut current_line: Vec<PendingFragment> = Vec::new();
575 let mut cursor_x: f32 = 0.0;
576
577 for item in &items {
578 match item {
579 InlineItemKind::Word {
580 text,
581 font_size,
582 color,
583 text_decoration,
584 background_color,
585 } => {
586 let word_width = measure_text_width(font, text, *font_size);
587
588 // If this word doesn't fit and the line isn't empty, break.
589 if cursor_x > 0.0 && cursor_x + word_width > available_width {
590 all_lines.push(std::mem::take(&mut current_line));
591 cursor_x = 0.0;
592 }
593
594 current_line.push(PendingFragment {
595 text: text.clone(),
596 x: cursor_x,
597 width: word_width,
598 font_size: *font_size,
599 color: *color,
600 text_decoration: *text_decoration,
601 background_color: *background_color,
602 });
603 cursor_x += word_width;
604 }
605 InlineItemKind::Space { font_size } => {
606 // Only add space if we have content on the line.
607 if !current_line.is_empty() {
608 let space_width = measure_text_width(font, " ", *font_size);
609 if cursor_x + space_width <= available_width {
610 cursor_x += space_width;
611 }
612 }
613 }
614 InlineItemKind::ForcedBreak => {
615 all_lines.push(std::mem::take(&mut current_line));
616 cursor_x = 0.0;
617 }
618 InlineItemKind::InlineStart {
619 margin_left,
620 padding_left,
621 border_left,
622 } => {
623 cursor_x += margin_left + padding_left + border_left;
624 }
625 InlineItemKind::InlineEnd {
626 margin_right,
627 padding_right,
628 border_right,
629 } => {
630 cursor_x += margin_right + padding_right + border_right;
631 }
632 }
633 }
634
635 // Flush the last line.
636 if !current_line.is_empty() {
637 all_lines.push(current_line);
638 }
639
640 if all_lines.is_empty() {
641 parent.rect.height = 0.0;
642 return;
643 }
644
645 // Position lines vertically and apply text-align.
646 let mut text_lines = Vec::new();
647 let mut y = parent.rect.y;
648 let num_lines = all_lines.len();
649
650 for (line_idx, line_fragments) in all_lines.iter().enumerate() {
651 if line_fragments.is_empty() {
652 y += line_height;
653 continue;
654 }
655
656 // Compute line width from last fragment.
657 let line_width = match line_fragments.last() {
658 Some(last) => last.x + last.width,
659 None => 0.0,
660 };
661
662 // Compute text-align offset.
663 let is_last_line = line_idx == num_lines - 1;
664 let align_offset =
665 compute_align_offset(text_align, available_width, line_width, is_last_line);
666
667 for frag in line_fragments {
668 text_lines.push(TextLine {
669 text: frag.text.clone(),
670 x: parent.rect.x + frag.x + align_offset,
671 y,
672 width: frag.width,
673 font_size: frag.font_size,
674 color: frag.color,
675 text_decoration: frag.text_decoration,
676 background_color: frag.background_color,
677 });
678 }
679
680 y += line_height;
681 }
682
683 parent.rect.height = num_lines as f32 * line_height;
684 parent.lines = text_lines;
685}
686
687/// Compute the horizontal offset for text alignment.
688fn compute_align_offset(
689 align: TextAlign,
690 available_width: f32,
691 line_width: f32,
692 is_last_line: bool,
693) -> f32 {
694 let extra_space = (available_width - line_width).max(0.0);
695 match align {
696 TextAlign::Left => 0.0,
697 TextAlign::Center => extra_space / 2.0,
698 TextAlign::Right => extra_space,
699 TextAlign::Justify => {
700 // Don't justify the last line (CSS spec behavior).
701 if is_last_line {
702 0.0
703 } else {
704 // For justify, we shift the whole line by 0 — the actual distribution
705 // of space between words would need per-word spacing. For now, treat
706 // as left-aligned; full justify support is a future enhancement.
707 0.0
708 }
709 }
710 }
711}
712
713// ---------------------------------------------------------------------------
714// Text measurement
715// ---------------------------------------------------------------------------
716
717/// Measure the total advance width of a text string at the given font size.
718fn measure_text_width(font: &Font, text: &str, font_size: f32) -> f32 {
719 let shaped = font.shape_text(text, font_size);
720 match shaped.last() {
721 Some(last) => last.x_offset + last.x_advance,
722 None => 0.0,
723 }
724}
725
726// ---------------------------------------------------------------------------
727// Public API
728// ---------------------------------------------------------------------------
729
730/// Build and lay out from a styled tree (produced by `resolve_styles`).
731///
732/// Returns a `LayoutTree` with positioned boxes ready for rendering.
733pub fn layout(
734 styled_root: &StyledNode,
735 doc: &Document,
736 viewport_width: f32,
737 _viewport_height: f32,
738 font: &Font,
739 image_sizes: &HashMap<NodeId, (f32, f32)>,
740) -> LayoutTree {
741 let mut root = match build_box(styled_root, doc, image_sizes) {
742 Some(b) => b,
743 None => {
744 return LayoutTree {
745 root: LayoutBox::new(BoxType::Anonymous, &ComputedStyle::default()),
746 width: viewport_width,
747 height: 0.0,
748 };
749 }
750 };
751
752 compute_layout(&mut root, 0.0, 0.0, viewport_width, font, doc);
753
754 let height = root.margin_box_height();
755 LayoutTree {
756 root,
757 width: viewport_width,
758 height,
759 }
760}
761
762#[cfg(test)]
763mod tests {
764 use super::*;
765 use we_dom::Document;
766 use we_style::computed::{extract_stylesheets, resolve_styles};
767
768 fn test_font() -> Font {
769 let paths = [
770 "/System/Library/Fonts/Geneva.ttf",
771 "/System/Library/Fonts/Monaco.ttf",
772 ];
773 for path in &paths {
774 let p = std::path::Path::new(path);
775 if p.exists() {
776 return Font::from_file(p).expect("failed to parse font");
777 }
778 }
779 panic!("no test font found");
780 }
781
782 fn layout_doc(doc: &Document) -> LayoutTree {
783 let font = test_font();
784 let sheets = extract_stylesheets(doc);
785 let styled = resolve_styles(doc, &sheets).unwrap();
786 layout(&styled, doc, 800.0, 600.0, &font, &HashMap::new())
787 }
788
789 #[test]
790 fn empty_document() {
791 let doc = Document::new();
792 let font = test_font();
793 let sheets = extract_stylesheets(&doc);
794 let styled = resolve_styles(&doc, &sheets);
795 if let Some(styled) = styled {
796 let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
797 assert_eq!(tree.width, 800.0);
798 }
799 }
800
801 #[test]
802 fn single_paragraph() {
803 let mut doc = Document::new();
804 let root = doc.root();
805 let html = doc.create_element("html");
806 let body = doc.create_element("body");
807 let p = doc.create_element("p");
808 let text = doc.create_text("Hello world");
809 doc.append_child(root, html);
810 doc.append_child(html, body);
811 doc.append_child(body, p);
812 doc.append_child(p, text);
813
814 let tree = layout_doc(&doc);
815
816 assert!(matches!(tree.root.box_type, BoxType::Block(_)));
817
818 let body_box = &tree.root.children[0];
819 assert!(matches!(body_box.box_type, BoxType::Block(_)));
820
821 let p_box = &body_box.children[0];
822 assert!(matches!(p_box.box_type, BoxType::Block(_)));
823
824 assert!(!p_box.lines.is_empty(), "p should have text fragments");
825
826 // Collect all text on the first visual line.
827 let first_y = p_box.lines[0].y;
828 let line_text: String = p_box
829 .lines
830 .iter()
831 .filter(|l| (l.y - first_y).abs() < 0.01)
832 .map(|l| l.text.as_str())
833 .collect::<Vec<_>>()
834 .join(" ");
835 assert!(
836 line_text.contains("Hello") && line_text.contains("world"),
837 "line should contain Hello and world, got: {line_text}"
838 );
839
840 assert_eq!(p_box.margin.top, 16.0);
841 assert_eq!(p_box.margin.bottom, 16.0);
842 }
843
844 #[test]
845 fn blocks_stack_vertically() {
846 let mut doc = Document::new();
847 let root = doc.root();
848 let html = doc.create_element("html");
849 let body = doc.create_element("body");
850 let p1 = doc.create_element("p");
851 let t1 = doc.create_text("First");
852 let p2 = doc.create_element("p");
853 let t2 = doc.create_text("Second");
854 doc.append_child(root, html);
855 doc.append_child(html, body);
856 doc.append_child(body, p1);
857 doc.append_child(p1, t1);
858 doc.append_child(body, p2);
859 doc.append_child(p2, t2);
860
861 let tree = layout_doc(&doc);
862 let body_box = &tree.root.children[0];
863 let first = &body_box.children[0];
864 let second = &body_box.children[1];
865
866 assert!(
867 second.rect.y > first.rect.y,
868 "second p (y={}) should be below first p (y={})",
869 second.rect.y,
870 first.rect.y
871 );
872 }
873
874 #[test]
875 fn heading_larger_than_body() {
876 let mut doc = Document::new();
877 let root = doc.root();
878 let html = doc.create_element("html");
879 let body = doc.create_element("body");
880 let h1 = doc.create_element("h1");
881 let h1_text = doc.create_text("Title");
882 let p = doc.create_element("p");
883 let p_text = doc.create_text("Text");
884 doc.append_child(root, html);
885 doc.append_child(html, body);
886 doc.append_child(body, h1);
887 doc.append_child(h1, h1_text);
888 doc.append_child(body, p);
889 doc.append_child(p, p_text);
890
891 let tree = layout_doc(&doc);
892 let body_box = &tree.root.children[0];
893 let h1_box = &body_box.children[0];
894 let p_box = &body_box.children[1];
895
896 assert!(
897 h1_box.font_size > p_box.font_size,
898 "h1 font_size ({}) should be > p font_size ({})",
899 h1_box.font_size,
900 p_box.font_size
901 );
902 assert_eq!(h1_box.font_size, 32.0);
903
904 assert!(
905 h1_box.rect.height > p_box.rect.height,
906 "h1 height ({}) should be > p height ({})",
907 h1_box.rect.height,
908 p_box.rect.height
909 );
910 }
911
912 #[test]
913 fn body_has_default_margin() {
914 let mut doc = Document::new();
915 let root = doc.root();
916 let html = doc.create_element("html");
917 let body = doc.create_element("body");
918 let p = doc.create_element("p");
919 let text = doc.create_text("Test");
920 doc.append_child(root, html);
921 doc.append_child(html, body);
922 doc.append_child(body, p);
923 doc.append_child(p, text);
924
925 let tree = layout_doc(&doc);
926 let body_box = &tree.root.children[0];
927
928 assert_eq!(body_box.margin.top, 8.0);
929 assert_eq!(body_box.margin.right, 8.0);
930 assert_eq!(body_box.margin.bottom, 8.0);
931 assert_eq!(body_box.margin.left, 8.0);
932
933 assert_eq!(body_box.rect.x, 8.0);
934 assert_eq!(body_box.rect.y, 8.0);
935 }
936
937 #[test]
938 fn text_wraps_at_container_width() {
939 let mut doc = Document::new();
940 let root = doc.root();
941 let html = doc.create_element("html");
942 let body = doc.create_element("body");
943 let p = doc.create_element("p");
944 let text =
945 doc.create_text("The quick brown fox jumps over the lazy dog and more words to wrap");
946 doc.append_child(root, html);
947 doc.append_child(html, body);
948 doc.append_child(body, p);
949 doc.append_child(p, text);
950
951 let font = test_font();
952 let sheets = extract_stylesheets(&doc);
953 let styled = resolve_styles(&doc, &sheets).unwrap();
954 let tree = layout(&styled, &doc, 100.0, 600.0, &font, &HashMap::new());
955 let body_box = &tree.root.children[0];
956 let p_box = &body_box.children[0];
957
958 // Count distinct y-positions to count visual lines.
959 let mut ys: Vec<f32> = p_box.lines.iter().map(|l| l.y).collect();
960 ys.sort_by(|a, b| a.partial_cmp(b).unwrap());
961 ys.dedup_by(|a, b| (*a - *b).abs() < 0.01);
962
963 assert!(
964 ys.len() > 1,
965 "text should wrap to multiple lines, got {} visual lines",
966 ys.len()
967 );
968 }
969
970 #[test]
971 fn layout_produces_positive_dimensions() {
972 let mut doc = Document::new();
973 let root = doc.root();
974 let html = doc.create_element("html");
975 let body = doc.create_element("body");
976 let div = doc.create_element("div");
977 let text = doc.create_text("Content");
978 doc.append_child(root, html);
979 doc.append_child(html, body);
980 doc.append_child(body, div);
981 doc.append_child(div, text);
982
983 let tree = layout_doc(&doc);
984
985 for b in tree.iter() {
986 assert!(b.rect.width >= 0.0, "width should be >= 0");
987 assert!(b.rect.height >= 0.0, "height should be >= 0");
988 }
989
990 assert!(tree.height > 0.0, "layout height should be > 0");
991 }
992
993 #[test]
994 fn head_is_hidden() {
995 let mut doc = Document::new();
996 let root = doc.root();
997 let html = doc.create_element("html");
998 let head = doc.create_element("head");
999 let title = doc.create_element("title");
1000 let title_text = doc.create_text("Page Title");
1001 let body = doc.create_element("body");
1002 let p = doc.create_element("p");
1003 let p_text = doc.create_text("Visible");
1004 doc.append_child(root, html);
1005 doc.append_child(html, head);
1006 doc.append_child(head, title);
1007 doc.append_child(title, title_text);
1008 doc.append_child(html, body);
1009 doc.append_child(body, p);
1010 doc.append_child(p, p_text);
1011
1012 let tree = layout_doc(&doc);
1013
1014 assert_eq!(
1015 tree.root.children.len(),
1016 1,
1017 "html should have 1 child (body), head should be hidden"
1018 );
1019 }
1020
1021 #[test]
1022 fn mixed_block_and_inline() {
1023 let mut doc = Document::new();
1024 let root = doc.root();
1025 let html = doc.create_element("html");
1026 let body = doc.create_element("body");
1027 let div = doc.create_element("div");
1028 let text1 = doc.create_text("Text");
1029 let p = doc.create_element("p");
1030 let p_text = doc.create_text("Block");
1031 let text2 = doc.create_text("More");
1032 doc.append_child(root, html);
1033 doc.append_child(html, body);
1034 doc.append_child(body, div);
1035 doc.append_child(div, text1);
1036 doc.append_child(div, p);
1037 doc.append_child(p, p_text);
1038 doc.append_child(div, text2);
1039
1040 let tree = layout_doc(&doc);
1041 let body_box = &tree.root.children[0];
1042 let div_box = &body_box.children[0];
1043
1044 assert_eq!(
1045 div_box.children.len(),
1046 3,
1047 "div should have 3 children (anon, block, anon), got {}",
1048 div_box.children.len()
1049 );
1050
1051 assert!(matches!(div_box.children[0].box_type, BoxType::Anonymous));
1052 assert!(matches!(div_box.children[1].box_type, BoxType::Block(_)));
1053 assert!(matches!(div_box.children[2].box_type, BoxType::Anonymous));
1054 }
1055
1056 #[test]
1057 fn inline_elements_contribute_text() {
1058 let mut doc = Document::new();
1059 let root = doc.root();
1060 let html = doc.create_element("html");
1061 let body = doc.create_element("body");
1062 let p = doc.create_element("p");
1063 let t1 = doc.create_text("Hello ");
1064 let em = doc.create_element("em");
1065 let t2 = doc.create_text("world");
1066 let t3 = doc.create_text("!");
1067 doc.append_child(root, html);
1068 doc.append_child(html, body);
1069 doc.append_child(body, p);
1070 doc.append_child(p, t1);
1071 doc.append_child(p, em);
1072 doc.append_child(em, t2);
1073 doc.append_child(p, t3);
1074
1075 let tree = layout_doc(&doc);
1076 let body_box = &tree.root.children[0];
1077 let p_box = &body_box.children[0];
1078
1079 assert!(!p_box.lines.is_empty());
1080
1081 let first_y = p_box.lines[0].y;
1082 let line_texts: Vec<&str> = p_box
1083 .lines
1084 .iter()
1085 .filter(|l| (l.y - first_y).abs() < 0.01)
1086 .map(|l| l.text.as_str())
1087 .collect();
1088 let combined = line_texts.join("");
1089 assert!(
1090 combined.contains("Hello") && combined.contains("world") && combined.contains("!"),
1091 "line should contain all text, got: {combined}"
1092 );
1093 }
1094
1095 #[test]
1096 fn collapse_whitespace_works() {
1097 assert_eq!(collapse_whitespace("hello world"), "hello world");
1098 assert_eq!(collapse_whitespace(" spaces "), " spaces ");
1099 assert_eq!(collapse_whitespace("\n\ttabs\n"), " tabs ");
1100 assert_eq!(collapse_whitespace("no-extra"), "no-extra");
1101 assert_eq!(collapse_whitespace(" "), " ");
1102 }
1103
1104 #[test]
1105 fn content_width_respects_body_margin() {
1106 let mut doc = Document::new();
1107 let root = doc.root();
1108 let html = doc.create_element("html");
1109 let body = doc.create_element("body");
1110 let div = doc.create_element("div");
1111 let text = doc.create_text("Content");
1112 doc.append_child(root, html);
1113 doc.append_child(html, body);
1114 doc.append_child(body, div);
1115 doc.append_child(div, text);
1116
1117 let font = test_font();
1118 let sheets = extract_stylesheets(&doc);
1119 let styled = resolve_styles(&doc, &sheets).unwrap();
1120 let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
1121 let body_box = &tree.root.children[0];
1122
1123 assert_eq!(body_box.rect.width, 784.0);
1124
1125 let div_box = &body_box.children[0];
1126 assert_eq!(div_box.rect.width, 784.0);
1127 }
1128
1129 #[test]
1130 fn multiple_heading_levels() {
1131 let mut doc = Document::new();
1132 let root = doc.root();
1133 let html = doc.create_element("html");
1134 let body = doc.create_element("body");
1135 doc.append_child(root, html);
1136 doc.append_child(html, body);
1137
1138 let tags = ["h1", "h2", "h3"];
1139 for tag in &tags {
1140 let h = doc.create_element(tag);
1141 let t = doc.create_text(tag);
1142 doc.append_child(body, h);
1143 doc.append_child(h, t);
1144 }
1145
1146 let tree = layout_doc(&doc);
1147 let body_box = &tree.root.children[0];
1148
1149 let h1 = &body_box.children[0];
1150 let h2 = &body_box.children[1];
1151 let h3 = &body_box.children[2];
1152 assert!(h1.font_size > h2.font_size);
1153 assert!(h2.font_size > h3.font_size);
1154
1155 assert!(h2.rect.y > h1.rect.y);
1156 assert!(h3.rect.y > h2.rect.y);
1157 }
1158
1159 #[test]
1160 fn layout_tree_iteration() {
1161 let mut doc = Document::new();
1162 let root = doc.root();
1163 let html = doc.create_element("html");
1164 let body = doc.create_element("body");
1165 let p = doc.create_element("p");
1166 let text = doc.create_text("Test");
1167 doc.append_child(root, html);
1168 doc.append_child(html, body);
1169 doc.append_child(body, p);
1170 doc.append_child(p, text);
1171
1172 let tree = layout_doc(&doc);
1173 let count = tree.iter().count();
1174 assert!(count >= 3, "should have at least html, body, p boxes");
1175 }
1176
1177 #[test]
1178 fn css_style_affects_layout() {
1179 let html_str = r#"<!DOCTYPE html>
1180<html>
1181<head>
1182<style>
1183p { margin-top: 50px; margin-bottom: 50px; }
1184</style>
1185</head>
1186<body>
1187<p>First</p>
1188<p>Second</p>
1189</body>
1190</html>"#;
1191 let doc = we_html::parse_html(html_str);
1192 let font = test_font();
1193 let sheets = extract_stylesheets(&doc);
1194 let styled = resolve_styles(&doc, &sheets).unwrap();
1195 let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
1196
1197 let body_box = &tree.root.children[0];
1198 let first = &body_box.children[0];
1199 let second = &body_box.children[1];
1200
1201 assert_eq!(first.margin.top, 50.0);
1202 assert_eq!(first.margin.bottom, 50.0);
1203
1204 assert!(second.rect.y > first.rect.y + 100.0);
1205 }
1206
1207 #[test]
1208 fn inline_style_affects_layout() {
1209 let html_str = r#"<!DOCTYPE html>
1210<html>
1211<body>
1212<div style="padding-top: 20px; padding-bottom: 20px;">
1213<p>Content</p>
1214</div>
1215</body>
1216</html>"#;
1217 let doc = we_html::parse_html(html_str);
1218 let font = test_font();
1219 let sheets = extract_stylesheets(&doc);
1220 let styled = resolve_styles(&doc, &sheets).unwrap();
1221 let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
1222
1223 let body_box = &tree.root.children[0];
1224 let div_box = &body_box.children[0];
1225
1226 assert_eq!(div_box.padding.top, 20.0);
1227 assert_eq!(div_box.padding.bottom, 20.0);
1228 }
1229
1230 #[test]
1231 fn css_color_propagates_to_layout() {
1232 let html_str = r#"<!DOCTYPE html>
1233<html>
1234<head><style>p { color: red; background-color: blue; }</style></head>
1235<body><p>Colored</p></body>
1236</html>"#;
1237 let doc = we_html::parse_html(html_str);
1238 let font = test_font();
1239 let sheets = extract_stylesheets(&doc);
1240 let styled = resolve_styles(&doc, &sheets).unwrap();
1241 let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
1242
1243 let body_box = &tree.root.children[0];
1244 let p_box = &body_box.children[0];
1245
1246 assert_eq!(p_box.color, Color::rgb(255, 0, 0));
1247 assert_eq!(p_box.background_color, Color::rgb(0, 0, 255));
1248 }
1249
1250 // --- New inline layout tests ---
1251
1252 #[test]
1253 fn inline_elements_have_per_fragment_styling() {
1254 let html_str = r#"<!DOCTYPE html>
1255<html>
1256<head><style>em { color: red; }</style></head>
1257<body><p>Hello <em>world</em></p></body>
1258</html>"#;
1259 let doc = we_html::parse_html(html_str);
1260 let font = test_font();
1261 let sheets = extract_stylesheets(&doc);
1262 let styled = resolve_styles(&doc, &sheets).unwrap();
1263 let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
1264
1265 let body_box = &tree.root.children[0];
1266 let p_box = &body_box.children[0];
1267
1268 let colors: Vec<Color> = p_box.lines.iter().map(|l| l.color).collect();
1269 assert!(
1270 colors.iter().any(|c| *c == Color::rgb(0, 0, 0)),
1271 "should have black text"
1272 );
1273 assert!(
1274 colors.iter().any(|c| *c == Color::rgb(255, 0, 0)),
1275 "should have red text from <em>"
1276 );
1277 }
1278
1279 #[test]
1280 fn br_element_forces_line_break() {
1281 let html_str = r#"<!DOCTYPE html>
1282<html>
1283<body><p>Line one<br>Line two</p></body>
1284</html>"#;
1285 let doc = we_html::parse_html(html_str);
1286 let font = test_font();
1287 let sheets = extract_stylesheets(&doc);
1288 let styled = resolve_styles(&doc, &sheets).unwrap();
1289 let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
1290
1291 let body_box = &tree.root.children[0];
1292 let p_box = &body_box.children[0];
1293
1294 let mut ys: Vec<f32> = p_box.lines.iter().map(|l| l.y).collect();
1295 ys.sort_by(|a, b| a.partial_cmp(b).unwrap());
1296 ys.dedup_by(|a, b| (*a - *b).abs() < 0.01);
1297
1298 assert!(
1299 ys.len() >= 2,
1300 "<br> should produce 2 visual lines, got {}",
1301 ys.len()
1302 );
1303 }
1304
1305 #[test]
1306 fn text_align_center() {
1307 let html_str = r#"<!DOCTYPE html>
1308<html>
1309<head><style>p { text-align: center; }</style></head>
1310<body><p>Hi</p></body>
1311</html>"#;
1312 let doc = we_html::parse_html(html_str);
1313 let font = test_font();
1314 let sheets = extract_stylesheets(&doc);
1315 let styled = resolve_styles(&doc, &sheets).unwrap();
1316 let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
1317
1318 let body_box = &tree.root.children[0];
1319 let p_box = &body_box.children[0];
1320
1321 assert!(!p_box.lines.is_empty());
1322 let first = &p_box.lines[0];
1323 // Center-aligned: text should be noticeably offset from content x.
1324 assert!(
1325 first.x > p_box.rect.x + 10.0,
1326 "center-aligned text x ({}) should be offset from content x ({})",
1327 first.x,
1328 p_box.rect.x
1329 );
1330 }
1331
1332 #[test]
1333 fn text_align_right() {
1334 let html_str = r#"<!DOCTYPE html>
1335<html>
1336<head><style>p { text-align: right; }</style></head>
1337<body><p>Hi</p></body>
1338</html>"#;
1339 let doc = we_html::parse_html(html_str);
1340 let font = test_font();
1341 let sheets = extract_stylesheets(&doc);
1342 let styled = resolve_styles(&doc, &sheets).unwrap();
1343 let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
1344
1345 let body_box = &tree.root.children[0];
1346 let p_box = &body_box.children[0];
1347
1348 assert!(!p_box.lines.is_empty());
1349 let first = &p_box.lines[0];
1350 let right_edge = p_box.rect.x + p_box.rect.width;
1351 assert!(
1352 (first.x + first.width - right_edge).abs() < 1.0,
1353 "right-aligned text end ({}) should be near right edge ({})",
1354 first.x + first.width,
1355 right_edge
1356 );
1357 }
1358
1359 #[test]
1360 fn inline_padding_offsets_text() {
1361 let html_str = r#"<!DOCTYPE html>
1362<html>
1363<head><style>span { padding-left: 20px; padding-right: 20px; }</style></head>
1364<body><p>A<span>B</span>C</p></body>
1365</html>"#;
1366 let doc = we_html::parse_html(html_str);
1367 let font = test_font();
1368 let sheets = extract_stylesheets(&doc);
1369 let styled = resolve_styles(&doc, &sheets).unwrap();
1370 let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
1371
1372 let body_box = &tree.root.children[0];
1373 let p_box = &body_box.children[0];
1374
1375 // Should have at least 3 fragments: A, B, C
1376 assert!(
1377 p_box.lines.len() >= 3,
1378 "should have fragments for A, B, C, got {}",
1379 p_box.lines.len()
1380 );
1381
1382 // B should be offset by the span's padding.
1383 let a_frag = &p_box.lines[0];
1384 let b_frag = &p_box.lines[1];
1385 let gap = b_frag.x - (a_frag.x + a_frag.width);
1386 // Gap should include the 20px padding-left from the span.
1387 assert!(
1388 gap >= 19.0,
1389 "gap between A and B ({gap}) should include span padding-left (20px)"
1390 );
1391 }
1392
1393 #[test]
1394 fn text_fragments_have_correct_font_size() {
1395 let html_str = r#"<!DOCTYPE html>
1396<html>
1397<body><h1>Big</h1><p>Small</p></body>
1398</html>"#;
1399 let doc = we_html::parse_html(html_str);
1400 let font = test_font();
1401 let sheets = extract_stylesheets(&doc);
1402 let styled = resolve_styles(&doc, &sheets).unwrap();
1403 let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
1404
1405 let body_box = &tree.root.children[0];
1406 let h1_box = &body_box.children[0];
1407 let p_box = &body_box.children[1];
1408
1409 assert!(!h1_box.lines.is_empty());
1410 assert!(!p_box.lines.is_empty());
1411 assert_eq!(h1_box.lines[0].font_size, 32.0);
1412 assert_eq!(p_box.lines[0].font_size, 16.0);
1413 }
1414
1415 #[test]
1416 fn line_height_from_computed_style() {
1417 let html_str = r#"<!DOCTYPE html>
1418<html>
1419<head><style>p { line-height: 30px; }</style></head>
1420<body><p>Line one Line two Line three</p></body>
1421</html>"#;
1422 let doc = we_html::parse_html(html_str);
1423 let font = test_font();
1424 let sheets = extract_stylesheets(&doc);
1425 let styled = resolve_styles(&doc, &sheets).unwrap();
1426 // Narrow viewport to force wrapping.
1427 let tree = layout(&styled, &doc, 100.0, 600.0, &font, &HashMap::new());
1428
1429 let body_box = &tree.root.children[0];
1430 let p_box = &body_box.children[0];
1431
1432 let mut ys: Vec<f32> = p_box.lines.iter().map(|l| l.y).collect();
1433 ys.sort_by(|a, b| a.partial_cmp(b).unwrap());
1434 ys.dedup_by(|a, b| (*a - *b).abs() < 0.01);
1435
1436 if ys.len() >= 2 {
1437 let gap = ys[1] - ys[0];
1438 assert!(
1439 (gap - 30.0).abs() < 1.0,
1440 "line spacing ({gap}) should be ~30px from line-height"
1441 );
1442 }
1443 }
1444}