1
- 'use strict'
2
-
3
- var repeat = require ( 'repeat-string' )
4
- var convert = require ( 'hast-util-is-element/convert' )
5
- var findAfter = require ( 'unist-util-find-after' )
6
-
7
- module . exports = toText
1
+ import repeat from 'repeat-string'
2
+ import { convertElement } from 'hast-util-is-element'
3
+ import { findAfter } from 'unist-util-find-after'
8
4
9
5
var searchLineFeeds = / \n / g
10
6
var searchTabOrSpaces = / [ \t ] + / g
11
7
12
- var br = convert ( 'br' )
13
- var p = convert ( 'p' )
14
- var cell = convert ( [ 'th' , 'td' ] )
15
- var row = convert ( 'tr' )
8
+ var br = convertElement ( 'br' )
9
+ var p = convertElement ( 'p' )
10
+ var cell = convertElement ( [ 'th' , 'td' ] )
11
+ var row = convertElement ( 'tr' )
16
12
17
13
// Note that we don’t need to include void elements here as they don’t have text.
18
14
// See: <https://github.com/wooorm/html-void-elements>
19
- var notRendered = convert ( [
15
+ var notRendered = convertElement ( [
20
16
// List from: <https://html.spec.whatwg.org/#hidden-elements>
21
17
'datalist' ,
22
18
'head' ,
@@ -35,7 +31,7 @@ var notRendered = convert([
35
31
] )
36
32
37
33
// See: <https://html.spec.whatwg.org/#the-css-user-agent-style-sheet-and-presentational-hints>
38
- var blockOrCaption = convert ( [
34
+ var blockOrCaption = convertElement ( [
39
35
'address' , // Flow content
40
36
'article' , // Sections and headings
41
37
'aside' , // Sections and headings
@@ -81,10 +77,10 @@ var blockOrCaption = convert([
81
77
// <https://html.spec.whatwg.org/#the-innertext-idl-attribute>
82
78
// Note that we act as if `node` is being rendered, and as if we’re a
83
79
// CSS-supporting user agent.
84
- function toText ( node ) {
80
+ export function toText ( node ) {
85
81
var children = node . children || [ ]
86
82
var block = blockOrCaption ( node )
87
- var whiteSpace = inferWhiteSpace ( node , { } )
83
+ var whitespace = inferWhitespace ( node , { } )
88
84
var index = - 1
89
85
var results
90
86
var result
@@ -101,7 +97,7 @@ function toText(node) {
101
97
// ignored.
102
98
if ( node . type === 'text' || node . type === 'comment' ) {
103
99
return collectText ( node , {
104
- whiteSpace : whiteSpace ,
100
+ whitespace ,
105
101
breakBefore : true ,
106
102
breakAfter : true
107
103
} )
@@ -129,7 +125,7 @@ function toText(node) {
129
125
// 3.2. For each item item in current, append item to results.
130
126
results = results . concat (
131
127
innerTextCollection ( children [ index ] , index , node , {
132
- whiteSpace : whiteSpace ,
128
+ whitespace ,
133
129
breakBefore : index ? null : block ,
134
130
breakAfter :
135
131
index < children . length - 1 ? br ( children [ index + 1 ] ) : block
@@ -171,7 +167,7 @@ function innerTextCollection(node, index, parent, options) {
171
167
172
168
if ( node . type === 'text' ) {
173
169
return [
174
- options . whiteSpace === 'normal'
170
+ options . whitespace === 'normal'
175
171
? collectText ( node , options )
176
172
: collectPreText ( node , options )
177
173
]
@@ -183,7 +179,7 @@ function innerTextCollection(node, index, parent, options) {
183
179
// Collect an element.
184
180
function collectElement ( node , _ , parent , options ) {
185
181
// First we infer the `white-space` property.
186
- var whiteSpace = inferWhiteSpace ( node , options )
182
+ var whitespace = inferWhitespace ( node , options )
187
183
var children = node . children || [ ]
188
184
var index = - 1
189
185
var items = [ ]
@@ -249,7 +245,7 @@ function collectElement(node, _, parent, options) {
249
245
while ( ++ index < children . length ) {
250
246
items = items . concat (
251
247
innerTextCollection ( children [ index ] , index , node , {
252
- whiteSpace : whiteSpace ,
248
+ whitespace ,
253
249
breakBefore : index ? null : prefix ,
254
250
breakAfter :
255
251
index < children . length - 1 ? br ( children [ index + 1 ] ) : suffix
@@ -313,7 +309,7 @@ function collectText(node, options) {
313
309
// they were not there.
314
310
value
315
311
. slice ( start , end )
316
- . replace ( / [ \u061c \u200e \u200f \u202a - \u202e \u2066 - \u2069 ] / g, '' ) ,
312
+ . replace ( / [ \u061C \u200E \u200F \u202A - \u202E \u2066 - \u2069 ] / g, '' ) ,
317
313
options . breakBefore ,
318
314
options . breakAfter
319
315
)
@@ -411,9 +407,9 @@ function trimAndcollapseSpacesAndTabs(value, breakBefore, breakAfter) {
411
407
}
412
408
413
409
// We don’t support void elements here (so `nobr wbr` -> `normal` is ignored).
414
- function inferWhiteSpace ( node , options ) {
410
+ function inferWhitespace ( node , options ) {
415
411
var props = node . properties || { }
416
- var inherit = options . whiteSpace || 'normal'
412
+ var inherit = options . whitespace || 'normal'
417
413
418
414
switch ( node . tagName ) {
419
415
case 'listing' :
0 commit comments