summaryrefslogtreecommitdiffstats
path: root/dom/html/test/reflect.js
blob: 44f73ae4a2f4e330a05572f766bbd1ee3e56c46b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * reflect.js is a collection of methods to test HTML attribute reflection.
 * Each of attribute is reflected differently, depending on various parameters,
 * see:
 * http://www.whatwg.org/html/#reflecting-content-attributes-in-idl-attributes
 *
 * Do not forget to add these line at the beginning of each new reflect* method:
 * ok(attr in element, attr + " should be an IDL attribute of this element");
 * is(typeof element[attr], <type>, attr + " IDL attribute should be a <type>");
 */

/**
 * Checks that a given attribute is correctly reflected as a string.
 *
 * @param aParameters   Object    object containing the parameters, which are:
 *  - element           Element   node to test
 *  - attribute         String    name of the attribute
 *     OR
 *    attribute         Object    object containing two attributes, 'content' and 'idl'
 *  - otherValues       Array     [optional] other values to test in addition of the default ones
 *  - extendedAttributes Object   object which can have 'TreatNullAs': "EmptyString"
 */
function reflectString(aParameters) {
  var element = aParameters.element;
  var contentAttr =
    typeof aParameters.attribute === "string"
      ? aParameters.attribute
      : aParameters.attribute.content;
  var idlAttr =
    typeof aParameters.attribute === "string"
      ? aParameters.attribute
      : aParameters.attribute.idl;
  var otherValues =
    aParameters.otherValues !== undefined ? aParameters.otherValues : [];
  var treatNullAs = aParameters.extendedAttributes
    ? aParameters.extendedAttributes.TreatNullAs
    : null;

  ok(
    idlAttr in element,
    idlAttr + " should be an IDL attribute of this element"
  );
  is(
    typeof element[idlAttr],
    "string",
    "'" + idlAttr + "' IDL attribute should be a string"
  );

  // Tests when the attribute isn't set.
  is(
    element.getAttribute(contentAttr),
    null,
    "When not set, the content attribute should be null."
  );
  is(
    element[idlAttr],
    "",
    "When not set, the IDL attribute should return the empty string"
  );

  /**
   * TODO: as long as null stringification doesn't follow the WebIDL
   * specifications, don't add it to the loop below and keep it here.
   */
  element.setAttribute(contentAttr, null);
  is(
    element.getAttribute(contentAttr),
    "null",
    "null should have been stringified to 'null' for '" + contentAttr + "'"
  );
  is(
    element[idlAttr],
    "null",
    "null should have been stringified to 'null' for '" + idlAttr + "'"
  );
  element.removeAttribute(contentAttr);

  element[idlAttr] = null;
  if (treatNullAs == "EmptyString") {
    is(
      element.getAttribute(contentAttr),
      "",
      "null should have been stringified to '' for '" + contentAttr + "'"
    );
    is(
      element[idlAttr],
      "",
      "null should have been stringified to '' for '" + idlAttr + "'"
    );
  } else {
    is(
      element.getAttribute(contentAttr),
      "null",
      "null should have been stringified to 'null' for '" + contentAttr + "'"
    );
    is(
      element[idlAttr],
      "null",
      "null should have been stringified to 'null' for '" + contentAttr + "'"
    );
  }
  element.removeAttribute(contentAttr);

  // Tests various strings.
  var stringsToTest = [
    // [ test value, expected result ]
    ["", ""],
    ["null", "null"],
    ["undefined", "undefined"],
    ["foo", "foo"],
    [contentAttr, contentAttr],
    [idlAttr, idlAttr],
    // TODO: uncomment this when null stringification will follow the specs.
    // [ null, "null" ],
    [undefined, "undefined"],
    [true, "true"],
    [false, "false"],
    [42, "42"],
    // ES5, verse 8.12.8.
    [
      {
        toString() {
          return "foo";
        },
      },
      "foo",
    ],
    [
      {
        valueOf() {
          return "foo";
        },
      },
      "[object Object]",
    ],
    [
      {
        valueOf() {
          return "quux";
        },
        toString: undefined,
      },
      "quux",
    ],
    [
      {
        valueOf() {
          return "foo";
        },
        toString() {
          return "bar";
        },
      },
      "bar",
    ],
  ];

  otherValues.forEach(function (v) {
    stringsToTest.push([v, v]);
  });

  stringsToTest.forEach(function ([v, r]) {
    element.setAttribute(contentAttr, v);
    is(
      element[idlAttr],
      r,
      "IDL attribute '" +
        idlAttr +
        "' should return the value it has been set to."
    );
    is(
      element.getAttribute(contentAttr),
      r,
      "Content attribute '" +
        contentAttr +
        "'should return the value it has been set to."
    );
    element.removeAttribute(contentAttr);

    element[idlAttr] = v;
    is(
      element[idlAttr],
      r,
      "IDL attribute '" +
        idlAttr +
        "' should return the value it has been set to."
    );
    is(
      element.getAttribute(contentAttr),
      r,
      "Content attribute '" +
        contentAttr +
        "' should return the value it has been set to."
    );
    element.removeAttribute(contentAttr);
  });

  // Tests after removeAttribute() is called. Should be equivalent with not set.
  is(
    element.getAttribute(contentAttr),
    null,
    "When not set, the content attribute should be null."
  );
  is(
    element[idlAttr],
    "",
    "When not set, the IDL attribute should return the empty string"
  );
}

