2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
#include <AK/Utf8View.h>
|
2019-11-18 13:12:58 +03:00
|
|
|
#include <LibHTML/CSS/PropertyID.h>
|
2019-06-21 21:54:13 +03:00
|
|
|
#include <LibHTML/CSS/StyleSheet.h>
|
2019-10-13 00:26:47 +03:00
|
|
|
#include <LibHTML/DOM/Comment.h>
|
2019-06-16 00:41:15 +03:00
|
|
|
#include <LibHTML/DOM/Document.h>
|
2019-10-09 21:17:01 +03:00
|
|
|
#include <LibHTML/DOM/DocumentType.h>
|
2019-06-16 00:41:15 +03:00
|
|
|
#include <LibHTML/DOM/Element.h>
|
|
|
|
#include <LibHTML/DOM/Text.h>
|
2019-06-15 19:55:47 +03:00
|
|
|
#include <LibHTML/Dump.h>
|
2019-10-03 16:20:13 +03:00
|
|
|
#include <LibHTML/Layout/LayoutBlock.h>
|
2019-06-16 00:41:15 +03:00
|
|
|
#include <LibHTML/Layout/LayoutNode.h>
|
|
|
|
#include <LibHTML/Layout/LayoutText.h>
|
2019-06-15 19:55:47 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2019-06-15 23:49:44 +03:00
|
|
|
void dump_tree(const Node& node)
|
2019-06-15 19:55:47 +03:00
|
|
|
{
|
|
|
|
static int indent = 0;
|
|
|
|
for (int i = 0; i < indent; ++i)
|
2019-10-03 11:25:00 +03:00
|
|
|
dbgprintf(" ");
|
2019-10-06 21:37:39 +03:00
|
|
|
if (is<Document>(node)) {
|
2019-10-03 11:25:00 +03:00
|
|
|
dbgprintf("*Document*\n");
|
2019-10-06 21:37:39 +03:00
|
|
|
} else if (is<Element>(node)) {
|
|
|
|
dbgprintf("<%s", to<Element>(node).tag_name().characters());
|
|
|
|
to<Element>(node).for_each_attribute([](auto& name, auto& value) {
|
2019-10-03 11:25:00 +03:00
|
|
|
dbgprintf(" %s=%s", name.characters(), value.characters());
|
2019-06-15 22:08:36 +03:00
|
|
|
});
|
2019-10-03 11:25:00 +03:00
|
|
|
dbgprintf(">\n");
|
2019-10-06 21:37:39 +03:00
|
|
|
} else if (is<Text>(node)) {
|
2019-10-03 11:25:00 +03:00
|
|
|
dbgprintf("\"%s\"\n", static_cast<const Text&>(node).data().characters());
|
2019-10-09 21:17:01 +03:00
|
|
|
} else if (is<DocumentType>(node)) {
|
|
|
|
dbgprintf("<!DOCTYPE>\n");
|
2019-10-13 00:26:47 +03:00
|
|
|
} else if (is<Comment>(node)) {
|
|
|
|
dbgprintf("<!--%s-->\n", to<Comment>(node).data().characters());
|
2019-06-15 19:55:47 +03:00
|
|
|
}
|
|
|
|
++indent;
|
2019-10-06 21:37:39 +03:00
|
|
|
if (is<ParentNode>(node)) {
|
2019-06-15 23:49:44 +03:00
|
|
|
static_cast<const ParentNode&>(node).for_each_child([](auto& child) {
|
2019-06-15 19:55:47 +03:00
|
|
|
dump_tree(child);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
--indent;
|
|
|
|
}
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2019-06-16 12:28:47 +03:00
|
|
|
void dump_tree(const LayoutNode& layout_node)
|
2019-06-15 23:49:44 +03:00
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
static size_t indent = 0;
|
|
|
|
for (size_t i = 0; i < indent; ++i)
|
2019-10-03 11:25:00 +03:00
|
|
|
dbgprintf(" ");
|
2019-06-16 12:28:47 +03:00
|
|
|
|
|
|
|
String tag_name;
|
|
|
|
if (layout_node.is_anonymous())
|
|
|
|
tag_name = "(anonymous)";
|
2019-10-06 21:37:39 +03:00
|
|
|
else if (is<Text>(layout_node.node()))
|
2019-06-16 12:28:47 +03:00
|
|
|
tag_name = "#text";
|
2019-10-06 21:37:39 +03:00
|
|
|
else if (is<Document>(layout_node.node()))
|
2019-06-16 12:28:47 +03:00
|
|
|
tag_name = "#document";
|
2019-10-06 21:37:39 +03:00
|
|
|
else if (is<Element>(layout_node.node()))
|
|
|
|
tag_name = to<Element>(*layout_node.node()).tag_name();
|
2019-06-16 12:28:47 +03:00
|
|
|
else
|
|
|
|
tag_name = "???";
|
|
|
|
|
2019-10-15 17:48:38 +03:00
|
|
|
if (!layout_node.is_box()) {
|
|
|
|
dbgprintf("%s {%s}\n", layout_node.class_name(), tag_name.characters());
|
|
|
|
} else {
|
|
|
|
auto& layout_box = to<LayoutBox>(layout_node);
|
2019-11-18 22:26:15 +03:00
|
|
|
dbgprintf("%s {%s} at (%g,%g) size %gx%g",
|
2019-10-15 17:48:38 +03:00
|
|
|
layout_box.class_name(),
|
|
|
|
tag_name.characters(),
|
|
|
|
layout_box.x(),
|
|
|
|
layout_box.y(),
|
|
|
|
layout_box.width(),
|
|
|
|
layout_box.height());
|
2019-08-18 09:09:56 +03:00
|
|
|
|
2019-10-15 17:48:38 +03:00
|
|
|
// Dump the horizontal box properties
|
2019-11-18 22:26:15 +03:00
|
|
|
dbgprintf(" [%g+%g+%g %g %g+%g+%g]",
|
2019-10-15 17:48:38 +03:00
|
|
|
layout_box.box_model().margin().left.to_px(),
|
|
|
|
layout_box.box_model().border().left.to_px(),
|
|
|
|
layout_box.box_model().padding().left.to_px(),
|
|
|
|
layout_box.width(),
|
|
|
|
layout_box.box_model().padding().right.to_px(),
|
|
|
|
layout_box.box_model().border().right.to_px(),
|
|
|
|
layout_box.box_model().margin().right.to_px());
|
2019-08-18 09:37:53 +03:00
|
|
|
|
2019-10-15 17:48:38 +03:00
|
|
|
// And the vertical box properties
|
2019-11-18 22:26:15 +03:00
|
|
|
dbgprintf(" [%g+%g+%g %g %g+%g+%g]",
|
2019-10-15 17:48:38 +03:00
|
|
|
layout_box.box_model().margin().top.to_px(),
|
|
|
|
layout_box.box_model().border().top.to_px(),
|
|
|
|
layout_box.box_model().padding().top.to_px(),
|
|
|
|
layout_box.height(),
|
|
|
|
layout_box.box_model().padding().bottom.to_px(),
|
|
|
|
layout_box.box_model().border().bottom.to_px(),
|
|
|
|
layout_box.box_model().margin().bottom.to_px());
|
2019-08-18 09:09:56 +03:00
|
|
|
|
2019-10-15 17:48:38 +03:00
|
|
|
dbgprintf("\n");
|
|
|
|
}
|
2019-06-21 21:54:13 +03:00
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
if (layout_node.is_block() && static_cast<const LayoutBlock&>(layout_node).children_are_inline()) {
|
|
|
|
auto& block = static_cast<const LayoutBlock&>(layout_node);
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < indent; ++i)
|
2019-10-03 16:20:13 +03:00
|
|
|
dbgprintf(" ");
|
|
|
|
dbgprintf(" Line boxes (%d):\n", block.line_boxes().size());
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t line_box_index = 0; line_box_index < block.line_boxes().size(); ++line_box_index) {
|
2019-10-03 16:20:13 +03:00
|
|
|
auto& line_box = block.line_boxes()[line_box_index];
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < indent; ++i)
|
2019-10-03 16:20:13 +03:00
|
|
|
dbgprintf(" ");
|
2019-11-18 22:26:15 +03:00
|
|
|
dbgprintf(" [%d] width: %g\n", line_box_index, line_box.width());
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t fragment_index = 0; fragment_index < line_box.fragments().size(); ++fragment_index) {
|
2019-10-03 16:20:13 +03:00
|
|
|
auto& fragment = line_box.fragments()[fragment_index];
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < indent; ++i)
|
2019-10-03 16:20:13 +03:00
|
|
|
dbgprintf(" ");
|
|
|
|
dbgprintf(" [%d] layout_node: %s{%p}, start: %d, length: %d, rect: %s\n",
|
|
|
|
fragment_index,
|
|
|
|
fragment.layout_node().class_name(),
|
|
|
|
&fragment.layout_node(),
|
|
|
|
fragment.start(),
|
|
|
|
fragment.length(),
|
|
|
|
fragment.rect().to_string().characters());
|
|
|
|
if (fragment.layout_node().is_text()) {
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < indent; ++i)
|
2019-10-03 16:20:13 +03:00
|
|
|
dbgprintf(" ");
|
|
|
|
auto& layout_text = static_cast<const LayoutText&>(fragment.layout_node());
|
2019-11-19 20:36:33 +03:00
|
|
|
auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
|
|
|
|
dbgprintf(" text: \"%s\"\n", fragment_text.characters());
|
2019-10-03 16:20:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 16:34:19 +03:00
|
|
|
layout_node.style().for_each_property([&](auto property_id, auto& value) {
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < indent; ++i)
|
2019-10-03 11:25:00 +03:00
|
|
|
dbgprintf(" ");
|
2019-11-18 13:12:58 +03:00
|
|
|
dbgprintf(" (%s: %s)\n", CSS::string_from_property_id(property_id), value.to_string().characters());
|
2019-06-28 22:17:34 +03:00
|
|
|
});
|
2019-09-21 15:32:17 +03:00
|
|
|
|
2019-06-28 22:17:34 +03:00
|
|
|
++indent;
|
2019-09-21 15:32:17 +03:00
|
|
|
layout_node.for_each_child([](auto& child) {
|
2019-06-28 22:17:34 +03:00
|
|
|
dump_tree(child);
|
|
|
|
});
|
|
|
|
--indent;
|
|
|
|
}
|
|
|
|
|
2019-06-27 21:40:21 +03:00
|
|
|
void dump_rule(const StyleRule& rule)
|
|
|
|
{
|
2019-10-03 11:25:00 +03:00
|
|
|
dbgprintf("Rule:\n");
|
2019-06-27 21:40:21 +03:00
|
|
|
for (auto& selector : rule.selectors()) {
|
2019-10-03 11:25:00 +03:00
|
|
|
dbgprintf(" Selector:\n");
|
2019-11-27 22:37:36 +03:00
|
|
|
|
|
|
|
for (auto& complex_selector : selector.complex_selectors()) {
|
|
|
|
dbgprintf(" ");
|
|
|
|
|
2019-10-06 10:28:10 +03:00
|
|
|
const char* relation_description = "";
|
2019-11-27 22:37:36 +03:00
|
|
|
switch (complex_selector.relation) {
|
|
|
|
case Selector::ComplexSelector::Relation::None:
|
2019-10-06 10:28:10 +03:00
|
|
|
break;
|
2019-11-27 22:37:36 +03:00
|
|
|
case Selector::ComplexSelector::Relation::ImmediateChild:
|
|
|
|
relation_description = "ImmediateChild";
|
2019-10-06 10:28:10 +03:00
|
|
|
break;
|
2019-11-27 22:37:36 +03:00
|
|
|
case Selector::ComplexSelector::Relation::Descendant:
|
|
|
|
relation_description = "Descendant";
|
2019-10-06 12:05:57 +03:00
|
|
|
break;
|
2019-11-27 22:37:36 +03:00
|
|
|
case Selector::ComplexSelector::Relation::AdjacentSibling:
|
|
|
|
relation_description = "AdjacentSibling";
|
2019-10-06 20:59:07 +03:00
|
|
|
break;
|
2019-11-27 22:37:36 +03:00
|
|
|
case Selector::ComplexSelector::Relation::GeneralSibling:
|
|
|
|
relation_description = "GeneralSibling";
|
2019-11-21 22:07:43 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-27 22:37:36 +03:00
|
|
|
if (*relation_description)
|
|
|
|
dbgprintf("{%s} ", relation_description);
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < complex_selector.compound_selector.size(); ++i) {
|
2019-11-27 22:37:36 +03:00
|
|
|
auto& simple_selector = complex_selector.compound_selector[i];
|
|
|
|
const char* type_description = "Unknown";
|
|
|
|
switch (simple_selector.type) {
|
|
|
|
case Selector::SimpleSelector::Type::Invalid:
|
|
|
|
type_description = "Invalid";
|
|
|
|
break;
|
|
|
|
case Selector::SimpleSelector::Type::Universal:
|
|
|
|
type_description = "Universal";
|
|
|
|
break;
|
|
|
|
case Selector::SimpleSelector::Type::Id:
|
|
|
|
type_description = "Id";
|
|
|
|
break;
|
|
|
|
case Selector::SimpleSelector::Type::Class:
|
|
|
|
type_description = "Class";
|
|
|
|
break;
|
|
|
|
case Selector::SimpleSelector::Type::TagName:
|
|
|
|
type_description = "TagName";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const char* attribute_match_type_description = "";
|
|
|
|
switch (simple_selector.attribute_match_type) {
|
|
|
|
case Selector::SimpleSelector::AttributeMatchType::None:
|
|
|
|
break;
|
|
|
|
case Selector::SimpleSelector::AttributeMatchType::HasAttribute:
|
|
|
|
attribute_match_type_description = "HasAttribute";
|
|
|
|
break;
|
|
|
|
case Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
|
|
|
|
attribute_match_type_description = "ExactValueMatch";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbgprintf("%s:%s", type_description, simple_selector.value.characters());
|
|
|
|
if (simple_selector.attribute_match_type != Selector::SimpleSelector::AttributeMatchType::None) {
|
|
|
|
dbgprintf(" [%s, name='%s', value='%s']", attribute_match_type_description, simple_selector.attribute_name.characters(), simple_selector.attribute_value.characters());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != complex_selector.compound_selector.size() - 1)
|
|
|
|
dbgprintf(", ");
|
2019-11-21 22:07:43 +03:00
|
|
|
}
|
|
|
|
dbgprintf("\n");
|
2019-06-27 21:40:21 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-03 11:25:00 +03:00
|
|
|
dbgprintf(" Declarations:\n");
|
2019-09-30 21:06:17 +03:00
|
|
|
for (auto& property : rule.declaration().properties()) {
|
2019-11-21 22:07:43 +03:00
|
|
|
dbgprintf(" %s: '%s'\n", CSS::string_from_property_id(property.property_id), property.value->to_string().characters());
|
2019-06-27 21:40:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 21:54:13 +03:00
|
|
|
void dump_sheet(const StyleSheet& sheet)
|
|
|
|
{
|
2019-10-03 11:25:00 +03:00
|
|
|
dbgprintf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size());
|
2019-06-25 07:31:47 +03:00
|
|
|
|
|
|
|
for (auto& rule : sheet.rules()) {
|
2019-06-27 21:40:21 +03:00
|
|
|
dump_rule(rule);
|
2019-06-25 07:31:47 +03:00
|
|
|
}
|
2019-06-21 21:54:13 +03:00
|
|
|
}
|