MCPcopy
hub / github.com/jstat/jstat / sh_highlightString

Function sh_highlightString

doc/assets/sh_main.js:62–251  ·  view source on GitHub ↗

Highlights all elements containing source code in a text string. The return value is an array of objects, each representing an HTML start or end tag. Each object has a property named pos, which is an integer representing the text offset of the tag. Every start tag also has a property named node, w

(inputString, language)

Source from the content-addressed store, hash-verified

60@return an array of tag objects
61*/
62function sh_highlightString(inputString, language) {
63 if (/Konqueror/.test(navigator.userAgent)) {
64 if (! language.konquered) {
65 for (var s = 0; s < language.length; s++) {
66 for (var p = 0; p < language[s].length; p++) {
67 var r = language[s][p][0];
68 if (r.source === '$') {
69 r.exec = sh_konquerorExec;
70 }
71 }
72 }
73 language.konquered = true;
74 }
75 }
76
77 var a = document.createElement('a');
78 var span = document.createElement('span');
79
80 // the result
81 var tags = [];
82 var numTags = 0;
83
84 // each element is a pattern object from language
85 var patternStack = [];
86
87 // the current position within inputString
88 var pos = 0;
89
90 // the name of the current style, or null if there is no current style
91 var currentStyle = null;
92
93 var output = function(s, style) {
94 var length = s.length;
95 // this is more than just an optimization - we don't want to output empty <span></span> elements
96 if (length === 0) {
97 return;
98 }
99 if (! style) {
100 var stackLength = patternStack.length;
101 if (stackLength !== 0) {
102 var pattern = patternStack[stackLength - 1];
103 // check whether this is a state or an environment
104 if (! pattern[3]) {
105 // it's not a state - it's an environment; use the style for this environment
106 style = pattern[1];
107 }
108 }
109 }
110 if (currentStyle !== style) {
111 if (currentStyle) {
112 tags[numTags++] = {pos: pos};
113 if (currentStyle === 'sh_url') {
114 sh_setHref(tags, numTags, inputString);
115 }
116 }
117 if (style) {
118 var clone;
119 if (style === 'sh_url') {

Callers 1

sh_highlightElementFunction · 0.85

Calls 2

outputFunction · 0.85
sh_setHrefFunction · 0.85

Tested by

no test coverage detected