Skip to content
This repository has been archived by the owner on Feb 25, 2018. It is now read-only.

Commit

Permalink
Add option to ignore external stylesheets
Browse files Browse the repository at this point in the history
This adds an option called `disableCrossDomain: 'true'`
to ignore external stylesheets.

This is needed when you can’t allow CORS due to your CSP
or can’t set a CSP at all. This closes #7, closes #15
and fixes some bug reports related to that behavior.
  • Loading branch information
anselmh committed Feb 23, 2015
1 parent b24d08a commit 7f0400d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

### HEAD
* Add option `disableCrossDomain: 'true'` to ignore external stylesheets (CORS/CSP) (#7, #15)

### 0.3.7 (January, 26th, 2015)
* Update README with latest browser developments
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ Then include the CSS file [`polyfill.object-fit.css`](https://github.com/anselmh
<script>
objectFit.polyfill({
selector: 'img', // this can be any CSS selector
fittype: 'cover' // either contain, cover, fill or none
fittype: 'cover', // either contain, cover, fill or none
disableCrossDomain: 'true' // either 'true' or 'false' to not parse external CSS files.
});
</script>

Expand Down
23 changes: 19 additions & 4 deletions dist/polyfill.object-fit.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@
// handles extraction of `cssRules` as an `Array` from a stylesheet or something that behaves the same
var getSheetRules = function (stylesheet) {
var sheetMedia = stylesheet.media && stylesheet.media.mediaText;
var sheetHost = getCSSHost(stylesheet.href);
var sheetHost;

// if this sheet is cross-origin and option is set skip it
if ((sheetHost !== window.location.host)) { // && avoidExternalStylesheets) {
return [];
if (objectFit.disableCrossDomain == 'true') {
sheetHost = getCSSHost(stylesheet.href);

if ((sheetHost !== window.location.host)) {
return [];
}
}


// if this sheet is disabled skip it
if (stylesheet.disabled) {
return [];
Expand Down Expand Up @@ -280,6 +285,8 @@

objectFit.observer = null;

objectFit.disableCrossDomain = 'false';

objectFit.getComputedStyle = function(element, context) {
context = context || window;

Expand Down Expand Up @@ -403,7 +410,13 @@
if (!args.selector || !args.replacedElements) {
return;
}

// Set option objectFit.disableCrossDomain
objectFit.disableCrossDomain = args.disableCrossDomain || 'false';

// Set option fit-type
args.fittype = args.fittype || 'none';

switch (args.fittype) {
default:
return;
Expand All @@ -415,7 +428,9 @@
break;
}

// Set option replacedElements
var replacedElements = args.replacedElements;

if(!replacedElements.length) {
return;
}
Expand Down Expand Up @@ -624,7 +639,7 @@
console.log('object-fit not natively supported');
}
// If the library is loaded after document onload event
if(document.readyState === 'complete') {
if (document.readyState === 'complete') {
objectFit.init(args);
} else {
// Otherwise attach event listeners
Expand Down
2 changes: 1 addition & 1 deletion dist/polyfill.object-fit.min.js

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions src/polyfill.getMatchedCSSRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@
// handles extraction of `cssRules` as an `Array` from a stylesheet or something that behaves the same
var getSheetRules = function (stylesheet) {
var sheetMedia = stylesheet.media && stylesheet.media.mediaText;
var sheetHost = getCSSHost(stylesheet.href);
var sheetHost;

// if this sheet is cross-origin and option is set skip it
if ((sheetHost !== window.location.host)) { // && avoidExternalStylesheets) {
return [];
if (objectFit.disableCrossDomain == 'true') {
sheetHost = getCSSHost(stylesheet.href);

if ((sheetHost !== window.location.host)) {
return [];
}
}


// if this sheet is disabled skip it
if (stylesheet.disabled) {
return [];
Expand Down
12 changes: 11 additions & 1 deletion src/polyfill.object-fit.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

objectFit.observer = null;

objectFit.disableCrossDomain = 'false';

objectFit.getComputedStyle = function(element, context) {
context = context || window;

Expand Down Expand Up @@ -142,7 +144,13 @@
if (!args.selector || !args.replacedElements) {
return;
}

// Set option objectFit.disableCrossDomain
objectFit.disableCrossDomain = args.disableCrossDomain || 'false';

// Set option fit-type
args.fittype = args.fittype || 'none';

switch (args.fittype) {
default:
return;
Expand All @@ -154,7 +162,9 @@
break;
}

// Set option replacedElements
var replacedElements = args.replacedElements;

if(!replacedElements.length) {
return;
}
Expand Down Expand Up @@ -363,7 +373,7 @@
console.log('object-fit not natively supported');
}
// If the library is loaded after document onload event
if(document.readyState === 'complete') {
if (document.readyState === 'complete') {
objectFit.init(args);
} else {
// Otherwise attach event listeners
Expand Down
3 changes: 2 additions & 1 deletion tests/index-external-css.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ <h1><code>object-fit: cover;</code></h1>
document.addEventListener('DOMContentLoaded', function () {
objectFit.polyfill({
selector: 'img',
fittype: 'cover'
fittype: 'cover',
disableCrossDomain: 'false'
});
});
</script>
Expand Down

0 comments on commit 7f0400d

Please sign in to comment.