Carvex.Attributes (Carvex v0.0.1)

Copy Markdown View Source

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

  • id - the #id, or nil
  • classes - the .class names, in order
  • key_values - other key=value pairs (string keys)
  • order - the written order of the slots, as "#id", ".class" or a key name; [] when not recorded. Used by Carvex.to_carve/2.

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

Summary

Functions

Appends a class.

Removes key.

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", otherwise the key/value entry. nil when absent.

Sets key. "class" replaces the classes with the whitespace-split value.

Types

t()

@type t() :: %Carvex.Attributes{
  classes: [String.t()],
  id: String.t() | nil,
  key_values: %{required(String.t()) => String.t()},
  order: [String.t()]
}

Functions

add_class(attrs, class)

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

Appends a class.

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

delete(attrs, key)

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

Removes key.

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

empty?(attrs)

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

True for nil or an attribute set with nothing in it.

iex> Carvex.Attributes.empty?(nil)
true
iex> Carvex.Attributes.empty?(%Carvex.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", otherwise the key/value entry. nil when absent.

iex> attrs = %Carvex.Attributes{id: "x", classes: ["a", "b"], key_values: %{"k" => "v"}}
iex> Carvex.Attributes.get(attrs, "class")
"a b"
iex> Carvex.Attributes.get(attrs, "id")
"x"
iex> Carvex.Attributes.get(attrs, "k")
"v"
iex> Carvex.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.

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