Skip to content

Option to add trailing slash to void elements #15

@christophhagen

Description

@christophhagen

First off, thanks for starting this library. I'm currently experimenting with it to generate HTML pages with swift, and ran across an issue:

I created a custom path element to write inline svg:

public typealias path = HTMLVoidElement<HTMLTag.path>

public extension HTMLTag {

    enum path: HTMLTrait.Unpaired, HTMLTrait.RenderedInline { public static let name = "path" }
}

public extension HTMLAttribute where Tag == HTMLTag.path {

    static func fill(_ value: String) -> HTMLAttribute<HTMLTag.path> {
        .init(name: "fill", value: value)
    }

    static func d(_ value: String) -> HTMLAttribute<HTMLTag.path> {
        .init(name: "d", value: value)
    }
}

I can then write simple svg content:

svg(.id("some")) {
    path(.fill("currentColor"), .d("..."))
    path(.fill("currentColor"), .d("..."))
}

Once I render this, it produces the following HTML:

<svg id="some">
    <path fill="currentColor" d="...">
    <path fill="currentColor" d="...">
</svg>

I think this is valid HTML, but if I try to display the svg in Safari, it doesn't render correctly.
Apparently Safari needs a trailing slash for path, like so:

<svg id="some">
    <path fill="currentColor" d="..." />
    <path fill="currentColor" d="..." />
</svg>

Is there a way to force certain void elements to add a trailing slash when rendering? Or should I use a different approach?

I guess the alternative would be to use HTMLTrait.Paired instead of HTMLTrait.Unpaired, but that would make the HTML less compact.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions