JSPM

hast-util-from-text

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 47
  • Score
    100M100P100Q83128F
  • License MIT

hast utility to set the plain-text value of a node according to the `innerText` algorithm

Package Exports

  • hast-util-from-text

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hast-util-from-text) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

hast-util-from-text

Build Coverage Downloads Size Sponsors Backers Chat

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.

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.

License

MIT © Titus Wormer