Load MathML polyfill stylesheet only when needed

pull/511/head
Thibaut Courouble 8 years ago
parent deaad00668
commit 9612016351

@ -12,3 +12,4 @@ app.config =
sentry_dsn: '<%= App.sentry_dsn %>' sentry_dsn: '<%= App.sentry_dsn %>'
version: <%= Time.now.to_i %> version: <%= Time.now.to_i %>
release: <%= Time.now.utc.httpdate.to_json %> release: <%= Time.now.utc.httpdate.to_json %>
mathml_stylesheet: 'https://cdn.devdocs.io/mathml.css'

@ -2,25 +2,19 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
* Adapted from: https://github.com/fred-wang/mathml.css */ * Adapted from: https://github.com/fred-wang/mathml.css */
/*jslint browser: true*/
(function () { (function () {
"use strict"; window.addEventListener("load", function() {
window.addEventListener("load", function () { var box, div, link, namespaceURI;
var box, div, link, namespaceURI; // First check whether the page contains any <math> element.
// First check whether the page contains any <math> element. namespaceURI = "http://www.w3.org/1998/Math/MathML";
namespaceURI = "http://www.w3.org/1998/Math/MathML"; // Create a div to test mspace, using Kuma's "offscreen" CSS
// Create a div to test mspace, using Kuma's "offscreen" CSS document.body.insertAdjacentHTML("afterbegin", "<div style='border: 0; clip: rect(0 0 0 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px;'><math xmlns='" + namespaceURI + "'><mspace height='23px' width='77px'></mspace></math></div>");
document.body.insertAdjacentHTML("afterbegin", "<div style='border: 0; clip: rect(0 0 0 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px;'><math xmlns='" + namespaceURI + "'><mspace height='23px' width='77px'></mspace></math></div>"); div = document.body.firstChild;
div = document.body.firstChild; box = div.firstChild.firstChild.getBoundingClientRect();
box = div.firstChild.firstChild.getBoundingClientRect(); document.body.removeChild(div);
document.body.removeChild(div); if (Math.abs(box.height - 23) > 1 || Math.abs(box.width - 77) > 1) {
if (Math.abs(box.height - 23) > 1 || Math.abs(box.width - 77) > 1) { window.supportsMathML = false;
// Insert the mathml.css stylesheet. }
link = document.createElement("link"); });
link.href = "https://cdn.devdocs.io/mathml.css";
link.rel = "stylesheet";
document.head.appendChild(link);
}
});
}()); }());

@ -37,6 +37,7 @@ class app.views.EntryPage extends app.View
if app.disabledDocs.findBy 'slug', @entry.doc.slug if app.disabledDocs.findBy 'slug', @entry.doc.slug
@hiddenView = new app.views.HiddenPage @el, @entry @hiddenView = new app.views.HiddenPage @el, @entry
@delay @polyfillMathML
@trigger 'loaded' @trigger 'loaded'
return return
@ -49,6 +50,12 @@ class app.views.EntryPage extends app.View
el.appendChild(@clipBoardLink.cloneNode()) for el in @el.querySelectorAll('pre') el.appendChild(@clipBoardLink.cloneNode()) for el in @el.querySelectorAll('pre')
return return
polyfillMathML: ->
return unless window.supportsMathML is false and !@polyfilledMathML and @find('math')
@polyfilledMathML = true
$.append document.head, """<link rel="stylesheet" href="#{app.config.mathml_stylesheet}">"""
return
LINKS = LINKS =
home: 'Homepage' home: 'Homepage'
code: 'Source code' code: 'Source code'

Loading…
Cancel
Save