-
-
Notifications
You must be signed in to change notification settings - Fork 468
feat: add support for iframes (options.insertInto)
#248
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
Changes from 2 commits
933085d
e786fb9
685e503
196641e
f8a8582
1b788f2
6b8dec5
b998280
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,9 +28,19 @@ var getElement = (function (fn) { | |
|
|
||
| return function(selector) { | ||
| if (typeof memo[selector] === "undefined") { | ||
| memo[selector] = fn.call(this, selector); | ||
| var styleTarget = fn.call(this, selector); | ||
| // Special case to return head of iframe insetead of iframe itself | ||
|
||
| try { | ||
| if (styleTarget instanceof window.HTMLIFrameElement) { | ||
|
||
| // This will throw an exception if access to iframe is blocked | ||
| // due to cross-origin restrictions | ||
| styleTarget = styleTarget.contentDocument.head; | ||
| } | ||
| } catch(e) { | ||
| styleTarget = null; | ||
| } | ||
| memo[selector] = styleTarget; | ||
| } | ||
|
|
||
| return memo[selector] | ||
| }; | ||
| })(function (target) { | ||
|
|
@@ -209,19 +219,19 @@ function addStyle (obj, options) { | |
|
|
||
| // If a transform function was defined, run it on the css | ||
| if (options.transform && obj.css) { | ||
| result = options.transform(obj.css); | ||
|
|
||
| if (result) { | ||
| // If transform returns a value, use that instead of the original css. | ||
| // This allows running runtime transformations on the css. | ||
| obj.css = result; | ||
| } else { | ||
| // If the transform function returns a falsy value, don't add this css. | ||
| // This allows conditional loading of css | ||
| return function() { | ||
| // noop | ||
| }; | ||
| } | ||
| result = options.transform(obj.css); | ||
|
||
|
|
||
| if (result) { | ||
| // If transform returns a value, use that instead of the original css. | ||
| // This allows running runtime transformations on the css. | ||
| obj.css = result; | ||
| } else { | ||
| // If the transform function returns a falsy value, don't add this css. | ||
| // This allows conditional loading of css | ||
| return function() { | ||
| // noop | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| if (options.singleton) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ describe("basic tests", function() { | |
| "<div class='target'>", | ||
| checkValue, | ||
| "</div>", | ||
| "<iframe class='iframeTarget'/>", | ||
| "</body>", | ||
| "</html>" | ||
| ].join("\n"); | ||
|
|
@@ -103,6 +104,17 @@ describe("basic tests", function() { | |
| runCompilerTest(expected, done, undefined, selector); | ||
| }); // it insert into | ||
|
|
||
| it("insert into iframe", function(done) { | ||
| let selector = "iframe.iframeTarget"; | ||
| styleLoaderOptions.insertInto = selector; | ||
|
|
||
| let expected = requiredStyle; | ||
|
|
||
| runCompilerTest(expected, done, function() { | ||
| return this.document.querySelector(selector).contentDocument.head.innerHTML; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, I can improve that ;-)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it works without the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }, selector); | ||
| }); // it insert into | ||
|
|
||
| it("singleton", function(done) { | ||
| // Setup | ||
| styleLoaderOptions.singleton = true; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iframe=>[IFrame](https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement)