Skip to content

HTML constructor should only accept AbstractString #38540

@clarkevans

Description

@clarkevans

The Base.HTML datatype seems an ideal wrapper to indicate that a given string object is suitable for direct presentation to a browser. However, currently the HTML type's constructor accepts any sort of object for its argument and does not use the object's text/html representation.

function HTML(xs...)
    HTML() do io
        for x in xs
            print(io, x)
        end
    end
end

By printing, without providing the text/html mimetype, the HTML constructor is not using the hypertext representation of an object, as one would expect. Indeed, it doesn't even handle nesting of itself in a reasonable way.

julia> fragment = html"<span>Hello!</span>"
HTML{String}("<span>Hello!</span>")

julia> display("text/html", fragment)
<span>Hello!</span>

julia> display("text/html", HTML(fragment))
HTML{String}("<span>Hello!</span>")

This inability to handle itself (or other objects with hypertext rendering) poses a challenge when trying to use HTML as a unit of composition between various libraries and components that produce HTML.

A conservative option is to limit the HTML constructor to only AbstractString.

Long term, a coherent design rationale for this datatype and supporting operations could be worked out and implemented. For example, join could be made to accept an array of HTML objects and providing an HTML output of the concatenated content strings.

As more and more applications of Julia involve deep interactions with a web browser, having a standardized unit of composition for hypertext fragments would be immensely useful. This ticket could be discussed in a discourse thread.

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