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

Commit 7f0400d

Browse files
committed
Add option to ignore external stylesheets
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.
1 parent b24d08a commit 7f0400d

File tree

7 files changed

+44
-11
lines changed

7 files changed

+44
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

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

56
### 0.3.7 (January, 26th, 2015)
67
* Update README with latest browser developments

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ Then include the CSS file [`polyfill.object-fit.css`](https://github.com/anselmh
5858
<script>
5959
objectFit.polyfill({
6060
selector: 'img', // this can be any CSS selector
61-
fittype: 'cover' // either contain, cover, fill or none
61+
fittype: 'cover', // either contain, cover, fill or none
62+
disableCrossDomain: 'true' // either 'true' or 'false' to not parse external CSS files.
6263
});
6364
</script>
6465

dist/polyfill.object-fit.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,18 @@
4646
// handles extraction of `cssRules` as an `Array` from a stylesheet or something that behaves the same
4747
var getSheetRules = function (stylesheet) {
4848
var sheetMedia = stylesheet.media && stylesheet.media.mediaText;
49-
var sheetHost = getCSSHost(stylesheet.href);
49+
var sheetHost;
5050

5151
// if this sheet is cross-origin and option is set skip it
52-
if ((sheetHost !== window.location.host)) { // && avoidExternalStylesheets) {
53-
return [];
52+
if (objectFit.disableCrossDomain == 'true') {
53+
sheetHost = getCSSHost(stylesheet.href);
54+
55+
if ((sheetHost !== window.location.host)) {
56+
return [];
57+
}
5458
}
5559

60+
5661
// if this sheet is disabled skip it
5762
if (stylesheet.disabled) {
5863
return [];
@@ -280,6 +285,8 @@
280285

281286
objectFit.observer = null;
282287

288+
objectFit.disableCrossDomain = 'false';
289+
283290
objectFit.getComputedStyle = function(element, context) {
284291
context = context || window;
285292

@@ -403,7 +410,13 @@
403410
if (!args.selector || !args.replacedElements) {
404411
return;
405412
}
413+
414+
// Set option objectFit.disableCrossDomain
415+
objectFit.disableCrossDomain = args.disableCrossDomain || 'false';
416+
417+
// Set option fit-type
406418
args.fittype = args.fittype || 'none';
419+
407420
switch (args.fittype) {
408421
default:
409422
return;
@@ -415,7 +428,9 @@
415428
break;
416429
}
417430

431+
// Set option replacedElements
418432
var replacedElements = args.replacedElements;
433+
419434
if(!replacedElements.length) {
420435
return;
421436
}
@@ -624,7 +639,7 @@
624639
console.log('object-fit not natively supported');
625640
}
626641
// If the library is loaded after document onload event
627-
if(document.readyState === 'complete') {
642+
if (document.readyState === 'complete') {
628643
objectFit.init(args);
629644
} else {
630645
// Otherwise attach event listeners

dist/polyfill.object-fit.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/polyfill.getMatchedCSSRules.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,18 @@
4646
// handles extraction of `cssRules` as an `Array` from a stylesheet or something that behaves the same
4747
var getSheetRules = function (stylesheet) {
4848
var sheetMedia = stylesheet.media && stylesheet.media.mediaText;
49-
var sheetHost = getCSSHost(stylesheet.href);
49+
var sheetHost;
5050

5151
// if this sheet is cross-origin and option is set skip it
52-
if ((sheetHost !== window.location.host)) { // && avoidExternalStylesheets) {
53-
return [];
52+
if (objectFit.disableCrossDomain == 'true') {
53+
sheetHost = getCSSHost(stylesheet.href);
54+
55+
if ((sheetHost !== window.location.host)) {
56+
return [];
57+
}
5458
}
5559

60+
5661
// if this sheet is disabled skip it
5762
if (stylesheet.disabled) {
5863
return [];

src/polyfill.object-fit.core.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
objectFit.observer = null;
2121

22+
objectFit.disableCrossDomain = 'false';
23+
2224
objectFit.getComputedStyle = function(element, context) {
2325
context = context || window;
2426

@@ -142,7 +144,13 @@
142144
if (!args.selector || !args.replacedElements) {
143145
return;
144146
}
147+
148+
// Set option objectFit.disableCrossDomain
149+
objectFit.disableCrossDomain = args.disableCrossDomain || 'false';
150+
151+
// Set option fit-type
145152
args.fittype = args.fittype || 'none';
153+
146154
switch (args.fittype) {
147155
default:
148156
return;
@@ -154,7 +162,9 @@
154162
break;
155163
}
156164

165+
// Set option replacedElements
157166
var replacedElements = args.replacedElements;
167+
158168
if(!replacedElements.length) {
159169
return;
160170
}
@@ -363,7 +373,7 @@
363373
console.log('object-fit not natively supported');
364374
}
365375
// If the library is loaded after document onload event
366-
if(document.readyState === 'complete') {
376+
if (document.readyState === 'complete') {
367377
objectFit.init(args);
368378
} else {
369379
// Otherwise attach event listeners

tests/index-external-css.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ <h1><code>object-fit: cover;</code></h1>
2828
document.addEventListener('DOMContentLoaded', function () {
2929
objectFit.polyfill({
3030
selector: 'img',
31-
fittype: 'cover'
31+
fittype: 'cover',
32+
disableCrossDomain: 'false'
3233
});
3334
});
3435
</script>

0 commit comments

Comments
 (0)