home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 March / PCWorld_2005-03_cd.bin / komunikace / kmeleon / kmeleon09.exe / comm.jar / content / navigator / viewsource.js < prev    next >
Text File  |  2004-08-11  |  17KB  |  559 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is mozilla.org code.
  13.  * 
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are
  16.  * Copyright (C) Netscape Communications Corporation.  All
  17.  * Rights Reserved.
  18.  * 
  19.  * Contributor(s): 
  20.  *    Doron Rosenberg (doronr@naboonline.com) 
  21.  *    Neil Rashbrook (neil@parkwaycc.co.uk)
  22.  */
  23.  
  24. const pageLoaderIface = Components.interfaces.nsIWebPageDescriptor;
  25. const nsISelectionPrivate = Components.interfaces.nsISelectionPrivate;
  26. const nsISelectionController = Components.interfaces.nsISelectionController;
  27. var gBrowser = null;
  28. var gViewSourceBundle = null;
  29. var gPrefs = null;
  30.  
  31. var gLastLineFound = '';
  32. var gGoToLine = 0;
  33.  
  34. try {
  35.   var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  36.                               .getService(Components.interfaces.nsIPrefService);
  37.   gPrefs = prefService.getBranch(null);
  38. } catch (ex) {
  39. }
  40.  
  41. var gSelectionListener = {
  42.   timeout: 0,
  43.   notifySelectionChanged: function(doc, sel, reason)
  44.   {
  45.     // Coalesce notifications within 100ms intervals.
  46.     if (!this.timeout)
  47.       this.timeout = setTimeout(updateStatusBar, 100);
  48.   }
  49. }
  50.  
  51. function onLoadViewSource() 
  52. {
  53.   viewSource(window.arguments[0]);
  54.   document.commandDispatcher.focusedWindow = content;
  55. }
  56.  
  57. function getBrowser()
  58. {
  59.   if (!gBrowser)
  60.     gBrowser = document.getElementById("content");
  61.   return gBrowser;
  62. }
  63.  
  64. function getSelectionController()
  65. {
  66.   return getBrowser().docShell
  67.     .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  68.     .getInterface(Components.interfaces.nsISelectionDisplay)
  69.     .QueryInterface(nsISelectionController);
  70.  
  71. }
  72.  
  73. function getViewSourceBundle()
  74. {
  75.   if (!gViewSourceBundle)
  76.     gViewSourceBundle = document.getElementById("viewSourceBundle");
  77.   return gViewSourceBundle;
  78. }
  79.  
  80. function viewSource(url)
  81. {
  82.   if (!url)
  83.     return false; // throw Components.results.NS_ERROR_FAILURE;
  84.  
  85.   getBrowser().addEventListener("unload", onUnloadContent, true);
  86.   getBrowser().addEventListener("load", onLoadContent, true);
  87.  
  88.   var loadFromURL = true;
  89.   //
  90.   // Parse the 'arguments' supplied with the dialog.
  91.   //    arg[0] - URL string.
  92.   //    arg[1] - Charset value in the form 'charset=xxx'.
  93.   //    arg[2] - Page descriptor used to load content from the cache.
  94.   //    arg[3] - Line number to go to.
  95.   //
  96.   if ("arguments" in window) {
  97.     var arg;
  98.     //
  99.     // Set the charset of the viewsource window...
  100.     //
  101.     if (window.arguments.length >= 2) {
  102.       arg = window.arguments[1];
  103.  
  104.       try {
  105.         if (typeof(arg) == "string" && arg.indexOf('charset=') != -1) {
  106.           var arrayArgComponents = arg.split('=');
  107.           if (arrayArgComponents) {
  108.             //we should "inherit" the charset menu setting in a new window
  109.             getMarkupDocumentViewer().defaultCharacterSet = arrayArgComponents[1];
  110.           } 
  111.         }
  112.       } catch (ex) {
  113.         // Ignore the failure and keep processing arguments...
  114.       }
  115.     }
  116.     //
  117.     // Get any specified line to jump to.
  118.     //
  119.     if (window.arguments.length >= 4) {
  120.       arg = window.arguments[3];
  121.       gGoToLine = parseInt(arg);
  122.     }
  123.     //
  124.     // Use the page descriptor to load the content from the cache (if
  125.     // available).
  126.     //
  127.     if (window.arguments.length >= 3) {
  128.       arg = window.arguments[2];
  129.  
  130.       try {
  131.         if (typeof(arg) == "object" && arg != null) {
  132.           var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  133.  
  134.           //
  135.           // Load the page using the page descriptor rather than the URL.
  136.           // This allows the content to be fetched from the cache (if
  137.           // possible) rather than the network...
  138.           //
  139.           PageLoader.LoadPage(arg, pageLoaderIface.DISPLAY_AS_SOURCE);
  140.           // The content was successfully loaded from the page cookie.
  141.           loadFromURL = false;
  142.         }
  143.       } catch(ex) {
  144.         // Ignore the failure.  The content will be loaded via the URL
  145.         // that was supplied in arg[0].
  146.       }
  147.     }
  148.   }
  149.  
  150.   if (loadFromURL) {
  151.     //
  152.     // Currently, an exception is thrown if the URL load fails...
  153.     //
  154.     var loadFlags = Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE;
  155.     var viewSrcUrl = "view-source:" + url;
  156.     getBrowser().webNavigation.loadURI(viewSrcUrl, loadFlags, null, null, null);
  157.   }
  158.  
  159.   //check the view_source.wrap_long_lines pref and set the menuitem's checked attribute accordingly
  160.   if (gPrefs) {
  161.     try {
  162.       var wraplonglinesPrefValue = gPrefs.getBoolPref("view_source.wrap_long_lines");
  163.  
  164.       if (wraplonglinesPrefValue)
  165.         document.getElementById('menu_wrapLongLines').setAttribute("checked", "true");
  166.     } catch (ex) {
  167.     }
  168.     try {
  169.       document.getElementById("menu_highlightSyntax").setAttribute("checked", gPrefs.getBoolPref("view_source.syntax_highlight"));
  170.     } catch (ex) {
  171.     }
  172.   } else {
  173.     document.getElementById("menu_highlightSyntax").setAttribute("hidden", "true");
  174.   }
  175.  
  176.   window._content.focus();
  177.  
  178.   return true;
  179. }
  180.  
  181. function onLoadContent()
  182. {
  183.   //
  184.   // If the view source was opened with a "go to line" argument.
  185.   //
  186.   if (gGoToLine > 0) {
  187.     goToLine(gGoToLine);
  188.     gGoToLine = 0;
  189.   }
  190.   document.getElementById('cmd_goToLine').removeAttribute('disabled');
  191.  
  192.   // Register a listener so that we can show the caret position on the status bar.
  193.   window._content.getSelection()
  194.    .QueryInterface(nsISelectionPrivate)
  195.    .addSelectionListener(gSelectionListener);
  196. }
  197.  
  198. function onUnloadContent()
  199. {
  200.   //
  201.   // Disable "go to line" while reloading due to e.g. change of charset
  202.   // or toggling of syntax highlighting.
  203.   //
  204.   document.getElementById('cmd_goToLine').setAttribute('disabled', 'true');
  205. }
  206.  
  207. function ViewSourceClose()
  208. {
  209.   window.close();
  210. }
  211.  
  212. function BrowserReload()
  213. {
  214.   // Reload will always reload from cache which is probably not what's wanted
  215.   BrowserReloadSkipCache();
  216. }
  217.  
  218. function BrowserReloadSkipCache()
  219. {
  220.   const webNavigation = getBrowser().webNavigation;
  221.   webNavigation.reload(webNavigation.LOAD_FLAGS_BYPASS_PROXY | webNavigation.LOAD_FLAGS_BYPASS_CACHE);
  222. }
  223.  
  224. // Strips the |view-source:| for editPage()
  225. function ViewSourceEditPage()
  226. {
  227.   editPage(window.content.location.href.substring(12), window, false);
  228. }
  229.  
  230. // Strips the |view-source:| for saveURL()
  231. function ViewSourceSavePage()
  232. {
  233.   saveURL(window.content.location.href.substring(12), null, "SaveLinkTitle");
  234. }
  235.  
  236. function ViewSourceGoToLine()
  237. {
  238.   var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  239.         .getService(Components.interfaces.nsIPromptService);
  240.   var viewSourceBundle = getViewSourceBundle();
  241.  
  242.   var input = {value:gLastLineFound};
  243.   for (;;) {
  244.     var ok = promptService.prompt(
  245.         window,
  246.         viewSourceBundle.getString("goToLineTitle"),
  247.         viewSourceBundle.getString("goToLineText"),
  248.         input,
  249.         null,
  250.         {value:0});
  251.  
  252.     if (!ok) return;
  253.  
  254.     var line = parseInt(input.value);
  255.  
  256.     if (!(line > 0)) {
  257.       promptService.alert(window,
  258.           viewSourceBundle.getString("invalidInputTitle"),
  259.           viewSourceBundle.getString("invalidInputText"));
  260.   
  261.       continue;
  262.     }
  263.  
  264.     var found = goToLine(line);
  265.  
  266.     if (found) {
  267.       break;
  268.     }
  269.  
  270.     promptService.alert(window,
  271.         viewSourceBundle.getString("outOfRangeTitle"),
  272.         viewSourceBundle.getString("outOfRangeText"));
  273.   }
  274. }
  275.  
  276. function goToLine(line)
  277. {
  278.   var viewsource = window._content.document.body;
  279.  
  280.   //
  281.   // The source document is made up of a number of pre elements with
  282.   // id attributes in the format <pre id="line123">, meaning that
  283.   // the first line in the pre element is number 123.
  284.   // Do binary search to find the pre element containing the line.
  285.   //
  286.   var pre;
  287.   for (var lbound = 0, ubound = viewsource.childNodes.length; ; ) {
  288.     var middle = (lbound + ubound) >> 1;
  289.     pre = viewsource.childNodes[middle];
  290.  
  291.     var firstLine = parseInt(pre.id.substring(4));
  292.  
  293.     if (lbound == ubound - 1) {
  294.       break;
  295.     }
  296.  
  297.     if (line >= firstLine) {
  298.       lbound = middle;
  299.     } else {
  300.       ubound = middle;
  301.     }
  302.   }
  303.  
  304.   var result = {};
  305.   var found = findLocation(pre, line, null, -1, false, result);
  306.  
  307.   if (!found) {
  308.     return false;
  309.   }
  310.  
  311.   var selection = window._content.getSelection();
  312.   selection.removeAllRanges();
  313.  
  314.   // In our case, the range's startOffset is after "\n" on the previous line.
  315.   // Tune the selection at the beginning of the next line and do some tweaking
  316.   // to position the focusNode and the caret at the beginning of the line.
  317.  
  318.   selection.QueryInterface(nsISelectionPrivate)
  319.     .interlinePosition = true;    
  320.  
  321.   selection.addRange(result.range);
  322.  
  323.   if (!selection.isCollapsed) {
  324.     selection.collapseToEnd();
  325.  
  326.     var offset = result.range.startOffset;
  327.     var node = result.range.startContainer;
  328.     if (offset < node.data.length) {
  329.       // The same text node spans across the "\n", just focus where we were.
  330.       selection.extend(node, offset);
  331.     }
  332.     else {
  333.       // There is another tag just after the "\n", hook there. We need
  334.       // to focus a safe point because there are edgy cases such as
  335.       // <span>...\n</span><span>...</span> vs.
  336.       // <span>...\n<span>...</span></span><span>...</span>
  337.       node = node.nextSibling ? node.nextSibling : node.parentNode.nextSibling;
  338.       selection.extend(node, 0);
  339.     }
  340.   }
  341.  
  342.   var selCon = getSelectionController();
  343.   selCon.setDisplaySelection(nsISelectionController.SELECTION_ON);
  344.   selCon.setCaretEnabled(true);
  345.   selCon.setCaretVisibilityDuringSelection(true);
  346.  
  347.   // Scroll the beginning of the line into view.
  348.   selCon.scrollSelectionIntoView(
  349.     nsISelectionController.SELECTION_NORMAL,
  350.     nsISelectionController.SELECTION_FOCUS_REGION,
  351.     true);
  352.  
  353.   gLastLineFound = line;
  354.  
  355.   document.getElementById("statusbar-line-col").label = getViewSourceBundle()
  356.       .getFormattedString("statusBarLineCol", [line, 1]);
  357.  
  358.   return true;
  359. }
  360.  
  361. function updateStatusBar()
  362. {
  363.   // Reset the coalesce flag.
  364.   gSelectionListener.timeout = 0;
  365.  
  366.   var statusBarField = document.getElementById("statusbar-line-col");
  367.  
  368.   var selection = window._content.getSelection();
  369.   if (!selection.focusNode) {
  370.     statusBarField.label = '';
  371.     return;
  372.   }
  373.   if (selection.focusNode.nodeType != Node.TEXT_NODE) {
  374.     return;
  375.   }
  376.  
  377.   var selCon = getSelectionController();
  378.   selCon.setDisplaySelection(nsISelectionController.SELECTION_ON);
  379.   selCon.setCaretEnabled(true);
  380.   selCon.setCaretVisibilityDuringSelection(true);
  381.  
  382.   var interlinePosition = selection
  383.       .QueryInterface(nsISelectionPrivate).interlinePosition;
  384.  
  385.   var result = {};
  386.   findLocation(null, -1, 
  387.       selection.focusNode, selection.focusOffset, interlinePosition, result);
  388.  
  389.   statusBarField.label = getViewSourceBundle()
  390.       .getFormattedString("statusBarLineCol", [result.line, result.col]);
  391. }
  392.  
  393. //
  394. // Loops through the text lines in the pre element. The arguments are either
  395. // (pre, line) or (node, offset, interlinePosition). result is an out
  396. // argument. If (pre, line) are specified (and node == null), result.range is
  397. // a range spanning the specified line. If the (node, offset,
  398. // interlinePosition) are specified, result.line and result.col are the line
  399. // and column number of the specified offset in the specified node relative to
  400. // the whole file.
  401. //
  402. function findLocation(pre, line, node, offset, interlinePosition, result)
  403. {
  404.   if (node && !pre) {
  405.     //
  406.     // Look upwards to find the current pre element.
  407.     //
  408.     for (pre = node;
  409.          pre.nodeName != "PRE";
  410.          pre = pre.parentNode);
  411.   }
  412.  
  413.   //
  414.   // The source document is made up of a number of pre elements with
  415.   // id attributes in the format <pre id="line123">, meaning that
  416.   // the first line in the pre element is number 123.
  417.   //
  418.   var curLine = parseInt(pre.id.substring(4));
  419.  
  420.   //
  421.   // Walk through each of the text nodes and count newlines.
  422.   //
  423.   var treewalker = window._content.document
  424.       .createTreeWalker(pre, NodeFilter.SHOW_TEXT, null, false);
  425.  
  426.   //
  427.   // The column number of the first character in the current text node.
  428.   //
  429.   var firstCol = 1;
  430.  
  431.   var found = false;
  432.   for (var textNode = treewalker.firstChild();
  433.        textNode && !found;
  434.        textNode = treewalker.nextNode()) {
  435.  
  436.     //
  437.     // \r is not a valid character in the DOM, so we only check for \n.
  438.     //
  439.     var lineArray = textNode.data.split(/\n/);
  440.     var lastLineInNode = curLine + lineArray.length - 1;
  441.  
  442.     //
  443.     // Check if we can skip the text node without further inspection.
  444.     //
  445.     if (node ? (textNode != node) : (lastLineInNode < line)) {
  446.       if (lineArray.length > 1) {
  447.         firstCol = 1;
  448.       }
  449.       firstCol += lineArray[lineArray.length - 1].length;
  450.       curLine = lastLineInNode;
  451.       continue;
  452.     }
  453.  
  454.     //
  455.     // curPos is the offset within the current text node of the first
  456.     // character in the current line.
  457.     //
  458.     for (var i = 0, curPos = 0;
  459.          i < lineArray.length;
  460.          curPos += lineArray[i++].length + 1) {
  461.  
  462.       if (i > 0) {
  463.         curLine++;
  464.       }
  465.  
  466.       if (node) {
  467.         if (offset >= curPos && offset <= curPos + lineArray[i].length) {
  468.           //
  469.           // If we are right after the \n of a line and interlinePosition is
  470.           // false, the caret looks as if it were at the end of the previous
  471.           // line, so we display that line and column instead.
  472.           //
  473.           if (i > 0 && offset == curPos && !interlinePosition) {
  474.             result.line = curLine - 1;
  475.             var prevPos = curPos - lineArray[i - 1].length;
  476.             result.col = (i == 1 ? firstCol : 1) + offset - prevPos;
  477.  
  478.           } else {
  479.             result.line = curLine;
  480.             result.col = (i == 0 ? firstCol : 1) + offset - curPos;
  481.           }
  482.           found = true;
  483.  
  484.           break;
  485.         }
  486.  
  487.       } else {
  488.         if (curLine == line && !("range" in result)) {
  489.           result.range = document.createRange();
  490.           result.range.setStart(textNode, curPos);
  491.  
  492.           //
  493.           // This will always be overridden later, except when we look for
  494.           // the very last line in the file (this is the only line that does
  495.           // not end with \n).
  496.           //
  497.           result.range.setEndAfter(pre.lastChild);
  498.  
  499.         } else if (curLine == line + 1) {
  500.           result.range.setEnd(textNode, curPos - 1);
  501.           found = true;
  502.           break;
  503.         }
  504.       }
  505.     }
  506.   }
  507.  
  508.   return found || ("range" in result);
  509. }
  510.  
  511. //function to toggle long-line wrapping and set the view_source.wrap_long_lines 
  512. //pref to persist the last state
  513. function wrapLongLines()
  514. {
  515.   var myWrap = window._content.document.body;
  516.  
  517.   if (myWrap.className == '')
  518.     myWrap.className = 'wrap';
  519.   else myWrap.className = '';
  520.  
  521.   //since multiple viewsource windows are possible, another window could have 
  522.   //affected the pref, so instead of determining the new pref value via the current
  523.   //pref value, we use myWrap.className  
  524.   if (gPrefs){
  525.     try {
  526.       if (myWrap.className == '') {
  527.         gPrefs.setBoolPref("view_source.wrap_long_lines", false);
  528.       }
  529.       else {
  530.         gPrefs.setBoolPref("view_source.wrap_long_lines", true);
  531.       }
  532.     } catch (ex) {
  533.     }
  534.   }
  535. }
  536.  
  537. //function to toggle syntax highlighting and set the view_source.syntax_highlight
  538. //pref to persist the last state
  539. function highlightSyntax()
  540. {
  541.   var highlightSyntaxMenu = document.getElementById("menu_highlightSyntax");
  542.   var highlightSyntax = (highlightSyntaxMenu.getAttribute("checked") == "true");
  543.   gPrefs.setBoolPref("view_source.syntax_highlight", highlightSyntax);
  544.  
  545.   var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  546.   PageLoader.LoadPage(PageLoader.currentDescriptor, pageLoaderIface.DISPLAY_NORMAL);
  547. }
  548.  
  549. // Fix for bug 136322: this function overrides the function in
  550. // browser.js to call PageLoader.LoadPage() instead of BrowserReloadWithFlags()
  551. function BrowserSetForcedCharacterSet(aCharset)
  552. {
  553.   var docCharset = getBrowser().docShell.QueryInterface(
  554.                             Components.interfaces.nsIDocCharset);
  555.   docCharset.charset = aCharset;
  556.   var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  557.   PageLoader.LoadPage(PageLoader.currentDescriptor, pageLoaderIface.DISPLAY_NORMAL);
  558. }
  559.