Skip to content

Commit 7d72cfc

Browse files
committed
.
0 parents  commit 7d72cfc

10 files changed

+358
-0
lines changed

.editorconfig

+9
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

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
*.log
3+
.nyc_output/
4+
coverage/
5+
node_modules/
6+
yarn.lock

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
*.json
3+
*.md

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- lts/dubnium
4+
- node
5+
after_script: bash <(curl -s https://codecov.io/bash)

index.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = toString
2+
3+
function toString(node) {
4+
// A root or an element
5+
if ('children' in node) {
6+
return all(node)
7+
}
8+
9+
return 'value' in node ? node.value : ''
10+
}
11+
12+
function one(node) {
13+
if (node.type === 'text') {
14+
return node.value
15+
}
16+
17+
// Ignore things like comments, instruction, cdata.
18+
return node.children ? all(node) : ''
19+
}
20+
21+
function all(node) {
22+
var children = node.children
23+
var index = -1
24+
var result = []
25+
26+
while (++index < children.length) {
27+
result[index] = one(children[index])
28+
}
29+
30+
return result.join('')
31+
}

license

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2020 Titus Wormer <[email protected]>
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

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"name": "xast-util-to-string",
3+
"version": "0.0.0",
4+
"description": "xast utility to get the text value of a node",
5+
"license": "MIT",
6+
"keywords": [
7+
"unist",
8+
"xast",
9+
"xast-util",
10+
"util",
11+
"utility",
12+
"xml",
13+
"string",
14+
"content",
15+
"text"
16+
],
17+
"repository": "syntax-tree/xast-util-to-string",
18+
"bugs": "https://github.com/syntax-tree/xast-util-to-string/issues",
19+
"funding": {
20+
"type": "opencollective",
21+
"url": "https://opencollective.com/unified"
22+
},
23+
"author": "Titus Wormer <[email protected]> (https://wooorm.com)",
24+
"contributors": [
25+
"Titus Wormer <[email protected]> (https://wooorm.com)"
26+
],
27+
"files": [
28+
"index.js"
29+
],
30+
"dependencies": {},
31+
"devDependencies": {
32+
"nyc": "^15.0.0",
33+
"prettier": "^2.0.0",
34+
"remark-cli": "^9.0.0",
35+
"remark-preset-wooorm": "^8.0.0",
36+
"tape": "^5.0.0",
37+
"unist-builder": "^2.0.0",
38+
"xo": "^0.33.0"
39+
},
40+
"scripts": {
41+
"format": "remark . -qfo && prettier . --write && xo --fix",
42+
"test-api": "node test",
43+
"test-coverage": "nyc --reporter lcov tape test.js",
44+
"test": "npm run format && npm run test-coverage"
45+
},
46+
"nyc": {
47+
"check-coverage": true,
48+
"lines": 100,
49+
"functions": 100,
50+
"branches": 100
51+
},
52+
"prettier": {
53+
"tabWidth": 2,
54+
"useTabs": false,
55+
"singleQuote": true,
56+
"bracketSpacing": false,
57+
"semi": false,
58+
"trailingComma": "none"
59+
},
60+
"xo": {
61+
"prettier": true,
62+
"esnext": false
63+
},
64+
"remarkConfig": {
65+
"plugins": [
66+
"preset-wooorm"
67+
]
68+
}
69+
}

