diff --git a/Applications/Browser/InspectorWidget.cpp b/Applications/Browser/InspectorWidget.cpp index f7dab25ac3..e3cad9c67a 100644 --- a/Applications/Browser/InspectorWidget.cpp +++ b/Applications/Browser/InspectorWidget.cpp @@ -3,9 +3,11 @@ #include #include #include +#include #include #include #include +#include #include InspectorWidget::InspectorWidget(GWidget* parent) @@ -17,13 +19,24 @@ InspectorWidget::InspectorWidget(GWidget* parent) m_dom_tree_view->on_selection = [this](auto& index) { auto* node = static_cast(index.internal_data()); node->document().set_inspected_node(node); - if (node->is_element()) - m_style_table_view->set_model(DOMElementStyleModel::create(to(*node))); - else + if (node->is_element()) { + auto element = to(*node); + m_style_table_view->set_model(DOMElementStyleModel::create(element)); + m_computed_style_table_view->set_model(DOMComputedElementStyleModel::create(element)); + } else { m_style_table_view->set_model(nullptr); + m_computed_style_table_view->set_model(nullptr); + } }; - m_style_table_view = GTableView::construct(splitter); + m_style_table_view = GTableView::construct(nullptr); m_style_table_view->set_size_columns_to_fit_content(true); + + m_computed_style_table_view = GTableView::construct(nullptr); + m_computed_style_table_view->set_size_columns_to_fit_content(true); + + auto tabwidget = GTabWidget::construct(splitter); + tabwidget->add_widget("Styles", m_style_table_view); + tabwidget->add_widget("Computed", m_computed_style_table_view); } InspectorWidget::~InspectorWidget() diff --git a/Applications/Browser/InspectorWidget.h b/Applications/Browser/InspectorWidget.h index 5a1e0de62a..1ef75ee2e6 100644 --- a/Applications/Browser/InspectorWidget.h +++ b/Applications/Browser/InspectorWidget.h @@ -16,5 +16,6 @@ private: RefPtr m_dom_tree_view; RefPtr m_style_table_view; + RefPtr m_computed_style_table_view; RefPtr m_document; }; diff --git a/Libraries/LibHTML/DOMComputedElementStyleModel.cpp b/Libraries/LibHTML/DOMComputedElementStyleModel.cpp new file mode 100644 index 0000000000..1c427730f1 --- /dev/null +++ b/Libraries/LibHTML/DOMComputedElementStyleModel.cpp @@ -0,0 +1,50 @@ +#include "DOMComputedElementStyleModel.h" +#include +#include +#include + +DOMComputedElementStyleModel::DOMComputedElementStyleModel(const Element& element) + : m_element(element) +{ + if (element.layout_node() != nullptr && element.layout_node()->has_style()) { + element.layout_node()->style().for_each_property([&](auto property_id, auto& property_value) { + Value value; + value.name = CSS::string_from_property_id(property_id); + value.value = property_value.to_string(); + m_values.append(value); + }); + } +} + +int DOMComputedElementStyleModel::row_count(const GModelIndex&) const +{ + return m_values.size(); +} + +String DOMComputedElementStyleModel::column_name(int column_index) const +{ + switch (column_index) { + case Column::PropertyName: + return "Name"; + case Column::PropertyValue: + return "Value"; + default: + ASSERT_NOT_REACHED(); + } +} +GVariant DOMComputedElementStyleModel::data(const GModelIndex& index, Role role) const +{ + auto& value = m_values[index.row()]; + if (role == Role::Display) { + if (index.column() == Column::PropertyName) + return value.name; + if (index.column() == Column::PropertyValue) + return value.value; + } + return {}; +} + +void DOMComputedElementStyleModel::update() +{ + did_update(); +} diff --git a/Libraries/LibHTML/DOMComputedElementStyleModel.h b/Libraries/LibHTML/DOMComputedElementStyleModel.h new file mode 100644 index 0000000000..b118c35254 --- /dev/null +++ b/Libraries/LibHTML/DOMComputedElementStyleModel.h @@ -0,0 +1,33 @@ +#include +#include + +class Element; + +class DOMComputedElementStyleModel final : public GModel { +public: + enum Column { + PropertyName, + PropertyValue, + __Count + }; + + static NonnullRefPtr create(const Element& element) { return adopt(*new DOMComputedElementStyleModel(element)); } + + virtual int row_count(const GModelIndex& = GModelIndex()) const override; + virtual int column_count(const GModelIndex& = GModelIndex()) const override { return Column::__Count; } + virtual String column_name(int) const override; + virtual GVariant data(const GModelIndex&, Role = Role::Display) const override; + virtual void update() override; + +private: + explicit DOMComputedElementStyleModel(const Element&); + const Element& element() const { return *m_element; } + + NonnullRefPtr m_element; + + struct Value { + String name; + String value; + }; + Vector m_values; +}; diff --git a/Libraries/LibHTML/Makefile b/Libraries/LibHTML/Makefile index a0518ad08a..775fac466a 100644 --- a/Libraries/LibHTML/Makefile +++ b/Libraries/LibHTML/Makefile @@ -35,6 +35,7 @@ LIBHTML_OBJS = \ DOM/ParentNode.o \ DOM/Text.o \ DOMElementStyleModel.o \ + DOMComputedElementStyleModel.o \ DOMTreeModel.o \ Dump.o \ FontCache.o \