Commit | Line | Data |
---|---|---|
cf10f7b7 TD |
1 | /** |
2 | * Augments the Prism syntax highlighter with additional functions. | |
3 | * | |
4 | * @author Tim Duesterhus | |
7b7b9764 | 5 | * @copyright 2001-2019 WoltLab GmbH |
cf10f7b7 TD |
6 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
7 | * @module WoltLabSuite/Core/Prism | |
8 | */ | |
9 | ||
10 | window.Prism = window.Prism || {} | |
11 | window.Prism.manual = true | |
12 | ||
13 | define(['prism/prism'], function () { | |
108cb5a5 | 14 | Prism.wscSplitIntoLines = function (container) { |
14611ac9 | 15 | var frag = document.createDocumentFragment(); |
108cb5a5 TD |
16 | var lineNo = 1; |
17 | var it, node, line; | |
cf10f7b7 TD |
18 | |
19 | function newLine() { | |
20 | var line = elCreate('span'); | |
21 | elData(line, 'number', lineNo++); | |
22 | frag.appendChild(line); | |
23 | ||
24 | return line; | |
25 | } | |
26 | ||
14611ac9 TD |
27 | // IE11 expects a fourth, non-standard, parameter (entityReferenceExpansion) and a valid function as third |
28 | it = document.createNodeIterator(container, NodeFilter.SHOW_TEXT, function () { | |
786d596e | 29 | return NodeFilter.FILTER_ACCEPT; |
14611ac9 | 30 | }, false); |
cf10f7b7 TD |
31 | |
32 | line = newLine(lineNo); | |
33 | while (node = it.nextNode()) { | |
34 | node.data.split(/\r?\n/).forEach(function (codeLine, index) { | |
35 | var current, parent; | |
36 | ||
37 | // We are behind a newline, insert \n and create new container. | |
38 | if (index >= 1) { | |
39 | line.appendChild(document.createTextNode("\n")); | |
40 | line = newLine(lineNo); | |
41 | } | |
42 | ||
43 | current = document.createTextNode(codeLine); | |
44 | ||
45 | // Copy hierarchy (to preserve CSS classes). | |
46 | parent = node.parentNode | |
47 | while (parent !== container) { | |
48 | var clone = parent.cloneNode(false); | |
49 | clone.appendChild(current); | |
50 | current = clone; | |
51 | parent = parent.parentNode; | |
52 | } | |
53 | ||
54 | line.appendChild(current); | |
55 | }); | |
56 | } | |
57 | ||
58 | return frag; | |
59 | }; | |
60 | ||
61 | return Prism; | |
786d596e | 62 | }); |