| 
									
										
										
										
											2019-06-21 20:54:13 +02:00
										 |  |  | #include <LibHTML/CSS/StyleSheet.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-15 23:41:15 +02:00
										 |  |  | #include <LibHTML/DOM/Document.h>
 | 
					
						
							|  |  |  | #include <LibHTML/DOM/Element.h>
 | 
					
						
							|  |  |  | #include <LibHTML/DOM/Text.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | #include <LibHTML/Dump.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-15 23:41:15 +02:00
										 |  |  | #include <LibHTML/Layout/LayoutNode.h>
 | 
					
						
							|  |  |  | #include <LibHTML/Layout/LayoutText.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  | void dump_tree(const Node& node) | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     static int indent = 0; | 
					
						
							|  |  |  |     for (int i = 0; i < indent; ++i) | 
					
						
							| 
									
										
										
										
											2019-06-16 11:28:47 +02:00
										 |  |  |         printf("  "); | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |     if (node.is_document()) { | 
					
						
							|  |  |  |         printf("*Document*\n"); | 
					
						
							|  |  |  |     } else if (node.is_element()) { | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  |         printf("<%s", static_cast<const Element&>(node).tag_name().characters()); | 
					
						
							|  |  |  |         static_cast<const Element&>(node).for_each_attribute([](auto& name, auto& value) { | 
					
						
							| 
									
										
										
										
											2019-06-15 21:08:36 +02:00
										 |  |  |             printf(" %s=%s", name.characters(), value.characters()); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         printf(">\n"); | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |     } else if (node.is_text()) { | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  |         printf("\"%s\"\n", static_cast<const Text&>(node).data().characters()); | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     ++indent; | 
					
						
							|  |  |  |     if (node.is_parent_node()) { | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  |         static_cast<const ParentNode&>(node).for_each_child([](auto& child) { | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |             dump_tree(child); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     --indent; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-16 11:28:47 +02:00
										 |  |  | void dump_tree(const LayoutNode& layout_node) | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     static int indent = 0; | 
					
						
							|  |  |  |     for (int i = 0; i < indent; ++i) | 
					
						
							| 
									
										
										
										
											2019-09-21 15:32:17 +03:00
										 |  |  |         printf("    "); | 
					
						
							| 
									
										
										
										
											2019-06-16 11:28:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     String tag_name; | 
					
						
							|  |  |  |     if (layout_node.is_anonymous()) | 
					
						
							|  |  |  |         tag_name = "(anonymous)"; | 
					
						
							|  |  |  |     else if (layout_node.node()->is_text()) | 
					
						
							|  |  |  |         tag_name = "#text"; | 
					
						
							|  |  |  |     else if (layout_node.node()->is_document()) | 
					
						
							|  |  |  |         tag_name = "#document"; | 
					
						
							|  |  |  |     else if (layout_node.node()->is_element()) | 
					
						
							|  |  |  |         tag_name = static_cast<const Element&>(*layout_node.node()).tag_name(); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         tag_name = "???"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     printf("%s {%s} at (%d,%d) size %dx%d", | 
					
						
							|  |  |  |         layout_node.class_name(), | 
					
						
							|  |  |  |         tag_name.characters(), | 
					
						
							|  |  |  |         layout_node.rect().x(), | 
					
						
							|  |  |  |         layout_node.rect().y(), | 
					
						
							|  |  |  |         layout_node.rect().width(), | 
					
						
							|  |  |  |         layout_node.rect().height()); | 
					
						
							| 
									
										
										
										
											2019-08-18 08:09:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Dump the horizontal box properties
 | 
					
						
							|  |  |  |     printf(" [%d+%d+%d %d %d+%d+%d]", | 
					
						
							|  |  |  |         layout_node.style().margin().left.to_px(), | 
					
						
							|  |  |  |         layout_node.style().border().left.to_px(), | 
					
						
							|  |  |  |         layout_node.style().padding().left.to_px(), | 
					
						
							|  |  |  |         layout_node.rect().width(), | 
					
						
							| 
									
										
										
										
											2019-08-18 08:37:53 +02:00
										 |  |  |         layout_node.style().padding().right.to_px(), | 
					
						
							| 
									
										
										
										
											2019-08-18 08:09:56 +02:00
										 |  |  |         layout_node.style().border().right.to_px(), | 
					
						
							| 
									
										
										
										
											2019-08-18 08:37:53 +02:00
										 |  |  |         layout_node.style().margin().right.to_px()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // And the vertical box properties
 | 
					
						
							|  |  |  |     printf(" [%d+%d+%d %d %d+%d+%d]", | 
					
						
							|  |  |  |         layout_node.style().margin().top.to_px(), | 
					
						
							|  |  |  |         layout_node.style().border().top.to_px(), | 
					
						
							|  |  |  |         layout_node.style().padding().top.to_px(), | 
					
						
							|  |  |  |         layout_node.rect().height(), | 
					
						
							|  |  |  |         layout_node.style().padding().bottom.to_px(), | 
					
						
							|  |  |  |         layout_node.style().border().bottom.to_px(), | 
					
						
							|  |  |  |         layout_node.style().margin().bottom.to_px()); | 
					
						
							| 
									
										
										
										
											2019-08-18 08:09:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:36:44 +03:00
										 |  |  |     if (layout_node.is_text()) { | 
					
						
							|  |  |  |         const LayoutText& layout_text = static_cast<const LayoutText&>(layout_node); | 
					
						
							|  |  |  |         printf(" \"%s\", %d runs", layout_text.text().characters(), layout_text.runs().size()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-18 08:09:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  |     printf("\n"); | 
					
						
							| 
									
										
										
										
											2019-06-21 20:54:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 15:32:17 +03:00
										 |  |  |     layout_node.style_properties().for_each_property([&](auto& key, auto& value) { | 
					
						
							| 
									
										
										
										
											2019-06-28 21:17:34 +02:00
										 |  |  |         for (int i = 0; i < indent; ++i) | 
					
						
							|  |  |  |             printf("    "); | 
					
						
							|  |  |  |         printf("  (%s: %s)\n", key.characters(), value.to_string().characters()); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-09-21 15:32:17 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-28 21:17:34 +02:00
										 |  |  |     ++indent; | 
					
						
							| 
									
										
										
										
											2019-09-21 15:32:17 +03:00
										 |  |  |     layout_node.for_each_child([](auto& child) { | 
					
						
							| 
									
										
										
										
											2019-06-28 21:17:34 +02:00
										 |  |  |         dump_tree(child); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     --indent; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 20:40:21 +02:00
										 |  |  | void dump_rule(const StyleRule& rule) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     printf("Rule:\n"); | 
					
						
							|  |  |  |     for (auto& selector : rule.selectors()) { | 
					
						
							|  |  |  |         printf("  Selector:\n"); | 
					
						
							|  |  |  |         for (auto& component : selector.components()) { | 
					
						
							|  |  |  |             const char* type_description = "Unknown"; | 
					
						
							|  |  |  |             switch (component.type) { | 
					
						
							|  |  |  |             case Selector::Component::Type::Invalid: | 
					
						
							|  |  |  |                 type_description = "Invalid"; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case Selector::Component::Type::Id: | 
					
						
							|  |  |  |                 type_description = "Id"; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case Selector::Component::Type::Class: | 
					
						
							|  |  |  |                 type_description = "Class"; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case Selector::Component::Type::TagName: | 
					
						
							|  |  |  |                 type_description = "TagName"; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             printf("    %s:%s\n", type_description, component.value.characters()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     printf("  Declarations:\n"); | 
					
						
							|  |  |  |     for (auto& declaration : rule.declarations()) { | 
					
						
							|  |  |  |         printf("    '%s': '%s'\n", declaration.property_name().characters(), declaration.value().to_string().characters()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-21 20:54:13 +02:00
										 |  |  | void dump_sheet(const StyleSheet& sheet) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     printf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size()); | 
					
						
							| 
									
										
										
										
											2019-06-25 06:31:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (auto& rule : sheet.rules()) { | 
					
						
							| 
									
										
										
										
											2019-06-27 20:40:21 +02:00
										 |  |  |         dump_rule(rule); | 
					
						
							| 
									
										
										
										
											2019-06-25 06:31:47 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-21 20:54:13 +02:00
										 |  |  | } |