Add WoltLabSuite/Core/Prism/Helper
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 5 Jan 2021 14:11:11 +0000 (15:11 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 5 Jan 2021 15:56:10 +0000 (16:56 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Prism.js
wcfsetup/install/files/js/WoltLabSuite/Core/Prism/Helper.js [new file with mode: 0644]
wcfsetup/install/files/ts/WoltLabSuite/Core/Prism.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Prism/Helper.ts [new file with mode: 0644]

index 5fe52c74dae9f748605dcf1927f7eabee3165fc2..68b09543715c5a5ffc45a617c3b3a13cf8391afd 100644 (file)
@@ -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 <http://opensource.org/licenses/lgpl-license.php>
  * @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 (file)
index 0000000..b929d81
--- /dev/null
@@ -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 <http://opensource.org/licenses/lgpl-license.php>
+     * @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;
+});
index 5fe52c74dae9f748605dcf1927f7eabee3165fc2..68b09543715c5a5ffc45a617c3b3a13cf8391afd 100644 (file)
@@ -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 <http://opensource.org/licenses/lgpl-license.php>
  * @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 (file)
index 0000000..dee334b
--- /dev/null
@@ -0,0 +1,50 @@
+/**
+ * Provide helper functions for prism processing.
+ *
+ * @author     Tim Duesterhus
+ * @copyright  2001-2021 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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;
+}