From 37e5a662f9b7c820142ab0cc873bfd8acc330bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20C=C3=A1ceres?= Date: Thu, 19 May 2016 23:38:25 +1000 Subject: [PATCH] Feat(hightlight): use new highlighter (#767) * Feat(hightlight): use new highlighter * Feat(google-code-prettify): drop google prettify --- js/core/highlight.js | 77 +- js/google-code-prettify.js | 1655 ----------------------------- js/profile-w3c-common.js | 2 +- karma.conf.js | 5 + tests/spec/core/highlight-spec.js | 39 +- 5 files changed, 75 insertions(+), 1703 deletions(-) delete mode 100644 js/google-code-prettify.js diff --git a/js/core/highlight.js b/js/core/highlight.js index 88fa0dadc5..fac56a777d 100644 --- a/js/core/highlight.js +++ b/js/core/highlight.js @@ -1,46 +1,43 @@ - // Module core/highlight // Does syntax highlighting to all pre and code that have a class of "highlight" - -// A potential improvement would be to call cb() immediately and benefit from the asynchronous -// ability of prettyPrint() (but only call msg.pub() in the callback to remain accurate as to -// the end of processing) - +// An improvement would be to use web workers to do the highlighting. +"use strict"; define( - ["text!core/css/highlight.css", "google-code-prettify"], - function (css, PR) { - return { - run: function (conf, doc, cb, msg) { - msg.pub("start", "core/highlight"); + [ + "core/utils", + "highlight", + "text!highlightStyles/github.css", + ], + function(utils, hljs, ghCss) { + // Opportunistically insert the style into the head to reduce FOUC. + var codeStyle = document.createElement("style"); + codeStyle.textContent = ghCss; + var swapStyleOwner = utils.makeOwnerSwapper(codeStyle); + swapStyleOwner(document, document.head); + return { + run: function(conf, doc, cb, msg) { + // Nothing to do + if (conf.noHighlightCSS) { + return cb(); + } + + if (codeStyle.ownerDocument !== doc) { + swapStyleOwner(doc, doc.head); + } - // fix old classes - var oldies = "sh_css sh_html sh_javascript sh_javascript_dom sh_xml".split(" "); - for (var i = 0, n = oldies.length; i < n; i++) { - var old = oldies[i]; - $("." + old).each(function () { - $(this).removeClass(old).addClass("highlight"); - msg.pub("warn", "Old highlighting class '" + old + "', use 'highlight' instead."); - }); - } + if (doc.querySelector("highlight")) { + msg.pub("warn", "pre elements don't need a 'highlight' class anymore."); + } - // prettify - var $highs = $("pre.highlight, code.highlight") - , done = function () { - msg.pub("end", "core/highlight"); - cb(); - } - ; - if ($highs.length) { - if (!conf.noHighlightCSS) { - $(doc).find("head link").first().before($("