Donner 0.5.0
Embeddable browser-grade SVG2 engine
Loading...
Searching...
No Matches
donner::svg::SVGElement Class Reference

Represents a single SVG element (e.g., <rect>, <circle>, <g>, <text>, etc.) within an SVGDocument. More...

#include "donner/svg/SVGElement.h"

Inheritance diagram for donner::svg::SVGElement:
[legend]

Public Member Functions

 SVGElement (const SVGElement &other)
 Create another reference to the same SVGElement.
 SVGElement (SVGElement &&other) noexcept
 Move constructor.
 ~SVGElement () noexcept=default
 Destructor.
SVGElementoperator= (const SVGElement &other)
 Create another reference to the same SVGElement.
SVGElementoperator= (SVGElement &&other) noexcept
 Move assignment operator.
ElementType type () const
 Get the ElementType for known XML element types.
xml::XMLQualifiedNameRef tagName () const
 Get the XML tag name string for this element.
bool isKnownType () const
 Returns true if this is a known element type, returns false if this is an SVGUnknownElement.
EntityHandle entityHandle () const
 Get the underlying EntityHandle, for advanced use-cases that require direct access to the ECS.
RcString id () const
 Get the element id, the value of the "id" attribute.
void setId (std::string_view id)
 Set the element id, the value of the "id" attribute.
RcString className () const
 Get the element class name, the value of the "class" attribute.
void setClassName (std::string_view name)
 Set the element class name, the value of the "class" attribute.
void setStyle (std::string_view style)
 Set the element style, the value of the "style" attribute.
void updateStyle (std::string_view style)
 Update the element style, adding new attributes or overridding existing ones (without removing them).
ParseResult< bool > trySetPresentationAttribute (std::string_view name, std::string_view value)
 Set the value of a presentation attribute, such as "fill" or "stroke".
bool hasAttribute (const xml::XMLQualifiedNameRef &name) const
 Returns true if the element has an attribute with the given name.
std::optional< RcStringgetAttribute (const xml::XMLQualifiedNameRef &name) const
 Get the value of an attribute, if it exists.
SmallVector< xml::XMLQualifiedNameRef, 1 > findMatchingAttributes (const xml::XMLQualifiedNameRef &matcher) const
 Find attributes matching the given name matcher.
void setAttribute (const xml::XMLQualifiedNameRef &name, std::string_view value)
 Set the value of a generic XML attribute, which may be either a presentation attribute or custom user-provided attribute.
void removeAttribute (const xml::XMLQualifiedNameRef &name)
 Remove an attribute, which may be either a presentation attribute or custom user-provided attribute.
SVGDocument ownerDocument ()
 Get an owning reference to the SVGDocument containing this element.
std::optional< SVGElementparentElement () const
 Get this element's parent, if it exists.
std::optional< SVGElementfirstChild () const
 Get the first child of this element, if it exists.
std::optional< SVGElementlastChild () const
 Get the last child of this element, if it exists.
std::optional< SVGElementpreviousSibling () const
 Get the previous sibling of this element, if it exists.
std::optional< SVGElementnextSibling () const
 Get the next sibling of this element, if it exists.
void insertBefore (const SVGElement &newNode, std::optional< SVGElement > referenceNode)
 Insert newNode as a child, before referenceNode.
void appendChild (const SVGElement &child)
 Append child as a child of the current node.
void replaceChild (const SVGElement &newChild, const SVGElement &oldChild)
 Replace oldChild with newChild in the tree, removing oldChild and inserting newChild in its place.
void removeChild (const SVGElement &child)
 Remove child from this node.
void remove ()
 Remove this node from its parent, if it has one.
bool operator== (const SVGElement &other) const
 Returns true if the two SVGElement handles reference the same underlying document.
bool operator!= (const SVGElement &other) const
 Returns true if the two SVGElement handles reference the same underlying document.
template<typename Derived>
bool isa () const
 Return true if this element "is a" instance of type, if it be cast to a specific type with cast.
