1 // Constructs a validate accessibility object
2 function WebDeveloperValidateAccessibility()
5 this.formElement
= null;
6 this.inputElement
= null;
7 this.validationRequest
= null;
11 WebDeveloperValidateAccessibility
.prototype.cleanUp = function()
15 // If the validation request is set
16 if(this.validationRequest
)
18 this.validationRequest
.abort();
22 // Creates a source file
23 WebDeveloperValidateAccessibility
.prototype.createSourceFile = function(uri
)
25 var temporaryDirectory
= Components
.classes
["@mozilla.org/file/directory_service;1"].getService(Components
.interfaces
.nsIProperties
).get("TmpD", Components
.interfaces
.nsIFile
);
27 // If the temporary directory exists, is a directory and is writable
28 if(temporaryDirectory
.exists() && temporaryDirectory
.isDirectory() && temporaryDirectory
.isWritable())
31 var sourceFile
= Components
.classes
["@mozilla.org/file/local;1"].createInstance(Components
.interfaces
.nsILocalFile
);
33 // Try to get the host
43 temporaryDirectory
.append("webdeveloper_" + fileName
+ "_" + new Date().getTime() + ".html");
44 sourceFile
.initWithPath(temporaryDirectory
.path
);
50 webdeveloper_error(document
.getElementById("webdeveloper-string-bundle").getFormattedString("webdeveloper_tempDirectoryFailed", [temporaryDirectory
.path
]));
57 WebDeveloperValidateAccessibility
.prototype.deleteFile = function()
62 // Try to delete the file
65 this.file
.remove(false);
76 // Returns the post data
77 WebDeveloperValidateAccessibility
.prototype.getPostData = function()
79 // Try to get the post data
82 var sessionHistory
= getWebNavigation().sessionHistory
;
83 var entry
= sessionHistory
.getEntryAtIndex(sessionHistory
.index
, false).QueryInterface(Components
.interfaces
.nsISHEntry
);
85 return entry
.postData
;
93 // Parses the validation results by type
94 WebDeveloperValidateAccessibility
.prototype.parseValidationResultsByType = function(type
)
96 var resultsHTML
= this.validationRequest
.responseText
;
97 var count
= resultsHTML
.split(type
+ ".gif").length
;
99 // If the count is greater than 0
108 // Retrieves the HTML
109 WebDeveloperValidateAccessibility
.prototype.retrieveHTML = function()
111 var converter
= Components
.classes
["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components
.interfaces
.nsIScriptableUnicodeConverter
);
113 var inputStream
= Components
.classes
["@mozilla.org/network/file-input-stream;1"].createInstance(Components
.interfaces
.nsIFileInputStream
);
114 var scriptableStream
= Components
.classes
["@mozilla.org/scriptableinputstream;1"].createInstance(Components
.interfaces
.nsIScriptableInputStream
);
116 converter
.charset
= webdeveloper_getContentDocument().characterSet
;
118 inputStream
.init(this.file
, 0x01, 0444, null);
119 scriptableStream
.init(inputStream
);
121 htmlText
= converter
.ConvertToUnicode(scriptableStream
.read(scriptableStream
.available()));
123 scriptableStream
.close();
130 WebDeveloperValidateAccessibility
.prototype.saveHTML = function(uri
)
132 var webBrowserPersistInterface
= Components
.interfaces
.nsIWebBrowserPersist
;
133 var webBrowserPersist
= Components
.classes
["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(webBrowserPersistInterface
);
135 webBrowserPersist
.persistFlags
= webBrowserPersistInterface
.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION
| webBrowserPersistInterface
.PERSIST_FLAGS_FROM_CACHE
| webBrowserPersistInterface
.PERSIST_FLAGS_REPLACE_EXISTING_FILES
;
136 webBrowserPersist
.progressListener
= this;
138 webBrowserPersist
.saveURI(uri
, null, uri
, this.getPostData(), null, this.file
);
141 // Submits the background request to validate the accessibility
142 WebDeveloperValidateAccessibility
.prototype.submitBackgroundRequest = function()
144 var requestBody
= "CheckURL=1&URLTest=Your+HTML";
146 // If the priority 1 preference is set
147 if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.wai.priority1", true))
149 requestBody
+= "&p1=1";
152 // If the priority 2 preference is set
153 if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.wai.priority2", true))
155 requestBody
+= "&p2=1";
158 // If the priority 3 preference is set
159 if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.wai.priority3", true))
161 requestBody
+= "&p3=1";
164 // If the Section 508 preference is set
165 if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.section508", true))
167 requestBody
+= "&s508=1";
170 requestBody
+= "&myHTML=" + encodeURIComponent(this.retrieveHTML());
171 this.validationRequest
.onreadystatechange
= webdeveloper_updatePageAccessibilityValidationDetails
;
173 this.validationRequest
.open("post", "http://www.hermish.com/check_this.cfm", true);
175 // Try to set the request header
178 this.validationRequest
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
179 this.validationRequest
.send(requestBody
);
183 // Reset the validation request
184 this.validationRequest
= new XMLHttpRequest();
190 // Submits the form to validate the accessibility
191 WebDeveloperValidateAccessibility
.prototype.submitForm = function()
193 this.inputElement
.value
= this.retrieveHTML();
196 this.formElement
.submit();
199 // Validate the accessibility of the given URI in the background
200 WebDeveloperValidateAccessibility
.prototype.validateBackgroundAccessibility = function(uri
)
202 this.file
= this.createSourceFile(uri
);
204 // If the validation request is not set
205 if(!this.validationRequest
)
207 this.validationRequest
= new XMLHttpRequest();
213 // Validate the accessibility of the given URI
214 WebDeveloperValidateAccessibility
.prototype.validateAccessibility = function(uri
)
216 var oldTab
= getBrowser().selectedTab
;
217 var oldURL
= getBrowser().currentURI
.spec
;
218 var generatedDocument
= webdeveloper_generateDocument("");
219 var bodyElement
= webdeveloper_getDocumentBodyElement(generatedDocument
);
220 var imageElement
= generatedDocument
.createElement("img");
221 var inputElement
= null;
222 var pElement
= generatedDocument
.createElement("p");
223 var stringBundle
= document
.getElementById("webdeveloper-string-bundle");
225 generatedDocument
.title
= stringBundle
.getString("webdeveloper_validateAccessibility");
226 this.file
= this.createSourceFile(uri
);
227 this.formElement
= generatedDocument
.createElement("form");
229 webdeveloper_addGeneratedStyles(generatedDocument
);
231 imageElement
.setAttribute("alt", "loading");
232 imageElement
.setAttribute("src", "chrome://webdeveloper/content/images/content/loading.gif");
233 pElement
.appendChild(imageElement
);
234 pElement
.appendChild(generatedDocument
.createTextNode(stringBundle
.getString("webdeveloper_contactingValidator")));
235 pElement
.setAttribute("class", "loading");
236 bodyElement
.appendChild(pElement
);
238 this.formElement
.setAttribute("action", "http://www.hermish.com/check_this.cfm");
239 this.formElement
.setAttribute("method", "post");
240 this.formElement
.setAttribute("style", "display: none");
242 inputElement
= generatedDocument
.createElement("input");
244 inputElement
.setAttribute("name", "CheckURL");
245 inputElement
.setAttribute("type", "hidden");
246 inputElement
.setAttribute("value", "1");
247 this.formElement
.appendChild(inputElement
);
249 // If the priority 1 preference is set
250 if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.wai.priority1", true))
252 inputElement
= generatedDocument
.createElement("input");
254 inputElement
.setAttribute("name", "p1");
255 inputElement
.setAttribute("type", "hidden");
256 inputElement
.setAttribute("value", "1");
257 this.formElement
.appendChild(inputElement
);
260 // If the priority 2 preference is set
261 if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.wai.priority2", true))
263 inputElement
= generatedDocument
.createElement("input");
265 inputElement
.setAttribute("name", "p2");
266 inputElement
.setAttribute("type", "hidden");
267 inputElement
.setAttribute("value", "1");
268 this.formElement
.appendChild(inputElement
);
271 // If the priority 3 preference is set
272 if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.wai.priority3", true))
274 inputElement
= generatedDocument
.createElement("input");
276 inputElement
.setAttribute("name", "p3");
277 inputElement
.setAttribute("type", "hidden");
278 inputElement
.setAttribute("value", "1");
279 this.formElement
.appendChild(inputElement
);
282 // If the Section 508 preference is set
283 if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.section508", true))
285 inputElement
= generatedDocument
.createElement("input");
287 inputElement
.setAttribute("name", "s508");
288 inputElement
.setAttribute("type", "hidden");
289 inputElement
.setAttribute("value", "1");
290 this.formElement
.appendChild(inputElement
);
293 inputElement
= generatedDocument
.createElement("input");
295 inputElement
.setAttribute("name", "URLTest");
296 inputElement
.setAttribute("type", "hidden");
297 inputElement
.setAttribute("value", "Your HTML");
298 this.formElement
.appendChild(inputElement
);
300 this.inputElement
= generatedDocument
.createElement("input");
302 this.inputElement
.setAttribute("name", "myHTML");
303 this.inputElement
.setAttribute("type", "hidden");
304 this.formElement
.appendChild(this.inputElement
);
305 bodyElement
.appendChild(this.formElement
);
307 // If the open tabs in background preference is set to true
308 if(webdeveloper_getBooleanPreference("webdeveloper.open.tabs.background", true))
310 getBrowser().selectedTab
= oldTab
;
316 // Called when the progress state changes
317 WebDeveloperValidateAccessibility
.prototype.onStateChange = function(webProgress
, request
, stateFlags
, status
)
319 // If the progress has stopped
320 if(stateFlags
& Components
.interfaces
.nsIWebProgressListener
.STATE_STOP
)
322 // If the file is set and exists
323 if(this.file
&& this.file
.exists())
325 // If the validation request is set
326 if(this.validationRequest
)
328 this.submitBackgroundRequest();
338 // Indicates the interfaces this object supports
339 WebDeveloperValidateAccessibility
.prototype.QueryInterface = function(id
)
341 // If the query is for a supported interface
342 if(id
.equals(Components
.interfaces
.nsISupports
) || id
.equals(Components
.interfaces
.nsIWebProgressListener
))
347 throw Components
.results
.NS_NOINTERFACE
;
350 // Dummy methods requiring implementations
351 WebDeveloperValidateAccessibility
.prototype.onLocationChange = function(webProgress
, request
, location
) {}
352 WebDeveloperValidateAccessibility
.prototype.onProgressChange = function(webProgress
, request
, currentSelfProgress
, maximumSelfProgress
, currentTotalProgress
, maximumTotalProgress
) {}
353 WebDeveloperValidateAccessibility
.prototype.onSecurityChange = function(webProgress
, request
, state
) {}
354 WebDeveloperValidateAccessibility
.prototype.onStatusChange = function(webProgress
, request
, status
, message
) {}