Gumbo.Tag (gumbo v0.3.0)

Copy Markdown View Source

The parse-free tag surface.

t/0 values are the atoms of gumbo's GumboTag enum, named after the normalized names gumbo itself reports (:div, :"annotation-xml"), with :unknown for GUMBO_TAG_UNKNOWN. Everything here goes straight to the corresponding gumbo_* helper; no parsing is involved.

Summary

Types

t()

A gumbo tag (GumboTag).

Functions

Returns every tag in enum order, including :unknown.

Converts a tag name back to its t/0 value.

Extracts the tag name from a start or end tag, in original (source) case.

Returns the normalized name for tag, or nil if tag is not a tag.

Returns the case-corrected name for an SVG tag, or nil when no replacement applies.

Returns whether tag is an HTML void element.

Types

t()

@type t() :: atom()

A gumbo tag (GumboTag).

Functions

all()

@spec all() :: [t()]

Returns every tag in enum order, including :unknown.

Examples

iex> Enum.take(Gumbo.Tag.all(), 3)
[:html, :head, :title]

iex> Enum.take(Gumbo.Tag.all(), -1)
[:unknown]

from_name(name)

@spec from_name(binary()) :: t()

Converts a tag name back to its t/0 value.

Length-aware and case-insensitive, like gumbo_tagn_enum; unknown names become :unknown.

Examples

iex> Gumbo.Tag.from_name("DIV")
:div

iex> Gumbo.Tag.from_name("my-tag")
:unknown

from_original_text(text)

@spec from_original_text(binary()) :: {:ok, binary()} | {:error, :not_a_tag}

Extracts the tag name from a start or end tag, in original (source) case.

Accepts exactly <tagname> and </tagname> with the trailing > — the shape the parser stores in original_tag and original_end_tag — and returns {:error, :not_a_tag} for anything else, including "<>" and "</>", whose extracted name would be empty. Inputs that would trip gumbo_tag_from_original_text's asserts never reach it.

Examples

iex> Gumbo.Tag.from_original_text("<my-tag data-a=1>")
{:ok, "my-tag"}

iex> Gumbo.Tag.from_original_text("my-tag")
{:error, :not_a_tag}

name(tag)

@spec name(t()) :: binary() | nil

Returns the normalized name for tag, or nil if tag is not a tag.

Out-of-range integers return nil instead of tripping the assert inside gumbo_normalized_tagname, which would abort the VM. :unknown returning "" is gumbo's own behaviour — GUMBO_TAG_UNKNOWN normalizes to the empty string — and Gumbo.Node.tag_name/1 is the accessor that returns a real name for unknown tags.

Examples

iex> Gumbo.Tag.name(:div)
"div"

iex> Gumbo.Tag.name(:unknown)
""

iex> Gumbo.Tag.name(155)
""

svg_name(name)

@spec svg_name(binary()) :: binary() | nil

Returns the case-corrected name for an SVG tag, or nil when no replacement applies.

Examples

iex> Gumbo.Tag.svg_name("foreignObject")
"foreignObject"

iex> Gumbo.Tag.svg_name("circle")
nil

void?(tag)

@spec void?(t()) :: boolean()

Returns whether tag is an HTML void element.

Examples

iex> Gumbo.Tag.void?(:br)
true

iex> Gumbo.Tag.void?(:div)
false