mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 12:05:15 +00:00
Browser+LibHTML: Deduplicate inspector code
The `DOMElementStyleModel` and `DOMComputedElementStyleModel` classes were replaced by the `StylePropertiesModel`.
This commit is contained in:
parent
988d1deca8
commit
2ced4c4ec7
Notes:
sideshowbarker
2024-07-19 10:20:26 +09:00
Author: https://github.com/Matrix89 Commit: https://github.com/SerenityOS/serenity/commit/2ced4c4ec74 Pull-request: https://github.com/SerenityOS/serenity/pull/1006 Reviewed-by: https://github.com/awesomekling
7 changed files with 60 additions and 145 deletions
|
@ -6,9 +6,8 @@
|
|||
#include <LibGUI/GTabWidget.h>
|
||||
#include <LibHTML/DOM/Document.h>
|
||||
#include <LibHTML/DOM/Element.h>
|
||||
#include <LibHTML/DOMElementStyleModel.h>
|
||||
#include <LibHTML/DOMComputedElementStyleModel.h>
|
||||
#include <LibHTML/DOMTreeModel.h>
|
||||
#include <LibHTML/StylePropertiesModel.h>
|
||||
|
||||
InspectorWidget::InspectorWidget(GWidget* parent)
|
||||
: GWidget(parent)
|
||||
|
@ -21,8 +20,10 @@ InspectorWidget::InspectorWidget(GWidget* parent)
|
|||
node->document().set_inspected_node(node);
|
||||
if (node->is_element()) {
|
||||
auto element = to<Element>(*node);
|
||||
m_style_table_view->set_model(DOMElementStyleModel::create(element));
|
||||
m_computed_style_table_view->set_model(DOMComputedElementStyleModel::create(element));
|
||||
if (element.resolved_style())
|
||||
m_style_table_view->set_model(StylePropertiesModel::create(*element.resolved_style()));
|
||||
if (element.layout_node() && element.layout_node()->has_style())
|
||||
m_computed_style_table_view->set_model(StylePropertiesModel::create(element.layout_node()->style()));
|
||||
} else {
|
||||
m_style_table_view->set_model(nullptr);
|
||||
m_computed_style_table_view->set_model(nullptr);
|
||||
|
|
|
@ -1,50 +0,0 @@
|
|||
#include "DOMComputedElementStyleModel.h"
|
||||
#include <LibHTML/CSS/PropertyID.h>
|
||||
#include <LibHTML/DOM/Document.h>
|
||||
#include <LibHTML/DOM/Element.h>
|
||||
|
||||
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();
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
#include "DOMElementStyleModel.h"
|
||||
#include <LibHTML/CSS/PropertyID.h>
|
||||
#include <LibHTML/DOM/Document.h>
|
||||
#include <LibHTML/DOM/Element.h>
|
||||
|
||||
DOMElementStyleModel::DOMElementStyleModel(const Element& element)
|
||||
: m_element(element)
|
||||
{
|
||||
if (element.resolved_style()) {
|
||||
element.resolved_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 DOMElementStyleModel::row_count(const GModelIndex&) const
|
||||
{
|
||||
return m_values.size();
|
||||
}
|
||||
|
||||
String DOMElementStyleModel::column_name(int column_index) const
|
||||
{
|
||||
switch (column_index) {
|
||||
case Column::PropertyName:
|
||||
return "Name";
|
||||
case Column::PropertyValue:
|
||||
return "Value";
|
||||
default:
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
GVariant DOMElementStyleModel::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 DOMElementStyleModel::update()
|
||||
{
|
||||
did_update();
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
#include <AK/NonnullRefPtrVector.h>
|
||||
#include <LibGUI/GModel.h>
|
||||
|
||||
class Element;
|
||||
|
||||
class DOMElementStyleModel final : public GModel {
|
||||
public:
|
||||
enum Column {
|
||||
PropertyName,
|
||||
PropertyValue,
|
||||
__Count
|
||||
};
|
||||
|
||||
static NonnullRefPtr<DOMElementStyleModel> create(const Element& element) { return adopt(*new DOMElementStyleModel(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 DOMElementStyleModel(const Element&);
|
||||
const Element& element() const { return *m_element; }
|
||||
|
||||
NonnullRefPtr<Element> m_element;
|
||||
|
||||
struct Value {
|
||||
String name;
|
||||
String value;
|
||||
};
|
||||
Vector<Value> m_values;
|
||||
};
|
|
@ -34,8 +34,7 @@ LIBHTML_OBJS = \
|
|||
DOM/Node.o \
|
||||
DOM/ParentNode.o \
|
||||
DOM/Text.o \
|
||||
DOMElementStyleModel.o \
|
||||
DOMComputedElementStyleModel.o \
|
||||
StylePropertiesModel.o \
|
||||
DOMTreeModel.o \
|
||||
Dump.o \
|
||||
FontCache.o \
|
||||
|
|
48
Libraries/LibHTML/StylePropertiesModel.cpp
Normal file
48
Libraries/LibHTML/StylePropertiesModel.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include "StylePropertiesModel.h"
|
||||
#include <LibHTML/CSS/PropertyID.h>
|
||||
#include <LibHTML/DOM/Document.h>
|
||||
#include <LibHTML/CSS/StyleProperties.h>
|
||||
|
||||
StylePropertiesModel::StylePropertiesModel(const StyleProperties& properties)
|
||||
: m_properties(properties)
|
||||
{
|
||||
properties.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 StylePropertiesModel::row_count(const GModelIndex&) const
|
||||
{
|
||||
return m_values.size();
|
||||
}
|
||||
|
||||
String StylePropertiesModel::column_name(int column_index) const
|
||||
{
|
||||
switch (column_index) {
|
||||
case Column::PropertyName:
|
||||
return "Name";
|
||||
case Column::PropertyValue:
|
||||
return "Value";
|
||||
default:
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
GVariant StylePropertiesModel::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 StylePropertiesModel::update()
|
||||
{
|
||||
did_update();
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
#include <AK/NonnullRefPtrVector.h>
|
||||
#include <LibGUI/GModel.h>
|
||||
|
||||
class Element;
|
||||
class StyleProperties;
|
||||
|
||||
class DOMComputedElementStyleModel final : public GModel {
|
||||
class StylePropertiesModel final : public GModel {
|
||||
public:
|
||||
enum Column {
|
||||
PropertyName,
|
||||
|
@ -11,7 +11,7 @@ public:
|
|||
__Count
|
||||
};
|
||||
|
||||
static NonnullRefPtr<DOMComputedElementStyleModel> create(const Element& element) { return adopt(*new DOMComputedElementStyleModel(element)); }
|
||||
static NonnullRefPtr<StylePropertiesModel> create(const StyleProperties& properties) { return adopt(*new StylePropertiesModel(properties)); }
|
||||
|
||||
virtual int row_count(const GModelIndex& = GModelIndex()) const override;
|
||||
virtual int column_count(const GModelIndex& = GModelIndex()) const override { return Column::__Count; }
|
||||
|
@ -20,10 +20,10 @@ public:
|
|||
virtual void update() override;
|
||||
|
||||
private:
|
||||
explicit DOMComputedElementStyleModel(const Element&);
|
||||
const Element& element() const { return *m_element; }
|
||||
explicit StylePropertiesModel(const StyleProperties& properties);
|
||||
const StyleProperties& properties() const { return *m_properties; }
|
||||
|
||||
NonnullRefPtr<Element> m_element;
|
||||
NonnullRefPtr<StyleProperties> m_properties;
|
||||
|
||||
struct Value {
|
||||
String name;
|
Loading…
Add table
Reference in a new issue