1 // Constructs a validate CSS object
2 function WebDeveloperValidateCSS()
5 this.validationRequest
= null;
9 WebDeveloperValidateCSS
.prototype.cleanUp = function()
14 // Try to delete the file
17 this.file
.remove(false);
27 // If the validation request is set
28 if(this.validationRequest
)
30 this.validationRequest
.abort();
34 // Creates a source file
35 WebDeveloperValidateCSS
.prototype.createSourceFile = function(uri
)
37 var temporaryDirectory
= Components
.classes
["@mozilla.org/file/directory_service;1"].getService(Components
.interfaces
.nsIProperties
).get("TmpD", Components
.interfaces
.nsIFile
);
39 // If the temporary directory exists, is a directory and is writable
40 if(temporaryDirectory
.exists() && temporaryDirectory
.isDirectory() && temporaryDirectory
.isWritable())
43 var sourceFile
= Components
.classes
["@mozilla.org/file/local;1"].createInstance(Components
.interfaces
.nsILocalFile
);
45 // Try to get the host
55 temporaryDirectory
.append("webdeveloper_" + fileName
+ "_" + new Date().getTime() + ".css");
56 sourceFile
.initWithPath(temporaryDirectory
.path
);
62 webdeveloper_error(document
.getElementById("webdeveloper-string-bundle").getFormattedString("webdeveloper_tempDirectoryFailed", [temporaryDirectory
.path
]));
68 // Parses the validation results by type
69 WebDeveloperValidateCSS
.prototype.parseValidationResultsByType = function(type
)
72 var resultsHTML
= this.validationRequest
.responseText
;
73 var startPosition
= resultsHTML
.indexOf('<div id="' + type
+ '">');
75 // If the start position is greater than 0
78 var endPosition
= resultsHTML
.indexOf("</div>", startPosition
);
80 // If the end position is greater than 0
83 count
= resultsHTML
.slice(startPosition
, endPosition
).split("<li>").length
;
87 // If the count is greater than 0
96 // Retrieves the CSS for the given document list
97 WebDeveloperValidateCSS
.prototype.retrieveCSS = function(documentList
)
99 var documentLength
= documentList
.length
;
100 var documentURL
= null;
101 var pageDocument
= null;
102 var stringBundle
= document
.getElementById("webdeveloper-string-bundle");
103 var styleSheet
= null;
104 var styleSheetHref
= null;
105 var styleSheetLength
= null;
106 var styleSheetList
= null;
108 var styleElement
= null;
110 // Loop through the documents
111 for(var i
= 0; i
< documentLength
; i
++)
113 pageDocument
= documentList
[i
];
114 documentURL
= pageDocument
.documentURI
;
115 styleSheetList
= pageDocument
.getElementsByTagName("style");
116 styleSheetLength
= styleSheetList
.length
;
118 // Loop through the style sheets
119 for(var j
= 0; j
< styleSheetLength
; j
++)
121 styleElement
= styleSheetList
[j
];
122 styleSheet
= styleElement
.sheet
;
124 // If this is a valid style sheet
125 if(webdeveloper_isValidStyleSheet(styleSheet
))
127 styleText
+= "/* " + stringBundle
.getFormattedString("webdeveloper_embeddedStylesFrom", [styleSheet
.href
]) + " */\n\n";
128 styleText
+= styleElement
.innerHTML
.trim() + "\n\n";
132 styleSheetList
= webdeveloper_getStyleSheetsForDocument(pageDocument
, true, true);
133 styleSheetLength
= styleSheetList
.length
;
135 // Loop through the style sheets
136 for(j
= 0; j
< styleSheetLength
; j
++)
138 styleSheetHref
= styleSheetList
[j
].href
;
140 // If this is not an inline style sheet
141 if(styleSheetHref
!= documentURL
)
143 styleText
+= webdeveloper_retrieveSource(styleSheetHref
);
151 // Validate the CSS from the given URI and document list in the background
152 WebDeveloperValidateCSS
.prototype.validateBackgroundCSS = function(uri
, documentList
)
154 var boundaryString
= new Date().getTime();
155 var boundary
= "--" + boundaryString
;
156 var requestBody
= boundary
+ "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"css.css\"\r\n";
158 // If the validation request is not set
159 if(!this.validationRequest
)
161 this.validationRequest
= new XMLHttpRequest();
164 this.validationRequest
.onreadystatechange
= webdeveloper_updatePageCSSValidationDetails
;
166 requestBody
+= "Content-Type: text/css\r\n\r\n";
167 requestBody
+= this.retrieveCSS(documentList
) + "\r\n";
168 requestBody
+= boundary
+ "\r\n";
169 requestBody
+= "Content-Disposition: form-data; name=\"profile\"\r\n\r\n" + webdeveloper_getStringPreference("webdeveloper.validate.local.css.profile", true) + "\r\n";
170 requestBody
+= boundary
+ "\r\n";
171 requestBody
+= "Content-Disposition: form-data; name=\"usermedium\"\r\n\r\nall\r\n";
172 requestBody
+= boundary
+ "\r\n";
173 requestBody
+= "Content-Disposition: form-data; name=\"warning\"\r\n\r\n0\r\n";
174 requestBody
+= boundary
+ "--";
176 this.validationRequest
.open("post", "http://jigsaw.w3.org/css-validator/validator", true);
178 // Try to set the request header
181 this.validationRequest
.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundaryString
);
182 this.validationRequest
.send(requestBody
);
186 // Reset the validation request
187 this.validationRequest
= new XMLHttpRequest();
191 // Validate the CSS from the given URI and document list
192 WebDeveloperValidateCSS
.prototype.validateCSS = function(uri
, documentList
)
194 var oldTab
= getBrowser().selectedTab
;
195 var generatedDocument
= webdeveloper_generateDocument("");
196 var bodyElement
= webdeveloper_getDocumentBodyElement(generatedDocument
);
197 var formElement
= generatedDocument
.createElement("form");
198 var imageElement
= generatedDocument
.createElement("img");
199 var inputElement
= null;
200 var outputStream
= Components
.classes
["@mozilla.org/network/file-output-stream;1"].createInstance(Components
.interfaces
.nsIFileOutputStream
);
201 var pElement
= generatedDocument
.createElement("p");
202 var scriptElement
= generatedDocument
.createElement("script");
203 var stringBundle
= document
.getElementById("webdeveloper-string-bundle");
204 var styleText
= null;
206 generatedDocument
.title
= stringBundle
.getString("webdeveloper_validateCSS");
207 this.file
= this.createSourceFile(uri
);
209 webdeveloper_addGeneratedStyles(generatedDocument
);
211 imageElement
.setAttribute("alt", "loading");
212 imageElement
.setAttribute("src", "chrome://webdeveloper/content/images/content/loading.gif");
213 pElement
.appendChild(imageElement
);
214 pElement
.appendChild(generatedDocument
.createTextNode(stringBundle
.getString("webdeveloper_contactingValidator")));
215 pElement
.setAttribute("class", "loading");
216 bodyElement
.appendChild(pElement
);
218 styleText
= this.retrieveCSS(documentList
);
220 this.file
.create(Components
.interfaces
.nsIFile
.NORMAL_FILE_TYPE
, 00644);
221 outputStream
.init(this.file
, 0x04 | 0x08 | 0x20 | 0x40, 00644, null);
223 outputStream
.write(styleText
, styleText
.length
);
224 outputStream
.close();
226 formElement
.setAttribute("action", "http://jigsaw.w3.org/css-validator/validator");
227 formElement
.setAttribute("enctype", "multipart/form-data");
228 formElement
.setAttribute("method", "post");
229 formElement
.setAttribute("style", "display: none");
231 inputElement
= generatedDocument
.createElement("input");
233 inputElement
.setAttribute("name", "profile");
234 inputElement
.setAttribute("type", "hidden");
235 inputElement
.setAttribute("value", webdeveloper_getStringPreference("webdeveloper.validate.local.css.profile", true));
236 formElement
.appendChild(inputElement
);
238 inputElement
= generatedDocument
.createElement("input");
240 inputElement
.setAttribute("name", "usermedium");
241 inputElement
.setAttribute("type", "hidden");
242 inputElement
.setAttribute("value", "all");
243 formElement
.appendChild(inputElement
);
245 inputElement
= generatedDocument
.createElement("input");
247 inputElement
.setAttribute("name", "warning");
248 inputElement
.setAttribute("type", "hidden");
249 inputElement
.setAttribute("value", "0");
250 formElement
.appendChild(inputElement
);
252 inputElement
= generatedDocument
.createElement("input")
254 inputElement
.setAttribute("name", "file");
255 inputElement
.setAttribute("type", "file");
257 inputElement
.value
= this.file
.path
;
259 formElement
.appendChild(inputElement
);
260 bodyElement
.appendChild(formElement
);
261 formElement
.submit();
263 // If the open tabs in background preference is set to true
264 if(webdeveloper_getBooleanPreference("webdeveloper.open.tabs.background", true))
266 getBrowser().selectedTab
= oldTab
;