From: Tim Düsterhus Date: Tue, 5 Jan 2021 14:11:11 +0000 (+0100) Subject: Add WoltLabSuite/Core/Prism/Helper X-Git-Tag: 5.4.0_Alpha_1~480^2~4 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=ea8094745ccc29e8cc57ea92c296476b4273b1be;p=GitHub%2FWoltLab%2FWCF.git Add WoltLabSuite/Core/Prism/Helper --- diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Prism.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Prism.js index 5fe52c74da..68b0954371 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Prism.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Prism.js @@ -1,14 +1,17 @@ /** - * Augments the Prism syntax highlighter with additional functions. + * Loads Prism while disabling automated highlighting. * * @author Tim Duesterhus - * @copyright 2001-2019 WoltLab GmbH + * @copyright 2001-2021 WoltLab GmbH * @license GNU Lesser General Public License * @module WoltLabSuite/Core/Prism */ window.Prism = window.Prism || {}; window.Prism.manual = true; define(['prism/prism'], function () { + /** + * @deprecated 5.4 - Use WoltLabSuite/Core/Prism/Helper#splitIntoLines. + */ Prism.wscSplitIntoLines = function (container) { var frag = document.createDocumentFragment(); var lineNo = 1; diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Prism/Helper.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Prism/Helper.js new file mode 100644 index 0000000000..b929d81224 --- /dev/null +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Prism/Helper.js @@ -0,0 +1,53 @@ +define(["require", "exports"], function (require, exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.splitIntoLines = void 0; + /** + * Provide helper functions for prism processing. + * + * @author Tim Duesterhus + * @copyright 2001-2021 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Prism/Helper + */ + function splitIntoLines(container) { + const frag = document.createDocumentFragment(); + let lineNo = 1; + const newLine = () => { + const line = document.createElement("span"); + line.dataset.number = lineNo.toString(); + lineNo++; + frag.appendChild(line); + return line; + }; + const it = document.createNodeIterator(container, NodeFilter.SHOW_TEXT, { + acceptNode() { + return NodeFilter.FILTER_ACCEPT; + }, + }); + let line = newLine(); + let node; + while ((node = it.nextNode())) { + const text = node; + text.data.split(/\r?\n/).forEach((codeLine, index) => { + // We are behind a newline, insert \n and create new container. + if (index >= 1) { + line.appendChild(document.createTextNode("\n")); + line = newLine(); + } + let current = document.createTextNode(codeLine); + // Copy hierarchy (to preserve CSS classes). + let parent = text.parentNode; + while (parent && parent !== container) { + const clone = parent.cloneNode(false); + clone.appendChild(current); + current = clone; + parent = parent.parentNode; + } + line.appendChild(current); + }); + } + return frag; + } + exports.splitIntoLines = splitIntoLines; +}); diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Prism.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Prism.js index 5fe52c74da..68b0954371 100644 --- a/wcfsetup/install/files/ts/WoltLabSuite/Core/Prism.js +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Prism.js @@ -1,14 +1,17 @@ /** - * Augments the Prism syntax highlighter with additional functions. + * Loads Prism while disabling automated highlighting. * * @author Tim Duesterhus - * @copyright 2001-2019 WoltLab GmbH + * @copyright 2001-2021 WoltLab GmbH * @license GNU Lesser General Public License * @module WoltLabSuite/Core/Prism */ window.Prism = window.Prism || {}; window.Prism.manual = true; define(['prism/prism'], function () { + /** + * @deprecated 5.4 - Use WoltLabSuite/Core/Prism/Helper#splitIntoLines. + */ Prism.wscSplitIntoLines = function (container) { var frag = document.createDocumentFragment(); var lineNo = 1; diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Prism/Helper.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Prism/Helper.ts new file mode 100644 index 0000000000..dee334bd1c --- /dev/null +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Prism/Helper.ts @@ -0,0 +1,50 @@ +/** + * Provide helper functions for prism processing. + * + * @author Tim Duesterhus + * @copyright 2001-2021 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Prism/Helper + */ +export function splitIntoLines(container: Node): DocumentFragment { + const frag = document.createDocumentFragment(); + let lineNo = 1; + const newLine = () => { + const line = document.createElement("span"); + line.dataset.number = lineNo.toString(); + lineNo++; + frag.appendChild(line); + return line; + }; + + const it = document.createNodeIterator(container, NodeFilter.SHOW_TEXT, { + acceptNode() { + return NodeFilter.FILTER_ACCEPT; + }, + }); + + let line = newLine(); + let node; + while ((node = it.nextNode())) { + const text = node as Text; + text.data.split(/\r?\n/).forEach((codeLine, index) => { + // We are behind a newline, insert \n and create new container. + if (index >= 1) { + line.appendChild(document.createTextNode("\n")); + line = newLine(); + } + + let current: Node = document.createTextNode(codeLine); + // Copy hierarchy (to preserve CSS classes). + let parent = text.parentNode; + while (parent && parent !== container) { + const clone = parent.cloneNode(false); + clone.appendChild(current); + current = clone; + parent = parent.parentNode; + } + line.appendChild(current); + }); + } + return frag; +}