Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <html> |
michael@0 | 2 | <head> |
michael@0 | 3 | <title>Testing Mixed Up Microformat APIs</title> |
michael@0 | 4 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 5 | <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script> |
michael@0 | 6 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"></link> |
michael@0 | 7 | </head> |
michael@0 | 8 | <body id="contentbody"> |
michael@0 | 9 | |
michael@0 | 10 | <!-- Parsing tests are well covered, we're going to do some data type stuff |
michael@0 | 11 | here --> |
michael@0 | 12 | <div id="test1"> |
michael@0 | 13 | <p class="mftest" id="mftest-version1"> |
michael@0 | 14 | <span class="title"><b>Can Really be Anything, even HTML</b></span> |
michael@0 | 15 | <span class="htmlval"><i>foo</i></span> |
michael@0 | 16 | <span class="htmlval">what about just text?</span> |
michael@0 | 17 | <span class="tel">011-23-45-7867-897890</span> |
michael@0 | 18 | <span class="uri">http://www.mozilla.org </span> |
michael@0 | 19 | <span class="email">mailto:joe@nowhere.org</span> |
michael@0 | 20 | <span class="geo"> |
michael@0 | 21 | <span class="latitude">0.0</span> |
michael@0 | 22 | <span class="longitude">0.0</span> |
michael@0 | 23 | </span> |
michael@0 | 24 | <span class="date">20080401T235900</span> |
michael@0 | 25 | </p> |
michael@0 | 26 | </div> |
michael@0 | 27 | |
michael@0 | 28 | <!-- This one is invalid until we add the new version of the hTest MF --> |
michael@0 | 29 | <div id="test2"> |
michael@0 | 30 | <span class="mftest" id="mftest-version2"> |
michael@0 | 31 | <abbr class="title" title="<b>Can Really be Anything, even HTML</b>"/> |
michael@0 | 32 | <span class="uri" title="http://www.mozilla.org"/> |
michael@0 | 33 | <abbr class="email" title="joe@nowhere.org">joe's email</ABBR> |
michael@0 | 34 | <span class="geo"> |
michael@0 | 35 | <span class="latitude">0.0</span> |
michael@0 | 36 | <span class="longitude">0.0"</span> |
michael@0 | 37 | </span> |
michael@0 | 38 | <!-- Throw a zulu in there! --> |
michael@0 | 39 | <span class="date" title="20080401T235900Z"/> |
michael@0 | 40 | </span> |
michael@0 | 41 | </div> |
michael@0 | 42 | |
michael@0 | 43 | <!-- This one is invalid in both versions (no title)--> |
michael@0 | 44 | <div id="test3"> |
michael@0 | 45 | <div class="mftest"> |
michael@0 | 46 | <abbr class="htmlval" title="what about just text?">more values</abbr> |
michael@0 | 47 | <span class="uri">http://foo.com</span> |
michael@0 | 48 | </div> |
michael@0 | 49 | </div> |
michael@0 | 50 | |
michael@0 | 51 | <!-- Contains an invalid geo --> |
michael@0 | 52 | <div id="test4"> |
michael@0 | 53 | <span class="mftest"> |
michael@0 | 54 | <abbr class="title" title="<b>Can Really be Anything, even HTML</b>"/> |
michael@0 | 55 | <abbr class="htmlval" title="<html><body>foo</body></html>"> |
michael@0 | 56 | An HTML document! |
michael@0 | 57 | </abbr> |
michael@0 | 58 | <abbr class="htmlval" title="what about just text?">more values</abbr> |
michael@0 | 59 | <span class="tel" title="011-23-45-7867-897890"/> |
michael@0 | 60 | <span class="uri" title="http://www.mozilla.org"/> |
michael@0 | 61 | <abbr class="email" title="joe@nowhere.org">joe's email</ABBR> |
michael@0 | 62 | <span class="geo"> |
michael@0 | 63 | <span class="latitude">659</span> |
michael@0 | 64 | <span class="longitude">-362.5</span> |
michael@0 | 65 | </span> |
michael@0 | 66 | <span class="date" title="20080401T235900"/> |
michael@0 | 67 | </span> |
michael@0 | 68 | </div> |
michael@0 | 69 | |
michael@0 | 70 | <!-- Contains an invalid date --> |
michael@0 | 71 | <div id="test5"> |
michael@0 | 72 | <span class="mftest"> |
michael@0 | 73 | <abbr class="title htmlval" title="another test">thehtmlvaltodoescapeme</abbr> |
michael@0 | 74 | <abbr class="date" title="200311T032Z">invalid date</abbr> |
michael@0 | 75 | </span> |
michael@0 | 76 | </div> |
michael@0 | 77 | |
michael@0 | 78 | <!-- Ok, the test, here we go --> |
michael@0 | 79 | <pre id="test"> |
michael@0 | 80 | <script class="testbody" type="text/javascript"> |
michael@0 | 81 | |
michael@0 | 82 | function hTest(node, validate) { |
michael@0 | 83 | var Microformats = SpecialPowers.Cu.import("resource://gre/modules/Microformats.js").Microformats; |
michael@0 | 84 | if (node) |
michael@0 | 85 | Microformats.parser.newMicroformat(this, node, "hTest", validate); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | hTest.prototype.toString = function () { |
michael@0 | 89 | return("This is a test"); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | // Define a new microformat to add to the Microformats collection |
michael@0 | 93 | var hTest_definition = { |
michael@0 | 94 | mfObject: hTest, |
michael@0 | 95 | className: "mftest", |
michael@0 | 96 | required: ["title", "htmlval"], |
michael@0 | 97 | properties: { |
michael@0 | 98 | "title" : {}, |
michael@0 | 99 | "htmlval" : { |
michael@0 | 100 | plural: true, |
michael@0 | 101 | datatype: "HTML" |
michael@0 | 102 | }, |
michael@0 | 103 | "tel" : { |
michael@0 | 104 | datatype: "tel", |
michael@0 | 105 | }, |
michael@0 | 106 | "uri" : { |
michael@0 | 107 | dataypte: "anyURI" |
michael@0 | 108 | }, |
michael@0 | 109 | "email" : { |
michael@0 | 110 | datatype: "email" |
michael@0 | 111 | }, |
michael@0 | 112 | "geo" : { |
michael@0 | 113 | datatype: "microformat", |
michael@0 | 114 | microformat: "geo" |
michael@0 | 115 | }, |
michael@0 | 116 | "date" : { |
michael@0 | 117 | datatype: "dateTime", |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | }; |
michael@0 | 121 | |
michael@0 | 122 | // Define another version of this microformat to overwrite it - this one |
michael@0 | 123 | // removes the requirement to have a htmlval and also removes the tel prop. |
michael@0 | 124 | var hTest_definition2 = { |
michael@0 | 125 | mfObject: hTest, |
michael@0 | 126 | className: "mftest", |
michael@0 | 127 | required: ["title"], |
michael@0 | 128 | properties: { |
michael@0 | 129 | "title" : {}, |
michael@0 | 130 | "htmlval" : { |
michael@0 | 131 | plural: true, |
michael@0 | 132 | datatype: "HTML" |
michael@0 | 133 | }, |
michael@0 | 134 | "uri" : { |
michael@0 | 135 | dataypte: "anyURI" |
michael@0 | 136 | }, |
michael@0 | 137 | "email" : { |
michael@0 | 138 | datatype: "email" |
michael@0 | 139 | }, |
michael@0 | 140 | "geo" : { |
michael@0 | 141 | datatype: "microformat", |
michael@0 | 142 | microformat: "geo" |
michael@0 | 143 | }, |
michael@0 | 144 | "date" : { |
michael@0 | 145 | datatype: "dateTime", |
michael@0 | 146 | } |
michael@0 | 147 | } |
michael@0 | 148 | }; |
michael@0 | 149 | test_MicroformatsAPI(); |
michael@0 | 150 | |
michael@0 | 151 | function doTest3_4_and5() { |
michael@0 | 152 | var Microformats = SpecialPowers.Cu.import("resource://gre/modules/Microformats.js").Microformats; |
michael@0 | 153 | ok(Microformats, "Make sure we still have a microformats object"); |
michael@0 | 154 | |
michael@0 | 155 | var mfs = Microformats.get("hTest", |
michael@0 | 156 | document.getElementById("test3"), |
michael@0 | 157 | {recurseExternalFrames: true}); |
michael@0 | 158 | |
michael@0 | 159 | is(mfs.length, 0, "Check hTest 3 is invalid"); |
michael@0 | 160 | |
michael@0 | 161 | mfs = Microformats.get("hTest", |
michael@0 | 162 | document.getElementById("test4"), |
michael@0 | 163 | {recurseExternalFrames: true}); |
michael@0 | 164 | |
michael@0 | 165 | is(mfs.length, 1, "Check hTest 4 is valid"); |
michael@0 | 166 | // Test 4 is a valid hTest, but it's embedded Geo is invalid, |
michael@0 | 167 | // test that assumption |
michael@0 | 168 | ok(!(mfs.geo), "Ensure that the test 4 geo is reporting as invalid"); |
michael@0 | 169 | |
michael@0 | 170 | mfs = Microformats.get("hTest", |
michael@0 | 171 | document.getElementById("test5"), |
michael@0 | 172 | {recurseExternalFrames: true}); |
michael@0 | 173 | is(mfs.length, 1, "Check hTest 5 is valid"); |
michael@0 | 174 | |
michael@0 | 175 | try { |
michael@0 | 176 | var jsDate = new Date(Microformats.parser.dateFromISO8601(mfs[0].date)); |
michael@0 | 177 | ok(false, "Invalid JS Date should throw"); |
michael@0 | 178 | } catch (ex) { |
michael@0 | 179 | ok(true, "Check that getting invalid jsdate throws"); |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | function test_MicroformatsAPI() { |
michael@0 | 184 | var Microformats = SpecialPowers.Cu.import("resource://gre/modules/Microformats.js").Microformats; |
michael@0 | 185 | |
michael@0 | 186 | // Make sure they aren't microformats yet |
michael@0 | 187 | is(Microformats.isMicroformat(document.getElementById("mftest-version1")), |
michael@0 | 188 | false, "Check that the mfTest microformat does not exist yet"); |
michael@0 | 189 | |
michael@0 | 190 | try { |
michael@0 | 191 | Microformats.add("hTest", hTest_definition); |
michael@0 | 192 | } catch (ex) { |
michael@0 | 193 | ok(false, "Threw while adding hTest definition 1"); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | ok(Microformats.isMicroformat(document.getElementById("mftest-version1")), |
michael@0 | 197 | "Check that the mfTest microformat now exists"); |
michael@0 | 198 | |
michael@0 | 199 | var mf = Microformats.get("hTest", document.getElementById("test1"), |
michael@0 | 200 | {recurseExternalFrames: true}); |
michael@0 | 201 | |
michael@0 | 202 | is(mf.length, 1, "Check that test1 is a valid microformat"); |
michael@0 | 203 | is(mf[0].title, "Can Really be Anything, even HTML", "Check test1 title"); |
michael@0 | 204 | |
michael@0 | 205 | is(mf[0].geo.latitude, 0.0, "Check test1 geo"); |
michael@0 | 206 | |
michael@0 | 207 | // Make sure that our test2 doesn't pass validation until we add |
michael@0 | 208 | // version 2 of the hTest microformat |
michael@0 | 209 | var mf2 = Microformats.get("hTest", document.getElementById("test2"), {}); |
michael@0 | 210 | is(mf2.length, 0, "Check that the mfTest microformat version 2 is not a MF"); |
michael@0 | 211 | |
michael@0 | 212 | doTest3_4_and5(false); |
michael@0 | 213 | |
michael@0 | 214 | // Ok, add the version 2 hTest |
michael@0 | 215 | try { |
michael@0 | 216 | Microformats.add("hTest", hTest_definition2); |
michael@0 | 217 | } catch (ex) { |
michael@0 | 218 | ok(false, "Threw while adding hTest definition 2"); |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | // The old version's microformat is still valid |
michael@0 | 222 | mf = Microformats.get("hTest", document.getElementById("test1"), |
michael@0 | 223 | {recurseExternalFrames: true}); |
michael@0 | 224 | |
michael@0 | 225 | ok(mf.length, 1, "Check that test1 is a valid microformat"); |
michael@0 | 226 | |
michael@0 | 227 | // Verify that the version 2 microformat is now also considered valid |
michael@0 | 228 | var mf2 = Microformats.get("hTest", document.getElementById("test2"), {}); |
michael@0 | 229 | |
michael@0 | 230 | ok(mf2.length, 1, "Check that the mfTest microformat version 2 is now valid"); |
michael@0 | 231 | doTest3_4_and5(true); |
michael@0 | 232 | |
michael@0 | 233 | Microformats.remove("hTest"); |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | </script> |
michael@0 | 237 | </body> |
michael@0 | 238 | </html> |