Annotation of chimera/GeckoUtils.h, revision 1.4

1.1       hyatt       1: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
                      2: /* ***** BEGIN LICENSE BLOCK *****
                      3:  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
                      4:  *
                      5:  * The contents of this file are subject to the Netscape Public License
                      6:  * Version 1.1 (the "License"); you may not use this file except in
                      7:  * compliance with the License. You may obtain a copy of the License at
                      8:  * http://www.mozilla.org/NPL/
                      9:  *
                     10:  * Software distributed under the License is distributed on an "AS IS" basis,
                     11:  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
                     12:  * for the specific language governing rights and limitations under the
                     13:  * License.
                     14:  *
                     15:  * The Original Code is mozilla.org code.
                     16:  *
                     17:  * The Initial Developer of the Original Code is 
                     18:  * Netscape Communications Corporation.
                     19:  * Portions created by the Initial Developer are Copyright (C) 2002
                     20:  * the Initial Developer. All Rights Reserved.
                     21:  *
                     22:  * Contributor(s):
                     23:  *
                     24:  * Alternatively, the contents of this file may be used under the terms of
                     25:  * either the GNU General Public License Version 2 or later (the "GPL"), or 
                     26:  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
                     27:  * in which case the provisions of the GPL or the LGPL are applicable instead
                     28:  * of those above. If you wish to allow use of your version of this file only
                     29:  * under the terms of either the GPL or the LGPL, and not to allow others to
                     30:  * use your version of this file under the terms of the NPL, indicate your
                     31:  * decision by deleting the provisions above and replace them with the notice
                     32:  * and other provisions required by the GPL or the LGPL. If you do not delete
                     33:  * the provisions above, a recipient may use your version of this file under
                     34:  * the terms of any one of the NPL, the GPL or the LGPL.
                     35:  *
                     36:  * ***** END LICENSE BLOCK ***** */
                     37: 
                     38: #ifndef __GeckoUtils_h__
                     39: #define __GeckoUtils_h__
                     40: 
1.4     ! hyatt      41: #include "nsIDOMHTMLAnchorElement.h"
        !            42: #include "nsIDOMHTMLAreaElement.h"
        !            43: #include "nsIDOMHTMLLinkElement.h"
        !            44: #include "nsIDOMHTMLImageElement.h"
        !            45: #include "nsIDOMCharacterData.h"
        !            46: 
