PageRenderTime 53ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 19ms app.codeStats 0ms

/Documentation/Help/scripts/highlight.js

#
JavaScript | 187 lines | 124 code | 31 blank | 32 comment | 31 complexity | eb229ad4961005776897cc69098faf66 MD5 | raw file
  1//=============================================================================
  2// System  : Color Syntax Highlighter
  3// File    : Highlight.js
  4// Author  : Eric Woodruff  (Eric@EWoodruff.us)
  5// Updated : 11/13/2007
  6// Note    : Copyright 2006, Eric Woodruff, All rights reserved
  7//
  8// This contains the script to expand and collapse the regions in the
  9// syntax highlighted code.
 10//
 11//=============================================================================
 12
 13// Expand/collapse a region
 14function HighlightExpandCollapse(showId, hideId)
 15{
 16    var showSpan = document.getElementById(showId),
 17        hideSpan = document.getElementById(hideId);
 18
 19    showSpan.style.display = "inline";
 20    hideSpan.style.display = "none";
 21}
 22
 23// Copy the code if Enter or Space is hit with the image focused
 24function CopyColorizedCodeCheckKey(titleDiv, eventObj)
 25{
 26    if(eventObj != undefined && (eventObj.keyCode == 13 ||
 27      eventObj.keyCode == 32))
 28        CopyColorizedCode(titleDiv);
 29}
 30
 31// Change the icon as the mouse moves in and out of the Copy Code link
 32// There should be an image with the same name but an "_h" suffix just
 33// before the extension.
 34function CopyCodeChangeIcon(linkSpan)
 35{
 36    var image = linkSpan.firstChild.src;
 37    var pos = image.lastIndexOf(".");
 38
 39    if(linkSpan.className == "highlight-copycode")
 40    {
 41        linkSpan.className = "highlight-copycode_h";
 42        linkSpan.firstChild.src = image.substr(0, pos) + "_h" +
 43            image.substr(pos);
 44    }
 45    else
 46    {
 47        linkSpan.className = "highlight-copycode";
 48        linkSpan.firstChild.src = image.substr(0, pos - 2) + image.substr(pos);
 49    }
 50}
 51
 52// Copy the code from a colorized code block to the clipboard.
 53function CopyColorizedCode(titleDiv)
 54{
 55    var preTag, idx, line, block, htmlLines, lines, codeText, hasLineNos,
 56        hasRegions, clip, trans, copyObject, clipID;
 57    var reLineNo = /^\s*\d{1,4}/;
 58    var reRegion = /^\s*\d{1,4}\+.*?\d{1,4}-/;
 59    var reRegionText = /^\+.*?\-/;
 60
 61    // Find the <pre> tag containing the code.  It should be in the next
 62    // element or one of its children.
 63    block = titleDiv.nextSibling;
 64
 65    while(block.nodeName == "#text")
 66        block = block.nextSibling;
 67
 68    while(block.tagName != "PRE")
 69    {
 70        block = block.firstChild;
 71
 72        while(block.nodeName == "#text")
 73            block = block.nextSibling;
 74    }
 75
 76    if(block.innerText != undefined)
 77        codeText = block.innerText;
 78    else
 79        codeText = block.textContent;
 80
 81    hasLineNos = block.innerHTML.indexOf("highlight-lineno");
 82    hasRegions = block.innerHTML.indexOf("highlight-collapsebox");
 83    htmlLines = block.innerHTML.split("\n");
 84    lines = codeText.split("\n");
 85
 86    // Remove the line numbering and collapsible regions if present
 87    if(hasLineNos != -1 || hasRegions != -1)
 88    {
 89        codeText = "";
 90
 91        for(idx = 0; idx < lines.length; idx++)
 92        {
 93            line = lines[idx];
 94
 95            if(hasRegions && reRegion.test(line))
 96                line = line.replace(reRegion, "");
 97            else
 98            {
 99                line = line.replace(reLineNo, "");
100
101                // Lines in expanded blocks have an extra space
102                if(htmlLines[idx].indexOf("highlight-expanded") != -1 ||
103                  htmlLines[idx].indexOf("highlight-endblock") != -1)
104                    line = line.substr(1);
105            }
106
107            if(hasRegions && reRegionText.test(line))
108                line = line.replace(reRegionText, "");
109
110            codeText += line;
111
112            // Not all browsers keep the line feed when split
113            if(line[line.length - 1] != "\n")
114                codeText += "\n";
115        }
116    }
117
118    // IE or FireFox/Netscape?
119    if(window.clipboardData)
120        window.clipboardData.setData("Text", codeText);
121    else
122        if(window.netscape)
123        {
124            // Give unrestricted access to browser APIs using XPConnect
125            try
126            {
127                netscape.security.PrivilegeManager.enablePrivilege(
128                    "UniversalXPConnect");
129            }
130            catch(e)
131            {
132                alert("Universal Connect was refused, cannot copy to " +
133                    "clipboard.  Go to about:config and set " +
134                    "signed.applets.codebase_principal_support to true to " +
135                    "enable clipboard support.");
136                return;
137            }
138
139            // Creates an instance of nsIClipboard
140            clip = Components.classes[
141                "@mozilla.org/widget/clipboard;1"].createInstance(
142                Components.interfaces.nsIClipboard);
143
144            // Creates an instance of nsITransferable
145            if(clip)
146                trans = Components.classes[
147                    "@mozilla.org/widget/transferable;1"].createInstance(
148                    Components.interfaces.nsITransferable);
149
150            if(!trans)
151            {
152                alert("Copy to Clipboard is not supported by this browser");
153                return;
154            }
155
156            // Register the data flavor
157            trans.addDataFlavor("text/unicode");
158
159            // Create object to hold the data
160            copyObject = new Object();
161
162            // Creates an instance of nsISupportsString
163            copyObject = Components.classes[
164                "@mozilla.org/supports-string;1"].createInstance(
165                Components.interfaces.nsISupportsString);
166
167            // Assign the data to be copied
168            copyObject.data = codeText;
169
170            // Add data objects to transferable
171            trans.setTransferData("text/unicode", copyObject,
172                codeText.length * 2);
173
174            clipID = Components.interfaces.nsIClipboard;
175
176            if(!clipID)
177            {
178                alert("Copy to Clipboard is not supported by this browser");
179                return;
180            }
181
182            // Transfer the data to the clipboard
183            clip.setData(trans, null, clipID.kGlobalClipboard);
184        }
185        else
186            alert("Copy to Clipboard is not supported by this browser");
187}