/**
 * Checks that a given attribute name for a given element is correctly reflected
 * as an unsigned int.
 *
 * @param aParameters   Object    object containing the parameters, which are:
 *  - element           Element   node to test on
 *  - attribute         String    name of the attribute
 *  - nonZero           Boolean   whether the attribute should be non-null
 *  - defaultValue      Integer   [optional] default value, if different from the default one
 */
function reflectUnsignedInt(aParameters) {
  var element = aParameters.element;
  var attr = aParameters.attribute;
  var nonZero = aParameters.nonZero;
  var defaultValue = aParameters.defaultValue;
  var fallback = aParameters.fallback;

  if (defaultValue === undefined) {
    if (nonZero) {
      defaultValue = 1;
    } else {
      defaultValue = 0;
    }
  }

  if (fallback === undefined) {
    fallback = false;
  }

  ok(attr in element, attr + " should be an IDL attribute of this element");
  is(
    typeof element[attr],
    "number",
    attr + " IDL attribute should be a number"
  );

  // Check default value.
  is(element[attr], defaultValue, "default value should be " + defaultValue);
  ok(!element.hasAttribute(attr), attr + " shouldn't be present");

  var values = [1, 3, 42, 2147483647];

  for (var value of values) {
    element[attr] = value;
    is(element[attr], value, "." + attr + " should be equals " + value);
    is(
      element.getAttribute(attr),
      String(value),
      "@" + attr + " should be equals " + value
    );

    element.setAttribute(attr, value);
    is(element[attr], value, "." + attr + " should be equals " + value);
    is(
      element.getAttribute(attr),
      String(value),
      "@" + attr + " should be equals " + value
    );
  }

  // -3000000000 is equivalent to 1294967296 when using the IDL attribute.
  element[attr] = -3000000000;
  is(element[attr], 1294967296, "." + attr + " should be equals to 1294967296");
  is(
    element.getAttribute(attr),
    "1294967296",
    "@" + attr + " should be equals to 1294967296"
  );

  // When setting the content attribute, it's a string so it will be invalid.
  element.setAttribute(attr, -3000000000);
  is(
    element.getAttribute(attr),
    "-3000000000",
    "@" + attr + " should be equals to " + -3000000000
  );
  is(
    element[attr],
    defaultValue,
    "." + attr + " should be equals to " + defaultValue
  );

  // When interpreted as unsigned 32-bit integers, all of these fall between
  // 2^31 and 2^32 - 1, so per spec they return the default value.
  var nonValidValues = [-2147483648, -1, 3147483647];

  for (var value of nonValidValues) {
    element[attr] = value;
    is(
      element.getAttribute(attr),
      String(defaultValue),
      "@" + attr + " should be equals to " + defaultValue
    );
    is(
      element[attr],
      defaultValue,
      "." + attr + " should be equals to " + defaultValue
    );
  }

  for (var values of nonValidValues) {
    element.setAttribute(attr, values[0]);
    is(
      element.getAttribute(attr),
      String(values[0]),
      "@" + attr + " should be equals to " + values[0]
    );
    is(
      element[attr],
      defaultValue,
      "." + attr + " should be equals to " + defaultValue
    );
  }

  // Setting to 0 should throw an error if nonZero is true.
  var caught = false;
  try {
    element[attr] = 0;
  } catch (e) {
    caught = true;
    is(e.name, "IndexSizeError", "exception should be IndexSizeError");
    is(
      e.code,
      DOMException.INDEX_SIZE_ERR,
      "exception code should be INDEX_SIZE_ERR"
    );
  }

  if (nonZero && !fallback) {
    ok(caught, "an exception should have been caught");
  } else {
    ok(!caught, "no exception should have been caught");
  }

  // If 0 is set in @attr, it will be ignored when calling .attr.
  element.setAttribute(attr, "0");
  is(element.getAttribute(attr), "0", "@" + attr + " should be equals to 0");
  if (nonZero) {
    is(
      element[attr],
      defaultValue,
      "." + attr + " should be equals to " + defaultValue
    );
  } else {
    is(element[attr], 0, "." + attr + " should be equals to 0");
  }
}

