Skip to content

Commit 31403fd

Browse files
committed
Initial commit
0 parents  commit 31403fd

File tree

10 files changed

+419
-0
lines changed

10 files changed

+419
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
*.log
3+
.nyc_output/
4+
coverage/
5+
node_modules/
6+
hast-util-from-text.js
7+
hast-util-from-text.min.js
8+
yarn.lock

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
hast-util-from-text.js
3+
hast-util-from-text.min.js

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
node_js:
3+
- lts/boron
4+
- node
5+
after_script: bash <(curl -s https://codecov.io/bash)
6+
deploy:
7+
provider: releases
8+
skip_cleanup: true
9+
api_key:
10+
secure: ocAa3z+P9JiMb7khpruMLyQ2+BfHh70mR4PYeJn+Ziuxbq/tVFrlrRJ5pU5heOXVl0YnILqo+itxUSAsdjhBA6rOErZ663Xe7aeoSIWUgLZr29GI2IhSEQQOhvmZoZyVLhJAXZ2s4D6Uo4FMS/OtmWLMutBVrvZsDP/Z77j3yfEA/NwsOLzlaKWl/cIbnqXFUxYcGwtvr7WaiXc94wvFdPQVKpCFdAx6EFAwVn9poofxIK/EDTAILSghzd+gpBSvRjV7X7RIVwn44WdT/IbybILM45J1/jQ7vlxqPICtSpU81bcd1AaV1xj7qt8tr+zIXdazAaIlpbBJyyDyKvERSk0oIofn34ZncvvhiQxfLmFSmeC85SFkm0jWjAmPVC9b0YQYwRHNai47GaAfLu1n2ZJvdRIrAqS6d1K1dP1GrxjzbFA3QpP2uCCQ1Q8h9RH4gtKyJIbj6pMDkYPocaLneU+xUIogSoxFd2E7ZTNqvL3djXjus9IYXu/l+GbudEpA9xfJSvl99mLmGAOjIyhOWWYG5G2YNr+k8eCReIYmhbXTrc/r9yOAT020TyOCYt+hTXdllsrrHJZIQIZNL+JWtwgyiJDGfbAp7z5FxzVzBsJW/xet8AbI6Hukhcu/KLxnffS8V/UUS42Y3Hv/syzgLRBSVr7w/L5SocVU0f/t9Ec=
11+
file:
12+
- 'hast-util-from-text.js'
13+
- 'hast-util-from-text.min.js'
14+
on:
15+
tags: true

index.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict'
2+
3+
var eof = 0 // ''
4+
var lineFeed = 10 // '\n'
5+
var carriageReturn = 13 // '\r'
6+
7+
module.exports = fromText
8+
9+
// Implementation of the `innerText` setter:
10+
// <https://html.spec.whatwg.org/#the-innertext-idl-attribute>
11+
// Note that `innerText` only exists on element. In this utility, we accept
12+
// all parent nodes and handle them as elements, and for all literals we set
13+
// the `value` of the given node the the given value.
14+
function fromText(node, value) {
15+
var fn = 'children' in node ? setParent : setLiteral
16+
var val = value == null ? '' : String(value)
17+
18+
fn(node, val)
19+
20+
return node
21+
}
22+
23+
function setParent(node, value) {
24+
var nodes = []
25+
var length = value.length
26+
var index = 0
27+
var start = 0
28+
var end = -1
29+
var char = 0
30+
var br = false
31+
32+
node.children = []
33+
34+
while (index <= length) {
35+
char = index === length ? 0 : value.charCodeAt(index)
36+
37+
switch (char) {
38+
case eof:
39+
end = index
40+
break
41+
case lineFeed:
42+
end = index
43+
br = true
44+
break
45+
case carriageReturn:
46+
end = index
47+
br = true
48+
49+
if (value.charCodeAt(index + 1) === lineFeed) {
50+
index++
51+
}
52+
53+
break
54+
default:
55+
break
56+
}
57+
58+
index++
59+
60+
if (end !== -1) {
61+
if (end !== start) {
62+
nodes.push({type: 'text', value: value.slice(start, end)})
63+
}
64+
65+
if (br) {
66+
br = false
67+
nodes.push({
68+
type: 'element',
69+
tagName: 'br',
70+
properties: {},
71+
children: []
72+
})
73+
}
74+
75+
end = -1
76+
start = index
77+
}
78+
}
79+
80+
node.children = nodes
81+
}
82+
83+
function setLiteral(node, value) {
84+
node.value = value
85+
}

license

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2019 Titus Wormer <tituswormer@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package.json

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"name": "hast-util-from-text",
3+
"version": "0.0.0",
4+
"description": "Set the plain-text value of a node according to the `innerText` algorithm",
5+
"license": "MIT",
6+
"keywords": [
7+
"rehype",
8+
"hast",
9+
"string",
10+
"innertext"
11+
],
12+
"repository": "syntax-tree/hast-util-from-text",
13+
"bugs": "https://github.com/syntax-tree/hast-util-from-text/issues",
14+
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
15+
"contributors": [
16+
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
17+
],
18+
"files": [
19+
"index.js"
20+
],
21+
"dependencies": {},
22+
"devDependencies": {
23+
"browserify": "^16.0.0",
24+
"hastscript": "^5.0.0",
25+
"nyc": "^13.0.0",
26+
"prettier": "^1.13.5",
27+
"remark-cli": "^6.0.0",
28+
"remark-preset-wooorm": "^4.0.0",
29+
"tape": "^4.0.0",
30+
"tinyify": "^2.4.3",
31+
"unist-builder": "^1.0.3",
32+
"xo": "^0.24.0"
33+
},
34+
"scripts": {
35+
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
36+
"build-bundle": "browserify . -s hastUtilFromText > hast-util-from-text.js",
37+
"build-mangle": "browserify . -s hastUtilFromText -p tinyify > hast-util-from-text.min.js",
38+
"build": "npm run build-bundle && npm run build-mangle",
39+
"test-api": "node test",
40+
"test-coverage": "nyc --reporter lcov tape test.js",
41+
"test": "npm run format && npm run build && npm run test-coverage"
42+
},
43+
"nyc": {
44+
"check-coverage": true,
45+
"lines": 100,
46+
"functions": 100,
47+
"branches": 100
48+
},
49+
"prettier": {
50+
"tabWidth": 2,
51+
"useTabs": false,
52+
"singleQuote": true,
53+
"bracketSpacing": false,
54+
"semi": false,
55+
"trailingComma": "none"
56+
},
57+
"xo": {
58+
"prettier": true,
59+
"esnext": false,
60+
"rules": {
61+
"no-eq-null": "off",
62+
"eqeqeq": [
63+
"error",
64+
"always",
65+
{
66+
"null": "ignore"
67+
}
68+
]
69+
},
70+
"ignores": [
71+
"hast-util-from-text.js"
72+
]
73+
},
74+
"remarkConfig": {
75+
"plugins": [
76+
"preset-wooorm"
77+
]
78+
}
79+
}

