resourceloader: Remove creation of dynamic-styles marker
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 26 Apr 2018 00:27:30 +0000 (01:27 +0100)
committerKrinkle <krinklemail@gmail.com>
Thu, 26 Apr 2018 15:53:29 +0000 (15:53 +0000)
Firstly, this code is never used because the marker is unconditionally
created by OutputPage. But, we may change that one day, and we
want to reduce dependency on server-side specifics so that the
loader can (in theory) work on any web page. So we want to keep
some kind of fallback for now.

It's a private getter used in two places:

1. addEmbeddedCSS (private): Call newStyleTag with nextNode=marker.
   The newStyleTag function already treats nextNode as optional,
   and falls back to doing the same thing getMarker() was doing,
   which is: append to document.head.

2. addLink (private, debug-mode only): Used the marker as insertion
   point for a new element. Basically the same as newStyleTag,
   except it was using the wrapper of `$().before()` instead
   of calling Node#insertBefore() directly. Made it optional,
   with as fallback appending to document.head. Same as newStyleTag.

Also removed an unused jQuery object in addEmbeddedCSS, from
passing result of newStyleTag() to $(), but not using it.

Bug: T192623
Change-Id: If04c801c073b4cf74bf111d02ce3dc133bb862d1

resources/src/mediawiki/mediawiki.js

index 3fe276b..5392702 100644 (file)
                                 */
                                jobs = [],
 
-                               // For getMarker()
-                               marker = null,
+                               /**
+                                * For #addEmbeddedCSS() and #addLink()
+                                *
+                                * @private
+                                * @property {HTMLElement|null} marker
+                                */
+                               marker = document.querySelector( 'meta[name="ResourceLoaderDynamicStyles"]' ),
 
                                // For addEmbeddedCSS()
                                cssBuffer = '',
                                cssCallbacks = $.Callbacks(),
                                rAF = window.requestAnimationFrame || setTimeout;
 
-                       function getMarker() {
-                               if ( !marker ) {
-                                       // Cache
-                                       marker = document.querySelector( 'meta[name="ResourceLoaderDynamicStyles"]' );
-                                       if ( !marker ) {
-                                               mw.log( 'Created ResourceLoaderDynamicStyles marker dynamically' );
-                                               marker = document.createElement( 'meta' );
-                                               marker.name = 'ResourceLoaderDynamicStyles';
-                                               document.head.appendChild( marker );
-                                       }
-                               }
-                               return marker;
-                       }
-
                        /**
                         * Create a new style element and add it to the DOM.
                         *
                         * @private
                         * @param {string} text CSS text
-                        * @param {Node} [nextNode] The element where the style tag
+                        * @param {Node|null} [nextNode] The element where the style tag
                         *  should be inserted before
                         * @return {HTMLElement} Reference to the created style element
                         */
                        function newStyleTag( text, nextNode ) {
-                               var s = document.createElement( 'style' );
-
-                               s.appendChild( document.createTextNode( text ) );
+                               var el = document.createElement( 'style' );
+                               el.appendChild( document.createTextNode( text ) );
                                if ( nextNode && nextNode.parentNode ) {
-                                       nextNode.parentNode.insertBefore( s, nextNode );
+                                       nextNode.parentNode.insertBefore( el, nextNode );
                                } else {
-                                       document.head.appendChild( s );
+                                       document.head.appendChild( el );
                                }
-
-                               return s;
+                               return el;
                        }
 
                        /**
                                        cssBuffer = '';
                                }
 
-                               $( newStyleTag( cssText, getMarker() ) );
+                               newStyleTag( cssText, marker );
 
                                fireCallbacks();
                        }
                                // see #addEmbeddedCSS, T33676, T43331, and T49277 for details.
                                el.href = url;
 
-                               $( getMarker() ).before( el );
+                               if ( marker && marker.parentNode ) {
+                                       marker.parentNode.insertBefore( el, marker );
+                               } else {
+                                       document.head.appendChild( el );
+                               }
                        }
 
                        /**