Eml.Element
Eml.Element
defines a struct that represents an element in Eml.
In practice, you will mostly use the element macro’s instead of directly
creating Eml.Element
structs, but the functions in this module can be
valuable when querying, manipulating or transforming eml
.
Summary↑
add(el, data, opts \\ []) | Adds content to an element |
attr(element, field) | Gets a specific attribute |
attr(el, field, value) | Sets a attribute |
attrs(element) | Gets the attributes map of an element |
attrs(el, attrs) | Merges the passed attributes with the current attributes |
class(element) | Gets the class or classes of an element |
class(el, class) | Sets the class or classes of an element |
content(element) | Gets the content of an element |
content(el, data) | Sets the content of an element |
has?(el, opts) | Returns true if all properties of the opts argument are matching with the provided element |
id(element) | Gets the id of an element.
Returns |
id(el, id) | Sets the id of an element |
new() | Creates a new |
new(tag, attrs \\ %{}, content \\ []) | Creates a new |
remove(el, to_remove) | Removes content from an element that matches any term in the |
remove_attr(el, field) | Removes an attribute from an element |
tag(element) | Gets the tag of an element |
tag(el, tag) | Sets the tag of an element |
update(el, fun) | Update content in an element by calling fun on the content to get new content |
Types ↑
attr_name :: atom
attr_value :: String.t | Eml.Parameter.t | [String.t | Eml.Parameter]
attrs :: %{attr_name => attr_value}
attr_value_in :: String.t | atom | number | Eml.Parameter.t | [String.t | atom | number | Eml.Parameter.t]
attrs_in :: [{attr_name, attr_value_in}] | %{attr_name => attr_value_in}
t :: %Eml.Element{tag: atom, content: Eml.content, attrs: attrs}
Functions
Specs:
- add(t, Eml.Data.t, Keyword.t) :: t
Adds content to an element.
Before being added to the element, input
data is parsed to valid eml
.
Example
iex> div = Eml.Element.new(:div, [], [])
#div<>
iex> div = Eml.Element.content(div, ["Hallo ", 2, 0, [1, 5]])
#div<["Hallo 2015"]>
iex> Eml.Element.add(div, " !!!")
#div<["Hallo 2015 !!!"]>
Specs:
- attr(t, atom) :: attr_value
Gets a specific attribute.
If the attribute does not exist, nil is returned.
Specs:
- attr(t, atom, attr_value_in) :: t
Sets a attribute.
If the attribute already exists, the old value gets overwritten.
Specs:
Gets the attributes map of an element.
Specs:
Merges the passed attributes with the current attributes.
Specs:
- class(t) :: attr_value
Gets the class or classes of an element.
Multiple classes are stored in the form ["class1", "class2"]
.
Specs:
- class(t, attr_value_in) :: t
Sets the class or classes of an element.
Multiple classes can be assigned by providing a list of strings.
Specs:
- content(t) :: Eml.content
Gets the content of an element.
Note that content in Eml always is a list, so when an element’s content is empty, it returns an empty list.
Specs:
- content(t, Eml.Data.t) :: t
Sets the content of an element.
Before being assigned to the element, input
data is parsed to valid eml
.
Example
iex> div = Eml.Element.new(:div, [], [])
#div<>
iex> Eml.Element.content(div, ["Hallo ", 2, 0, [1, 5]])
#div<["Hallo 2015"]>
Specs:
Returns true if all properties of the opts argument are matching with the provided element.
Example
iex> e = Eml.Element.new(:img, id: "duck-photo", src: "http://i.imgur.com/4xPWp.jpg")
#img<%{id: "duck-photo", src: "http://i.imgur.com/4xPWp.jpg"}>
iex> Eml.Element.has?(e, id: "duck-photo")
true
iex> Eml.Element.has?(e, src: "http://i.imgur.com/4xPWp.jpg")
true
iex> Eml.Element.has?(e, src: "http://i.imgur.com/4xPWp.jpg", id: "wrong")
false
Specs:
- id(t) :: attr_value
Gets the id of an element.
Returns nil
if the id
attribute is not set for the element.
Specs:
- id(t, attr_value_in) :: t
Sets the id of an element.
Specs:
- new :: t
Creates a new Eml.Element
structure with default values.
Specs:
- new(atom, attrs_in, Eml.Data.t) :: t
Creates a new Eml.Element
structure.
Example
iex> e = Eml.Element.new(:div, [id: 42], "hallo!")
#div<%{id: "42"} ["hallo!"]>
iex> Eml.render(e)
"<div id='42'>hallo!</div>"
Specs:
- remove(t, Eml.t | Eml.content) :: t
Removes content from an element that matches any term in the to_remove
list.
Example
iex> div1 = Eml.Element.new(:div, [], "hallo")
#div<["hallo"]>
iex> div2 = Eml.Element.new(:div, [], "world")
#div<["world"]>
iex> div3 = Eml.Element.new(:div, [], [div1, div2])
#div<[#div<["hallo"]>, #div<["world"]>]>
iex> Eml.Element.remove(div3, [div1, div2])
#div<>
Specs:
Removes an attribute from an element.
Specs:
- tag(t) :: atom
Gets the tag of an element.
Specs:
Sets the tag of an element.
Specs:
- update(t, (Eml.t -> Eml.Data.t)) :: t
Update content in an element by calling fun on the content to get new content.
Before being added to the element, input
data is parsed to valid eml
.
Example
iex> div = Eml.Element.new(:div, [], "hallo")
#div<["hallo"]>
iex> Eml.Element.update(div, fn content -> String.upcase(content) end)
#div<["HALLO"]>