mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 04:08:11 +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
7 changed files with 60 additions and 145 deletions
|
@ -6,9 +6,8 @@
|
||||||
#include <LibGUI/GTabWidget.h>
|
#include <LibGUI/GTabWidget.h>
|
||||||
#include <LibHTML/DOM/Document.h>
|
#include <LibHTML/DOM/Document.h>
|
||||||
#include <LibHTML/DOM/Element.h>
|
#include <LibHTML/DOM/Element.h>
|
||||||
#include <LibHTML/DOMElementStyleModel.h>
|
|
||||||
#include <LibHTML/DOMComputedElementStyleModel.h>
|
|
||||||
#include <LibHTML/DOMTreeModel.h>
|
#include <LibHTML/DOMTreeModel.h>
|
||||||
|
#include <LibHTML/StylePropertiesModel.h>
|
||||||
|
|
||||||
InspectorWidget::InspectorWidget(GWidget* parent)
|
InspectorWidget::InspectorWidget(GWidget* parent)
|
||||||
: GWidget(parent)
|
: GWidget(parent)
|
||||||
|
@ -21,8 +20,10 @@ InspectorWidget::InspectorWidget(GWidget* parent)
|
||||||
node->document().set_inspected_node(node);
|
node->document().set_inspected_node(node);
|
||||||
if (node->is_element()) {
|
if (node->is_element()) {
|
||||||
auto element = to<Element>(*node);
|
auto element = to<Element>(*node);
|
||||||
m_style_table_view->set_model(DOMElementStyleModel::create(element));
|
if (element.resolved_style())
|
||||||
m_computed_style_table_view->set_model(DOMComputedElementStyleModel::create(element));
|
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 {
|
} else {
|
||||||
m_style_table_view->set_model(nullptr);
|
m_style_table_view->set_model(nullptr);
|
||||||
m_computed_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/Node.o \
|
||||||
DOM/ParentNode.o \
|
DOM/ParentNode.o \
|
||||||
DOM/Text.o \
|
DOM/Text.o \
|
||||||
DOMElementStyleModel.o \
|
StylePropertiesModel.o \
|
||||||
DOMComputedElementStyleModel.o \
|
|
||||||
DOMTreeModel.o \
|
DOMTreeModel.o \
|
||||||
Dump.o \
|
Dump.o \
|
||||||
FontCache.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 <AK/NonnullRefPtrVector.h>
|
||||||
#include <LibGUI/GModel.h>
|
#include <LibGUI/GModel.h>
|
||||||
|
|
||||||
class Element;
|
class StyleProperties;
|
||||||
|
|
||||||
class DOMComputedElementStyleModel final : public GModel {
|
class StylePropertiesModel final : public GModel {
|
||||||
public:
|
public:
|
||||||
enum Column {
|
enum Column {
|
||||||
PropertyName,
|
PropertyName,
|
||||||
|
@ -11,7 +11,7 @@ public:
|
||||||
__Count
|
__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 row_count(const GModelIndex& = GModelIndex()) const override;
|
||||||
virtual int column_count(const GModelIndex& = GModelIndex()) const override { return Column::__Count; }
|
virtual int column_count(const GModelIndex& = GModelIndex()) const override { return Column::__Count; }
|
||||||
|
@ -20,10 +20,10 @@ public:
|
||||||
virtual void update() override;
|
virtual void update() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit DOMComputedElementStyleModel(const Element&);
|
explicit StylePropertiesModel(const StyleProperties& properties);
|
||||||
const Element& element() const { return *m_element; }
|
const StyleProperties& properties() const { return *m_properties; }
|
||||||
|
|
||||||
NonnullRefPtr<Element> m_element;
|
NonnullRefPtr<StyleProperties> m_properties;
|
||||||
|
|
||||||
struct Value {
|
struct Value {
|
||||||
String name;
|
String name;
|
Loading…
Add table
Add a link
Reference in a new issue