readme.md

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# xast-util-to-string
2+
3+
[![Build][build-badge]][build]
4+
[![Coverage][coverage-badge]][coverage]
5+
[![Downloads][downloads-badge]][downloads]
6+
[![Size][size-badge]][size]
7+
[![Sponsors][sponsors-badge]][collective]
8+
[![Backers][backers-badge]][collective]
9+
[![Chat][chat-badge]][chat]
10+
11+
[**xast**][xast] utility to get the plain text value of a [*node*][node].
12+
13+
This is like the DOMs `Node#textContent` getter but there are some deviations.
14+
The resulting text is returned.
15+
16+
## Install
17+
18+
[npm][]:
19+
20+
```sh
21+
npm install xast-util-to-string
22+
```
23+
24+
## Use
25+
26+
```js
27+
var x = require('xastscript')
28+
var toString = require('xast-util-to-string')
29+
30+
var tree = x(
31+
'ncx',
32+
{xmlns: 'http://www.daisy.org/z3986/2005/ncx/', version: '2005-1'},
33+
[
34+
x('head', [
35+
x('meta', {name: 'dtb:uid', content: 'urn:isbn:9781234567891'})
36+
]),
37+
x('docTitle', [x('text', 'A Christmas Carol')]),
38+
x('docAuthor', [x('text', 'Charles Dickens')])
39+
]
40+
)
41+
42+
console.log(toString(tree))
43+
```
44+
45+
Yields:
46+
47+
```txt
48+
A Christmas CarolCharles Dickens
49+
```
50+
51+
## API
52+
53+
### `toString(node)`
54+
55+
Utility to get the plain text value of a [*node*][node].
56+
If the node has a `value` field ([*cdata*][cdata], [*comment*][comment],
57+
[*doctype*][doctype], [*instruction*][instruction], or [*text*][text]), returns
58+
it.
59+
If the node has a `children` field ([*root*][root] or [*element*][element]),
60+
recurses into it to concatenate all [*text*][text]s.
61+
62+
###### Returns
63+
64+
`string` — Serialized `node`.
65+
66+
## Security
67+
68+
`xast-util-to-string` does not change the syntax tree so there are no openings
69+
for [cross-site scripting (XSS)][xss] attacks.
70+
71+
## Related
72+
73+
* [`xast-util-to-xml`](https://github.com/syntax-tree/xast-util-to-xml)
74+
— serialize xast to XML
75+
* [`hast-util-to-string`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-to-string)
76+
— get the plain-text value (`textContent`)
77+
* [`hast-util-to-text`](https://github.com/syntax-tree/hast-util-to-text)
78+
— get the plain-text value (`innerText`)
79+
* [`hast-util-from-text`](https://github.com/syntax-tree/hast-util-from-text)
80+
— set the plain-text value (`innerText`)
81+
* [`hast-util-from-string`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-from-string)
82+
— set the plain-text value (`textContent`)
83+
84+
## Contribute
85+
86+
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
87+
started.
88+
See [`support.md`][support] for ways to get help.
89+
90+
This project has a [code of conduct][coc].
91+
By interacting with this repository, organization, or community you agree to
92+
abide by its terms.
93+
94+
## License
95+
96+
[MIT][license] © [Titus Wormer][author]
97+
98+
<!-- Definitions -->
99+
100+
[build-badge]: https://img.shields.io/travis/syntax-tree/xast-util-to-string.svg
101+
102+
[build]: https://travis-ci.org/syntax-tree/xast-util-to-string
103+
104+
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/xast-util-to-string.svg
105+
106+
[coverage]: https://codecov.io/github/syntax-tree/xast-util-to-string
107+
108+
[downloads-badge]: https://img.shields.io/npm/dm/xast-util-to-string.svg
109+
110+
[downloads]: https://www.npmjs.com/package/xast-util-to-string
111+
112+
[size-badge]: https://img.shields.io/bundlephobia/minzip/xast-util-to-string.svg
113+
114+
[size]: https://bundlephobia.com/result?p=xast-util-to-string
115+
116+
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
117+
118+
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
119+
120+
[collective]: https://opencollective.com/unified
121+
122+
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
123+
124+
[chat]: https://github.com/syntax-tree/unist/discussions
125+
126+
[npm]: https://docs.npmjs.com/cli/install
127+
128+
[license]: license
129+
130+
[author]: https://wooorm.com
131+
132+
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
133+
134+
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
135+
136+
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
137+
138+
[xast]: https://github.com/syntax-tree/xast
139+
140+
[node]: https://github.com/syntax-tree/xast#nodes
141+
142+
[root]: https://github.com/syntax-tree/xast#root
143+
144+
[comment]: https://github.com/syntax-tree/xast#comment
145+
146+
[cdata]: https://github.com/syntax-tree/xast#cdata
147+
148+
[doctype]: https://github.com/syntax-tree/xast#doctype
149+
150+
[element]: https://github.com/syntax-tree/xast#element
151+
152+
[instruction]: https://github.com/syntax-tree/xast#instruction
153+
154+
[text]: https://github.com/syntax-tree/xast#text
155+
156+
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting

test.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict'
2+
3+
var test = require('tape')
4+
var u = require('unist-builder')
5+
var toString = require('.')
6+
7+
test('xast-util-to-string', function (t) {
8+
t.deepEqual(
9+
toString(u('cdata', '<greeting>Hello, world!</greeting>')),
10+
'<greeting>Hello, world!</greeting>',
11+
'should serialize cdata'
12+
)
13+
14+
t.deepEqual(toString(u('comment', 'foo')), 'foo', 'should serialize comments')
15+
16+
t.deepEqual(
17+
toString(u('instruction', {name: 'xml'}, 'version="1.0" encoding="UTF-8"')),
18+
'version="1.0" encoding="UTF-8"',
19+
'should serialize instructions'
20+
)
21+
22+
t.deepEqual(toString(u('text', 'foo')), 'foo', 'should serialize texts')
23+
24+
t.deepEqual(
25+
toString(u('doctype', {name: 'html'})),
26+
'',
27+
'should return empty for doctypes'
28+
)
29+
30+
t.deepEqual(
31+
toString(
32+
u('element', {name: 'package'}, [
33+
u('text', 'foo '),
34+
u('comment', 'bar'),
35+
u('element', {name: 'thing'}, [u('text', ' baz')])
36+
])
37+
),
38+
'foo baz',
39+
'should serialize elements (excluding non-parent and non-text descendants)'
40+
)
41+
42+
t.deepEqual(
43+
toString(
44+
u('root', [
45+
u('doctype', {name: 'html'}),
46+
u('text', 'foo '),
47+
u('comment', 'bar'),
48+
u('element', {name: 'thing'}, [u('text', ' baz')])
49+
])
50+
),
51+
'foo baz',
52+
'should serialize roots (excluding non-parent and non-text descendants)'
53+
)
54+
55+
t.end()
56+
})

0 commit comments

Comments
 (0)