Internet Explorer(at least 6-8, I've not tested it with 9) ignores stylesheets injecting dynamically via dom, when the @import statement is used.
IE has it's own function to add stylesheets to its import collection(http://msdn.microsoft.com/en-us/library/ms531193%28VS.85%29.aspx).
Here is a simple example which won't work(copied from this issue: http://stackoverflow.com/questions/3237775/adding-import-statement-in-a-...):
var string = '@import url(test.css)';
var style = document.createElement('style');
if (style.styleSheet) { // IE
style.styleSheet.cssText = string;
} else {
var cssText = document.createTextNode(string);
style.appendChild(cssText);
}
document.getElementsByTagName('head')[0].appendChild(style);
It would only work this way:
document.styleSheets[0].addImport("test.css");
and of course this works only for internet explorer, other browsers will throw an error...
This could cause problems in drupal, when a module has multi step forms, each step is loaded via ajax and one of those steps uses a javascript functions with dependencies on css files(for example the jquery ui widgets). But I'm sure there are other issues...
| Comment | File | Size | Author |
|---|---|---|---|
| #125 | ie8-linkit-lazy-load-css.png | 12.27 KB | miroslavbanov |
| #121 | 1071818-121.patch | 6.37 KB | gielfeldt |
| #119 | 1071818-119.patch | 6.37 KB | gielfeldt |
| #118 | 1071818-118-test-module-do-not-test.patch | 3.15 KB | kristofferwiklund |
| #115 | 1071818-114-test-module-do-not-test.patch | 3.22 KB | star-szr |
Comments
Comment #1
rfayWow, nice work on the diagnosis. Subscribing.
Comment #2
fagoI'd say it's generally troublesome if the ajax system needs to add-in additional CSS via ajax_render(). We ran into this issue with the Rules UI as it caused the data-selection autocomplete to fail in IE.
Comment #3
droplet commentedSubscribing
Comment #4
sunsubscribing
Comment #5
amitaibuSubscribing
Comment #6
sepgil commentedI tried to fix this issue, but unfortunately its way more complicated than I thought.
As far as I know the @import statemant are used in drupal to bypass the limitation of 31 stylesheets. But as far as I've found out, the only way to avoid this problem is to import stylesheets inside other stylesheets. So what drupal needs is some sort of stylesheet rendering function which create stylesheets, that import stylesheets.
Comment #7
sunComment #8
Bojhan.core commented@fago how did you fix this? I am sure there are other modules that fix this now too?
Comment #9
JeremyFrench commentedThis also affects IE9
Comment #10
JeremyFrench commentedLooking at this issue, I think an approach to take would be to create a new 'add_styles' ajax command. Which runs either appends the style or runs
.addImport("test.css");on any import declarations if that function exists.We can then replace the
$extra_commands[] = ajax_command_prepend('head', $styles);with that command.If someone agrees with this general approach I'll see if I can get something working.
Comment #11
JeremyFrench commentedI have attached a module which serves as a test harness for this issue.
Comment #12
JeremyFrench commentedPatch added. It creates a new add_css command which will add css to the head and then use addImport on any import statements in the css that was added if the function exists.
I've tested using the test case I added on ie6-9 and it seems to work but further testing would be appreciated.
Comment #13
xjmThanks for the patch! Here are a couple really minor notes on code style:
Minor point, but we probably want "cross-browser" hyphenated.
Can we add an @see to ajax.js at the end of the docblock?
There's an extra blank line here (which we'll want to keep if we add that @see after it.)
Trailing whitespace.
Also, note that the Drupal 8.x patch will need to be rerolled, because the core directory structure for Drupal 8 has now changed. (For more information, see #22336: Move all core Drupal files under a /core folder to improve usability and upgrades). When the patch has been rerolled, please set the issue back to "Needs Review."
Tagging as novice for the task of rerolling the Drupal 8.x patch with the cleanup described above.
If you need help rerolling this patch, you can come to core office hours or ask in #drupal-gitsupport on IRC.
Comment #14
reglogge commentedassigning this to me for a reroll
Comment #15
reglogge commentedRerolled with the suggestions from #13 and some additional path changes in code comment blocks.
Comment #16
xjmThanks @reglogge!
Nice catch on the path corrections. However, since those don't specifically target this bug, we should open a separate (documentation) issue for them and roll a separate patch. See: http://webchick.net/please-stop-eating-baby-kittens
Comment #17
tstoecklerThis wraps early, i.e. before 80 chars.
28 days to next Drupal core point release.
Comment #18
reglogge commentedNew patch with a fix for #17 and removing the path corrections.
Comment #19
reglogge commentedbot...
Comment #20
xjmMmm, it looks to me like the indentation is off here. There should be a space before the stars. Did I just not notice it before? Sorry. :(
Also, did you open that followup by any chance with your other fixes? You can link it here for reference.
27 days to next Drupal core point release.
Comment #21
xjmNot quite sure how or why I assigned this to myself.
Comment #22
JeremyFrench commentedFixed formatting as mentioned in #20.
Comment #23
JeremyFrench commentedAdding correctly named file.
Comment #24
mradcliffeI tested a workaround for Drupal 7 while I was working with CTools and vertical tabs that relied on the same conditional code (see hack below).
I am slightly concerned about the use of regex, but I don't think there is a better alternative since the input is being fed by drupal_get_css(). Is it possible to overload addImport() with bogus data? Otherwise this looks good to me.
Comment #25
nod_Tagging,
#23 looks pretty good, don't have IE to test though. Maybe sepgil can confirm ?
Comment #26
xjmTagging for manual testing.
Comment #27
xjmAhem.
Comment #28
anthbel commented#23: lazy-loading-css-ie-1071818-22.patch queued for re-testing.
Comment #30
anthbel commentedRerolled for Drupal 8.x.
Comment #31
xjmThanks anthbel!
Looks like there's a small indentation issue here. We'll want to fix that, then assuming the patch passes testbot, we can try to test it manually again.
Comment #32
anthbel commentedCorrected the indentation.
Comment #33
xjmFixing the whitespace in a docblock. (Not changing the wording on account of #1337022: [policy, no patch] Create/adopt JavaScript docs standards compatible with a JS documentation parser.)
Now let's get some manual testing in all versions of IE, and also confirm that there are no regressions in other browsers. If someone could post explicit steps to reproduce the bug, that would help. Thanks!
Comment #34
JeremyFrench commentedThanks for the re roll.
XJM there is a minimal module in #11 which demonstrates the bug. It should be self explanatory.
Comment #35
Niklas Fiekas commentedLooks like an empty line is missing there.
Comment #36
xjmSilly JavaScript.
Comment #37
star-szrI attempted a manual test using the module in #11.
1. Fresh install of 8.x-dev
2. Install and enable the module in #11
3. Navigate to /test1071818
4. Click "Test Issue" link
I then got sent to a page full of JSON at /test1071818/test:
[{"command":"settings","settings":{"basePath":"\/","pathPrefix":"","ajaxPageState":{"theme":"bartik","theme_token":"hL6gVpPUzHmLE_zf1mCgC6h6RKtu545LqQ-JmW3Refo"},"overlay":{"paths":{"admin":"node\/*\/edit\nnode\/*\/delete\nnode\/*\/revisions\nnode\/*\/revisions\/*\/revert\nnode\/*\/revisions\/*\/delete\nnode\/add\nnode\/add\/*\noverlay\/dismiss-message\nuser\/*\/shortcuts\nadmin\nadmin\/*\nbatch\ntaxonomy\/term\/*\/edit\ntaxonomy\/term\/*\/delete\nuser\/*\/cancel\nuser\/*\/edit\nuser\/*\/edit\/*","non_admin":"admin\/structure\/block\/demo\/*\nadmin\/reports\/status\/php"},"ajaxCallback":"overlay-ajax"}},"merge":true},{"command":"insert","method":"append","selector":".region-content .content","data":"\u003Cdiv class=\u0022issue_pass\u0022\u003E\u003Ch3\u003EPass\u003C\/h3\u003EThis should be shown if the issue is fixed in this setup.\u003C\/div\u003E\n \u003Cdiv class=\u0022issue_fail\u0022\u003E\u003Ch3\u003EFail\u003C\/h3\u003EThis will be shown if the issue is not fixed in this setup.\u003C\/div\u003E","settings":null}]Testing the module in IE6-8, the browser tried to download the JSON-containing file instead of showing it in the browser. Applying the patch in #36 didn't make a difference in any browser I tested in.
My impression from looking at the test module code is that the pass/fail is supposed to be displayed in-browser. I attempted clearing my cache as well with no change.
Perhaps we can clarify how the test module is supposed to work and any other necessary configuration that may be required.
Comment #38
anthbel commentedRerolled the module in #11, because of the change in Drupal 8 core directory structure.
Comment #39
anthbel commentedApplied #36 and tested with #38 on IE7 and IE8. Results attached.


Have not found an impact on chrome and FF.
Comment #40
Zgear commentedTested for in safari and opera, both passed. I daresay RTBC.
Comment #41
catchLet's give nod_ a chance to sign off on this before it goes in, assigning to the new JavaScript maintainer!
Comment #42
nod_Actually I've been stalking this one for a while :D
There is a leaking var in there. Otherwise this looks good, I'm doing a few more tests and I'll send a patch later.
Comment #43
nod_Ok so there are several issues here.
@importis only used for unaggregated CSS, which is the uncommon use case (production last way longer than development) so it is only useful ifresponse.datahas@importsomewhere.Here is an updated patch, I don't have IE but that should work fine :)
Comment #44
sun1) Had to read the description twice to parse it, and after parsing, I think (or hope) I get what it tries to tell, but the actual text is not proper English.
2) I do not understand why the styles are added both in the normal way and with the proprietary method. The JSDoc description should also clarify and explain why that is done.
3) The inline comments within the function body could use some better wording, too.
Lastly, "CSS" in comments should always be all-uppercase.
1) It looks like the check for document.styleSheets[0].addImport is the primary and much faster condition, so it should come first.
2) It looks like we're performing two different regex matches on the response.data, whereas the initial is less specific, but the actual one is not very complex either, so we should just perform the actual/second one.
3) I'm concerned about any possible false-positives of this manual parsing. Did anyone investigate this? If so, the results of that could deserve an inline comment.
4) Has this been tested with inline CSS returned by the server?
Comment #45
JeremyFrench commented@nod
Is this the case when drupal_add_css us used in an ajax command?
@sun
does this read better?
This command is used to add css in ajax commands. In most cases this is simply adding the CSS to the head. But some browsers ignore @import statements when this is done. In these cases this function will detect the existence of a proprietary addImport method to add the import style sheets.
Comment #46
nod_Here is for the code:
Haven't touched at comments
Comment #47
JeremyFrench commentedPatch looks good to me. Needs more eyes though.
Comment #48
mradcliffeI think Firefox 10 may be throwing a JS error on this now where it wasn't before in previous versions (I think, I can't test anymore). Or maybe it's Firebug?
Comment #49
JeremyFrench commentedCouldn't reproduce the firefox10 bug. But doing a quick test in IE showed it was no longer working.
The regex was being used for a test and not being reset for the loop. This should now be fixed in this patch.
Comment #50
JeremyFrench commentedOoops. Attaching correctly named file.
Comment #52
JeremyFrench commentedSorry for spam. This should work.
Comment #53
ryan.ryan commentedThe patch in 52 passed testing in FF and Webkit browsers.
Comment #54
NROTC_Webmaster commentedThe patch in #52 works for Chrome v19 and IE 8
Comment #55
nod_learing stuff on regxp is always fun :)
so using
string.search(regexp)will give the same result asregexp.test(string), but without messing with the search index and it's faster too :) http://jsperf.com/drupal-import-regxp/3needed a reroll after the doc change too.
Comment #56
sunSorry, but what I meant was this.
And I don't really care for performance of IE, as long as it requires dirty workarounds like this.
That said, this needs to be tested against IE10, as Microsoft announced that they removed/resolved the stylesheets limitations, so the regular/standard way should work in IE10, too -- however, the addImport method might still exist, as Microsoft tends to keep quirks around, and so in turn, we need to make sure that stylesheets don't get imported twice.
Comment #57
nod_You can't use regexp like that in JS. To have access to the substring you have to use
.exec(),.match()gives you the whole thing.Your patch doesn't work for me.
Comment #58
dig1 commentedOk here are some testing results for IE9:
First install
1) Downloaded & installed latest D8 --dev
2) Downloaded & installed #38 test-rig
3) Cleared cache on server and browser
4) Loaded /test1071818
5) Clicked on 'Test' and got ' Fail This will be shown if the issue is not fixed in this setup.'
6) Downloaded & patched http://drupal.org/files/core-js-IE-addimport-1071818-55.patch
7) Cleared cache on server and browser
8) Loaded /test1071818
5) Clicked on 'Test' and got 'Pass This should be shown if the issue is fixed in this setup.'
Second install
1) Downloaded & installed latest D8 --dev
2) Downloaded & installed #38 test-rig
3) Cleared cache on server and browser
4) Loaded /test1071818
5) Clicked on 'Test' and got ' Fail This will be shown if the issue is not fixed in this setup.'
6) Downloaded & patched http://drupal.org/files/drupal8.ajax-css-ie.56.patch
7) Cleared cache on server and browser
8) Loaded /test1071818
5) Clicked on 'Test' and got 'Fail This will be shown if the issue is not fixed in this setup.'
So for IE9 at the moment it appears that:
- core-js-IE-addimport-1071818-55.patch works
- drupal8.ajax-css-ie.56.patch does not work
Cheers
Comment #59
NROTC_Webmaster commentedThe patch in 56 fails in IE8
Comment #60
xjmThanks Dig1 and NROTC_Webmaster!
Can we settle on #55 so we can finish manually testing this on all browsers and versions of IE, and get this bugfix in?
Comment #61
JeremyFrench commentedTested patch in #55 on IE10 and IE10PP4, I get pass in both platforms.
Issue still occurs without patch, so they haven't fixed this particular issue even in IE10.
Personally I think the #55 patch should be RBTC, but I have had to much of a hand it it to change status myself.
Comment #62
kristiaanvandeneyndeCross referencing with #1461322: Fix AJAX add_css – insert the needed CSS assets after the already-inserted ones
The patch in #55 still prepends scripts to the head.
If we are replacing ajax_command_prepend with a new command (ajax_command_add_css), we may as well 'get it right' in that new command?
Comment #63
xjmChanges like that should probably be done in followup issues, I think, but as it's JS I don't know the full implications. It merits its own, separate discussion (probably in the linked issue). This issue is specifically about resolving the bug, whereas the other issue needs discussion and consensus before we'd go forward with it. Thanks!
Comment #64
kristiaanvandeneyndeI agree that the discussion about that subject should take place in the ticket for it.
However, the code that is causing that issue in the first place will be completely removed by the patch in #55 and replaced with new code that still implements the weird behaviour.
If we are replacing the code anyway, why not try to squash two issues in one patch?
The only change to the patch in #55 would be appending the lazy loaded styles to the head instead of prepending them.
The alternative would be:
May as well benefit from the current momentum?
Comment #65
xjmThanks @kristiaanvandeneynde. My perspective is that this is a major bug that has a complete, existing solution. The other is not. In general, I don't think it's wise to postpone major bugs on non-major tasks.
If the JavaScript needs to be changed, I don't know enough to evaluate the proposed changes, but much of the "current momentum" as you call it is from people working on this issue during core office hours, which I facilitate. However, I am not going to keep spending people's time re-testing it over cleanups that don't change the functionality.
Untagging. Retag when this patch is ready for testing that needs to precede an RTBC.
Comment #66
JeremyFrench commented@kristiaanvandeneynde
Unfortunately this patch is very near consensus and being RBTC, compounding two issues will potentially take much longer to work on and decide if it is even needed. Trying to include it in here is more likely to kill the momentum rather than getting two issues resolved.
I can understand that it looks strange but it means that both issues get proper discussion in isolation.
Just out of interest have you tried patch #55 you may find that it resolves the issue you have. Prepending or Appending in IE will still fail without this patch.
Comment #67
effulgentsia commentedI think append vs. prepend warrants a separate discussion, and therefore, issue, and not be combined here. One problem with append is that it would mean module CSS potentially overriding theme CSS. Maybe it's better than the current prepend, but to me, it's not yet obvious that it is, and if it is, that it can be backported to D7, so let's hash that out there without derailing this bugfix.
Comment #68
JeremyFrench commentedRemove tag that was added in error.
(is this a novice issue?)
Comment #69
xjmFixing crosspost. @nod_, please let me know when this issue is ready for testing again! Thanks.
Comment #70
xjmNovice was also for the manual testing. :)
Comment #71
nod_#55 is good for review and «works for me»™ on IE7–9, unless sun has something to say about it.
Comment #72
effulgentsia commentedIf this is necessary for the JavaScript to work, then we need to add a test to ensure someone doesn't come along and try to remove those seemingly unnecessary newlines. A code comment also wouldn't hurt.
Otherwise, I'm +1 for this, but am not qualified to RTBC it.
Comment #73
xjmTagging per #72.
Comment #74
nod_The point of line breaks are to make the javascript regex very simple. I'm not particularly good at writing doc and comments, i'll let that to someone else :)
before the code returned by the add_css command was like that:
To dramatically simplify the regex the string returned needs to be like that:
now we can use the multi-line flag of javascript regex, treating each line (separated by \n) as a completely different string. That means we can use
^and$on one line at a time. We don't have to worry about style tags since they'll never match the regex.Comment #75
NROTC_Webmaster commentedUpdated #55 with comments based on #74.
Comment #76
xjmThat comment looks good. Thanks @NROTC_Webmaster.
So now we just need an automated test that confirms that the $element['#value'] begins and ends with a linebreak.
Comment #77
nod_Let's see what bot says
Comment #79
nod_Cool, CSS newline test passes. The new ajax_command_add_css fails though.
Comment #80
maxtorete commentedIs this bug present on Drupal 7? I'm having the same troubles with css injection on IE 7-9. If so, will the patch backported to D7 core?
Thanks!
Comment #81
nod_You should really turn css aggregation on Maxtorete, it's good for your site and should get rid of this issue.
Comment #82
JeremyFrench commented@nod_ I don't think aggregation will always get rid of this issue.
@maxtorete yes this is an issue in D7. And the patch should be backported.
Comment #83
nod_it will unless you're adding @import in some custom inline CSS. and even then it might be picked up anyway.
help on the test is welcome btw :)
Comment #84
lliss commentedSince there were so many changes surrounding psr-0 this had to be rerolled. Doesn't fix the problem yet but at least the patch can be applied cleanly again.
Comment #85
lliss commentedComment #87
yashadev commented#84: core-js-IE-addimport-1071818-84.patch queued for re-testing.
Comment #89
nod_Don't have time to deal with it.
Bug is fixed by the patch, the new tests needs some fixing.
Comment #90
star-szrI'm going to take a look at the tests.
Comment #91
star-szrI did some manual testing using the test module in #38 . The JavaScript change introduced back in #55 breaks the test module (failure message is displayed) in IE 6-8, IE 9 still tests fine. The patch I'm attaching here is still broken in IE 6-8.
I tested with these steps:
The patch here adds to ajax_forms_test.module to allow the new add_css command to be tested, and corrects the expected return in the add_css test.
Also removed an extra newline introduced in #84 .
Edit: linkified comment numbers.
Comment #93
star-szr#91: 1071818-91.patch queued for re-testing.
Just searched and found #1655422: Random test failures in SearchCommentTest, so I'm thinking the failure was unrelated.
Comment #94
star-szrMuch better. Now we need to fix this in IE 6-8.
Comment #95
yashadev commentedI thought Drupal 8 is dropping support for IE 6?
Comment #96
effulgentsia commentedYes, for D8, we only need this to work on IE8. For a D7 backport though, it needs to work on IE6 and 7.
Comment #97
star-szrRight, thanks for clarifying that @effulgentsia!
Comment #98
nod_Took the JS code from #52, please retest. If it works rtbc it please :)
Comment #99
star-szrThanks @nod_! I just did manual testing in IE 6-9, they all passed now. I think I'm a bit too close to the issue to RTBC though.
Removing the "Needs tests" tag.
Comment #100
nod_nah it's fine, it's an edge case anyway. Thanks for giving this the last push it needed :)
RTBC since #99 successfully tested the patch and it's green.
Comment #101
webchickI confess I don't know fully what's happening here, but it seems like a decent bug fix and comes with tests and has buy-in from the right people.
Committed and pushed to 8.x.
This will need a backport to D7.
Comment #102
star-szrComment #103
star-szrBackported to D7. I had to change the test assertion for the newlines within
<style>tags in CascadingStylesheetsTestCase::testRenderFile() (see #74). This assertion needed to be revised because in D7<style>tags have atype="text/css"attribute added by default.Re-tested in IE 6-9 with the test module in #11, all passed.
Comment #104
beeradb commented#103: 1071818-103-tests.patch queued for re-testing.
Comment #105
beeradb commentedI just tested this in IE9. I tried it both via adding commands directly to the ajax commands array, and calling drupal_add_css and letting ajax_render() add the command. In both cases the add_css ajax method was called, and even passed through to the line document.styleSheets[0].addImport(match[1]); with the correct CSS file, but the file was either never actually loaded, or did not properly apply.
Comment #106
nod_Comment #107
star-szrCan we get another confirmation for the result in #105? I used the test module in #38, and the patch in #103 passed in IE 6-9.
Patch still applies cleanly to 7.x.
Comment #108
star-szrI meant the test module from #11 (before the /core change).
Comment #109
jantimon commented#103 works for me (IE 8)
Comment #110
Snipon commentedIt works fine in IE8 until you turn CSS aggregation on.
Comment #111
micbar commentedworks for me on IE 7 and 8
Comment #112
micbar commented#103: 1071818-103.patch queued for re-testing.
Comment #113
gielfeldt commentedRebased (and updated) for 7.x-dev.
Comment #114
star-szrThanks @gielfeldt. This needs another reroll.
I am uploading the test module from #38 as a patch for easier testing (it can also be tested via simplytest.me).
Comment #115
star-szrAhem, here is the actual patch for the test module :)
Comment #116
kristofferwiklund commentedI will look at that.
Comment #117
kristofferwiklund commentedPatch in #113 is fine. Does not need rerolling.
Comment #118
kristofferwiklund commentedUppdated test code in #114 for 7.x.
The test fails for current 7.x for IE 8. The test pass for IE10.
After applying patch in #113 the code works also for IE8.
Comment #119
gielfeldt commentedNew patch for latest dev
Comment #120
miroslavbanov commentedThe patch in #119 applies successfully and completely breaks ajax.js :
At the end of the add_css function, there should be a "
," or the JS object is broken and can't be parsed. I wish there was a test that can catch this problem.Comment #121
gielfeldt commentedI've attached a patch addressing this for now. But we should add some tests.
I agree. However, I had problems with jslint and gave up ...
Comment #122
gielfeldt commentedComment #123
miroslavbanov commentedI can say that the patch #121 works for me, for some pretty complex Drupal installations, but I don't know how the patch works, and I haven't actually reviewed it, so I won't mark it RTBC.
Comment #124
mgifford#121 is a good port from D8 from my review of it.
@MiroslavBanov can testify that it is working for his Drupal installations.
I'm not certain what is the best way to test this though. We don't do a lot of work with Windows & haven't experienced this Lazy Loading issue that I'm aware of.
This was committed to D8 2 years ago. This should be easy to mark RTBC. I'd just like to have a clear sense of what I'm testing.
Comment #125
miroslavbanov commentedIts easy enough to reproduce:
See linkit profile autocomplete with and withot patch #121:
![[Broken without patch]](/files/issues/ie8-linkit-lazy-load-css.png)
I'm going ahead and marking this RTBC, before the reason for this patch is completely dead.
Comment #128
dcam commentedRe-setting RTBC status.
Comment #131
dcam commentedComment #134
kristofferwiklund commentedComment #135
dcam commentedComment #138
dcam commentedComment #139
miroslavbanov commentedWhat is the deal with all this status change? The patch is green, but the there are messages that it "fails testing". Is this because of some problem with the testbot?
Comment #140
dcam commentedYes, it is. See #2246867: Intermittent "test did not complete due to a fatal error" failures when testing Drupal 7 patches.
Comment #141
micbar commentedlike #125 I hope that this patch gets committed before it becomes obsolete.
Comment #144
dcam commentedComment #147
dcam commentedComment #150
oriol_e9g#121 green
Comment #151
micbar commentedplease commit soon, the testbot is constantly reverting this to "needs work". See #140.
Comment #152
David_Rothstein commentedCommitted to 7.x - thanks!
Fixed the following on commit: