Professional Documents
Culture Documents
if(!window.addEventListener) {
return;
}
xhr.onreadystatechange = function() {
if(xhr.readyState === 4) {
process();
}
};
process = function() {
var css = xhr.responseText;
if(css && link.parentNode &&
(!xhr.status || xhr.status < 400 || xhr.status > 600)) {
css = self.fix(css, true, link);
// behavior URLs
shoudnt be converted (Issue #19)
// base should be
escaped before added to RegExp (Issue #81)
var escaped_base =
base.replace(/([\\\^\$*+[\]?{}.=!:(|)])/g,"\\$1");
css =
css.replace(RegExp('\\b(behavior:\\s*?url\\(\'?"?)' + escaped_base,
'gi'), '$1');
}
var style =
document.createElement('style');
style.textContent = '/*#
sourceURL='+link.getAttribute('href')+' */\n/*@
sourceURL='+link.getAttribute('href')+' */\n' + css;
style.media = link.media;
style.disabled = link.disabled;
style.setAttribute('data-href',
link.getAttribute('href'));
parent.insertBefore(style,
link);
parent.removeChild(link);
style.media = link.media; //
Duplicate is intentional. See issue #31
}
};
try {
xhr.open('GET', url);
xhr.send(null);
} catch (e) {
// Fallback to XDomainRequest if available
if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.onerror = xhr.onprogress =
function() {};
xhr.onload = process;
xhr.open("GET", url);
xhr.send(null);
}
}
link.setAttribute('data-inprogress', '');
},
styleElement: function(style) {
if (style.hasAttribute('data-noprefix')) {
return;
}
var disabled = style.disabled;
style.disabled = disabled;
},
styleAttribute: function(element) {
var css = element.getAttribute('style');
element.setAttribute('style', css);
},
process: function() {
// Linked stylesheets
$('link[rel="stylesheet"]:not([data-
inprogress])').forEach(StyleFix.link);
// Inline stylesheets
$('style').forEach(StyleFix.styleElement);
// Inline styles
$('[style]').forEach(StyleFix.styleAttribute);
},
return css;
},
camelCase: function(str) {
return str.replace(/-([a-z])/g, function($0, $1) {
return $1.toUpperCase(); }).replace('-','');
},
deCamelCase: function(str) {
return str.replace(/[A-Z]/g, function($0) { return '-
' + $0.toLowerCase() });
}
};
/**************************************
* Process styles
**************************************/
(function(){
setTimeout(function(){
$('link[rel="stylesheet"]').forEach(StyleFix.link);
}, 10);
document.addEventListener('DOMContentLoaded',
StyleFix.process, false);
})();
})();
/**
* PrefixFree
*/
(function(root){
if(!window.StyleFix || !window.getComputedStyle) {
return;
}
// Private helper
function fix(what, before, after, replacement, css) {
what = self[what];
if(what.length) {
var regex = RegExp(before + '(' + what.join('|') +
')' + after, 'gi');
return css;
}
if(raw) {
css = fix('selectors', '', '\\b',
self.prefixSelector, css);
css = fix('atrules', '@', '\\b', '@' + prefix
+ '$1', css);
}
// Prefix wildcard
css = css.replace(/-\*-(?=[a-z]+)/gi, self.prefix);
return css;
},
property: function(property) {
return (self.properties.indexOf(property) >=0 ?
self.prefix : '') + property;
},
if(self.valueProperties.indexOf(property) >= 0) {
value = fix('properties', '(^|\\s|,)',
'($|\\s|,)', '$1'+self.prefix+'$2$3', value);
}
return value;
},
prefixSelector: function(selector) {
return self.selectorMap[selector] || selector
},
/**************************************
* Properties
**************************************/
(function() {
var prefixes = {},
properties = [],
shorthands = {},
style = getComputedStyle(document.documentElement,
null),
dummy = document.createElement('div').style;
if(supported(shorthand) &&
properties.indexOf(shorthand) === -1) {
properties.push(shorthand);
}
}
}
},
supported = function(property) {
return StyleFix.camelCase(property) in dummy;
}
self.properties = [];
if(!supported(unprefixed)) {
self.properties.push(unprefixed);
}
}
}
// IE fix
if(self.Prefix == 'Ms'
&& !('transform' in dummy)
&& !('MsTransform' in dummy)
&& ('msTransform' in dummy)) {
self.properties.push('transform', 'transform-
origin');
}
self.properties.sort();
})();
/**************************************
* Values
**************************************/
(function() {
// Values that might need prefixing
var functions = {
'linear-gradient': {
property: 'backgroundImage',
params: 'red, teal'
},
'calc': {
property: 'width',
params: '1px + 5%'
},
'element': {
property: 'backgroundImage',
params: '#foo'
},
'cross-fade': {
property: 'backgroundImage',
params: 'url(a.png), url(b.png), 50%'
},
'image-set': {
property: 'backgroundImage',
params: 'url(a.png) 1x, url(b.png) 2x'
}
};
functions['repeating-linear-gradient'] =
functions['repeating-radial-gradient'] =
functions['radial-gradient'] =
functions['linear-gradient'];
self.functions = [];
self.keywords = [];
return !!style[property];
}
if (!supported(value, property)
&& supported(self.prefix + value, property)) {
// It's supported, but with a prefix
self.functions.push(func);
}
}
for (var keyword in keywords) {
var property = keywords[keyword];
if (!supported(keyword, property)
&& supported(self.prefix + keyword, property)) {
// It's supported, but with a prefix
self.keywords.push(keyword);
}
}
})();
/**************************************
* Selectors and @-rules
**************************************/
(function() {
var
selectors = {
':any-link': null,
'::backdrop': null,
':fullscreen': null,
':full-screen': ':fullscreen',
//sigh
'::placeholder': null,
':placeholder': '::placeholder',
'::input-placeholder': '::placeholder',
':input-placeholder': '::placeholder',
':read-only': null,
':read-write': null,
'::selection': null
},
atrules = {
'keyframes': 'name',
'viewport': null,
'document': 'regexp(".")'
};
self.selectors = [];
self.selectorMap = {};
self.atrules = [];
function supported(selector) {
style.textContent = selector + '{}'; // Safari 4 has issues
with style.innerHTML
return !!style.sheet.cssRules.length;
}
root.removeChild(style);
})();
})(document.documentElement);