template<typename Derived>
Derived cast ()
 Cast this element to its derived type.
template<typename Derived>
Derived cast () const
 Cast this element to its derived type (const version).
template<typename Derived>
std::optional< Derived > tryCast ()
 Cast this element to its derived type, if possible.
template<typename Derived>
std::optional< const Derived > tryCast () const
 Cast this element to its derived type, if possible.
std::optional< SVGElementquerySelector (std::string_view selector)
 Find the first element in the tree that matches the given CSS selector.
const PropertyRegistrygetComputedStyle () const
 Get the computed CSS style of this element, after the CSS cascade has been applied.

Protected Member Functions

 SVGElement (EntityHandle handle)
 Internal constructor to create an SVGElement from an EntityHandle.
Registryregistry () const
 Get the underlying ECS Registry, which holds all data for the document, for advanced use.
EntityHandle toHandle (Entity entity) const
 Convert an Entity to an EntityHandle, for advanced use.

Static Protected Member Functions

static EntityHandle CreateEmptyEntity (SVGDocument &document)
 Create a new Entity within the document ECS, and return a handle to it.
static void CreateEntityOn (EntityHandle handle, const xml::XMLQualifiedNameRef &tagName, ElementType Type)
 Create a new SVG element instance on a given Entity.

Protected Attributes

EntityHandle handle_
 The underlying ECS Entity for this element, which holds all data.

Friends

class DonnerController

Detailed Description

Represents a single SVG element (e.g., <rect>, <circle>, <g>, <text>, etc.) within an SVGDocument.

SVGElement provides a DOM-like API for traversing the document tree (parentElement(), firstChild(), nextSibling()), querying and setting attributes (getAttribute(), setAttribute()), and modifying the tree structure (appendChild(), removeChild()).

Elements are lightweight value types — copying an SVGElement creates another handle to the same underlying element, not a deep copy. Use isa<Derived>() and cast<Derived>() to work with element-specific APIs (e.g., SVGCircleElement, SVGPathElement).

For advanced queries like hit-testing, see DonnerController.

See also
SVGDocument
Examples
svg_filter_interaction.cc, and svg_text_interaction.cc.

Constructor & Destructor Documentation

◆ SVGElement()

donner::svg::SVGElement::SVGElement ( EntityHandle handle)
explicitprotected

Internal constructor to create an SVGElement from an EntityHandle.

To create an SVGElement, use the static Create methods on the derived class, such as SVGCircleElement::Create.

Parameters
handleEntityHandle to wrap.

Member Function Documentation

◆ appendChild()

void donner::svg::SVGElement::appendChild ( const SVGElement & child)

Append child as a child of the current node.

If child is already in the tree, it is first removed from its parent before reinsertion.

Precondition
child must not be an ancestor of this element (would create a cycle).
Parameters
childNode to append.
Examples
svg_text_interaction.cc.

◆ cast() [1/2]

template<typename Derived>
Derived donner::svg::SVGElement::cast ( )
inline

Cast this element to its derived type.

Precondition
Requires this element to be of type Derived::Type.
Returns
A reference to this element, cast to the derived type.
Examples
svg_filter_interaction.cc, svg_text_interaction.cc, and svg_tree_interaction.cc.

◆ cast() [2/2]

template<typename Derived>
Derived donner::svg::SVGElement::cast ( ) const
inline

Cast this element to its derived type (const version).

Precondition
Requires this element to be of type Derived::Type.
Returns
A reference to this element, cast to the derived type.

◆ CreateEmptyEntity()

EntityHandle donner::svg::SVGElement::CreateEmptyEntity ( SVGDocument & document)
staticprotected

Create a new Entity within the document ECS, and return a handle to it.

Parameters
documentContaining document.

◆ CreateEntityOn()

void donner::svg::SVGElement::CreateEntityOn ( EntityHandle handle,
const xml::XMLQualifiedNameRef & tagName,
ElementType Type )
staticprotected

Create a new SVG element instance on a given Entity.

