# `Markup.Carve.Attributes`
[🔗](https://github.com/paradox460/markup/blob/markup_carve-v0.1.0/markup_carve/lib/markup/carve/attributes.ex#L1)

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.

# `t`

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

# `add_class`

```elixir
@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`

```elixir
@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?`

```elixir
@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`

```elixir
@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`

```elixir
@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"}]}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
