Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/SVG/SVGGraphicsElement.h (C/C++)

Code before:
    SVGGraphicsElement(DOM::Document&, DOM::QualifiedName);

    virtual void initialize(JS::Realm&) override;

    Optional<Painting::PaintStyle> svg_paint_computed_value_to_gfx_paint_style(SVGPaintContext const& paint_context, Optional<CSS::SVGPaint> const& paint_value) const;

    Gfx::AffineTransform m_transform = {};

    template<typename T>
    GC::Ptr<T> try_resolve_url_to(CSS::URL const& url) const
    {
        // FIXME: Complete and use the entire URL, not just the fragment.
        Optional<FlyString> fragment;
        if (auto fragment_offset = url.url().find_byte_offset('#'); fragment_offset.has_value()) {
            fragment = MUST(url.url().substring_from_byte_offset_with_shared_superstring(fragment_offset.value() + 1));
        }
        if (!fragment.has_value())
            return {};
        if (auto node = document().get_element_by_id(*fragment); node && is<T>(*node))
            return static_cast<T&>(*node);

        auto containing_shadow = containing_shadow_root();
        if (containing_shadow) {
            auto node = containing_shadow->get_element_by_id(*fragment);
            if (node && is<T>(*node))
                return static_cast<T&>(*node);
        }

        return {};
    }

private:
    virtual bool is_svg_graphics_element() const final { return true; }
    float resolve_relative_to_viewport_size(CSS::LengthPercentage const& length_percentage) const;
};

Gfx::AffineTransform transform_from_transform_list(ReadonlySpan<Transform> transform_list);

}

namespace Web::DOM {

template<>
inline bool Node::fast_is<SVG::SVGGraphicsElement>() const { return is_svg_graphics_element(); }

}


Review comment (bug):
Nice catch, thanks for fixing this!

Instead of `is<T>()` and then `static_cast<T>()`, use `as_if<T>()` which will return null if the cast fails. Something like this:

```c++
if (auto* node = as_if<T>(containing_shadow->get_element_by_id(*fragment)))
    return node;
```

Code after:
    SVGGraphicsElement(DOM::Document&, DOM::QualifiedName);

    virtual void initialize(JS::Realm&) override;

    Optional<Painting::PaintStyle> svg_paint_computed_value_to_gfx_paint_style(SVGPaintContext const& paint_context, Optional<CSS::SVGPaint> const& paint_value) const;

    Gfx::AffineTransform m_transform = {};

    template<typename T>
    GC::Ptr<T> try_resolve_url_to(CSS::URL const& url) const
    {
        // FIXME: Complete and use the entire URL, not just the fragment.
        Optional<FlyString> fragment;
        if (auto fragment_offset = url.url().find_byte_offset('#'); fragment_offset.has_value()) {
            fragment = MUST(url.url().substring_from_byte_offset_with_shared_superstring(fragment_offset.value() + 1));
        }
        if (!fragment.has_value())
            return {};
        if (auto node = as_if<T>(document().get_element_by_id(*fragment).ptr()))
            return *node;

        auto containing_shadow = containing_shadow_root();
        if (containing_shadow) {
            if (auto node = as_if<T>(containing_shadow->get_element_by_id(*fragment).ptr()))
                return *node;
        }

        return {};
    }

private:
    virtual bool is_svg_graphics_element() const final { return true; }
    float resolve_relative_to_viewport_size(CSS::LengthPercentage const& length_percentage) const;
};

Gfx::AffineTransform transform_from_transform_list(ReadonlySpan<Transform> transform_list);

}

namespace Web::DOM {

template<>
inline bool Node::fast_is<SVG::SVGGraphicsElement>() const { return is_svg_graphics_element(); }

}