Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a LESS-based unit test for ExtensionUtils.loadStyleSheet

Removed the loadUrl function
Renamed addInlineStyleSheet to addEmbeddedStyleSheet
Renamed addStyleSheetReference to addLinkedStyleSheet
Aligned .done and .fail differently
  • Loading branch information...
commit fb9d1f9470f192f5ac9a5052a49148d2c680428d 1 parent bc9b5d4
@DennisKehrig DennisKehrig authored
View
57 src/utils/ExtensionUtils.js
@@ -37,7 +37,7 @@ define(function (require, exports, module) {
* @param {!string} css CSS code to use as the tag's content
* @return {!HTMLStyleElement} The generated HTML node
**/
- function addInlineStyleSheet(css) {
+ function addEmbeddedStyleSheet(css) {
return $("<style>").text(css).appendTo("head")[0];
}
@@ -47,7 +47,7 @@ define(function (require, exports, module) {
* @param {!string} url URL to a style sheet
* @return {!HTMLLinkElement} The generated HTML node
**/
- function addStyleSheetReference(url) {
+ function addLinkedStyleSheet(url) {
var attributes = {
type: "text/css",
rel: "stylesheet",
@@ -131,16 +131,6 @@ define(function (require, exports, module) {
}
/**
- * Performs a GET request using a URL.
- *
- * @param {!string} url URL of a resource
- * @return {!$.Promise} A promise object that is resolved with the contents of the requested resource
- **/
- function loadUrl(url) {
- return $.get(url);
- }
-
- /**
* Performs a GET request using a path relative to an extension module.
*
* The resulting URL can be retrieved in the resolve callback by accessing
@@ -151,7 +141,7 @@ define(function (require, exports, module) {
**/
function loadFile(module, path) {
var url = getModuleUrl(module, path),
- promise = loadUrl(url);
+ promise = $.get(url);
return promise;
}
@@ -166,27 +156,30 @@ define(function (require, exports, module) {
function loadStyleSheet(module, path) {
var result = new $.Deferred();
- loadFile(module, path).done(function (content) {
- var url = this.url;
-
- if (url.slice(-5) === ".less") {
- parseLessCode(content, url).done(function (css) {
- result.resolve(addInlineStyleSheet(css));
- }).fail(result.reject);
- } else {
- result.resolve(addStyleSheetReference(url));
- }
- }).fail(result.reject);
+ loadFile(module, path)
+ .done(function (content) {
+ var url = this.url;
+
+ if (url.slice(-5) === ".less") {
+ parseLessCode(content, url)
+ .done(function (css) {
+ result.resolve(addEmbeddedStyleSheet(css));
+ })
+ .fail(result.reject);
+ } else {
+ result.resolve(addLinkedStyleSheet(url));
+ }
+ })
+ .fail(result.reject);
return result.promise();
}
- exports.addInlineStyleSheet = addInlineStyleSheet;
- exports.addStyleSheetReference = addStyleSheetReference;
- exports.parseLessCode = parseLessCode;
- exports.getModulePath = getModulePath;
- exports.getModuleUrl = getModuleUrl;
- exports.loadUrl = loadUrl;
- exports.loadFile = loadFile;
- exports.loadStyleSheet = loadStyleSheet;
+ exports.addEmbeddedStyleSheet = addEmbeddedStyleSheet;
+ exports.addLinkedStyleSheet = addLinkedStyleSheet;
+ exports.parseLessCode = parseLessCode;
+ exports.getModulePath = getModulePath;
+ exports.getModuleUrl = getModuleUrl;
+ exports.loadFile = loadFile;
+ exports.loadStyleSheet = loadStyleSheet;
});
View
7 test/spec/ExtensionUtils-test-files/basic.less
@@ -0,0 +1,7 @@
+/* basic.css */
+@import "sub dir/fourth.less";
+@size: 33px;
+
+#project-title {
+ font-size: (2 * @size);
+}
View
5 test/spec/ExtensionUtils-test-files/sub dir/fifth.less
@@ -0,0 +1,5 @@
+// HighASCII_été.less
+
+#project-title {
+ letter-spacing: 9px;
+}
View
5 test/spec/ExtensionUtils-test-files/sub dir/fourth.less
@@ -0,0 +1,5 @@
+// fourth.less
+
+#project-title {
+ font-weight: 800;
+}
View
26 test/spec/ExtensionUtils-test.js
@@ -111,6 +111,32 @@ define(function (require, exports, module) {
var fontVariant = $projectTitle.css("font-variant");
expect(fontVariant).toEqual("small-caps");
});
+
+ runs(function () {
+ loadStyleSheet(testWindow.document, "ExtensionUtils-test-files/basic.less");
+ });
+
+ runs(function () {
+ // basic.less
+ var $projectTitle = testWindow.$("#project-title");
+ var fontSize = $projectTitle.css("font-size");
+ expect(fontSize).toEqual("66px");
+
+ // fourth.less is imported in basic.less
+ var fontWeight = $projectTitle.css("font-weight");
+ expect(fontWeight).toEqual("800");
+ });
+
+ runs(function () {
+ loadStyleSheet(testWindow.document, "ExtensionUtils-test-files/sub dir/fifth.less");
+ });
+
+ runs(function () {
+ // fifth.less
+ var $projectTitle = testWindow.$("#project-title");
+ var fontVariant = $projectTitle.css("letter-spacing");
+ expect(fontVariant).toEqual("9px");
+ });
});
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.