1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/microformats/tests/test_Microformats_add.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,238 @@ 1.4 +<html> 1.5 +<head> 1.6 + <title>Testing Mixed Up Microformat APIs</title> 1.7 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.8 + <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script> 1.9 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"></link> 1.10 +</head> 1.11 +<body id="contentbody"> 1.12 + 1.13 + <!-- Parsing tests are well covered, we're going to do some data type stuff 1.14 + here --> 1.15 + <div id="test1"> 1.16 + <p class="mftest" id="mftest-version1"> 1.17 + <span class="title"><b>Can Really be Anything, even HTML</b></span> 1.18 + <span class="htmlval"><i>foo</i></span> 1.19 + <span class="htmlval">what about just text?</span> 1.20 + <span class="tel">011-23-45-7867-897890</span> 1.21 + <span class="uri">http://www.mozilla.org </span> 1.22 + <span class="email">mailto:joe@nowhere.org</span> 1.23 + <span class="geo"> 1.24 + <span class="latitude">0.0</span> 1.25 + <span class="longitude">0.0</span> 1.26 + </span> 1.27 + <span class="date">20080401T235900</span> 1.28 + </p> 1.29 + </div> 1.30 + 1.31 + <!-- This one is invalid until we add the new version of the hTest MF --> 1.32 + <div id="test2"> 1.33 + <span class="mftest" id="mftest-version2"> 1.34 + <abbr class="title" title="<b>Can Really be Anything, even HTML</b>"/> 1.35 + <span class="uri" title="http://www.mozilla.org"/> 1.36 + <abbr class="email" title="joe@nowhere.org">joe's email</ABBR> 1.37 + <span class="geo"> 1.38 + <span class="latitude">0.0</span> 1.39 + <span class="longitude">0.0"</span> 1.40 + </span> 1.41 + <!-- Throw a zulu in there! --> 1.42 + <span class="date" title="20080401T235900Z"/> 1.43 + </span> 1.44 + </div> 1.45 + 1.46 + <!-- This one is invalid in both versions (no title)--> 1.47 + <div id="test3"> 1.48 + <div class="mftest"> 1.49 + <abbr class="htmlval" title="what about just text?">more values</abbr> 1.50 + <span class="uri">http://foo.com</span> 1.51 + </div> 1.52 + </div> 1.53 + 1.54 + <!-- Contains an invalid geo --> 1.55 + <div id="test4"> 1.56 + <span class="mftest"> 1.57 + <abbr class="title" title="<b>Can Really be Anything, even HTML</b>"/> 1.58 + <abbr class="htmlval" title="<html><body>foo</body></html>"> 1.59 + An HTML document! 1.60 + </abbr> 1.61 + <abbr class="htmlval" title="what about just text?">more values</abbr> 1.62 + <span class="tel" title="011-23-45-7867-897890"/> 1.63 + <span class="uri" title="http://www.mozilla.org"/> 1.64 + <abbr class="email" title="joe@nowhere.org">joe's email</ABBR> 1.65 + <span class="geo"> 1.66 + <span class="latitude">659</span> 1.67 + <span class="longitude">-362.5</span> 1.68 + </span> 1.69 + <span class="date" title="20080401T235900"/> 1.70 + </span> 1.71 + </div> 1.72 + 1.73 + <!-- Contains an invalid date --> 1.74 + <div id="test5"> 1.75 + <span class="mftest"> 1.76 + <abbr class="title htmlval" title="another test">thehtmlvaltodoescapeme</abbr> 1.77 + <abbr class="date" title="200311T032Z">invalid date</abbr> 1.78 + </span> 1.79 + </div> 1.80 + 1.81 + <!-- Ok, the test, here we go --> 1.82 + <pre id="test"> 1.83 + <script class="testbody" type="text/javascript"> 1.84 + 1.85 + function hTest(node, validate) { 1.86 + var Microformats = SpecialPowers.Cu.import("resource://gre/modules/Microformats.js").Microformats; 1.87 + if (node) 1.88 + Microformats.parser.newMicroformat(this, node, "hTest", validate); 1.89 + } 1.90 + 1.91 + hTest.prototype.toString = function () { 1.92 + return("This is a test"); 1.93 + } 1.94 + 1.95 + // Define a new microformat to add to the Microformats collection 1.96 + var hTest_definition = { 1.97 + mfObject: hTest, 1.98 + className: "mftest", 1.99 + required: ["title", "htmlval"], 1.100 + properties: { 1.101 + "title" : {}, 1.102 + "htmlval" : { 1.103 + plural: true, 1.104 + datatype: "HTML" 1.105 + }, 1.106 + "tel" : { 1.107 + datatype: "tel", 1.108 + }, 1.109 + "uri" : { 1.110 + dataypte: "anyURI" 1.111 + }, 1.112 + "email" : { 1.113 + datatype: "email" 1.114 + }, 1.115 + "geo" : { 1.116 + datatype: "microformat", 1.117 + microformat: "geo" 1.118 + }, 1.119 + "date" : { 1.120 + datatype: "dateTime", 1.121 + } 1.122 + } 1.123 + }; 1.124 + 1.125 + // Define another version of this microformat to overwrite it - this one 1.126 + // removes the requirement to have a htmlval and also removes the tel prop. 1.127 + var hTest_definition2 = { 1.128 + mfObject: hTest, 1.129 + className: "mftest", 1.130 + required: ["title"], 1.131 + properties: { 1.132 + "title" : {}, 1.133 + "htmlval" : { 1.134 + plural: true, 1.135 + datatype: "HTML" 1.136 + }, 1.137 + "uri" : { 1.138 + dataypte: "anyURI" 1.139 + }, 1.140 + "email" : { 1.141 + datatype: "email" 1.142 + }, 1.143 + "geo" : { 1.144 + datatype: "microformat", 1.145 + microformat: "geo" 1.146 + }, 1.147 + "date" : { 1.148 + datatype: "dateTime", 1.149 + } 1.150 + } 1.151 + }; 1.152 + test_MicroformatsAPI(); 1.153 + 1.154 + function doTest3_4_and5() { 1.155 + var Microformats = SpecialPowers.Cu.import("resource://gre/modules/Microformats.js").Microformats; 1.156 + ok(Microformats, "Make sure we still have a microformats object"); 1.157 + 1.158 + var mfs = Microformats.get("hTest", 1.159 + document.getElementById("test3"), 1.160 + {recurseExternalFrames: true}); 1.161 + 1.162 + is(mfs.length, 0, "Check hTest 3 is invalid"); 1.163 + 1.164 + mfs = Microformats.get("hTest", 1.165 + document.getElementById("test4"), 1.166 + {recurseExternalFrames: true}); 1.167 + 1.168 + is(mfs.length, 1, "Check hTest 4 is valid"); 1.169 + // Test 4 is a valid hTest, but it's embedded Geo is invalid, 1.170 + // test that assumption 1.171 + ok(!(mfs.geo), "Ensure that the test 4 geo is reporting as invalid"); 1.172 + 1.173 + mfs = Microformats.get("hTest", 1.174 + document.getElementById("test5"), 1.175 + {recurseExternalFrames: true}); 1.176 + is(mfs.length, 1, "Check hTest 5 is valid"); 1.177 + 1.178 + try { 1.179 + var jsDate = new Date(Microformats.parser.dateFromISO8601(mfs[0].date)); 1.180 + ok(false, "Invalid JS Date should throw"); 1.181 + } catch (ex) { 1.182 + ok(true, "Check that getting invalid jsdate throws"); 1.183 + } 1.184 + } 1.185 + 1.186 + function test_MicroformatsAPI() { 1.187 + var Microformats = SpecialPowers.Cu.import("resource://gre/modules/Microformats.js").Microformats; 1.188 + 1.189 + // Make sure they aren't microformats yet 1.190 + is(Microformats.isMicroformat(document.getElementById("mftest-version1")), 1.191 + false, "Check that the mfTest microformat does not exist yet"); 1.192 + 1.193 + try { 1.194 + Microformats.add("hTest", hTest_definition); 1.195 + } catch (ex) { 1.196 + ok(false, "Threw while adding hTest definition 1"); 1.197 + } 1.198 + 1.199 + ok(Microformats.isMicroformat(document.getElementById("mftest-version1")), 1.200 + "Check that the mfTest microformat now exists"); 1.201 + 1.202 + var mf = Microformats.get("hTest", document.getElementById("test1"), 1.203 + {recurseExternalFrames: true}); 1.204 + 1.205 + is(mf.length, 1, "Check that test1 is a valid microformat"); 1.206 + is(mf[0].title, "Can Really be Anything, even HTML", "Check test1 title"); 1.207 + 1.208 + is(mf[0].geo.latitude, 0.0, "Check test1 geo"); 1.209 + 1.210 + // Make sure that our test2 doesn't pass validation until we add 1.211 + // version 2 of the hTest microformat 1.212 + var mf2 = Microformats.get("hTest", document.getElementById("test2"), {}); 1.213 + is(mf2.length, 0, "Check that the mfTest microformat version 2 is not a MF"); 1.214 + 1.215 + doTest3_4_and5(false); 1.216 + 1.217 + // Ok, add the version 2 hTest 1.218 + try { 1.219 + Microformats.add("hTest", hTest_definition2); 1.220 + } catch (ex) { 1.221 + ok(false, "Threw while adding hTest definition 2"); 1.222 + } 1.223 + 1.224 + // The old version's microformat is still valid 1.225 + mf = Microformats.get("hTest", document.getElementById("test1"), 1.226 + {recurseExternalFrames: true}); 1.227 + 1.228 + ok(mf.length, 1, "Check that test1 is a valid microformat"); 1.229 + 1.230 + // Verify that the version 2 microformat is now also considered valid 1.231 + var mf2 = Microformats.get("hTest", document.getElementById("test2"), {}); 1.232 + 1.233 + ok(mf2.length, 1, "Check that the mfTest microformat version 2 is now valid"); 1.234 + doTest3_4_and5(true); 1.235 + 1.236 + Microformats.remove("hTest"); 1.237 + } 1.238 + 1.239 + </script> 1.240 +</body> 1.241 +</html>