Implement class attribute and Element type
Closed this issue · 5 comments
Hi @igagis, here is a suggestion for 2 new features that would be somewhat easy to implement, but would go a long way for your developers:
-
Implement class attribute: https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/class
-
Implement a method to return the Element type (eg:
Element::Type getType()
). Eg, for thePathElement
this method would returnElement::Type::Path
.
This information would be helpful if your developers need to access an Element by class
or type
(or already supported id
), through Javascript or through a CSS selector.
I don't know for sure, but depending on my schedule, I might be able to help out ;).
- This is in the backlog for some time, it's a complex task, I haven't had time to implement it so far.
- one can implement
Visitor
for that purpose.
Oh, for (1) I was not referring to full <style>
support, I do understand that this would be a major effort. I am only talking about having a std::vector<string> classes
member as part of an Element
for example, to complement the already supported id
: https://github.com/igagis/svgdom/blob/master/src/svgdom/elements/Element.hpp#L17
For (2), I am referring more to a getType()
method that would return a static element type, instead of doing this dynamically with a Visitor
, since we already know the type at compile time. Maybe Element
could have a virtual Element::Type getType() const
method, and this method would be implemented per element.
-
Ok, this looks simple.
-
Visitor
is actually made to add custom virtual methods toElement
objects, so this virtualgetType()
can be implemented usingVisitor
. The other question is why do you want to know the element type, do you want to make some kind ofswitch(element_type)
? TheVisitor
pattern is there to be able to perform custom actions on elements depending on their type, and to avoid thoseswitch/case
.
I understand your suggestion on using Visitor
, but my use-case is completely different: (2) would be necessary to support CSS stylesheets, or any other type of selector capabilities. If CSS selectors, for instance, have to be stored in a vector, you're going to end up storing selectors in a data structure similar to this vector<variant<ElementType, Id, vector<Class>>>
(this is very simplified). So, in other words, I don't want to "perform custom actions on elements", but instead, I would like to store element types on some selector structure :).
The Element type, combined with IDs and Classes, would be a stepping stone to future CSS support. Also, if you are interested, here is a real implementation of CSS selector parsing, that requires element type, classes, and IDs: https://github.com/emweb/wt/blob/master/src/Wt/Render/CssData_p.h#L18
- Done
- Added
const std::string& get_tag()
method tostyleable
and added the CSS support.