Просмотр исходного кода

LibWeb: Simplify type traits for SVGGraphicsElement

Andreas Kling 5 лет назад
Родитель
Сommit
1b1537c5a6

+ 1 - 0
Libraries/LibWeb/DOM/Node.h

@@ -82,6 +82,7 @@ public:
     bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; }
     bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
     bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
+    virtual bool is_svg_element() const { return false; }
 
     RefPtr<Node> append_child(NonnullRefPtr<Node>, bool notify = true);
     RefPtr<Node> insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify = true);

+ 9 - 0
Libraries/LibWeb/SVG/SVGElement.h

@@ -33,6 +33,15 @@ namespace Web::SVG {
 class SVGElement : public Element {
 public:
     SVGElement(Document&, const FlyString& tag_name);
+
+    virtual bool is_graphics_element() const { return false; }
+
+private:
+    virtual bool is_svg_element() const final { return true; }
 };
 
 }
+
+AK_BEGIN_TYPE_TRAITS(Web::SVG::SVGElement)
+static bool is_type(const Web::Node& node) { return node.is_svg_element(); }
+AK_END_TYPE_TRAITS()

+ 4 - 15
Libraries/LibWeb/SVG/SVGGraphicsElement.h

@@ -59,24 +59,13 @@ protected:
     Optional<Gfx::Color> m_fill_color;
     Optional<Gfx::Color> m_stroke_color;
     Optional<float> m_stroke_width;
+
+private:
+    virtual bool is_graphics_element() const final { return true; }
 };
 
 }
 
 AK_BEGIN_TYPE_TRAITS(Web::SVG::SVGGraphicsElement)
-static bool is_type(const Web::Node& node)
-{
-    if (!is<Web::Element>(node))
-        return false;
-
-    auto tag_name = downcast<Web::Element>(node).tag_name();
-
-#define __ENUMERATE_SVG_TAG(name) \
-    if (tag_name == #name)        \
-        return true;
-    ENUMERATE_SVG_TAGS
-#undef ENUMERATE_SVG_TAG
-
-    return false;
-}
+static bool is_type(const Web::Node& node) { return is<Web::SVG::SVGElement>(node) && downcast<Web::SVG::SVGElement>(node).is_graphics_element(); }
 AK_END_TYPE_TRAITS()