/**
 * Checks that a given attribute is correctly reflected as limited to known
 * values enumerated attribute.
 *
 * @param aParameters     Object   object containing the parameters, which are:
 *  - element             Element  node to test on
 *  - attribute           String   name of the attribute
 *     OR
 *    attribute           Object   object containing two attributes, 'content' and 'idl'
 *  - validValues         Array    valid values we support
 *  - invalidValues       Array    invalid values
 *  - defaultValue        String   [optional] default value when no valid value is set
 *     OR
 *    defaultValue        Object   [optional] object containing two attributes, 'invalid' and 'missing'
 *  - unsupportedValues   Array    [optional] valid values we do not support
 *  - nullable            boolean  [optional] whether the attribute is nullable
 */
function reflectLimitedEnumerated(aParameters) {
  var element = aParameters.element;
  var contentAttr =
    typeof aParameters.attribute === "string"
      ? aParameters.attribute
      : aParameters.attribute.content;
  var idlAttr =
    typeof aParameters.attribute === "string"
      ? aParameters.attribute
      : aParameters.attribute.idl;
  var validValues = aParameters.validValues;
  var invalidValues = aParameters.invalidValues;
  var defaultValueInvalid =
    aParameters.defaultValue === undefined
      ? ""
      : typeof aParameters.defaultValue === "string"
      ? aParameters.defaultValue
      : aParameters.defaultValue.invalid;
  var defaultValueMissing =
    aParameters.defaultValue === undefined
      ? ""
      : typeof aParameters.defaultValue === "string"
      ? aParameters.defaultValue
      : aParameters.defaultValue.missing;
  var unsupportedValues =
    aParameters.unsupportedValues !== undefined
      ? aParameters.unsupportedValues
      : [];
  var nullable = aParameters.nullable;

  ok(
    idlAttr in element,
    idlAttr + " should be an IDL attribute of this element"
  );
  if (nullable) {
    // The missing value default is null, which is typeof == "object"
    is(
      typeof element[idlAttr],
      "object",
      "'" +
        idlAttr +
        "' IDL attribute should be null, which has typeof == object"
    );
    is(
      element[idlAttr],
      null,
      "'" + idlAttr + "' IDL attribute should be null"
    );
  } else {
    is(
      typeof element[idlAttr],
      "string",
      "'" + idlAttr + "' IDL attribute should be a string"
    );
  }

  if (nullable) {
    element.setAttribute(contentAttr, "something");
    // Now it will be a string
    is(
      typeof element[idlAttr],
      "string",
      "'" + idlAttr + "' IDL attribute should be a string"
    );
  }

  // Explicitly check the default value.
  element.removeAttribute(contentAttr);
  is(
    element[idlAttr],
    defaultValueMissing,
    "When no attribute is set, the value should be the default value."
  );

  // Check valid values.
  validValues.forEach(function (v) {
    element.setAttribute(contentAttr, v);
    is(
      element[idlAttr],
      v,
      "'" + v + "' should be accepted as a valid value for " + idlAttr
    );
    is(
      element.getAttribute(contentAttr),
      v,
      "Content attribute should return the value it has been set to."
    );
    element.removeAttribute(contentAttr);

    element.setAttribute(contentAttr, v.toUpperCase());
    is(
      element[idlAttr],
      v,
      "Enumerated attributes should be case-insensitive."
    );
    is(
      element.getAttribute(contentAttr),
      v.toUpperCase(),
      "Content attribute should not be lower-cased."
    );
    element.removeAttribute(contentAttr);

    element[idlAttr] = v;
    is(
      element[idlAttr],
      v,
      "'" + v + "' should be accepted as a valid value for " + idlAttr
    );
    is(
      element.getAttribute(contentAttr),
      v,
      "Content attribute should return the value it has been set to."
    );
    element.removeAttribute(contentAttr);

    element[idlAttr] = v.toUpperCase();
    is(
      element[idlAttr],
      v,
      "Enumerated attributes should be case-insensitive."
    );
    is(
      element.getAttribute(contentAttr),
      v.toUpperCase(),
      "Content attribute should not be lower-cased."
    );
    element.removeAttribute(contentAttr);
  });

  // Check invalid values.
  invalidValues.forEach(function (v) {
    element.setAttribute(contentAttr, v);
    is(
      element[idlAttr],
      defaultValueInvalid,
      "When the content attribute is set to an invalid value, the default value should be returned."
    );
    is(
      element.getAttribute(contentAttr),
      v,
      "Content attribute should not have been changed."
    );
    element.removeAttribute(contentAttr);

    element[idlAttr] = v;
    is(
      element[idlAttr],
      defaultValueInvalid,
      "When the value is set to an invalid value, the default value should be returned."
    );
    is(
      element.getAttribute(contentAttr),
      v,
      "Content attribute should not have been changed."
    );
    element.removeAttribute(contentAttr);
  });

  // Check valid values we currently do not support.
  // Basically, it's like the checks for the valid values but with some todo's.
  unsupportedValues.forEach(function (v) {
    element.setAttribute(contentAttr, v);
    todo_is(
      element[idlAttr],
      v,
      "'" + v + "' should be accepted as a valid value for " + idlAttr
    );
    is(
      element.getAttribute(contentAttr),
      v,
      "Content attribute should return the value it has been set to."
    );
    element.removeAttribute(contentAttr);

    element.setAttribute(contentAttr, v.toUpperCase());
    todo_is(
      element[idlAttr],
      v,
      "Enumerated attributes should be case-insensitive."
    );
    is(
      element.getAttribute(contentAttr),
      v.toUpperCase(),
      "Content attribute should not be lower-cased."
    );
    element.removeAttribute(contentAttr);

    element[idlAttr] = v;
    todo_is(
      element[idlAttr],
      v,
      "'" + v + "' should be accepted as a valid value for " + idlAttr
    );
    is(
      element.getAttribute(contentAttr),
      v,
      "Content attribute should return the value it has been set to."
    );
    element.removeAttribute(contentAttr);

    element[idlAttr] = v.toUpperCase();
    todo_is(
      element[idlAttr],
      v,
      "Enumerated attributes should be case-insensitive."
    );
    is(
      element.getAttribute(contentAttr),
      v.toUpperCase(),
      "Content attribute should not be lower-cased."
    );
    element.removeAttribute(contentAttr);
  });

  if (nullable) {
    is(
      defaultValueMissing,
      null,
      "Missing default value should be null for nullable attributes"
    );
    ok(validValues.length, "We better have at least one valid value");
    element.setAttribute(contentAttr, validValues[0]);
    ok(
      element.hasAttribute(contentAttr),
      "Should have content attribute: we just set it"
    );
    element[idlAttr] = null;
    ok(
      !element.hasAttribute(contentAttr),
      "Should have removed content attribute"
    );
  }
}

