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.

Source

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 nil if the id attribute is not set for the element

id(el, id)

Sets the id of an element

new()

Creates a new Eml.Element structure with default values

new(tag, attrs \\ %{}, content \\ [])

Creates a new Eml.Element structure

remove(el, to_remove)

Removes content from an element that matches any term in the to_remove list

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]

attr_value_in :: String.t | atom | number | Eml.Parameter.t | [String.t | atom | number | Eml.Parameter.t]

t :: %Eml.Element{tag: atom, content: Eml.content, attrs: attrs}

Functions

add(el, data, opts \\ [])

Specs:

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 !!!"]>
Source
attr(element, field)

Specs:

Gets a specific attribute.

If the attribute does not exist, nil is returned.

Source
attr(el, field, value)

Specs:

Sets a attribute.

If the attribute already exists, the old value gets overwritten.

Source
attrs(element)

Specs:

Gets the attributes map of an element.

Source
attrs(el, attrs)

Specs:

Merges the passed attributes with the current attributes.

Source
class(element)

Specs:

Gets the class or classes of an element.

Multiple classes are stored in the form ["class1", "class2"].

Source
class(el, class)

Specs:

Sets the class or classes of an element.

Multiple classes can be assigned by providing a list of strings.

Source
content(element)

Specs:

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.

Source
content(el, data)

Specs:

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"]>
Source
has?(el, opts)

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
Source
id(element)

Specs:

Gets the id of an element. Returns nil if the id attribute is not set for the element.

Source
id(el, id)

Specs:

Sets the id of an element.

Source
new()

Specs:

  • new :: t

Creates a new Eml.Element structure with default values.

Source
new(tag, attrs \\ %{}, content \\ [])

Specs:

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>"
Source
remove(el, to_remove)

Specs:

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<>
Source
remove_attr(el, field)

Specs:

  • remove_attr(t, atom) :: t

Removes an attribute from an element.

Source
tag(element)

Specs:

  • tag(t) :: atom

Gets the tag of an element.

Source
tag(el, tag)

Specs:

  • tag(t, atom) :: t

Sets the tag of an element.

Source
update(el, fun)

Specs:

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"]>
Source