toolkit/components/microformats/tests/test_Microformats_add.html
author Dave Townsend <dtownsend@oxymoronical.com>
Wed, 03 Feb 2016 13:12:18 -0800
changeset 319500 345ff6c8b0a24a357c9f0b4d8d4f9c6e5c71eb16
parent 158370 45446ab0c72656eaa3afd505db47d94a686fa301
permissions -rw-r--r--
Bug 1245649: Merge browser and toolkit eslint rule settings. r=felipe

<html>
<head>
  <title>Testing Mixed Up Microformat APIs</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"></link>
</head>
<body id="contentbody">

  <!-- Parsing tests are well covered, we're going to do some data type stuff
       here -->
  <div id="test1">
    <p class="mftest" id="mftest-version1">
      <span class="title"><b>Can Really be Anything, even HTML</b></span>
      <span class="htmlval"><i>foo</i></span>
      <span class="htmlval">what about just text?</span>
      <span class="tel">011-23-45-7867-897890</span>
      <span class="uri">http://www.mozilla.org </span>
      <span class="email">mailto:joe@nowhere.org</span>
      <span class="geo">
        <span class="latitude">0.0</span>
        <span class="longitude">0.0</span>
      </span>
      <span class="date">20080401T235900</span>
    </p>
  </div>

  <!-- This one is invalid until we add the new version of the hTest MF -->
  <div id="test2">
    <span class="mftest" id="mftest-version2">
      <abbr class="title" title="<b>Can Really be Anything, even HTML</b>"/>
      <span class="uri" title="http://www.mozilla.org"/>
      <abbr class="email" title="joe@nowhere.org">joe's email</ABBR>
      <span class="geo">
        <span class="latitude">0.0</span>
        <span class="longitude">0.0"</span>
      </span>
      <!-- Throw a zulu in there! -->
      <span class="date" title="20080401T235900Z"/>
    </span>
  </div>

  <!-- This one is invalid in both versions (no title)-->
  <div id="test3">
    <div class="mftest">
      <abbr class="htmlval" title="what about just text?">more values</abbr>  
      <span class="uri">http://foo.com</span>
    </div>
  </div>

  <!-- Contains an invalid geo -->
  <div id="test4">
    <span class="mftest">
      <abbr class="title" title="<b>Can Really be Anything, even HTML</b>"/>
      <abbr class="htmlval" title="<html><body>foo</body></html>">
        An HTML document!
      </abbr>
      <abbr class="htmlval" title="what about just text?">more values</abbr>
      <span class="tel" title="011-23-45-7867-897890"/>
      <span class="uri" title="http://www.mozilla.org"/>
      <abbr class="email" title="joe@nowhere.org">joe's email</ABBR>
      <span class="geo">
        <span class="latitude">659</span>
        <span class="longitude">-362.5</span>
      </span>
      <span class="date" title="20080401T235900"/>
    </span>
  </div>

  <!-- Contains an invalid date -->
  <div id="test5">
    <span class="mftest">
      <abbr class="title htmlval" title="another test">thehtmlvaltodoescapeme</abbr>
      <abbr class="date" title="200311T032Z">invalid date</abbr>
    </span>
  </div>

  <!-- Ok, the test, here we go -->
  <pre id="test">
  <script class="testbody" type="text/javascript">

  function hTest(node, validate) {
    var Microformats = SpecialPowers.Cu.import("resource://gre/modules/Microformats.js").Microformats;
    if (node)
      Microformats.parser.newMicroformat(this, node, "hTest", validate);
  }

  hTest.prototype.toString = function () {
    return("This is a test");
  }

  // Define a new microformat to add to the Microformats collection
  var hTest_definition = {
    mfObject: hTest,
    className: "mftest",
    required: ["title", "htmlval"],
    properties: {
      "title" : {},
      "htmlval" : {
        plural: true,
        datatype: "HTML"
      },
      "tel" : {
        datatype: "tel",
      },
      "uri" : {
        dataypte: "anyURI"
      },
      "email" : {
        datatype: "email"
      },
      "geo" : {
        datatype: "microformat",
        microformat: "geo"
      },
      "date" : {
        datatype: "dateTime",
      }
    }
  };

  // Define another version of this microformat to overwrite it - this one
  // removes the requirement to have a htmlval and also removes the tel prop.
  var hTest_definition2 = {
    mfObject: hTest,
    className: "mftest",
    required: ["title"],
    properties: {
      "title" : {},
      "htmlval" : {
        plural: true,
        datatype: "HTML"
      },
      "uri" : {
        dataypte: "anyURI"
      },
      "email" : {
        datatype: "email"
      },
      "geo" : {
        datatype: "microformat",
        microformat: "geo"
      },
      "date" : {
        datatype: "dateTime",
      }
    }
  };
  test_MicroformatsAPI();

  function doTest3_4_and5() {
    var Microformats = SpecialPowers.Cu.import("resource://gre/modules/Microformats.js").Microformats;
    ok(Microformats, "Make sure we still have a microformats object");

    var mfs = Microformats.get("hTest",
                               document.getElementById("test3"),
                               {recurseExternalFrames: true});

    is(mfs.length, 0, "Check hTest 3 is invalid");

    mfs = Microformats.get("hTest",
                            document.getElementById("test4"),
                            {recurseExternalFrames: true});

    is(mfs.length, 1, "Check hTest 4 is valid");
    // Test 4 is a valid hTest, but it's embedded Geo is invalid,
    // test that assumption
    ok(!(mfs.geo), "Ensure that the test 4 geo is reporting as invalid");

    mfs = Microformats.get("hTest",
                            document.getElementById("test5"),
                            {recurseExternalFrames: true});
    is(mfs.length, 1, "Check hTest 5 is valid");

    try {
      var jsDate = new Date(Microformats.parser.dateFromISO8601(mfs[0].date));
      ok(false, "Invalid JS Date should throw");
    } catch (ex) {
      ok(true, "Check that getting invalid jsdate throws");
    }
  }

  function test_MicroformatsAPI() {
    var Microformats = SpecialPowers.Cu.import("resource://gre/modules/Microformats.js").Microformats;

    // Make sure they aren't microformats yet
    is(Microformats.isMicroformat(document.getElementById("mftest-version1")),
       false, "Check that the mfTest microformat does not exist yet");

    try {
      Microformats.add("hTest", hTest_definition);
    } catch (ex) {
      ok(false, "Threw while adding hTest definition 1");
    }

    ok(Microformats.isMicroformat(document.getElementById("mftest-version1")),
       "Check that the mfTest microformat now exists");

    var mf = Microformats.get("hTest", document.getElementById("test1"),
                              {recurseExternalFrames: true});

    is(mf.length, 1, "Check that test1 is a valid microformat");
    is(mf[0].title, "Can Really be Anything, even HTML", "Check test1 title");

    is(mf[0].geo.latitude, 0.0, "Check test1 geo");

    // Make sure that our test2 doesn't pass validation until we add
    // version 2 of the hTest microformat
    var mf2 = Microformats.get("hTest", document.getElementById("test2"), {});
    is(mf2.length, 0, "Check that the mfTest microformat version 2 is not a MF");

    doTest3_4_and5(false);

    // Ok, add the version 2 hTest
    try {
      Microformats.add("hTest", hTest_definition2);
    } catch (ex) {
      ok(false, "Threw while adding hTest definition 2");
    }

    // The old version's microformat is still valid
    mf = Microformats.get("hTest", document.getElementById("test1"),
                          {recurseExternalFrames: true});

    ok(mf.length, 1, "Check that test1 is a valid microformat");

    // Verify that the version 2 microformat is now also considered valid
    mf2 = Microformats.get("hTest", document.getElementById("test2"), {});

    ok(mf2.length, 1, "Check that the mfTest microformat version 2 is now valid");
    doTest3_4_and5(true);

    Microformats.remove("hTest");
  }

  </script>
</body>
</html>