Skip to content

Commit 18aa1a1

Browse files
committed
Change to only accept valid nodes, strings
1 parent a650df0 commit 18aa1a1

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

Diff for: index.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
/**
2+
* @typedef {import('hast').Nodes} Nodes
3+
*/
4+
5+
// HTML whitespace expression.
6+
// See <https://infra.spec.whatwg.org/#ascii-whitespace>.
7+
const re = /[ \t\n\f\r]/g
8+
19
/**
210
* Check if the given value is *inter-element whitespace*.
311
*
4-
* @param {unknown} thing
5-
* Thing to check (typically `Node` or `string`).
12+
* @param {Nodes | string} thing
13+
* Thing to check (`Node` or `string`).
614
* @returns {boolean}
715
* Whether the `value` is inter-element whitespace (`boolean`): consisting of
816
* zero or more of space, tab (`\t`), line feed (`\n`), carriage return
@@ -11,15 +19,17 @@
1119
* checked.
1220
*/
1321
export function whitespace(thing) {
14-
/** @type {string} */
15-
const value =
16-
// @ts-expect-error looks like a node.
17-
thing && typeof thing === 'object' && thing.type === 'text'
18-
? // @ts-expect-error looks like a text.
19-
thing.value || ''
20-
: thing
22+
return typeof thing === 'object'
23+
? thing.type === 'text'
24+
? empty(thing.value)
25+
: false
26+
: empty(thing)
27+
}
2128

22-
// HTML whitespace expression.
23-
// See <https://infra.spec.whatwg.org/#ascii-whitespace>.
24-
return typeof value === 'string' && value.replace(/[ \t\n\f\r]/g, '') === ''
29+
/**
30+
* @param {string} value
31+
* @returns {boolean}
32+
*/
33+
function empty(value) {
34+
return value.replace(re, '') === ''
2535
}

Diff for: package.json

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
"index.d.ts",
3535
"index.js"
3636
],
37+
"dependencies": {
38+
"@types/hast": "^3.0.0"
39+
},
3740
"devDependencies": {
3841
"@types/node": "^20.0.0",
3942
"c8": "^8.0.0",

Diff for: readme.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import {whitespace} from 'hast-util-whitespace'
6666
whitespace({
6767
type: 'element',
6868
tagName: 'div',
69+
properties: {},
6970
children: []
7071
}) // => false
7172

@@ -91,8 +92,8 @@ Check if the given value is [*inter-element whitespace*][spec].
9192

9293
###### Parameters
9394

94-
* `thing` (`unknown`, optional)
95-
— thing to check (typically [`Node`][node] or `string`)
95+
* `thing` ([`Node`][node] or `string`, optional)
96+
— thing to check
9697

9798
###### Returns
9899

Diff for: test.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ test('whitespace', () => {
1010
'should expose the public api'
1111
)
1212

13-
// @ts-expect-error: runtime.
14-
assert.equal(whitespace(), false, 'should return `false` without node')
15-
1613
assert.equal(
17-
whitespace({type: 'element', tagName: 'div'}),
14+
whitespace({type: 'comment', value: 'asd'}),
1815
false,
1916
'should return `false` without text'
2017
)
@@ -31,12 +28,6 @@ test('whitespace', () => {
3128
'should return `true` for inter-element white-space'
3229
)
3330

34-
assert.equal(
35-
whitespace({type: 'text'}),
36-
true,
37-
'should return `true` for `text` without value'
38-
)
39-
4031
assert.equal(
4132
whitespace(' \v'),
4233
false,

0 commit comments

Comments
 (0)