PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Documentation/Help/scripts/highlight.js

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