The Icon type, its three constructors, all fluent transform methods, and the output methods that produce HTML.
Overview
The Icon type represents a parsed SVG icon with inner content and attributes. Every fluent method returns a new *Icon; the original is never modified.
For a usage guide covering chaining, transforms, and sizing, see Icon Manipulation.
Types
Icon
type Icon struct { // unexported fields}Fields (unexported):
content string: SVG inner markup (without the<svg>wrapper).attributes map[string]string: SVG element attributes.
Constructors
New
func New(content string, attrs map[string]string) *IconCreates an Icon from raw SVG inner content and an attribute map. The attribute map is deep-copied; the caller's map is not retained. Passing nil for attrs produces an icon with an empty attribute map.
FromFile
func FromFile(path string) (*Icon, error)Reads the SVG file at path, parses and sanitizes it, and returns the resulting Icon. Returns an error wrapping ErrProviderError on I/O failure or ErrInvalidSVG if the content has no <svg> element.
FromString
func FromString(svg string) (*Icon, error)Parses an SVG string and returns the resulting Icon. Returns ErrInvalidSVG if the input is empty or contains no <svg> element. The inner content is sanitized automatically. When the input contains multiple <svg> elements, only the first is parsed.
Fluent methods
All methods return a new *Icon with deep-copied attributes.
Attr
func (ic *Icon) Attr(attrs map[string]string) *IconMerges the given attributes into the existing set. Empty-string values are ignored. For CSS classes, prefer Class (which appends) over Attr with a "class" key (which overwrites).
Class
func (ic *Icon) Class(classes ...string) *IconAppends the given CSS classes to any existing class attribute, space-separated. Does not replace existing classes.
Size
func (ic *Icon) Size(size int) *IconSets both width and height to size.
Width
func (ic *Icon) Width(w string) *IconSets width to w and derives height from the viewBox aspect ratio. Supports CSS units ("24", "1.5em", "2rem"). Special values: "auto" resolves to the viewBox width/height; "unset", "none", or "undefined" remove both width and height attributes.
Height
func (ic *Icon) Height(h string) *IconSets height to h and derives width from the viewBox aspect ratio. Same CSS unit support and special values as Width.
Fill
func (ic *Icon) Fill(fill string) *IconSets the fill attribute.
Stroke
func (ic *Icon) Stroke(stroke string) *IconSets the stroke attribute.
StrokeWidth
func (ic *Icon) StrokeWidth(w string) *IconSets the stroke-width attribute.
Opacity
func (ic *Icon) Opacity(o float64) *IconSets the opacity attribute. Formatted with %g (e.g., 0.5 becomes "0.5", 1.0 becomes "1").
Rotate
func (ic *Icon) Rotate(degrees float64) *IconAppends a CSS rotate(Xdeg) function to the style attribute's transform declaration. If a transform already exists in the style, the function is appended to it. Otherwise a new transform declaration is added.
Flip
func (ic *Icon) Flip(direction string) *IconAppends a CSS scale transform to the style attribute. Accepted values:
| Direction | CSS function |
|---|---|
"h" or "horizontal" |
scaleX(-1) |
"v" or "vertical" |
scaleY(-1) |
"both" |
scale(-1, -1) |
| Any other value | scaleX(-1) (default) |
Chaining Rotate and Flip produces a single transform declaration.
Title
func (ic *Icon) Title(title string) *IconPrepends a <title> element to the SVG content. The title text is HTML-escaped via html.EscapeString.
Output methods
ToHTML
func (ic *Icon) ToHTML() stringRenders the icon as a complete <svg> element string. Attributes are sorted alphabetically for deterministic output. Attribute names are validated against ^[a-zA-Z_:][\w:.\-]*$; invalid names are silently dropped. Attribute values are HTML-escaped.
String
func (ic *Icon) String() stringImplements fmt.Stringer. Returns the same value as ToHTML.
Content
func (ic *Icon) Content() stringReturns the SVG inner content without the <svg> wrapper.
Attributes
func (ic *Icon) Attributes() map[string]stringReturns a deep copy of the icon's attribute map. Modifying the returned map does not affect the icon.
IsEmpty
func (ic *Icon) IsEmpty() boolReports whether the icon has no inner content (empty string).
ViewBox
func (ic *Icon) ViewBox() (minX, minY, w, h int)Parses the viewBox attribute and returns its four components. Falls back to width/height attributes (with minX=0, minY=0) if viewBox is absent. Returns 0, 0, 0, 0 when neither is available.
ViewBoxSize
func (ic *Icon) ViewBoxSize() (w, h int)Returns the width and height from the viewBox. Convenience wrapper around ViewBox().
See also
- Icon Manipulation: practical guide to the fluent API with chaining examples
- Attribute Management: how attributes merge across five layers when the manager resolves an icon