B2G/gecko/content/html/content/test/reflect.js
/**
 * 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
 */
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
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
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 : [];
  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");
  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");
  element.setAttribute(contentAttr, null);
  todo_is(element.getAttribute(contentAttr), "null",
     "null should have been stringified to 'null'");
  todo_is(element[idlAttr], "null",
     "null should have been stringified to 'null'");
  element.removeAttribute(contentAttr);
  element[idlAttr] = null;
  if (element.localName == "textarea" && idlAttr == "wrap") {
    is(element.getAttribute(contentAttr), "null",
       "null should have been stringified to 'null'");
    is(element[idlAttr], "null", "null should have been stringified to 'null'");
    element.removeAttribute(contentAttr);
  } else {
    todo_is(element.getAttribute(contentAttr), "null",
       "null should have been stringified to 'null'");
    todo_is(element[idlAttr], "null",
       "null should have been stringified to 'null'");
    element.removeAttribute(contentAttr);
  }
  var stringsToTest = [
    [ "", "" ],
    [ "null", "null" ],
    [ "undefined", "undefined" ],
    [ "foo", "foo" ],
    [ contentAttr, contentAttr ],
    [ idlAttr, idlAttr ],
    [ undefined, "undefined" ],
    [ true, "true" ],
    [ false, "false" ],
    [ 42, "42" ],
    [ { toString: function() { return "foo" } },
      "foo" ],
    [ { valueOf: function() { return "foo" } },
      "[object Object]" ],
    [ { valueOf: function() { return "quux" },
       toString: undefined },
      "quux" ],
    [ { valueOf: function() { return "foo" },
        toString: function() { 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 should return the value it has been set to.");
    is(element.getAttribute(contentAttr), r,
       "Content attribute should return the value it has been set to.");
    element.removeAttribute(contentAttr);
    element[idlAttr] = v;
    is(element[idlAttr], r,
       "IDL attribute should return the value it has been set to.");
    is(element.getAttribute(contentAttr), r,
       "Content attribute should return the value it has been set to.");
    element.removeAttribute(contentAttr);
  });
  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");
}
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 : [ ] ; 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" ) ; 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" ) ; element . setAttribute ( contentAttr , null ) ; todo_is ( element . getAttribute ( contentAttr ) , "null" , "null should have been stringified to 'null'" ) ; todo_is ( element [ idlAttr ] , "null" , "null should have been stringified to 'null'" ) ; element . removeAttribute ( contentAttr ) ; element [ idlAttr ] = null ; if ( element . localName == "textarea" && idlAttr == "wrap" ) { is ( element . getAttribute ( contentAttr ) , "null" , "null should have been stringified to 'null'" ) ; is ( element [ idlAttr ] , "null" , "null should have been stringified to 'null'" ) ; element . removeAttribute ( contentAttr ) ; } else { todo_is ( element . getAttribute ( contentAttr ) , "null" , "null should have been stringified to 'null'" ) ; todo_is ( element [ idlAttr ] , "null" , "null should have been stringified to 'null'" ) ; element . removeAttribute ( contentAttr ) ; } var stringsToTest = [ [ "" , "" ] , [ "null" , "null" ] , [ "undefined" , "undefined" ] , [ "foo" , "foo" ] , [ contentAttr , contentAttr ] , [ idlAttr , idlAttr ] , [ undefined , "undefined" ] , [ true , "true" ] , [ false , "false" ] , [ 42 , "42" ] , [ { toString : function ( ) { return "foo" } } , "foo" ] , [ { valueOf : function ( ) { return "foo" } } , "[object Object]" ] , [ { valueOf : function ( ) { return "quux" } , toString : undefined } , "quux" ] , [ { valueOf : function ( ) { return "foo" } , toString : function ( ) { 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 should return the value it has been set to." ) ; is ( element . getAttribute ( contentAttr ) , r , "Content attribute should return the value it has been set to." ) ; element . removeAttribute ( contentAttr ) ; element [ idlAttr ] = v ; is ( element [ idlAttr ] , r , "IDL attribute should return the value it has been set to." ) ; is ( element . getAttribute ( contentAttr ) , r , "Content attribute should return the value it has been set to." ) ; element . removeAttribute ( contentAttr ) ; } ) ; 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 is correctly reflected as a string.
Checks that a given attribute is correctly reflected as a string .
// Tests when the attribute isn't set. /**
   * TODO: as long as null stringification doesn't follow the WebIDL
   * specifications, don't add it to the loop below and keep it here.
   */ // TODO: remove this ugly hack when null stringification will work as expected. // Tests various strings. // [ test value, expected result ] // TODO: uncomment this when null stringification will follow the specs. // [ null, "null" ], // ES5, verse 8.12.8. // Tests after removeAttribute() is called. Should be equivalent with not set.