/**
 * Checks that a given attribute is correctly reflected as a boolean.
 *
 * @param aParameters    Object    object containing the parameters, which are:
 *  - element            Element   node to test on
 *  - attribute          String    name of the attribute
 *     OR
 *    attribute          Object    object containing two attributes, 'content' and 'idl'
 */
function reflectBoolean(aParameters) {
  var element = aParameters.element;
  var contentAttr =
    typeof aParameters.attribute === "string"
      ? aParameters.attribute
      : aParameters.attribute.content;
  var idlAttr =
    typeof aParameters.attribute === "string"
      ? aParameters.attribute
      : aParameters.attribute.idl;

  ok(
    idlAttr in element,
    idlAttr + " should be an IDL attribute of this element"
  );
  is(
    typeof element[idlAttr],
    "boolean",
    idlAttr + " IDL attribute should be a boolean"
  );

  // Tests when the attribute isn't set.
  is(
    element.getAttribute(contentAttr),
    null,
    "When not set, the content attribute should be null."
  );
  is(
    element[idlAttr],
    false,
    "When not set, the IDL attribute should return false"
  );

  /**
   * Test various values.
   * Each value to test is actually an object containing a 'value' property
   * containing the value to actually test, a 'stringified' property containing
   * the stringified value and a 'result' property containing the expected
   * result when the value is set to the IDL attribute.
   */
  var valuesToTest = [
    { value: true, stringified: "true", result: true },
    { value: false, stringified: "false", result: false },
    { value: "true", stringified: "true", result: true },
    { value: "false", stringified: "false", result: true },
    { value: "foo", stringified: "foo", result: true },
    { value: idlAttr, stringified: idlAttr, result: true },
    { value: contentAttr, stringified: contentAttr, result: true },
    { value: "null", stringified: "null", result: true },
    { value: "undefined", stringified: "undefined", result: true },
    { value: "", stringified: "", result: false },
    { value: undefined, stringified: "undefined", result: false },
    { value: null, stringified: "null", result: false },
    { value: +0, stringified: "0", result: false },
    { value: -0, stringified: "0", result: false },
    { value: NaN, stringified: "NaN", result: false },
    { value: 42, stringified: "42", result: true },
    { value: Infinity, stringified: "Infinity", result: true },
    { value: -Infinity, stringified: "-Infinity", result: true },
    // ES5, verse 9.2.
    {
      value: {
        toString() {
          return "foo";
        },
      },
      stringified: "foo",
      result: true,
    },
    {
      value: {
        valueOf() {
          return "foo";
        },
      },
      stringified: "[object Object]",
      result: true,
    },
    {
      value: {
        valueOf() {
          return "quux";
        },
        toString: undefined,
      },
      stringified: "quux",
      result: true,
    },
    {
      value: {
        valueOf() {
          return "foo";
        },
        toString() {
          return "bar";
        },
      },
      stringified: "bar",
      result: true,
    },
    {
      value: {
        valueOf() {
          return false;
        },
      },
      stringified: "[object Object]",
      result: true,
    },
    {
      value: { foo: false, bar: false },
      stringified: "[object Object]",
      result: true,
    },
    { value: {}, stringified: "[object Object]", result: true },
  ];

  valuesToTest.forEach(function (v) {
    element.setAttribute(contentAttr, v.value);
    is(
      element[idlAttr],
      true,
      "IDL attribute should return always return 'true' if the content attribute has been set"
    );
    is(
      element.getAttribute(contentAttr),
      v.stringified,
      "Content attribute should return the stringified value it has been set to."
    );
    element.removeAttribute(contentAttr);

    element[idlAttr] = v.value;
    is(element[idlAttr], v.result, "IDL attribute should return " + v.result);
    is(
      element.getAttribute(contentAttr),
      v.result ? "" : null,
      v.result
        ? "Content attribute should return the empty string."
        : "Content attribute should return null."
    );
    is(
      element.hasAttribute(contentAttr),
      v.result,
      v.result
        ? contentAttr + " should not be present"
        : contentAttr + " should be present"
    );
    element.removeAttribute(contentAttr);
  });

  // Tests after removeAttribute() is called. Should be equivalent with not set.
  is(
    element.getAttribute(contentAttr),
    null,
    "When not set, the content attribute should be null."
  );
  is(
    element[contentAttr],
    false,
    "When not set, the IDL attribute should return false"
  );
}

