git-svn-id: svn://euphorik.ch/pompage@45 02bbb61a-6d21-0410-aba0-cb053bdfd66a
[pompage.git] / doc / webdeveloper / features / display_page_validation.js
1 var webdeveloper_accessibilityValidator = new WebDeveloperValidateAccessibility();
2 var webdeveloper_cssValidator = new WebDeveloperValidateCSS();
3 var webdeveloper_htmlValidator = new WebDeveloperValidateHTML();
4 var webdeveloper_selectedValidationTab = -1;
5
6 // Clears the accessibility validation details for the page
7 function webdeveloper_clearPageAccessibilityValidation()
8 {
9 var validationButton = document.getElementById("webdeveloper-accessibility-validation");
10
11 // If the validation button is set
12 if(validationButton)
13 {
14 validationButton.label = "";
15
16 // If the validation button has a class attribute
17 if(validationButton.hasAttribute("class"))
18 {
19 validationButton.removeAttribute("class");
20 }
21 }
22
23 webdeveloper_accessibilityValidator.cleanUp();
24 }
25
26 // Clears the CSS validation details for the page
27 function webdeveloper_clearPageCSSValidation()
28 {
29 var validationButton = document.getElementById("webdeveloper-css-validation");
30
31 // If the validation button is set
32 if(validationButton)
33 {
34 validationButton.label = "";
35
36 // If the validation button has a class attribute
37 if(validationButton.hasAttribute("class"))
38 {
39 validationButton.removeAttribute("class");
40 }
41 }
42
43 webdeveloper_cssValidator.cleanUp();
44 }
45
46 // Clears the HTML validation details for the page
47 function webdeveloper_clearPageHTMLValidation()
48 {
49 var validationButton = document.getElementById("webdeveloper-html-validation");
50
51 // If the validation button is set
52 if(validationButton)
53 {
54 validationButton.label = "";
55
56 // If the validation button has a class attribute
57 if(validationButton.hasAttribute("class"))
58 {
59 validationButton.removeAttribute("class");
60 }
61 }
62
63 webdeveloper_htmlValidator.cleanUp();
64 }
65
66 // Displays page validation
67 function webdeveloper_displayPageValidation(element)
68 {
69 var checked = false;
70 var windowContent = window.document.getElementById("content");
71
72 // If the element is set
73 if(element)
74 {
75 checked = element.getAttribute("checked");
76 }
77
78 webdeveloper_configureElement(document.getElementById("webdeveloper-page-validation-toolbar"), "hidden", !checked);
79
80 // If the window content is set
81 if(windowContent)
82 {
83 var tabBox = windowContent.mTabBox;
84
85 webdeveloper_clearPageAccessibilityValidation();
86 webdeveloper_clearPageCSSValidation();
87 webdeveloper_clearPageHTMLValidation();
88
89 // If displaying page validation
90 if(checked)
91 {
92 var contentDocument = webdeveloper_getContentDocument();
93
94 webdeveloper_updatePageAccessibilityValidation(contentDocument);
95 webdeveloper_updatePageCSSValidation(contentDocument);
96 webdeveloper_updatePageHTMLValidation(contentDocument);
97
98 windowContent.addEventListener("load", webdeveloper_pageValidationLoad, true);
99 windowContent.addEventListener("unload", webdeveloper_pageValidationUnload, true);
100
101 // If the tab box is set
102 if(tabBox)
103 {
104 tabBox.addEventListener("select", webdeveloper_tabValidationSelect, false);
105 }
106 }
107 else
108 {
109 // Try to remove the event listener
110 try
111 {
112 windowContent.removeEventListener("load", webdeveloper_pageValidationLoad, true);
113 }
114 catch(exception)
115 {
116 // Do nothing
117 }
118
119 // Try to remove the event listener
120 try
121 {
122 windowContent.removeEventListener("unload", webdeveloper_pageValidationUnload, true);
123 }
124 catch(exception)
125 {
126 // Do nothing
127 }
128
129 // If the tab box is set
130 if(tabBox)
131 {
132 // Try to remove the event listener
133 try
134 {
135 tabBox.removeEventListener("select", webdeveloper_tabValidationSelect, false);
136 }
137 catch(exception)
138 {
139 // Do nothing
140 }
141 }
142 }
143 }
144
145 webdeveloper_toggleAppliedStyle(element, "webdeveloper-display-page-validation");
146 }
147
148 // Checks if the page is validatable
149 function webdeveloper_isValidatablePage(uri)
150 {
151 // If the URI is set and is validatable
152 if(uri && uri != "about:blank" && uri != "http://www.hermish.com/check_this.cfm" && uri != "http://jigsaw.w3.org/css-validator/validator" && uri != "http://validator.w3.org/check")
153 {
154 return true;
155 }
156
157 return false;
158 }
159
160 // Handles the page being loaded
161 function webdeveloper_pageValidationLoad(event)
162 {
163 var eventTarget = event.target;
164
165 // If the page is the target
166 if(eventTarget && eventTarget.hasAttribute && eventTarget.hasAttribute("id") && eventTarget.getAttribute("id").toLowerCase() == "content")
167 {
168 var contentDocument = eventTarget.contentDocument;
169
170 // Try to get the original target
171 try
172 {
173 var loadedDocument = event.originalTarget;
174
175 // If the content document is set, the loaded document is set and has the same URI as the content document
176 if(contentDocument && loadedDocument && contentDocument.documentURI == loadedDocument.documentURI)
177 {
178 webdeveloper_clearPageAccessibilityValidation();
179 webdeveloper_clearPageCSSValidation();
180 webdeveloper_clearPageHTMLValidation();
181
182 // If the page is validatable
183 if(webdeveloper_isValidatablePage(contentDocument.documentURI))
184 {
185 webdeveloper_updatePageAccessibilityValidation(contentDocument);
186 webdeveloper_updatePageCSSValidation(contentDocument);
187 webdeveloper_updatePageHTMLValidation(contentDocument);
188 }
189 }
190 }
191 catch(exception)
192 {
193 // Do nothing
194 }
195 }
196 }
197
198 // Handles the page being unloaded
199 function webdeveloper_pageValidationUnload(event)
200 {
201 var eventTarget = event.target;
202
203 // Try to get the original target
204 try
205 {
206 var originalTarget = event.originalTarget;
207
208 // If the page is the target and the URI matches
209 if(eventTarget && originalTarget && eventTarget.contentDocument && eventTarget.hasAttribute && eventTarget.hasAttribute("id") && eventTarget.getAttribute("id").toLowerCase() == "content" && eventTarget.contentDocument.documentURI == originalTarget.documentURI)
210 {
211 webdeveloper_clearPageAccessibilityValidation();
212 webdeveloper_clearPageCSSValidation();
213 webdeveloper_clearPageHTMLValidation();
214 }
215 }
216 catch(exception)
217 {
218 // Do nothing
219 }
220 }
221
222 // Handles a tab being selected
223 function webdeveloper_tabValidationSelect(event)
224 {
225 var selectedTab = getBrowser().mTabBox.selectedIndex;
226
227 // If the selected tab is different
228 if(selectedTab != webdeveloper_selectedValidationTab)
229 {
230 var contentDocument = webdeveloper_getContentDocument();
231
232 webdeveloper_selectedValidationTab = selectedTab;
233
234 webdeveloper_clearPageAccessibilityValidation();
235 webdeveloper_clearPageCSSValidation();
236 webdeveloper_clearPageHTMLValidation();
237 webdeveloper_updatePageAccessibilityValidation(contentDocument);
238 webdeveloper_updatePageCSSValidation(contentDocument);
239 webdeveloper_updatePageHTMLValidation(contentDocument);
240 }
241 }
242
243 // Updates the accessibility validation for the page
244 function webdeveloper_updatePageAccessibilityValidation(contentDocument)
245 {
246 var validationButton = document.getElementById("webdeveloper-accessibility-validation");
247
248 // If the validation button is set
249 if(validationButton)
250 {
251 webdeveloper_accessibilityValidator.cleanUp();
252 webdeveloper_accessibilityValidator.validateBackgroundAccessibility(Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI(contentDocument.documentURI, null, null));
253
254 validationButton.label = document.getElementById("webdeveloper-string-bundle").getString("webdeveloper_validating");
255 }
256 }
257
258 // Updates the accessibility validation details for the page
259 function webdeveloper_updatePageAccessibilityValidationDetails()
260 {
261 // If the accessibility validation request is set and is ready
262 if(webdeveloper_accessibilityValidator.validationRequest && webdeveloper_accessibilityValidator.validationRequest.readyState == 4)
263 {
264 // Try to check the accessibility validation status
265 try
266 {
267 // If the accessibility validation status is set to success
268 if(webdeveloper_accessibilityValidator.validationRequest.status == 200)
269 {
270 var errors = webdeveloper_accessibilityValidator.parseValidationResultsByType("violation");
271 var validationButton = document.getElementById("webdeveloper-accessibility-validation");
272 var warnings = webdeveloper_accessibilityValidator.parseValidationResultsByType("caution");
273
274 // If the validation button is set
275 if(validationButton)
276 {
277 var stringBundle = document.getElementById("webdeveloper-string-bundle");
278
279 // If there are errors
280 if(errors > 0)
281 {
282 var labelValue = stringBundle.getString("webdeveloper_invalid") + ": " + errors + " " + stringBundle.getString("webdeveloper_errors").toLowerCase();
283
284 // If there are warnings
285 if(warnings > 0)
286 {
287 labelValue += ", " + warnings + " " + stringBundle.getString("webdeveloper_warnings").toLowerCase();
288 }
289
290 validationButton.label = labelValue;
291
292 validationButton.setAttribute("class", "error");
293 }
294 else if(warnings > 0)
295 {
296 validationButton.label = stringBundle.getString("webdeveloper_warning") + ": " + warnings + " " + stringBundle.getString("webdeveloper_warnings").toLowerCase();
297
298 validationButton.setAttribute("class", "warning");
299 }
300 else
301 {
302 validationButton.label = stringBundle.getString("webdeveloper_valid");
303
304 validationButton.setAttribute("class", "valid");
305 }
306 }
307 }
308 }
309 catch(exception)
310 {
311 // Do nothing
312 }
313
314 webdeveloper_accessibilityValidator.cleanUp();
315 }
316 }
317
318 // Updates the CSS validation for the page
319 function webdeveloper_updatePageCSSValidation(contentDocument)
320 {
321 var validationButton = document.getElementById("webdeveloper-css-validation");
322
323 // If the validation button is set
324 if(validationButton)
325 {
326 webdeveloper_cssValidator.cleanUp();
327 webdeveloper_cssValidator.validateBackgroundCSS(Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI(contentDocument.documentURI, null, null), webdeveloper_getDocuments(webdeveloper_getContentWindow()));
328
329 validationButton.label = document.getElementById("webdeveloper-string-bundle").getString("webdeveloper_validating");
330 }
331 }
332
333 // Updates the CSS validation details for the page
334 function webdeveloper_updatePageCSSValidationDetails()
335 {
336 // If the CSS validation request is set and is ready
337 if(webdeveloper_cssValidator.validationRequest && webdeveloper_cssValidator.validationRequest.readyState == 4)
338 {
339 // Try to check the CSS validation status
340 try
341 {
342 // If the CSS validation status is set to success
343 if(webdeveloper_cssValidator.validationRequest.status == 200)
344 {
345 var errors = webdeveloper_cssValidator.parseValidationResultsByType("errors");
346 var validationButton = document.getElementById("webdeveloper-css-validation");
347 var warnings = webdeveloper_cssValidator.parseValidationResultsByType("warnings");
348
349 // If the validation button is set
350 if(validationButton)
351 {
352 var stringBundle = document.getElementById("webdeveloper-string-bundle");
353
354 // If there are errors
355 if(errors > 0)
356 {
357 var labelValue = stringBundle.getString("webdeveloper_invalid") + ": " + errors + " " + stringBundle.getString("webdeveloper_errors").toLowerCase();
358
359 // If there are warnings
360 if(warnings > 0)
361 {
362 labelValue += ", " + warnings + " " + stringBundle.getString("webdeveloper_warnings").toLowerCase();
363 }
364
365 validationButton.label = labelValue;
366
367 validationButton.setAttribute("class", "error");
368 }
369 else if(warnings > 0)
370 {
371 validationButton.label = stringBundle.getString("webdeveloper_warning") + ": " + warnings + " " + stringBundle.getString("webdeveloper_warnings").toLowerCase();
372
373 validationButton.setAttribute("class", "warning");
374 }
375 else
376 {
377 validationButton.label = stringBundle.getString("webdeveloper_valid");
378
379 validationButton.setAttribute("class", "valid");
380 }
381 }
382 }
383 }
384 catch(exception)
385 {
386 // Do nothing
387 }
388
389 webdeveloper_cssValidator.cleanUp();
390 }
391 }
392
393 // Updates the HTML validation for the page
394 function webdeveloper_updatePageHTMLValidation(contentDocument)
395 {
396 var validationButton = document.getElementById("webdeveloper-html-validation");
397
398 // If the validation button is set
399 if(validationButton)
400 {
401 webdeveloper_htmlValidator.cleanUp();
402 webdeveloper_htmlValidator.validateBackgroundHTML(Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI(contentDocument.documentURI, null, null));
403
404 validationButton.label = document.getElementById("webdeveloper-string-bundle").getString("webdeveloper_validating");
405 }
406 }
407
408 // Updates the HTML validation details for the page
409 function webdeveloper_updatePageHTMLValidationDetails()
410 {
411 // If the HTML validation request is set and is ready
412 if(webdeveloper_htmlValidator.validationRequest && webdeveloper_htmlValidator.validationRequest.readyState == 4)
413 {
414 // Try to check the HTML validation status
415 try
416 {
417 // If the HTML validation status is set to success
418 if(webdeveloper_htmlValidator.validationRequest.status == 200)
419 {
420 var validationButton = document.getElementById("webdeveloper-html-validation");
421 var validationStatus = webdeveloper_htmlValidator.validationRequest.getResponseHeader("X-W3C-Validator-Status");
422
423 // If the validation button and validation status are set
424 if(validationButton && validationStatus)
425 {
426 var stringBundle = document.getElementById("webdeveloper-string-bundle");
427
428 // If the validation status is valid
429 if(validationStatus == "Valid")
430 {
431 validationButton.label = stringBundle.getString("webdeveloper_valid");
432
433 validationButton.setAttribute("class", "valid");
434 }
435 else
436 {
437 validationButton.label = stringBundle.getString("webdeveloper_invalid") + ": " + webdeveloper_htmlValidator.validationRequest.getResponseHeader("X-W3C-Validator-Errors") + " " + stringBundle.getString("webdeveloper_errors").toLowerCase();
438
439 validationButton.setAttribute("class", "invalid");
440 }
441 }
442 }
443 }
444 catch(exception)
445 {
446 // Do nothing
447 }
448
449 webdeveloper_htmlValidator.cleanUp();
450 }
451 }