Skip to content

Commit

Permalink
Merge pull request #65 from stealjs/slim-href
Browse files Browse the repository at this point in the history
Only add <link> to page if not already there in slim loader
  • Loading branch information
matthewp authored Aug 21, 2017
2 parents 1a56177 + cd7bfd4 commit 9394b31
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
5 changes: 4 additions & 1 deletion slim.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ CssModule.waitTimeout = 60;

CssModule.prototype.linkExists = function() {
var styleSheets = document.styleSheets;
var anchor = document.createElement("a");
anchor.href = this.address;
var href = anchor.href;

for (var i = 0; i < styleSheets.length; ++i) {
if (this.address === styleSheets[i].href) {
if (href === styleSheets[i].href) {
return true;
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/slim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var slimLoader = require("steal-css/slim");
var QUnit = require("steal-qunit");

QUnit.module("slim");

QUnit.test("Only adds links if not already in the page", function(assert){
var done = assert.async();

var config = {
bundles: {
"1": 2
},
paths: {
"2": "dist/foo.css"
}
};

Promise.all([
slimLoader(1, config),
slimLoader(1, config)
])
.then(function(){
var ss = document.styleSheets;
var foos = Array.prototype.slice.call(ss).filter(function(link){
return /foo/.test(link.href);
});

assert.equal(foos.length, 1, "There is only 1 link");
})
.then(done);
});
1 change: 1 addition & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<title>steal-css tests</title>
<link rel="stylesheet" href="./dist/foo.css">
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var QUnit = require("steal-qunit");
var makeIframe = require("./make-iframe");

require("./unit");
require("./slim");

QUnit.config.testTimeout = 10000;

Expand Down

0 comments on commit 9394b31

Please sign in to comment.