home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / communicator / contentAreaUtils.js < prev    next >
Encoding:
JavaScript  |  2001-07-10  |  4.4 KB  |  129 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributors:
  21.  * 
  22.  */
  23.  
  24. /**
  25.  * Determine whether or not a given focused DOMWindow is in the content
  26.  * area.
  27.  **/
  28.   function isDocumentFrame(aFocusedWindow)
  29.   {
  30.     var contentFrames = _content.frames;
  31.     if (contentFrames.length) {
  32.       for (var i = 0; i < contentFrames.length; ++i) {
  33.         if (aFocusedWindow == contentFrames[i])
  34.           return true;
  35.       }
  36.     }
  37.     return false;
  38.   }
  39.  
  40.   function urlSecurityCheck(url, doc) {
  41.     // URL Loading Security Check
  42.     var focusedWindow = doc.commandDispatcher.focusedWindow;
  43.     var sourceWin = isDocumentFrame(focusedWindow) ? focusedWindow.location.href : focusedWindow._content.location.href;
  44.     const nsIScriptSecurityManager = Components.interfaces.nsIScriptSecurityManager;
  45.     var secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"].getService().
  46.                      QueryInterface(nsIScriptSecurityManager);
  47.     try {
  48.       secMan.checkLoadURIStr(sourceWin, url, nsIScriptSecurityManager.STANDARD);
  49.     } catch (e) {
  50.        throw "Load of " + url + " denied.";
  51.     }
  52.   }
  53.  
  54.   function openNewWindowWith(url) {
  55.  
  56.     urlSecurityCheck(url, document);
  57.     var newWin;
  58.     var wintype = document.firstChild.getAttribute('windowtype');
  59.  
  60.     // if and only if the current window is a browser window and it has a document with a character
  61.     // set, then extract the current charset menu setting from the current document and use it to
  62.     // initialize the new browser window...
  63.     if (window && (wintype == "navigator:browser") &&
  64.       window._content && window._content.document) {
  65.       var DocCharset = window._content.document.characterSet;
  66.       var charsetArg = "charset="+DocCharset;
  67.  
  68.       //we should "inherit" the charset menu setting in a new window
  69.       newWin = window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", url, charsetArg, true );
  70.     }
  71.     else { // forget about the charset information.
  72.       newWin = window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", url, null, true );
  73.     }
  74.  
  75.     // Fix new window.    
  76.     newWin.saveFileAndPos = true;
  77.   }
  78.  
  79.   function savePage(url) 
  80.   {
  81.     var postData = null; // No post data, usually.
  82.     var cacheKey = null;
  83.     // Default is to save current page.
  84.     if ( !url )
  85.       url = window._content.location.href;
  86.  
  87.     try {
  88.       var sessionHistory = getWebNavigation().sessionHistory;
  89.       var entry = sessionHistory.getEntryAtIndex(sessionHistory.index, false).QueryInterface(Components.interfaces.nsISHEntry);
  90.       postData = entry.postData;
  91.       cacheKey = entry.cacheKey;
  92.     } catch(e) {
  93.     }
  94.  
  95.     // Use stream xfer component to prompt for destination and save.
  96.     var xfer = Components.classes["@mozilla.org/appshell/component/xfer;1"].getService(Components.interfaces["nsIStreamTransfer"]);
  97.     try {
  98.       xfer.SelectFileAndTransferLocationSpec( url, window, "", "", true, postData, cacheKey );
  99.     } catch( exception ) {
  100.       return false;
  101.     }
  102.  
  103.     return true;
  104.   }
  105.  
  106.   //Note: "function editPage(url)" was moved to utilityOverlay.js
  107.  
  108.   function findParentNode(node, parentNode)
  109.   {
  110.     if (node && node.nodeType == Node.TEXT_NODE) {
  111.       node = node.parentNode;
  112.     }
  113.     while (node) {
  114.       var nodeName = node.localName;
  115.       if (!nodeName)
  116.         return null;
  117.       nodeName = nodeName.toLowerCase();
  118.       if (nodeName == "body" || nodeName == "html" ||
  119.           nodeName == "#document") {
  120.         return null;
  121.       }
  122.       if (nodeName == parentNode)
  123.         return node;
  124.       node = node.parentNode;
  125.     }
  126.     return null;
  127.   }
  128.  
  129.