Parameters
handleEntity to create the element on.
tagNameXML element type, e.g. "svg" or "rect", which an optional namespace.
TypeType of the entity.

◆ findMatchingAttributes()

SmallVector< xml::XMLQualifiedNameRef, 1 > donner::svg::SVGElement::findMatchingAttributes ( const xml::XMLQualifiedNameRef & matcher) const

Find attributes matching the given name matcher.

Parameters
matcherMatcher to use to find attributes. If XMLQualifiedNameRef::namespacePrefix is "*", the matcher will match any namespace with the given attribute name.
Returns
A vector of attributes matching the given name matcher.

◆ firstChild()

std::optional< SVGElement > donner::svg::SVGElement::firstChild ( ) const

Get the first child of this element, if it exists.

Returns
The first child element, or std::nullopt if the element has no children.

◆ getAttribute()

std::optional< RcString > donner::svg::SVGElement::getAttribute ( const xml::XMLQualifiedNameRef & name) const

Get the value of an attribute, if it exists.

Parameters
nameName of the attribute to get.
Returns
The value of the attribute, or std::nullopt if the attribute does not exist.

◆ getComputedStyle()

const PropertyRegistry & donner::svg::SVGElement::getComputedStyle ( ) const

Get the computed CSS style of this element, after the CSS cascade has been applied.

The returned PropertyRegistry contains resolved values for all CSS properties (fill, stroke, font-size, etc.).

Examples
svg_tree_interaction.cc.

◆ hasAttribute()

bool donner::svg::SVGElement::hasAttribute ( const xml::XMLQualifiedNameRef & name) const

Returns true if the element has an attribute with the given name.

Parameters
nameName of the attribute to check.
Returns
true if the attribute exists, false otherwise.

◆ insertBefore()

void donner::svg::SVGElement::insertBefore ( const SVGElement & newNode,
std::optional< SVGElement > referenceNode )

Insert newNode as a child, before referenceNode.

If referenceNode is std::nullopt, append the child.

If newNode is already in the tree, it is first removed from its parent before reinsertion.

Precondition
newNode must not be an ancestor of this element (inserting a parent as a child of its descendant would create a cycle, which is undefined behavior).
Parameters
newNodeNew node to insert.
referenceNodeA child of this node to insert newNode before, or std::nullopt. Must be a child of the current node.
Examples
svg_tree_interaction.cc.

◆ isa()

template<typename Derived>
bool donner::svg::SVGElement::isa ( ) const
inline

Return true if this element "is a" instance of type, if it be cast to a specific type with cast.

Template Parameters
DerivedType to check.

◆ lastChild()

std::optional< SVGElement > donner::svg::SVGElement::lastChild ( ) const

Get the last child of this element, if it exists.

Returns
The last child element, or std::nullopt if the element has no children.

◆ nextSibling()

std::optional< SVGElement > donner::svg::SVGElement::nextSibling ( ) const

Get the next sibling of this element, if it exists.

Returns
The next sibling element, or std::nullopt if the element has no next sibling.

◆ parentElement()

std::optional< SVGElement > donner::svg::SVGElement::parentElement ( ) const

Get this element's parent, if it exists.

Returns
The parent element, or std::nullopt if this is the root element or the element has not been inserted into a document tree.

◆ previousSibling()

std::optional< SVGElement > donner::svg::SVGElement::previousSibling ( ) const

Get the previous sibling of this element, if it exists.

Returns
The previous sibling element, or std::nullopt if the element has no previous sibling.

◆ querySelector()

std::optional< SVGElement > donner::svg::SVGElement::querySelector ( std::string_view selector)

Find the first element in the tree that matches the given CSS selector.

auto element = document.svgElement().querySelector("#elementId");

To find things relative to the current element, use :scope:

auto rectInElement = element.querySelector(":scope > rect");
Parameters
selectorCSS selector to match. If the selector string is invalid, returns std::nullopt (no error is reported).
Returns
The first matching element, or std::nullopt if no element matches.

