# cotonic.tokenizer

Source: https://zotonic.com/cotonic/cotonic_tokenizer

URI: https://zotonic.com/id/2731

Language: en



<a name="cotonic.tokenizer"></a>
The tokenizer transforms text to html tokens. The tokenizer is used by the user interface composer in order to call the incremental-dom api. It uses incremental-dom to do in-place diffing of the dom-tree.  
*Note: The tokenizer does not parse or validate the html. It just tokenizes the input.*
<!-- tokens --> <a name="cotonic.tokenizer.tokens"></a>
tokens `cotonic.tokenizer.tokens(text)`
---------------------------------------

Transforms a string with html tags into a list of tokens. The tokens are objects of the form: <tt>{type: "type", [args]}</tt>, with type being one of:

<dl>
                <dt><tt>open</tt></dt>
                <dd>Represents an open tag. Contains the attribute <tt>tag</tt> which is
                set to the tagname of the element, and the attribute <tt>attributes</tt>
                which is a list of attributes of the element.
                </dd>

                <dt><tt>close</tt></dt>
                <dd>Represents a close tag. Contains the attribute <tt>tag</tt> which is
                set to the tagname of the close element.</dd>

                <dt><tt>void</tt></dt>
                <dd>Represents a void element. The attribute <tt>tag</tt> is set to the
                tagname of the void element.</dd>

                <dt><tt>text</tt></dt>
                <dd>Represents a test element. The attribute <tt>data</tt> is set to the
                text data.</dd>

                <dt><tt>doctype</tt></dt>
                <dd>Represents a doctype element. The attribute <tt>attributes</tt> is
                set to the attributes of the element.</dd>

                <dt><tt>pi</tt></dt>
                <dd>Represents a processing instruction element. The attribute <tt>tag</tt>
                is set to the tagname of the processing instruction. The attribute
                <tt>arguments</tt> contains the list of attributes.</dd>

                <dt><tt>comment</tt></dt>
                <dd>Represents a comment element. The attribute <tt>data</tt> contains
                the text in the comment element.</dd>
            </dl>

```
cotonic.tokenizer.tokens("<div class='example'>Tokenizing<br /> is cool</div>");
=> [{type: "open", tag: "div", attributes: ["class", "example"]},
    {type: "text", data: "Tokenizing"},
    {type: "void", tag: "br", attributes: []},
    {type: "text", data: " is cool"},
    {type: "close", tag: "div"}] 
```
<!-- charref --> <a name="cotonic.tokenizer.charref"></a>
charref `cotonic.tokenizer.charref(text)`
-----------------------------------------

Transforms a html charref into a character.

```
cotonic.tokenizer.charref("#128540");
=> "😜"
cotonic.tokenizer.charref("amp");
=> "&"
```

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/2731?language=en


## API access

- [API help](<https://zotonic.com/api-help>)
- [OpenAPI specification](<https://zotonic.com/openapi.json>)
- [This resource as JSON](<https://zotonic.com/api/agent/resource/2731?language=en>)
