michael@0: // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: Components.utils.import("resource://gre/modules/CharsetMenu.jsm"); michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: var gLastLineFound = ''; michael@0: var gGoToLine = 0; michael@0: michael@0: [ michael@0: ["gBrowser", "content"], michael@0: ["gViewSourceBundle", "viewSourceBundle"], michael@0: ["gContextMenu", "viewSourceContextMenu"] michael@0: ].forEach(function ([name, id]) { michael@0: window.__defineGetter__(name, function () { michael@0: var element = document.getElementById(id); michael@0: if (!element) michael@0: return null; michael@0: delete window[name]; michael@0: return window[name] = element; michael@0: }); michael@0: }); michael@0: michael@0: // viewZoomOverlay.js uses this michael@0: function getBrowser() { michael@0: return gBrowser; michael@0: } michael@0: michael@0: this.__defineGetter__("gPageLoader", function () { michael@0: var webnav = getWebNavigation(); michael@0: if (!webnav) michael@0: return null; michael@0: delete this.gPageLoader; michael@0: return this.gPageLoader = webnav.QueryInterface(Ci.nsIWebPageDescriptor); michael@0: }); michael@0: michael@0: var gSelectionListener = { michael@0: timeout: 0, michael@0: attached: false, michael@0: notifySelectionChanged: function(doc, sel, reason) michael@0: { michael@0: // Coalesce notifications within 100ms intervals. michael@0: if (!this.timeout) michael@0: this.timeout = setTimeout(updateStatusBar, 100); michael@0: } michael@0: } michael@0: michael@0: function onLoadViewSource() michael@0: { michael@0: viewSource(window.arguments[0]); michael@0: document.commandDispatcher.focusedWindow = content; michael@0: gBrowser.droppedLinkHandler = function (event, url, name) { michael@0: viewSource(url) michael@0: event.preventDefault(); michael@0: } michael@0: michael@0: if (!isHistoryEnabled()) { michael@0: // Disable the BACK and FORWARD commands and hide the related menu items. michael@0: var viewSourceNavigation = document.getElementById("viewSourceNavigation"); michael@0: viewSourceNavigation.setAttribute("disabled", "true"); michael@0: viewSourceNavigation.setAttribute("hidden", "true"); michael@0: } michael@0: } michael@0: michael@0: function isHistoryEnabled() { michael@0: return !gBrowser.hasAttribute("disablehistory"); michael@0: } michael@0: michael@0: function getSelectionController() { michael@0: return gBrowser.docShell michael@0: .QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsISelectionDisplay) michael@0: .QueryInterface(Ci.nsISelectionController); michael@0: } michael@0: michael@0: function viewSource(url) michael@0: { michael@0: if (!url) michael@0: return; // throw Components.results.NS_ERROR_FAILURE; michael@0: michael@0: var viewSrcUrl = "view-source:" + url; michael@0: michael@0: gBrowser.addEventListener("pagehide", onUnloadContent, true); michael@0: gBrowser.addEventListener("pageshow", onLoadContent, true); michael@0: gBrowser.addEventListener("click", onClickContent, false); michael@0: michael@0: var loadFromURL = true; michael@0: michael@0: // Parse the 'arguments' supplied with the dialog. michael@0: // arg[0] - URL string. michael@0: // arg[1] - Charset value in the form 'charset=xxx'. michael@0: // arg[2] - Page descriptor used to load content from the cache. michael@0: // arg[3] - Line number to go to. michael@0: // arg[4] - Whether charset was forced by the user michael@0: michael@0: if ("arguments" in window) { michael@0: var arg; michael@0: michael@0: // Set the charset of the viewsource window... michael@0: var charset; michael@0: if (window.arguments.length >= 2) { michael@0: arg = window.arguments[1]; michael@0: michael@0: try { michael@0: if (typeof(arg) == "string" && arg.indexOf('charset=') != -1) { michael@0: var arrayArgComponents = arg.split('='); michael@0: if (arrayArgComponents) { michael@0: // Remember the charset here so that it can be used below in case michael@0: // the document had a forced charset. michael@0: charset = arrayArgComponents[1]; michael@0: } michael@0: } michael@0: } catch (ex) { michael@0: // Ignore the failure and keep processing arguments... michael@0: } michael@0: } michael@0: // If the document had a forced charset, set it here also michael@0: if (window.arguments.length >= 5) { michael@0: arg = window.arguments[4]; michael@0: michael@0: try { michael@0: if (arg === true) { michael@0: gBrowser.docShell.charset = charset; michael@0: } michael@0: } catch (ex) { michael@0: // Ignore the failure and keep processing arguments... michael@0: } michael@0: } michael@0: michael@0: // Get any specified line to jump to. michael@0: if (window.arguments.length >= 4) { michael@0: arg = window.arguments[3]; michael@0: gGoToLine = parseInt(arg); michael@0: } michael@0: michael@0: // Use the page descriptor to load the content from the cache (if michael@0: // available). michael@0: if (window.arguments.length >= 3) { michael@0: arg = window.arguments[2]; michael@0: michael@0: try { michael@0: if (typeof(arg) == "object" && arg != null) { michael@0: // Load the page using the page descriptor rather than the URL. michael@0: // This allows the content to be fetched from the cache (if michael@0: // possible) rather than the network... michael@0: gPageLoader.loadPage(arg, gPageLoader.DISPLAY_AS_SOURCE); michael@0: michael@0: // The content was successfully loaded. michael@0: loadFromURL = false; michael@0: michael@0: // Record the page load in the session history so will work. michael@0: var shEntrySource = arg.QueryInterface(Ci.nsISHEntry); michael@0: var shEntry = Cc["@mozilla.org/browser/session-history-entry;1"].createInstance(Ci.nsISHEntry); michael@0: shEntry.setURI(makeURI(viewSrcUrl, null, null)); michael@0: shEntry.setTitle(viewSrcUrl); michael@0: shEntry.loadType = Ci.nsIDocShellLoadInfo.loadHistory; michael@0: shEntry.cacheKey = shEntrySource.cacheKey; michael@0: gBrowser.sessionHistory michael@0: .QueryInterface(Ci.nsISHistoryInternal) michael@0: .addEntry(shEntry, true); michael@0: } michael@0: } catch(ex) { michael@0: // Ignore the failure. The content will be loaded via the URL michael@0: // that was supplied in arg[0]. michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (loadFromURL) { michael@0: // Currently, an exception is thrown if the URL load fails... michael@0: var loadFlags = Ci.nsIWebNavigation.LOAD_FLAGS_NONE; michael@0: getWebNavigation().loadURI(viewSrcUrl, loadFlags, null, null, null); michael@0: } michael@0: michael@0: // Check the view_source.wrap_long_lines pref and set the menuitem's checked michael@0: // attribute accordingly. michael@0: var wraplonglinesPrefValue = Services.prefs.getBoolPref("view_source.wrap_long_lines"); michael@0: michael@0: if (wraplonglinesPrefValue) michael@0: document.getElementById("menu_wrapLongLines").setAttribute("checked", "true"); michael@0: michael@0: document.getElementById("menu_highlightSyntax") michael@0: .setAttribute("checked", michael@0: Services.prefs.getBoolPref("view_source.syntax_highlight")); michael@0: michael@0: window.addEventListener("AppCommand", HandleAppCommandEvent, true); michael@0: window.addEventListener("MozSwipeGesture", HandleSwipeGesture, true); michael@0: window.content.focus(); michael@0: } michael@0: michael@0: function onLoadContent() michael@0: { michael@0: // If the view source was opened with a "go to line" argument. michael@0: if (gGoToLine > 0) { michael@0: goToLine(gGoToLine); michael@0: gGoToLine = 0; michael@0: } michael@0: document.getElementById('cmd_goToLine').removeAttribute('disabled'); michael@0: michael@0: // Register a listener so that we can show the caret position on the status bar. michael@0: window.content.getSelection() michael@0: .QueryInterface(Ci.nsISelectionPrivate) michael@0: .addSelectionListener(gSelectionListener); michael@0: gSelectionListener.attached = true; michael@0: michael@0: if (isHistoryEnabled()) michael@0: UpdateBackForwardCommands(); michael@0: } michael@0: michael@0: function onUnloadContent() michael@0: { michael@0: // Disable "go to line" while reloading due to e.g. change of charset michael@0: // or toggling of syntax highlighting. michael@0: document.getElementById('cmd_goToLine').setAttribute('disabled', 'true'); michael@0: michael@0: // If we're not just unloading the initial "about:blank" which doesn't have michael@0: // a selection listener, get rid of it so it doesn't try to fire after the michael@0: // window has gone away. michael@0: if (gSelectionListener.attached) { michael@0: window.content.getSelection().QueryInterface(Ci.nsISelectionPrivate) michael@0: .removeSelectionListener(gSelectionListener); michael@0: gSelectionListener.attached = false; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Handle click events bubbling up from error page content michael@0: */ michael@0: function onClickContent(event) { michael@0: // Don't trust synthetic events michael@0: if (!event.isTrusted || event.target.localName != "button") michael@0: return; michael@0: michael@0: var target = event.originalTarget; michael@0: var errorDoc = target.ownerDocument; michael@0: michael@0: if (/^about:blocked/.test(errorDoc.documentURI)) { michael@0: // The event came from a button on a malware/phishing block page michael@0: // First check whether it's malware or phishing, so that we can michael@0: // use the right strings/links michael@0: var isMalware = /e=malwareBlocked/.test(errorDoc.documentURI); michael@0: michael@0: if (target == errorDoc.getElementById('getMeOutButton')) { michael@0: // Instead of loading some safe page, just close the window michael@0: window.close(); michael@0: } else if (target == errorDoc.getElementById('reportButton')) { michael@0: // This is the "Why is this site blocked" button. For malware, michael@0: // we can fetch a site-specific report, for phishing, we redirect michael@0: // to the generic page describing phishing protection. michael@0: michael@0: if (isMalware) { michael@0: // Get the stop badware "why is this blocked" report url, michael@0: // append the current url, and go there. michael@0: try { michael@0: let reportURL = Services.urlFormatter.formatURLPref("browser.safebrowsing.malware.reportURL", true); michael@0: reportURL += errorDoc.location.href.slice(12); michael@0: openURL(reportURL); michael@0: } catch (e) { michael@0: Components.utils.reportError("Couldn't get malware report URL: " + e); michael@0: } michael@0: } else { michael@0: // It's a phishing site, just link to the generic information page michael@0: let url = Services.urlFormatter.formatURLPref("app.support.baseURL"); michael@0: openURL(url + "phishing-malware"); michael@0: } michael@0: } else if (target == errorDoc.getElementById('ignoreWarningButton')) { michael@0: // Allow users to override and continue through to the site michael@0: gBrowser.loadURIWithFlags(content.location.href, michael@0: Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CLASSIFIER, michael@0: null, null, null); michael@0: } michael@0: } michael@0: } michael@0: michael@0: function HandleAppCommandEvent(evt) michael@0: { michael@0: evt.stopPropagation(); michael@0: switch (evt.command) { michael@0: case "Back": michael@0: BrowserBack(); michael@0: break; michael@0: case "Forward": michael@0: BrowserForward(); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: function HandleSwipeGesture(evt) { michael@0: evt.stopPropagation(); michael@0: switch (evt.direction) { michael@0: case SimpleGestureEvent.DIRECTION_LEFT: michael@0: BrowserBack(); michael@0: break; michael@0: case SimpleGestureEvent.DIRECTION_RIGHT: michael@0: BrowserForward(); michael@0: break; michael@0: case SimpleGestureEvent.DIRECTION_UP: michael@0: goDoCommand("cmd_scrollTop"); michael@0: break; michael@0: case SimpleGestureEvent.DIRECTION_DOWN: michael@0: goDoCommand("cmd_scrollBottom"); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: function ViewSourceClose() michael@0: { michael@0: window.close(); michael@0: } michael@0: michael@0: function ViewSourceReload() michael@0: { michael@0: gBrowser.reloadWithFlags(Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY | michael@0: Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE); michael@0: } michael@0: michael@0: // Strips the |view-source:| for internalSave() michael@0: function ViewSourceSavePage() michael@0: { michael@0: internalSave(window.content.location.href.substring(12), michael@0: null, null, null, null, null, "SaveLinkTitle", michael@0: null, null, window.content.document, null, gPageLoader); michael@0: } michael@0: michael@0: var PrintPreviewListener = { michael@0: getPrintPreviewBrowser: function () { michael@0: var browser = document.getElementById("ppBrowser"); michael@0: if (!browser) { michael@0: browser = document.createElement("browser"); michael@0: browser.setAttribute("id", "ppBrowser"); michael@0: browser.setAttribute("flex", "1"); michael@0: document.getElementById("appcontent"). michael@0: insertBefore(browser, document.getElementById("FindToolbar")); michael@0: } michael@0: return browser; michael@0: }, michael@0: getSourceBrowser: function () { michael@0: return gBrowser; michael@0: }, michael@0: getNavToolbox: function () { michael@0: return document.getElementById("appcontent"); michael@0: }, michael@0: onEnter: function () { michael@0: var toolbox = document.getElementById("viewSource-toolbox"); michael@0: toolbox.hidden = true; michael@0: gBrowser.collapsed = true; michael@0: }, michael@0: onExit: function () { michael@0: document.getElementById("ppBrowser").collapsed = true; michael@0: gBrowser.collapsed = false; michael@0: document.getElementById("viewSource-toolbox").hidden = false; michael@0: } michael@0: } michael@0: michael@0: function getWebNavigation() michael@0: { michael@0: try { michael@0: return gBrowser.webNavigation; michael@0: } catch (e) { michael@0: return null; michael@0: } michael@0: } michael@0: michael@0: function ViewSourceGoToLine() michael@0: { michael@0: var input = {value:gLastLineFound}; michael@0: for (;;) { michael@0: var ok = Services.prompt.prompt( michael@0: window, michael@0: gViewSourceBundle.getString("goToLineTitle"), michael@0: gViewSourceBundle.getString("goToLineText"), michael@0: input, michael@0: null, michael@0: {value:0}); michael@0: michael@0: if (!ok) michael@0: return; michael@0: michael@0: var line = parseInt(input.value, 10); michael@0: michael@0: if (!(line > 0)) { michael@0: Services.prompt.alert(window, michael@0: gViewSourceBundle.getString("invalidInputTitle"), michael@0: gViewSourceBundle.getString("invalidInputText")); michael@0: michael@0: continue; michael@0: } michael@0: michael@0: var found = goToLine(line); michael@0: michael@0: if (found) michael@0: break; michael@0: michael@0: Services.prompt.alert(window, michael@0: gViewSourceBundle.getString("outOfRangeTitle"), michael@0: gViewSourceBundle.getString("outOfRangeText")); michael@0: } michael@0: } michael@0: michael@0: function goToLine(line) michael@0: { michael@0: var viewsource = window.content.document.body; michael@0: michael@0: // The source document is made up of a number of pre elements with michael@0: // id attributes in the format
, meaning that
michael@0:   // the first line in the pre element is number 123.
michael@0:   // Do binary search to find the pre element containing the line.
michael@0:   // However, in the plain text case, we have only one pre without an
michael@0:   // attribute, so assume it begins on line 1.
michael@0: 
michael@0:   var pre;
michael@0:   for (var lbound = 0, ubound = viewsource.childNodes.length; ; ) {
michael@0:     var middle = (lbound + ubound) >> 1;
michael@0:     pre = viewsource.childNodes[middle];
michael@0: 
michael@0:     var firstLine = pre.id ? parseInt(pre.id.substring(4)) : 1;
michael@0: 
michael@0:     if (lbound == ubound - 1) {
michael@0:       break;
michael@0:     }
michael@0: 
michael@0:     if (line >= firstLine) {
michael@0:       lbound = middle;
michael@0:     } else {
michael@0:       ubound = middle;
michael@0:     }
michael@0:   }
michael@0: 
michael@0:   var result = {};
michael@0:   var found = findLocation(pre, line, null, -1, false, result);
michael@0: 
michael@0:   if (!found) {
michael@0:     return false;
michael@0:   }
michael@0: 
michael@0:   var selection = window.content.getSelection();
michael@0:   selection.removeAllRanges();
michael@0: 
michael@0:   // In our case, the range's startOffset is after "\n" on the previous line.
michael@0:   // Tune the selection at the beginning of the next line and do some tweaking
michael@0:   // to position the focusNode and the caret at the beginning of the line.
michael@0: 
michael@0:   selection.QueryInterface(Ci.nsISelectionPrivate)
michael@0:     .interlinePosition = true;
michael@0: 
michael@0:   selection.addRange(result.range);
michael@0: 
michael@0:   if (!selection.isCollapsed) {
michael@0:     selection.collapseToEnd();
michael@0: 
michael@0:     var offset = result.range.startOffset;
michael@0:     var node = result.range.startContainer;
michael@0:     if (offset < node.data.length) {
michael@0:       // The same text node spans across the "\n", just focus where we were.
michael@0:       selection.extend(node, offset);
michael@0:     }
michael@0:     else {
michael@0:       // There is another tag just after the "\n", hook there. We need
michael@0:       // to focus a safe point because there are edgy cases such as
michael@0:       // ...\n... vs.
michael@0:       // ...\n......
michael@0:       node = node.nextSibling ? node.nextSibling : node.parentNode.nextSibling;
michael@0:       selection.extend(node, 0);
michael@0:     }
michael@0:   }
michael@0: 
michael@0:   var selCon = getSelectionController();
michael@0:   selCon.setDisplaySelection(Ci.nsISelectionController.SELECTION_ON);
michael@0:   selCon.setCaretVisibilityDuringSelection(true);
michael@0: 
michael@0:   // Scroll the beginning of the line into view.
michael@0:   selCon.scrollSelectionIntoView(
michael@0:     Ci.nsISelectionController.SELECTION_NORMAL,
michael@0:     Ci.nsISelectionController.SELECTION_FOCUS_REGION,
michael@0:     true);
michael@0: 
michael@0:   gLastLineFound = line;
michael@0: 
michael@0:   document.getElementById("statusbar-line-col").label =
michael@0:     gViewSourceBundle.getFormattedString("statusBarLineCol", [line, 1]);
michael@0: 
michael@0:   return true;
michael@0: }
michael@0: 
michael@0: function updateStatusBar()
michael@0: {
michael@0:   // Reset the coalesce flag.
michael@0:   gSelectionListener.timeout = 0;
michael@0: 
michael@0:   var statusBarField = document.getElementById("statusbar-line-col");
michael@0: 
michael@0:   var selection = window.content.getSelection();
michael@0:   if (!selection.focusNode) {
michael@0:     statusBarField.label = '';
michael@0:     return;
michael@0:   }
michael@0:   if (selection.focusNode.nodeType != Node.TEXT_NODE) {
michael@0:     return;
michael@0:   }
michael@0: 
michael@0:   var selCon = getSelectionController();
michael@0:   selCon.setDisplaySelection(Ci.nsISelectionController.SELECTION_ON);
michael@0:   selCon.setCaretVisibilityDuringSelection(true);
michael@0: 
michael@0:   var interlinePosition = selection.QueryInterface(Ci.nsISelectionPrivate)
michael@0:                                    .interlinePosition;
michael@0: 
michael@0:   var result = {};
michael@0:   findLocation(null, -1, 
michael@0:       selection.focusNode, selection.focusOffset, interlinePosition, result);
michael@0: 
michael@0:   statusBarField.label = gViewSourceBundle.getFormattedString(
michael@0:                            "statusBarLineCol", [result.line, result.col]);
michael@0: }
michael@0: 
michael@0: // Loops through the text lines in the pre element. The arguments are either
michael@0: // (pre, line) or (node, offset, interlinePosition). result is an out
michael@0: // argument. If (pre, line) are specified (and node == null), result.range is
michael@0: // a range spanning the specified line. If the (node, offset,
michael@0: // interlinePosition) are specified, result.line and result.col are the line
michael@0: // and column number of the specified offset in the specified node relative to
michael@0: // the whole file.
michael@0: function findLocation(pre, line, node, offset, interlinePosition, result)
michael@0: {
michael@0:   if (node && !pre) {
michael@0:     // Look upwards to find the current pre element.
michael@0:     for (pre = node;
michael@0:          pre.nodeName != "PRE";
michael@0:          pre = pre.parentNode);
michael@0:   }
michael@0: 
michael@0:   // The source document is made up of a number of pre elements with
michael@0:   // id attributes in the format 
, meaning that
michael@0:   // the first line in the pre element is number 123.
michael@0:   // However, in the plain text case, there is only one 
 without an id,
michael@0:   // so assume line 1.
michael@0:   var curLine = pre.id ? parseInt(pre.id.substring(4)) : 1;
michael@0: 
michael@0:   // Walk through each of the text nodes and count newlines.
michael@0:   var treewalker = window.content.document
michael@0:       .createTreeWalker(pre, NodeFilter.SHOW_TEXT, null);
michael@0: 
michael@0:   // The column number of the first character in the current text node.
michael@0:   var firstCol = 1;
michael@0: 
michael@0:   var found = false;
michael@0:   for (var textNode = treewalker.firstChild();
michael@0:        textNode && !found;
michael@0:        textNode = treewalker.nextNode()) {
michael@0: 
michael@0:     // \r is not a valid character in the DOM, so we only check for \n.
michael@0:     var lineArray = textNode.data.split(/\n/);
michael@0:     var lastLineInNode = curLine + lineArray.length - 1;
michael@0: 
michael@0:     // Check if we can skip the text node without further inspection.
michael@0:     if (node ? (textNode != node) : (lastLineInNode < line)) {
michael@0:       if (lineArray.length > 1) {
michael@0:         firstCol = 1;
michael@0:       }
michael@0:       firstCol += lineArray[lineArray.length - 1].length;
michael@0:       curLine = lastLineInNode;
michael@0:       continue;
michael@0:     }
michael@0: 
michael@0:     // curPos is the offset within the current text node of the first
michael@0:     // character in the current line.
michael@0:     for (var i = 0, curPos = 0;
michael@0:          i < lineArray.length;
michael@0:          curPos += lineArray[i++].length + 1) {
michael@0: 
michael@0:       if (i > 0) {
michael@0:         curLine++;
michael@0:       }
michael@0: 
michael@0:       if (node) {
michael@0:         if (offset >= curPos && offset <= curPos + lineArray[i].length) {
michael@0:           // If we are right after the \n of a line and interlinePosition is
michael@0:           // false, the caret looks as if it were at the end of the previous
michael@0:           // line, so we display that line and column instead.
michael@0: 
michael@0:           if (i > 0 && offset == curPos && !interlinePosition) {
michael@0:             result.line = curLine - 1;
michael@0:             var prevPos = curPos - lineArray[i - 1].length;
michael@0:             result.col = (i == 1 ? firstCol : 1) + offset - prevPos;
michael@0:           } else {
michael@0:             result.line = curLine;
michael@0:             result.col = (i == 0 ? firstCol : 1) + offset - curPos;
michael@0:           }
michael@0:           found = true;
michael@0: 
michael@0:           break;
michael@0:         }
michael@0: 
michael@0:       } else {
michael@0:         if (curLine == line && !("range" in result)) {
michael@0:           result.range = document.createRange();
michael@0:           result.range.setStart(textNode, curPos);
michael@0: 
michael@0:           // This will always be overridden later, except when we look for
michael@0:           // the very last line in the file (this is the only line that does
michael@0:           // not end with \n).
michael@0:           result.range.setEndAfter(pre.lastChild);
michael@0: 
michael@0:         } else if (curLine == line + 1) {
michael@0:           result.range.setEnd(textNode, curPos - 1);
michael@0:           found = true;
michael@0:           break;
michael@0:         }
michael@0:       }
michael@0:     }
michael@0:   }
michael@0: 
michael@0:   return found || ("range" in result);
michael@0: }
michael@0: 
michael@0: // Toggle long-line wrapping and sets the view_source.wrap_long_lines
michael@0: // pref to persist the last state.
michael@0: function wrapLongLines()
michael@0: {
michael@0:   var myWrap = window.content.document.body;
michael@0:   myWrap.classList.toggle("wrap");
michael@0: 
michael@0:   // Since multiple viewsource windows are possible, another window could have
michael@0:   // affected the pref, so instead of determining the new pref value via the current
michael@0:   // pref value, we use myWrap.classList.
michael@0:   Services.prefs.setBoolPref("view_source.wrap_long_lines", myWrap.classList.contains("wrap"));
michael@0: }
michael@0: 
michael@0: // Toggles syntax highlighting and sets the view_source.syntax_highlight
michael@0: // pref to persist the last state.
michael@0: function highlightSyntax()
michael@0: {
michael@0:   var highlightSyntaxMenu = document.getElementById("menu_highlightSyntax");
michael@0:   var highlightSyntax = (highlightSyntaxMenu.getAttribute("checked") == "true");
michael@0:   Services.prefs.setBoolPref("view_source.syntax_highlight", highlightSyntax);
michael@0: 
michael@0:   gPageLoader.loadPage(gPageLoader.currentDescriptor, gPageLoader.DISPLAY_NORMAL);
michael@0: }
michael@0: 
michael@0: // Reload after change to character encoding or autodetection
michael@0: //
michael@0: // Fix for bug 136322: this function overrides the function in
michael@0: // browser.js to call PageLoader.loadPage() instead of BrowserReloadWithFlags()
michael@0: function BrowserCharsetReload()
michael@0: {
michael@0:   if (isHistoryEnabled()) {
michael@0:     gPageLoader.loadPage(gPageLoader.currentDescriptor,
michael@0:                          gPageLoader.DISPLAY_NORMAL);
michael@0:   } else {
michael@0:     gBrowser.reloadWithFlags(Ci.nsIWebNavigation.LOAD_FLAGS_CHARSET_CHANGE);
michael@0:   }
michael@0: }
michael@0: 
michael@0: function BrowserSetCharacterSet(aEvent)
michael@0: {
michael@0:   if (aEvent.target.hasAttribute("charset"))
michael@0:     gBrowser.docShell.charset = aEvent.target.getAttribute("charset");
michael@0:   BrowserCharsetReload();
michael@0: }
michael@0: 
michael@0: function BrowserForward(aEvent) {
michael@0:   try {
michael@0:     gBrowser.goForward();
michael@0:   }
michael@0:   catch(ex) {
michael@0:   }
michael@0: }
michael@0: 
michael@0: function BrowserBack(aEvent) {
michael@0:   try {
michael@0:     gBrowser.goBack();
michael@0:   }
michael@0:   catch(ex) {
michael@0:   }
michael@0: }
michael@0: 
michael@0: function UpdateBackForwardCommands() {
michael@0:   var backBroadcaster = document.getElementById("Browser:Back");
michael@0:   var forwardBroadcaster = document.getElementById("Browser:Forward");
michael@0: 
michael@0:   if (getWebNavigation().canGoBack)
michael@0:     backBroadcaster.removeAttribute("disabled");
michael@0:   else
michael@0:     backBroadcaster.setAttribute("disabled", "true");
michael@0: 
michael@0:   if (getWebNavigation().canGoForward)
michael@0:     forwardBroadcaster.removeAttribute("disabled");
michael@0:   else
michael@0:     forwardBroadcaster.setAttribute("disabled", "true");
michael@0: }
michael@0: 
michael@0: function contextMenuShowing() {
michael@0:   var isLink = false;
michael@0:   var isEmail = false;
michael@0:   if (gContextMenu.triggerNode && gContextMenu.triggerNode.localName == 'a') {
michael@0:     if (gContextMenu.triggerNode.href.indexOf('view-source:') == 0)
michael@0:       isLink = true;
michael@0:     if (gContextMenu.triggerNode.href.indexOf('mailto:') == 0)
michael@0:       isEmail = true;
michael@0:   }
michael@0:   document.getElementById('context-copyLink').hidden = !isLink;
michael@0:   document.getElementById('context-copyEmail').hidden = !isEmail;
michael@0: }
michael@0: 
michael@0: function contextMenuCopyLinkOrEmail() {
michael@0:   if (!gContextMenu.triggerNode)
michael@0:     return;
michael@0: 
michael@0:   var href = gContextMenu.triggerNode.href;
michael@0:   var clipboard = Cc['@mozilla.org/widget/clipboardhelper;1'].
michael@0:                   getService(Ci.nsIClipboardHelper);
michael@0:   clipboard.copyString(href.substring(href.indexOf(':') + 1), document);
michael@0: }