◆ remove()

void donner::svg::SVGElement::remove ( )

Remove this node from its parent, if it has one.

Has no effect if this has no parent.

◆ removeAttribute()

void donner::svg::SVGElement::removeAttribute ( const xml::XMLQualifiedNameRef & name)

Remove an attribute, which may be either a presentation attribute or custom user-provided attribute.

If this is a presentation attribute, the presentation attributes value will be removed (internally by setting the value to 'inherit').

Parameters
nameName of the attribute to remove.

◆ removeChild()

void donner::svg::SVGElement::removeChild ( const SVGElement & child)

Remove child from this node.

Parameters
childChild to remove, must be a child of the current node.

◆ replaceChild()

void donner::svg::SVGElement::replaceChild ( const SVGElement & newChild,
const SVGElement & oldChild )

Replace oldChild with newChild in the tree, removing oldChild and inserting newChild in its place.

If newChild is already in the tree, it is first removed from its parent before reinsertion.

Precondition
newChild must not be an ancestor of this element (would create a cycle).
Parameters
newChildNew child to insert.
oldChildOld child to remove, must be a child of the current node.

◆ setAttribute()

void donner::svg::SVGElement::setAttribute ( const xml::XMLQualifiedNameRef & name,
std::string_view value )

Set the value of a generic XML attribute, which may be either a presentation attribute or custom user-provided attribute.

This API supports a superset of trySetPresentationAttribute, however its parse errors are ignored. If the attribute is not a presentation attribute, or there are parse errors the attribute will be stored as a custom attribute instead.

Parameters
nameName of the attribute to set.
valueNew value to set.

◆ setClassName()

void donner::svg::SVGElement::setClassName ( std::string_view name)

Set the element class name, the value of the "class" attribute.

Parameters
nameNew class name to set.

◆ setId()

void donner::svg::SVGElement::setId ( std::string_view id)

Set the element id, the value of the "id" attribute.

Parameters
idNew id to set.

◆ setStyle()

void donner::svg::SVGElement::setStyle ( std::string_view style)

Set the element style, the value of the "style" attribute.

Parameters
styleNew style to set.
Examples
svg_text_interaction.cc, and svg_tree_interaction.cc.

◆ toHandle()

EntityHandle donner::svg::SVGElement::toHandle ( Entity entity) const
inlineprotected

Convert an Entity to an EntityHandle, for advanced use.

Parameters
entityEntity to convert.

◆ tryCast() [1/2]

template<typename Derived>
std::optional< Derived > donner::svg::SVGElement::tryCast ( )
inline

Cast this element to its derived type, if possible.

Return std::nullopt otherwise.

Returns
A reference to this element, cast to the derived type, or std::nullopt if the element is not of the correct type.

◆ tryCast() [2/2]

template<typename Derived>
std::optional< const Derived > donner::svg::SVGElement::tryCast ( ) const
inline

Cast this element to its derived type, if possible.

Return std::nullopt otherwise (const version).

Returns
A reference to this element, cast to the derived type, or std::nullopt if the element is not of the correct type.

◆ trySetPresentationAttribute()

ParseResult< bool > donner::svg::SVGElement::trySetPresentationAttribute ( std::string_view name,
std::string_view value )

Set the value of a presentation attribute, such as "fill" or "stroke".

Note that this accepts the CSS value, not the XML attribute value.

For example, for the following XML attributes they need to be mapped as follows before calling:

  • gradientTransform -> transform
  • patternTransform -> transform
Parameters
nameName of the attribute to set.
valueNew value to set.
Returns
true if the attribute was set, false if the attribute is not a valid presentation attribute for this element, or a ParseDiagnostic if the value is invalid.

◆ updateStyle()

void donner::svg::SVGElement::updateStyle ( std::string_view style)

Update the element style, adding new attributes or overridding existing ones (without removing them).

Parameters
styleStyle updates to apply, as a CSS style string (e.g. "fill:red;").

The documentation for this class was generated from the following files: