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