/**
 * Checks that a given attribute name for a given element is correctly reflected
 * as an signed integer.
 *
 * @param aParameters   Object    object containing the parameters, which are:
 *  - element           Element   node to test on
 *  - attribute         String    name of the attribute
 *  - nonNegative       Boolean   true if the attribute is limited to 'non-negative numbers', false otherwise
 *  - defaultValue      Integer   [optional] default value, if one exists
 */
function reflectInt(aParameters) {
  // Expected value returned by .getAttribute() when |value| has been previously passed to .setAttribute().
  function expectedGetAttributeResult(value) {
    return String(value);
  }

  function stringToInteger(value, nonNegative, defaultValue) {
    // Parse: Ignore leading whitespace, find [+/-][numbers]
    var result = /^[ \t\n\f\r]*([\+\-]?[0-9]+)/.exec(value);
    if (result) {
      var resultInt = parseInt(result[1], 10);
      if (
        (nonNegative ? 0 : -0x80000000) <= resultInt &&
        resultInt <= 0x7fffffff
      ) {
        // If the value is within allowed value range for signed/unsigned
        // integer, return it -- but add 0 to it to convert a possible -0 into
        // +0, the only zero present in the signed integer range.
        return resultInt + 0;
      }
    }
    return defaultValue;
  }

  // Expected value returned by .getAttribute(attr) or .attr if |value| has been set via the IDL attribute.
  function expectedIdlAttributeResult(value) {
    // This returns the result of calling the ES ToInt32 algorithm on value.
    return value << 0;
  }

  var element = aParameters.element;
  var attr = aParameters.attribute;
  var nonNegative = aParameters.nonNegative;

  var defaultValue =
    aParameters.defaultValue !== undefined
      ? aParameters.defaultValue
      : nonNegative
      ? -1
      : 0;

  ok(attr in element, attr + " should be an IDL attribute of this element");
  is(
    typeof element[attr],
    "number",
    attr + " IDL attribute should be a number"
  );

  // Check default value.
  is(element[attr], defaultValue, "default value should be " + defaultValue);
  ok(!element.hasAttribute(attr), attr + " shouldn't be present");

  /**
   * Test various values.
   * value: The test value that will be set using both setAttribute(value) and
   *        element[attr] = value
   */
  var valuesToTest = [
    // Test numeric inputs up to max signed integer
    0,
    1,
    55555,
    2147483647,
    +42,
    // Test string inputs up to max signed integer
    "0",
    "1",
    "777777",
    "2147483647",
    "+42",
    // Test negative numeric inputs up to min signed integer
    -0,
    -1,
    -3333,
    -2147483648,
    // Test negative string inputs up to min signed integer
    "-0",
    "-1",
    "-222",
    "-2147483647",
    "-2147483648",
    // Test numeric inputs that are outside legal 32 bit signed values
    -2147483649,
    -3000000000,
    -4294967296,
    2147483649,
    4000000000,
    -4294967297,
    // Test string inputs with extra padding
    "     1111111",
    "  23456   ",
    // Test non-numeric string inputs
    "",
    " ",
    "+",
    "-",
    "foo",
    "+foo",
    "-foo",
    "+     foo",
    "-     foo",
    "+-2",
    "-+2",
    "++2",
    "--2",
    "hello1234",
    "1234hello",
    "444 world 555",
    "why 567 what",
    "-3 nots",
    "2e5",
    "300e2",
    "42+-$",
    "+42foo",
    "-514not",
    "\vblah",
    "0x10FFFF",
    "-0xABCDEF",
    // Test decimal numbers
    1.2345,
    42.0,
    3456789.1,
    -2.3456,
    -6789.12345,
    -2147483649.1234,
    // Test decimal strings
    "1.2345",
    "42.0",
    "3456789.1",
    "-2.3456",
    "-6789.12345",
    "-2147483649.1234",
    // Test special values
    undefined,
    null,
    NaN,
    Infinity,
    -Infinity,
  ];

  valuesToTest.forEach(function (v) {
    var intValue = stringToInteger(v, nonNegative, defaultValue);

    element.setAttribute(attr, v);

    is(
      element.getAttribute(attr),
      expectedGetAttributeResult(v),
      element.localName +
        ".setAttribute(" +
        attr +
        ", " +
        v +
        "), " +
        element.localName +
        ".getAttribute(" +
        attr +
        ") "
    );

    is(
      element[attr],
      intValue,
      element.localName +
        ".setAttribute(" +
        attr +
        ", " +
        v +
        "), " +
        element.localName +
        "[" +
        attr +
        "] "
    );
    element.removeAttribute(attr);

    if (nonNegative && expectedIdlAttributeResult(v) < 0) {
      try {
        element[attr] = v;
        ok(
          false,
          element.localName +
            "[" +
            attr +
            "] = " +
            v +
            " should throw IndexSizeError"
        );
      } catch (e) {
        is(
          e.name,
          "IndexSizeError",
          element.localName +
            "[" +
            attr +
            "] = " +
            v +
            " should throw IndexSizeError"
        );
        is(
          e.code,
          DOMException.INDEX_SIZE_ERR,
          element.localName +
            "[" +
            attr +
            "] = " +
            v +
            " should throw INDEX_SIZE_ERR"
        );
      }
    } else {
      element[attr] = v;
      is(
        element[attr],
        expectedIdlAttributeResult(v),
        element.localName +
          "[" +
          attr +
          "] = " +
          v +
          ", " +
          element.localName +
          "[" +
          attr +
          "] "
      );
      is(
        element.getAttribute(attr),
        String(expectedIdlAttributeResult(v)),
        element.localName +
          "[" +
          attr +
          "] = " +
          v +
          ", " +
          element.localName +
          ".getAttribute(" +
          attr +
          ") "
      );
    }
    element.removeAttribute(attr);
  });

  // Tests after removeAttribute() is called. Should be equivalent with not set.
  is(
    element.getAttribute(attr),
    null,
    "When not set, the content attribute should be null."
  );
  is(
    element[attr],
    defaultValue,
    "When not set, the IDL attribute should return default value."
  );
}

/**
 * Checks that a given attribute is correctly reflected as a url.
 *
 * @param aParameters   Object    object containing the parameters, which are:
 *  - element           Element   node to test
 *  - attribute         String    name of the attribute
 *     OR
 *    attribute         Object    object containing two attributes, 'content' and 'idl'
 */
function reflectURL(aParameters) {
  var element = aParameters.element;
  var contentAttr =
    typeof aParameters.attribute === "string"
      ? aParameters.attribute
      : aParameters.attribute.content;
  var idlAttr =
    typeof aParameters.attribute === "string"
      ? aParameters.attribute
      : aParameters.attribute.idl;

  element[idlAttr] = "";
  is(
    element[idlAttr],
    document.URL,
    "Empty string should resolve to document URL"
  );
}