1.1       hyatt      47: class GeckoUtils
                     48: {
                     49: public:
                     50:   static void GatherTextUnder(nsIDOMNode* aNode, nsString& aResult) {
1.2       hyatt      51:     nsAutoString text;
                     52:     nsCOMPtr<nsIDOMNode> node;
                     53:     aNode->GetFirstChild(getter_AddRefs(node));
                     54:     PRUint32 depth = 1;
                     55:     while (node && depth) {
                     56:       nsCOMPtr<nsIDOMCharacterData> charData(do_QueryInterface(node));
                     57:       PRUint16 nodeType;
                     58:       node->GetNodeType(&nodeType);
                     59:       if (charData && nodeType == nsIDOMNode::TEXT_NODE) {
                     60:         // Add this text to our collection.
                     61:         text += NS_LITERAL_STRING(" ");
                     62:         nsAutoString data;
                     63:         charData->GetData(data);
                     64:         text += data;
                     65:       }
                     66:       else {
                     67:         nsCOMPtr<nsIDOMHTMLImageElement> img(do_QueryInterface(node));
                     68:         if (img) {
                     69:           nsAutoString altText;
                     70:           img->GetAlt(altText);
                     71:           if (!altText.IsEmpty()) {
                     72:             text = altText;
                     73:             break;
                     74:           }
                     75:         }
                     76:       }
1.3       hyatt      77: 
                     78:       // Find the next node to test.
                     79:       PRBool hasChildNodes;
                     80:       node->HasChildNodes(&hasChildNodes);
                     81:       if (hasChildNodes) {
                     82:         nsCOMPtr<nsIDOMNode> temp = node;
                     83:         temp->GetFirstChild(getter_AddRefs(node));
                     84:         depth++;
                     85:       }
                     86:       else {
                     87:         nsCOMPtr<nsIDOMNode> nextSibling;
                     88:         node->GetNextSibling(getter_AddRefs(nextSibling));
                     89:         if (nextSibling)
                     90:           node = nextSibling;
                     91:         else {
                     92:           nsCOMPtr<nsIDOMNode> parentNode;
                     93:           node->GetParentNode(getter_AddRefs(parentNode));
                     94:           if (!parentNode)
                     95:             node = nsnull;
                     96:           else {
                     97:             nsCOMPtr<nsIDOMNode> nextSibling;
                     98:             parentNode->GetNextSibling(getter_AddRefs(nextSibling));
                     99:             node = nextSibling;
                    100:             depth--;
                    101:           }
                    102:         }
                    103:       }
1.2       hyatt     104:     }
1.3       hyatt     105: 
                    106:     text.CompressWhitespace();
1.2       hyatt     107:     aResult = text;
1.1       hyatt     108:   };
1.4     ! hyatt     109: 
        !           110:   static void GetEnclosingLinkElementAndHref(nsIDOMNode* aNode, nsIDOMElement** aLinkContent, nsString& aHref)
        !           111:   {
        !           112:     nsCOMPtr<nsIDOMElement> content(do_QueryInterface(aNode));
        !           113:     nsAutoString localName;
        !           114:     if (content)
        !           115:       content->GetLocalName(localName);
        !           116: 
        !           117:     nsCOMPtr<nsIDOMElement> linkContent;
        !           118:     ToLowerCase(localName);
        !           119:     nsAutoString href;
        !           120:     if (localName.Equals(NS_LITERAL_STRING("a")) ||
        !           121:         localName.Equals(NS_LITERAL_STRING("area")) ||
        !           122:         localName.Equals(NS_LITERAL_STRING("link"))) {
        !           123:       PRBool hasAttr;
        !           124:       content->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr);
        !           125:       if (hasAttr) {
        !           126:         linkContent = content;
        !           127:         nsCOMPtr<nsIDOMHTMLAnchorElement> anchor(do_QueryInterface(linkContent));
        !           128:         if (anchor)
        !           129:           anchor->GetHref(href);
        !           130:         else {
        !           131:           nsCOMPtr<nsIDOMHTMLAreaElement> area(do_QueryInterface(linkContent));
        !           132:           if (area)
        !           133:             area->GetHref(href);
        !           134:           else {
        !           135:             nsCOMPtr<nsIDOMHTMLLinkElement> link(do_QueryInterface(linkContent));
        !           136:             if (link)
        !           137:               link->GetHref(href);
        !           138:           }
        !           139:         }
        !           140:       }
        !           141:     }
        !           142:     else {
        !           143:       nsCOMPtr<nsIDOMNode> curr = aNode;
        !           144:       nsCOMPtr<nsIDOMNode> temp = curr;
        !           145:       temp->GetParentNode(getter_AddRefs(curr));
        !           146:       while (curr) {
        !           147:         content = do_QueryInterface(curr);
        !           148:         if (!content)
        !           149:           break;
        !           150:         content->GetLocalName(localName);
        !           151:         ToLowerCase(localName);
        !           152:         if (localName.Equals(NS_LITERAL_STRING("a"))) {
        !           153:           PRBool hasAttr;
        !           154:           content->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr);
        !           155:           if (hasAttr) {
        !           156:             linkContent = content;
        !           157:             nsCOMPtr<nsIDOMHTMLAnchorElement> anchor(do_QueryInterface(linkContent));
        !           158:             if (anchor)
        !           159:               anchor->GetHref(href);
        !           160:           }
        !           161:           else
        !           162:             linkContent = nsnull; // Links can't be nested.
        !           163:           break;
        !           164:         }
        !           165: 
        !           166:         temp = curr;
        !           167:         temp->GetParentNode(getter_AddRefs(curr));
        !           168:       }
        !           169:     }
        !           170: 
        !           171:     *aLinkContent = linkContent;
        !           172:     NS_IF_ADDREF(*aLinkContent);
        !           173: 
        !           174:     aHref = href;
        !           175:   }
1.1       hyatt     176: };
                    177: 
                    178: 
                    179: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>