readme.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# hast-util-from-text
2+
3+
[![Build][build-badge]][build]
4+
[![Coverage][coverage-badge]][coverage]
5+
[![Downloads][downloads-badge]][downloads]
6+
[![Chat][chat-badge]][chat]
7+
8+
Set the plain-text value of a [hast][] node.
9+
This is like the DOMs `Node#innerText` setter.
10+
The given node is returned.
11+
12+
You’d typically want to use [`hast-util-from-string`][from-string]
13+
(`textContent`), but `hast-util-from-text` (`innerText`) adds `<br>` elements
14+
instead of line breaks.
15+
16+
## Installation
17+
18+
[npm][]:
19+
20+
```bash
21+
npm install hast-util-from-text
22+
```
23+
24+
## Usage
25+
26+
```javascript
27+
var h = require('hastscript')
28+
var fromText = require('hast-util-from-text')
29+
30+
fromText(h('p'), 'Alpha')
31+
// { type: 'element',
32+
// tagName: 'p',
33+
// properties: {},
34+
// children: [ { type: 'text', value: 'Alpha' } ] }
35+
36+
fromText(h('p', [h('b', 'Bravo'), '.']), 'Charlie')
37+
// { type: 'element',
38+
// tagName: 'p',
39+
// properties: {},
40+
// children: [ { type: 'text', value: 'Charlie' } ] }
41+
42+
fromText(h('p'), 'Delta\nEcho')
43+
// { type: 'element',
44+
// tagName: 'p',
45+
// properties: {},
46+
// children:
47+
// [ { type: 'text', value: 'Delta' },
48+
// { type: 'element', tagName: 'br', properties: {}, children: [] },
49+
// { type: 'text', value: 'Echo' } ] }
50+
```
51+
52+
## API
53+
54+
### `fromText(node[, value])`
55+
56+
If `node` is a literal (has a `value` property), set that to the given `value`
57+
or an empty string.
58+
If `node` is a parent node (has `children`), its children are replaced with new
59+
children: text nodes for every run of text and `<br>` elements for every line
60+
break (`\n`, `\r`, or `\r\n`).
61+
If no `value` is given (empty string `''`, `null`, or `undefined`), all
62+
children are removed.
63+
64+
## Related
65+
66+
* [`hast-util-from-string`](https://github.com/rehypejs/rehype-minify/tree/master/packages/hast-util-from-string)
67+
— Set the plain-text value (`textContent`)
68+
* [`hast-util-to-string`](https://github.com/rehypejs/rehype-minify/tree/master/packages/hast-util-to-string)
69+
— Get the plain-text value (`textContent`)
70+
71+
## Contribute
72+
73+
See [`contributing.md` in `syntax-tree/hast`][contributing] for ways to get
74+
started.
75+
76+
This organisation has a [Code of Conduct][coc]. By interacting with this
77+
repository, organisation, or community you agree to abide by its terms.
78+
79+
## License
80+
81+
[MIT][license] © [Titus Wormer][author]
82+
83+
<!-- Definitions -->
84+
85+
[build-badge]: https://img.shields.io/travis/syntax-tree/hast-util-from-text.svg
86+
87+
[build]: https://travis-ci.org/syntax-tree/hast-util-from-text
88+
89+
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-from-text.svg
90+
91+
[coverage]: https://codecov.io/github/syntax-tree/hast-util-from-text
92+
93+
[downloads-badge]: https://img.shields.io/npm/dm/hast-util-from-text.svg
94+
95+
[downloads]: https://www.npmjs.com/package/hast-util-from-text
96+
97+
[chat-badge]: https://img.shields.io/badge/join%20the%20community-on%20spectrum-7b16ff.svg
98+
99+
[chat]: https://spectrum.chat/unified/rehype
100+
101+
[npm]: https://docs.npmjs.com/cli/install
102+
103+
[license]: license
104+
105+
[author]: https://wooorm.com
106+
107+
[hast]: https://github.com/syntax-tree/hast
108+
109+
[contributing]: https://github.com/syntax-tree/hast/blob/master/contributing.md
110+
111+
[coc]: https://github.com/syntax-tree/hast/blob/master/code-of-conduct.md
112+
113+
[from-string]: https://github.com/rehypejs/rehype-minify/tree/master/packages/hast-util-from-string

0 commit comments

Comments
 (0)