Skip to content

Latest commit

 

History

History
38 lines (29 loc) · 1.46 KB

vtext.md

File metadata and controls

38 lines (29 loc) · 1.46 KB

VText

VText is a representation of a Text node.

In virtual-dom, a VText is turned into an actual text node with the createElement function. You can read the code at vdom/create-element

createElement turns a VText into a Text node using document#createTextNode.

Full Example

var createElement = require("virtual-dom").create
var VNode = require("virtual-dom/vnode/vnode")
var VText = require("virtual-dom/vnode/vtext")
var myText = new VText("Hello, World")

// Pass our VText as a child of our VNode
var myNode = new VNode("div", { id: "my-node" }, [myText])

var myElem = createElement(myNode)
document.body.appendChild(myElem)
// Will result in a dom string that looks like <div id="my-node">Hello, World</div>

Arguments

text
The string you would like the text node to contain.

HTML Injection

document#createTextNode will defend against HTML injection. You could use the innerHTML property, but it will most likely break virtual dom.

escapedText = new VText('<span>Example</span>')
escapedNode = new VNode('div', null, [escapedText])
// Will enter the dom as <div>&lt;span&gt;Example&lt;/span&gt;</div>

unescapedNode = new VNode('div', { innerHTML: "<span>Example</span>" })
// Will enter the dom as <div><span>Example</span></div>
// You should probably never do this