@@ -208,6 +208,112 @@ describe("The Patterns parser", function () {
208
208
expect ( _ . isEqual ( opts [ "json-color" ] , { color : "pink" } ) ) . toBeTruthy ( ) ;
209
209
} ) ;
210
210
211
+ describe ( "JSON data attributes" , function ( ) {
212
+ it ( "parses JSON objects" , function ( done ) {
213
+ const parser = new ArgumentParser ( "mypattern" ) ;
214
+ parser . addArgument ( "arg1" , 0 ) ;
215
+ const content = document . createElement ( "div" ) ;
216
+ content . innerHTML = `
217
+ <div data-pat-mypattern='{ "arg1": 1 }'></div>
218
+ ` ;
219
+ const opts = parser . parse ( content . querySelector ( "[data-pat-mypattern]" ) ) ;
220
+ expect ( opts ) . toEqual ( {
221
+ arg1 : 1 ,
222
+ } ) ;
223
+
224
+ done ( ) ;
225
+ } ) ;
226
+ it ( "parses JSON arrays" , function ( done ) {
227
+ const parser = new ArgumentParser ( "mypattern" ) ;
228
+ parser . addArgument ( "arg1" , 0 ) ;
229
+ const content = document . createElement ( "div" ) ;
230
+ content . innerHTML = `
231
+ <div data-pat-mypattern='[{ "arg1": 1 }]'></div>
232
+ ` ;
233
+ const opts = parser . parse ( content . querySelector ( "[data-pat-mypattern]" ) ) ;
234
+ expect ( opts ) . toEqual ( {
235
+ arg1 : 1 ,
236
+ } ) ;
237
+
238
+ done ( ) ;
239
+ } ) ;
240
+ it ( "parses JSON arrays with multiple configs" , function ( done ) {
241
+ const parser = new ArgumentParser ( "mypattern" ) ;
242
+ parser . addArgument ( "arg1" , 0 ) ;
243
+ parser . addArgument ( "arg2" , false ) ;
244
+ const content = document . createElement ( "div" ) ;
245
+ content . innerHTML = `
246
+ <div data-pat-mypattern='
247
+ [
248
+ {
249
+ "arg1": 1,
250
+ "arg2": true
251
+ },
252
+ {
253
+ "arg1": 2,
254
+ "arg2": false
255
+ }
256
+ ]
257
+ '></div>
258
+ ` ;
259
+ const opts = parser . parse (
260
+ content . querySelector ( "[data-pat-mypattern]" ) ,
261
+ { } ,
262
+ true
263
+ ) ;
264
+ expect ( opts . length ) . toBe ( 2 ) ;
265
+ expect ( opts [ 0 ] ) . toEqual ( {
266
+ arg1 : 1 ,
267
+ arg2 : true ,
268
+ } ) ;
269
+ expect ( opts [ 1 ] ) . toEqual ( {
270
+ arg1 : 2 ,
271
+ arg2 : false ,
272
+ } ) ;
273
+
274
+ done ( ) ;
275
+ } ) ;
276
+ it ( "parses JSON arrays and includes defaults" , function ( done ) {
277
+ const parser = new ArgumentParser ( "mypattern" ) ;
278
+ parser . addArgument ( "arg1" , 0 ) ;
279
+ parser . addArgument ( "arg2" , "okay" ) ;
280
+ parser . addArgument ( "arg3" , false ) ;
281
+ const content = document . createElement ( "div" ) ;
282
+ content . innerHTML = `
283
+ <div data-pat-mypattern='{"arg1": 1}'></div>
284
+ ` ;
285
+ const opts = parser . parse ( content . querySelector ( "[data-pat-mypattern]" ) ) ;
286
+ expect ( opts ) . toEqual ( {
287
+ arg1 : 1 ,
288
+ arg2 : "okay" ,
289
+ arg3 : false ,
290
+ } ) ;
291
+
292
+ done ( ) ;
293
+ } ) ;
294
+ it ( "parses JSON arrays and does not include defaults if set so" , function ( done ) {
295
+ const parser = new ArgumentParser ( "mypattern" ) ;
296
+ parser . addArgument ( "arg1" , 0 ) ;
297
+ parser . addArgument ( "arg2" , "okay" ) ;
298
+ parser . addArgument ( "arg3" , false ) ;
299
+ const content = document . createElement ( "div" ) ;
300
+ content . innerHTML = `
301
+ <div data-pat-mypattern='{"arg1": 1}'></div>
302
+ ` ;
303
+ const opts = parser . parse (
304
+ content . querySelector ( "[data-pat-mypattern]" ) ,
305
+ { } ,
306
+ false ,
307
+ false
308
+ ) ;
309
+ expect ( opts ) . toEqual ( {
310
+ arg1 : 1 ,
311
+ } ) ;
312
+
313
+ done ( ) ;
314
+ } ) ;
315
+ } ) ;
316
+
211
317
describe ( "the shorthand notation" , function ( ) {
212
318
it ( "Single argument" , function ( ) {
213
319
var parser = new ArgumentParser ( ) ;
0 commit comments