forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlight.js
More file actions
148 lines (122 loc) · 5.22 KB
/
highlight.js
File metadata and controls
148 lines (122 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//===============================================================================================================
// System : Color Syntax Highlighter
// File : Highlight.js
// Author : Eric Woodruff ([email protected])
// Updated : 10/21/2012
// Note : Copyright 2006-2012, Eric Woodruff, All rights reserved
//
// This contains the script to expand and collapse the regions in the syntax highlighted code.
//
// This is a customized version for the Sandcastle Help File Builder. It overrides the CopyCode() function
// from the Hana, Prototype, and VS2005 presentation styles to remove the line numbering and collapsible
// region elements. The VS2010 style does not currently use the CopyCode() function in here as it has its own
// version for copying the code.
//===============================================================================================================
// Expand/collapse a region
function HighlightExpandCollapse(showId, hideId)
{
var showSpan = document.getElementById(showId), hideSpan = document.getElementById(hideId);
showSpan.style.display = "inline";
hideSpan.style.display = "none";
}
// Copy the code from a colorized code block to the clipboard.
function CopyCode(key)
{
var idx, line, block, htmlLines, lines, codeText, hasLineNos, hasRegions, clip, trans,
copyObject, clipID;
var reLineNo = /^\s*\d{1,4}/;
var reRegion = /^\s*\d{1,4}\+.*?\d{1,4}-/;
var reRegionText = /^\+.*?\-/;
// Find the table row element containing the code
var trElements = document.getElementsByTagName("tr");
for(idx = 0; idx < trElements.length; idx++)
if(key.parentNode.parentNode.parentNode == trElements[idx].parentNode)
{
block = trElements[idx].nextSibling;
break;
}
if(block.innerText != undefined)
codeText = block.innerText;
else
codeText = block.textContent;
hasLineNos = block.innerHTML.indexOf("highlight-lineno");
hasRegions = block.innerHTML.indexOf("highlight-collapsebox");
htmlLines = block.innerHTML.split("\n");
lines = codeText.split("\n");
// Remove the line numbering and collapsible regions if present
if(hasLineNos != -1 || hasRegions != -1)
{
codeText = "";
for(idx = 0; idx < lines.length; idx++)
{
line = lines[idx];
if(hasRegions && reRegion.test(line))
line = line.replace(reRegion, "");
else
{
line = line.replace(reLineNo, "");
// Lines in expanded blocks have an extra space
if(htmlLines[idx].indexOf("highlight-expanded") != -1 ||
htmlLines[idx].indexOf("highlight-endblock") != -1)
line = line.substr(1);
}
if(hasRegions && reRegionText.test(line))
line = line.replace(reRegionText, "");
codeText += line;
// Not all browsers keep the line feed when split
if(line[line.length - 1] != "\n")
codeText += "\n";
}
}
// IE or FireFox/Netscape?
if(window.clipboardData)
window.clipboardData.setData("Text", codeText);
else
if(window.netscape)
{
// Give unrestricted access to browser APIs using XPConnect
try
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch(e)
{
alert("Universal Connect was refused, cannot copy to clipboard. Go to about:config and set " +
"signed.applets.codebase_principal_support to true to enable clipboard support.");
return;
}
// Creates an instance of nsIClipboard
clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(
Components.interfaces.nsIClipboard);
// Creates an instance of nsITransferable
if(clip)
trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(
Components.interfaces.nsITransferable);
if(!trans)
{
alert("Copy to Clipboard is not supported by this browser");
return;
}
// Register the data flavor
trans.addDataFlavor("text/unicode");
// Create object to hold the data
copyObject = new Object();
// Creates an instance of nsISupportsString
copyObject = Components.classes["@mozilla.org/supports-string;1"].createInstance(
Components.interfaces.nsISupportsString);
// Assign the data to be copied
copyObject.data = codeText;
// Add data objects to transferable
trans.setTransferData("text/unicode", copyObject, codeText.length * 2);
clipID = Components.interfaces.nsIClipboard;
if(!clipID)
{
alert("Copy to Clipboard is not supported by this browser");
return;
}
// Transfer the data to the clipboard
clip.setData(trans, null, clipID.kGlobalClipboard);
}
else
alert("Copy to Clipboard is not supported by this browser");
}