From bffdf972dbab2ea7f9bc19170ae77e2f14fc81dd Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Thu, 26 Apr 2018 01:27:30 +0100 Subject: [PATCH] resourceloader: Remove creation of dynamic-styles marker 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 | 45 ++++++++++++---------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/resources/src/mediawiki/mediawiki.js b/resources/src/mediawiki/mediawiki.js index 3fe276bbef..5392702d96 100644 --- a/resources/src/mediawiki/mediawiki.js +++ b/resources/src/mediawiki/mediawiki.js @@ -865,8 +865,13 @@ */ jobs = [], - // For getMarker() - marker = null, + /** + * For #addEmbeddedCSS() and #addLink() + * + * @private + * @property {HTMLElement|null} marker + */ + marker = document.querySelector( 'meta[name="ResourceLoaderDynamicStyles"]' ), // For addEmbeddedCSS() cssBuffer = '', @@ -874,40 +879,24 @@ 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; } /** @@ -964,7 +953,7 @@ cssBuffer = ''; } - $( newStyleTag( cssText, getMarker() ) ); + newStyleTag( cssText, marker ); fireCallbacks(); } @@ -1277,7 +1266,11 @@ // 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 ); + } } /** -- 2.20.1