Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / codemirror / addon / runmode / colorize.js
CommitLineData
837afb80
TD
1(function(mod) {
2 if (typeof exports == "object" && typeof module == "object") // CommonJS
3 mod(require("../../lib/codemirror"), require("./runmode"));
4 else if (typeof define == "function" && define.amd) // AMD
5 define(["../../lib/codemirror", "./runmode"], mod);
6 else // Plain browser env
7 mod(CodeMirror);
8})(function(CodeMirror) {
9 "use strict";
77b7b761
TD
10
11 var isBlock = /^(p|li|div|h\\d|pre|blockquote|td)$/;
12
13 function textContent(node, out) {
14 if (node.nodeType == 3) return out.push(node.nodeValue);
15 for (var ch = node.firstChild; ch; ch = ch.nextSibling) {
16 textContent(ch, out);
17 if (isBlock.test(node.nodeType)) out.push("\n");
18 }
19 }
20
837afb80 21 CodeMirror.colorize = function(collection, defaultMode) {
77b7b761
TD
22 if (!collection) collection = document.body.getElementsByTagName("pre");
23
24 for (var i = 0; i < collection.length; ++i) {
25 var node = collection[i];
26 var mode = node.getAttribute("data-lang") || defaultMode;
27 if (!mode) continue;
28
29 var text = [];
30 textContent(node, text);
31 node.innerHTML = "";
32 CodeMirror.runMode(text.join(""), mode, node);
33
34 node.className += " cm-s-default";
35 }
36 };
837afb80 37});