Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add handler for css files #160

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions lib/basket.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,39 @@
return promise;
};

var absUrl = function( u ) {
if (!/^(\.|\w+:)/.test(u))
u = location.origin + location.pathname + u;
return u;
};

var injectScript = function( obj ) {
var script = document.createElement('script');
script.defer = true;
// Have to use .text, since we support IE8,
// which won't allow appending to a script
script.text = obj.data;
var content = obj.data;
if (!/\/\/[#@]\s+sourceURL=.*\s*$/.test(content)) {
content += '//# sourceURL=' + absUrl(obj.url);
}
script.text = content;
head.appendChild( script );
};

var injectStyle = function( obj ) {
var style = document.createElement('style');
style.type = 'text/css';
var content = obj.data;
if (!/\/\*#\s+sourceURL=.*\s+\*\/\s*$/.test(content)) {
content += '/*# sourceURL=' + absUrl(obj.url) + ' */';
}
style.textContent = content;
head.appendChild( style );
};

var handlers = {
'default': injectScript
'default': injectScript,
'css': injectStyle
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use 'text/css' as key name instead 'css' cause obj.type must equal xhr.getResponseHeader('content-type'), which responsed as 'text/css', of course.

};

var execute = function( obj ) {
Expand Down