Markup.Carve.Attributes (Markup.Carve v0.1.0)

Copy Markdown View Source

A node's attributes, as carve's tree carries them.

  • id - the #id, or nil
  • classes - the .class names, in order
  • key_values - other key=value pairs, in written order, as {key, value} tuples with string keys

Every function accepts nil as an empty set and returns a struct.

Summary

Functions

Appends a class.

Removes key.

Returns true for nil or an attribute set with nothing in it.

Returns the value for key: the id for "id", the classes joined with a space for "class", and the key/value entry for anything else. Returns nil when the key isn't set.

Sets key. "class" replaces the classes with the whitespace-split value; a new key is appended, an existing one keeps its place.

Types

t()

@type t() :: %Markup.Carve.Attributes{
  classes: [String.t()],
  id: String.t() | nil,
  key_values: [{String.t(), String.t()}]
}

Functions

add_class(attrs, class)

@spec add_class(t() | nil, String.t()) :: t()

Appends a class.

iex> Markup.Carve.Attributes.add_class(%Markup.Carve.Attributes{classes: ["a"]}, "b")
%Markup.Carve.Attributes{classes: ["a", "b"]}

delete(attrs, key)

@spec delete(t() | nil, String.t()) :: t()

Removes key.

iex> Markup.Carve.Attributes.delete(%Markup.Carve.Attributes{id: "x", classes: ["a"]}, "class")
%Markup.Carve.Attributes{id: "x"}

empty?(attrs)

@spec empty?(t() | nil) :: boolean()

Returns true for nil or an attribute set with nothing in it.

iex> Markup.Carve.Attributes.empty?(nil)
true
iex> Markup.Carve.Attributes.empty?(%Markup.Carve.Attributes{id: "x"})
false

get(arg1, key)

@spec get(t() | nil, String.t()) :: String.t() | nil

Returns the value for key: the id for "id", the classes joined with a space for "class", and the key/value entry for anything else. Returns nil when the key isn't set.

iex> attrs = %Markup.Carve.Attributes{id: "x", classes: ["a", "b"], key_values: [{"k", "v"}]}
iex> Markup.Carve.Attributes.get(attrs, "class")
"a b"
iex> Markup.Carve.Attributes.get(attrs, "id")
"x"
iex> Markup.Carve.Attributes.get(attrs, "k")
"v"
iex> Markup.Carve.Attributes.get(nil, "class")
nil

put(attrs, key, value)

@spec put(t() | nil, String.t(), String.t()) :: t()

Sets key. "class" replaces the classes with the whitespace-split value; a new key is appended, an existing one keeps its place.

iex> Markup.Carve.Attributes.put(nil, "class", "a b")
%Markup.Carve.Attributes{classes: ["a", "b"]}
iex> Markup.Carve.Attributes.put(%Markup.Carve.Attributes{id: "x"}, "k", "v")
%Markup.Carve.Attributes{id: "x", key_values: [{"k", "v"}]}