@@ -67,7 +67,8 @@ export const shared: GraphQLFieldConfigMap<Element, any> = {
67
67
selector,
68
68
trim : {
69
69
type : GraphQLBoolean ,
70
- description : "Trim any leading and trailing whitespace from the value (optional, default: false)" ,
70
+ description :
71
+ "Trim any leading and trailing whitespace from the value (optional, default: false)" ,
71
72
defaultValue : false ,
72
73
} ,
73
74
} ,
@@ -79,22 +80,30 @@ export const shared: GraphQLFieldConfigMap<Element, any> = {
79
80
} ,
80
81
table : {
81
82
type : new GraphQLList ( new GraphQLList ( GraphQLString ) ) ,
82
- description : "Returns a two-dimensional array representing an HTML table element's contents. The first level is a list of rows (`<tr>`), and each row is an array of cell (`<td>`) contents." ,
83
+ description :
84
+ "Returns a two-dimensional array representing an HTML table element's contents. The first level is a list of rows (`<tr>`), and each row is an array of cell (`<td>`) contents." ,
83
85
args : {
84
86
selector,
87
+ trim : {
88
+ type : GraphQLBoolean ,
89
+ description :
90
+ "Trim any leading and trailing whitespace from the values (optional, default: false)" ,
91
+ defaultValue : false ,
92
+ } ,
85
93
} ,
86
- resolve ( element : Element , { selector } : ElementParams ) {
94
+ resolve ( element : Element , { selector, trim } : TextParams ) {
87
95
element = selector ? element . querySelector ( selector ) ! : element ;
88
96
89
97
const result = element && Array . from (
90
98
element . querySelectorAll ( "tr" ) ,
91
- ) . map ( ( row ) =>
92
- Array . from ( ( row as Element ) . querySelectorAll ( "td" ) ) . map ( ( td ) =>
93
- td . textContent . trim ( )
94
- )
99
+ ( row ) =>
100
+ Array . from (
101
+ ( row as Element ) . querySelectorAll ( "td" ) ,
102
+ ( td ) => ( trim ? td . textContent . trim ( ) : td . textContent ) ,
103
+ ) ,
95
104
) ;
96
105
97
- return result . filter ( ( row ) => row . length > 0 ) ;
106
+ return result . filter ( Boolean ) . filter ( ( row ) => row . length > 0 ) ;
98
107
} ,
99
108
} ,
100
109
tag : {
@@ -103,71 +112,85 @@ export const shared: GraphQLFieldConfigMap<Element, any> = {
103
112
args : { selector } ,
104
113
resolve ( element : Element , { selector } : ElementParams ) {
105
114
element = selector ? element . querySelector ( selector ) ! : element ;
106
-
107
- return element && element . tagName ;
115
+ return element ?. tagName ?? null ;
108
116
} ,
109
117
} ,
110
118
attr : {
111
119
type : GraphQLString ,
112
- description : "The value of a given attribute from the selected node (`href`, `src`, etc.), if it exists." ,
120
+ description :
121
+ "The value of a given attribute from the selected node (`href`, `src`, etc.), if it exists." ,
113
122
args : {
114
123
selector,
115
124
name : {
116
125
type : new GraphQLNonNull ( GraphQLString ) ,
117
126
description : "The name of the attribute" ,
118
127
} ,
128
+ trim : {
129
+ type : GraphQLBoolean ,
130
+ description :
131
+ "Trim any leading and trailing whitespace from the value (optional, default: false)" ,
132
+ defaultValue : false ,
133
+ } ,
119
134
} ,
120
- resolve ( element : Element , { selector, name } : TParams ) {
135
+ resolve ( element : Element , { selector, name, trim } : TParams ) {
121
136
element = selector ? element . querySelector ( selector ) ! : element ;
122
- if ( element == null || name == null ) {
123
- return null ;
124
- }
125
-
126
- const attribute = element . getAttribute ( name ) ;
127
- if ( attribute == null ) {
128
- return null ;
129
- }
130
-
131
- return attribute ;
137
+ return getAttributeOfElement ( element , name as string , trim ) ;
132
138
} ,
133
139
} ,
134
140
href : {
135
141
type : GraphQLString ,
136
- description : "Shorthand for `attr(name: \" href\" )`" ,
142
+ description : "Shorthand for `attr(name: ' href' )`" ,
137
143
args : {
138
144
selector,
145
+ trim : {
146
+ type : GraphQLBoolean ,
147
+ description :
148
+ "Trim any leading and trailing whitespace from the value (optional, default: false)" ,
149
+ defaultValue : false ,
150
+ } ,
139
151
} ,
140
- resolve ( element : Element , { selector } : ElementParams ) {
152
+ resolve ( element : Element , { selector, trim } : TextParams ) {
141
153
element = selector ? element . querySelector ( selector ) ! : element ;
142
- if ( element == null ) return null ;
143
-
144
- return getAttributeOfElement ( element , "href" ) ;
154
+ return getAttributeOfElement ( element , "href" , trim ) ;
145
155
} ,
146
156
} ,
147
157
src : {
148
158
type : GraphQLString ,
149
- description : "Shorthand for `attr(name: \" src\" )`" ,
159
+ description : "Shorthand for `attr(name: ' src' )`" ,
150
160
args : {
151
161
selector,
162
+ trim : {
163
+ type : GraphQLBoolean ,
164
+ description :
165
+ "Trim any leading and trailing whitespace from the value (optional, default: false)" ,
166
+ defaultValue : false ,
167
+ } ,
152
168
} ,
153
- resolve ( element : Element , { selector } : ElementParams ) {
169
+ resolve ( element : Element , { selector, trim } : TextParams ) {
154
170
element = selector ? element . querySelector ( selector ) ! : element ;
155
171
if ( element == null ) return null ;
156
172
157
- return getAttributeOfElement ( element , "src" ) ;
173
+ return getAttributeOfElement ( element , "src" , trim ) ;
158
174
} ,
159
175
} ,
160
176
class : {
161
177
type : GraphQLString ,
162
- description : "The class attribute of the selected node, if any exists. Formatted as a space-separated list of CSS class names." ,
178
+ description :
179
+ "The class attribute of the selected node, if any exists. Formatted as a space-separated list of CSS class names." ,
163
180
args : {
164
181
selector,
182
+ trim : {
183
+ type : GraphQLBoolean ,
184
+ description :
185
+ "Trim any leading and trailing whitespace from the value (optional, default: false)" ,
186
+ defaultValue : false ,
187
+ } ,
165
188
} ,
166
- resolve ( element : Element , { selector } : TParams ) {
189
+ resolve ( element : Element , { selector, trim } : TextParams ) {
167
190
element = selector ? element . querySelector ( selector ) ! : element ;
168
191
if ( element == null ) return null ;
169
192
170
- return getAttributeOfElement ( element , "class" ) ;
193
+ return getAttributeOfElement ( element , "class" , trim ) ;
171
194
} ,
172
195
} ,
173
196
classList : {
@@ -179,24 +202,22 @@ export const shared: GraphQLFieldConfigMap<Element, any> = {
179
202
resolve ( element : Element , { selector } : ElementParams ) {
180
203
element = selector ? element . querySelector ( selector ) ! : element ;
181
204
if ( element == null ) return null ;
182
-
183
- const attribute = getAttributeOfElement ( element , "class" ) ;
184
- if ( attribute == null ) return null ;
185
-
186
- return attribute . split ( " " ) ;
205
+ return [ ...( element ?. classList . values ( ) ?? [ ] ) ] ;
187
206
} ,
188
207
} ,
189
208
has : {
190
209
type : GraphQLBoolean ,
191
- description : "Returns true if an element with the given selector exists, otherwise false." ,
210
+ description :
211
+ "Returns true if an element with the given selector exists, otherwise false." ,
192
212
args : { selector } ,
193
213
resolve ( element : Element , { selector } : ElementParams ) {
194
214
return ! ! element . querySelector ( selector ! ) ;
195
215
} ,
196
216
} ,
197
217
count : {
198
218
type : GraphQLInt ,
199
- description : "Returns the number of DOM nodes that match the given selector, or 0 if no nodes match." ,
219
+ description :
220
+ "Returns the number of DOM nodes that match the given selector, or 0 if no nodes match." ,
200
221
args : { selector } ,
201
222
resolve ( element : Element , { selector } : ElementParams ) {
202
223
if ( ! selector ) return 0 ;
@@ -231,7 +252,8 @@ export const shared: GraphQLFieldConfigMap<Element, any> = {
231
252
} ,
232
253
childNodes : {
233
254
type : new GraphQLList ( TElement ) ,
234
- description : "Child nodes (not elements) of a selected node, including any text nodes." ,
255
+ description :
256
+ "Child nodes (not elements) of a selected node, including any text nodes." ,
235
257
resolve ( element : Element ) {
236
258
return Array . from ( element . childNodes ) ;
237
259
} ,
0 commit comments