hast-util-from-text
hast utility to get the plain-text value of a node.
This is like the DOMs Node#innerText
setter.
You’d typically want to use hast-util-from-string
(textContent
), but hast-util-from-text
(innerText
) adds <br>
elements instead of line breaks.
Install
npm:
npm install hast-util-from-text
Use
var h = require('hastscript')
var fromText = require('hast-util-from-text')
fromText(h('p'), 'Alpha')
// { type: 'element',
// tagName: 'p',
// properties: {},
// children: [ { type: 'text', value: 'Alpha' } ] }
fromText(h('p', [h('b', 'Bravo'), '.']), 'Charlie')
// { type: 'element',
// tagName: 'p',
// properties: {},
// children: [ { type: 'text', value: 'Charlie' } ] }
fromText(h('p'), 'Delta\nEcho')
// { type: 'element',
// tagName: 'p',
// properties: {},
// children:
// [ { type: 'text', value: 'Delta' },
// { type: 'element', tagName: 'br', properties: {}, children: [] },
// { type: 'text', value: 'Echo' } ] }
API
fromText(node[, value])
If the given node
is a literal, set that to the given value
or an empty string. If the given node
is a parent, its children are replaced with new children: texts for every run of text and <br>
elements for every line break (a line feed, \n
; a carriage return, \r
; or a carriage return + line feed, \r\n
). If no value
is given (empty string ''
, null
, or undefined
), the literal’s value is set to an empty string or the parent’s children are removed.
Security
Improper use can open you up to a cross-site scripting (XSS) attack as value
is injected into the syntax tree. If operating on a <script>
element, value
will run in a browser.
Do not use user input in value
when operating on script
elements or use hast-util-santize
.
Related
hast-util-to-text
— Get the plain-text value (innerText
)hast-util-to-string
— Get the plain-text value (textContent
)hast-util-from-string
— Set the plain-text value (textContent
)
Contribute
See contributing.md
in syntax-tree/.github
for ways to get started. See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.