1 // Constructs a validate HTML object
2 function WebDeveloperValidateHTML()
5 this.fileElement
= null;
6 this.formElement
= null;
7 this.validationRequest
= null;
11 WebDeveloperValidateHTML
.prototype.cleanUp = function()
16 // Try to delete the file
19 this.file
.remove(false);
29 // If the validation request is set
30 if(this.validationRequest
)
32 this.validationRequest
.abort();
36 // Creates a source file
37 WebDeveloperValidateHTML
.prototype.createSourceFile = function(uri
)
39 var temporaryDirectory
= Components
.classes
["@mozilla.org/file/directory_service;1"].getService(Components
.interfaces
.nsIProperties
).get("TmpD", Components
.interfaces
.nsIFile
);
41 // If the temporary directory exists, is a directory and is writable
42 if(temporaryDirectory
.exists() && temporaryDirectory
.isDirectory() && temporaryDirectory
.isWritable())
45 var sourceFile
= Components
.classes
["@mozilla.org/file/local;1"].createInstance(Components
.interfaces
.nsILocalFile
);
47 // Try to get the host
57 temporaryDirectory
.append("webdeveloper_" + fileName
+ "_" + new Date().getTime() + ".html");
58 sourceFile
.initWithPath(temporaryDirectory
.path
);
64 webdeveloper_error(document
.getElementById("webdeveloper-string-bundle").getFormattedString("webdeveloper_tempDirectoryFailed", [temporaryDirectory
.path
]));
70 // Returns the post data
71 WebDeveloperValidateHTML
.prototype.getPostData = function()
73 // Try to get the post data
76 var sessionHistory
= getWebNavigation().sessionHistory
;
77 var entry
= sessionHistory
.getEntryAtIndex(sessionHistory
.index
, false).QueryInterface(Components
.interfaces
.nsISHEntry
);
79 return entry
.postData
;
88 WebDeveloperValidateHTML
.prototype.saveHTML = function(uri
)
90 var webBrowserPersistInterface
= Components
.interfaces
.nsIWebBrowserPersist
;
91 var webBrowserPersist
= Components
.classes
["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(webBrowserPersistInterface
);
93 webBrowserPersist
.persistFlags
= webBrowserPersistInterface
.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION
| webBrowserPersistInterface
.PERSIST_FLAGS_FROM_CACHE
| webBrowserPersistInterface
.PERSIST_FLAGS_REPLACE_EXISTING_FILES
;
94 webBrowserPersist
.progressListener
= this;
96 webBrowserPersist
.saveURI(uri
, null, uri
, this.getPostData(), null, this.file
);
99 // Submits the background request to validate the HTML
100 WebDeveloperValidateHTML
.prototype.submitBackgroundRequest = function()
102 var boundaryString
= new Date().getTime();
103 var boundary
= "--" + boundaryString
;
104 var converter
= Components
.classes
["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components
.interfaces
.nsIScriptableUnicodeConverter
);
105 var inputStream
= Components
.classes
["@mozilla.org/network/file-input-stream;1"].createInstance(Components
.interfaces
.nsIFileInputStream
);
106 var scriptableStream
= Components
.classes
["@mozilla.org/scriptableinputstream;1"].createInstance(Components
.interfaces
.nsIScriptableInputStream
);
107 var requestBody
= boundary
+ "\r\nContent-Disposition: form-data; name=\"uploaded_file\"; filename=\"" + this.file
.leafName
+ "\"\r\n";
109 converter
.charset
= webdeveloper_getContentDocument().characterSet
;
110 this.validationRequest
.onreadystatechange
= webdeveloper_updatePageHTMLValidationDetails
;
112 inputStream
.init(this.file
, 0x01, 0444, null);
113 scriptableStream
.init(inputStream
);
115 requestBody
+= "Content-Type: text/html\r\n\r\n";
116 requestBody
+= converter
.ConvertToUnicode(scriptableStream
.read(scriptableStream
.available())) + "\r\n";
117 requestBody
+= boundary
+ "--";
119 scriptableStream
.close();
122 this.validationRequest
.open("post", "http://validator.w3.org/check", true);
124 // Try to set the request header
127 this.validationRequest
.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundaryString
);
128 this.validationRequest
.send(requestBody
);
132 // Reset the validation request
133 this.validationRequest
= new XMLHttpRequest();
137 // Submits the form to validate the HTML
138 WebDeveloperValidateHTML
.prototype.submitForm = function()
140 this.fileElement
.value
= this.file
.path
;
142 this.formElement
.submit();
145 // Validate the HTML from the given URI in the background
146 WebDeveloperValidateHTML
.prototype.validateBackgroundHTML = function(uri
)
148 this.file
= this.createSourceFile(uri
);
150 // If the validation request is not set
151 if(!this.validationRequest
)
153 this.validationRequest
= new XMLHttpRequest();
159 // Validate the HTML from the given URI
160 WebDeveloperValidateHTML
.prototype.validateHTML = function(uri
)
162 var oldTab
= getBrowser().selectedTab
;
163 var oldURL
= getBrowser().currentURI
.spec
;
164 var generatedDocument
= webdeveloper_generateDocument("");
165 var bodyElement
= webdeveloper_getDocumentBodyElement(generatedDocument
);
166 var imageElement
= generatedDocument
.createElement("img");
167 var inputElement
= null;
168 var pElement
= generatedDocument
.createElement("p");
169 var stringBundle
= document
.getElementById("webdeveloper-string-bundle");
171 generatedDocument
.title
= stringBundle
.getString("webdeveloper_validateHTML");
172 this.file
= this.createSourceFile(uri
);
173 this.formElement
= generatedDocument
.createElement("form");
175 webdeveloper_addGeneratedStyles(generatedDocument
);
177 imageElement
.setAttribute("alt", "loading");
178 imageElement
.setAttribute("src", "chrome://webdeveloper/content/images/content/loading.gif");
179 pElement
.appendChild(imageElement
);
180 pElement
.appendChild(generatedDocument
.createTextNode(stringBundle
.getString("webdeveloper_contactingValidator")));
181 pElement
.setAttribute("class", "loading");
182 bodyElement
.appendChild(pElement
);
184 this.formElement
.setAttribute("action", "http://validator.w3.org/check");
185 this.formElement
.setAttribute("enctype", "multipart/form-data");
186 this.formElement
.setAttribute("method", "post");
187 this.formElement
.setAttribute("style", "display: none");
189 // If the show outline preference is set
190 if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.html.show.outline", true))
192 inputElement
= generatedDocument
.createElement("input");
194 inputElement
.setAttribute("name", "outline");
195 inputElement
.setAttribute("type", "hidden");
196 inputElement
.setAttribute("value", "1");
197 this.formElement
.appendChild(inputElement
);
200 // If the show parse tree preference is set
201 if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.html.show.parse.tree", true))
203 inputElement
= generatedDocument
.createElement("input");
205 inputElement
.setAttribute("name", "sp");
206 inputElement
.setAttribute("type", "hidden");
207 inputElement
.setAttribute("value", "1");
208 this.formElement
.appendChild(inputElement
);
211 // If the show source preference is set
212 if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.html.show.source", true))
214 inputElement
= generatedDocument
.createElement("input");
216 inputElement
.setAttribute("name", "ss");
217 inputElement
.setAttribute("type", "hidden");
218 inputElement
.setAttribute("value", "1");
219 this.formElement
.appendChild(inputElement
);
222 inputElement
= generatedDocument
.createElement("input");
224 inputElement
.setAttribute("name", "verbose");
225 inputElement
.setAttribute("type", "hidden");
226 inputElement
.setAttribute("value", "1");
227 this.formElement
.appendChild(inputElement
);
229 this.fileElement
= generatedDocument
.createElement("input");
231 this.fileElement
.setAttribute("name", "uploaded_file");
232 this.fileElement
.setAttribute("type", "file");
233 this.formElement
.appendChild(this.fileElement
);
234 bodyElement
.appendChild(this.formElement
);
236 // If the open tabs in background preference is set to true
237 if(webdeveloper_getBooleanPreference("webdeveloper.open.tabs.background", true))
239 getBrowser().selectedTab
= oldTab
;
245 // Called when the progress state changes
246 WebDeveloperValidateHTML
.prototype.onStateChange = function(webProgress
, request
, stateFlags
, status
)
248 // If the progress has stopped
249 if(stateFlags
& Components
.interfaces
.nsIWebProgressListener
.STATE_STOP
)
251 // If the file is set and exists
252 if(this.file
&& this.file
.exists())
254 // If the validation request is set
255 if(this.validationRequest
)
257 this.submitBackgroundRequest();
267 // Indicates the interfaces this object supports
268 WebDeveloperValidateHTML
.prototype.QueryInterface = function(id
)
270 // If the query is for a supported interface
271 if(id
.equals(Components
.interfaces
.nsISupports
) || id
.equals(Components
.interfaces
.nsIWebProgressListener
))
276 throw Components
.results
.NS_NOINTERFACE
;
279 // Dummy methods requiring implementations
280 WebDeveloperValidateHTML
.prototype.onLocationChange = function(webProgress
, request
, location
) {}
281 WebDeveloperValidateHTML
.prototype.onProgressChange = function(webProgress
, request
, currentSelfProgress
, maximumSelfProgress
, currentTotalProgress
, maximumTotalProgress
) {}
282 WebDeveloperValidateHTML
.prototype.onSecurityChange = function(webProgress
, request
, state
) {}
283 WebDeveloperValidateHTML
.prototype.onStatusChange = function(webProgress
, request
, status
, message
) {}