• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

KHTML

util.cpp
Go to the documentation of this file.
00001 /*
00002  * util.cc - Copyright 2005 Frerich Raabe <raabe@kde.org>
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  * 
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 
00014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00015  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00016  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00017  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00018  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00019  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00020  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00021  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00023  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00024  */
00025 #include "util.h"
00026 #include "xml/dom_nodeimpl.h"
00027 #include "xml/dom_elementimpl.h"
00028 #include "xml/dom_nodelistimpl.h" 
00029 
00030 using namespace DOM;
00031 
00032 namespace khtml {
00033 namespace XPath {
00034 
00035 bool isRootDomNode( NodeImpl *node )
00036 {
00037     return node && !xpathParentNode(node);
00038 }
00039 
00040 static QString stringValueImpl( NodeImpl *node )
00041 {
00042     // ### how different is this from textContent?
00043     // ### "The string-value of a namespace node is the namespace URI that is being bound to the namespace prefix; if it is relative, it must be resolved just like a namespace URI in an expanded-name."
00044     switch ( node->nodeType() ) {
00045         case Node::ATTRIBUTE_NODE:
00046         case Node::PROCESSING_INSTRUCTION_NODE:
00047         case Node::COMMENT_NODE:
00048         case Node::TEXT_NODE:
00049         case Node::CDATA_SECTION_NODE:
00050             return node->nodeValue().string();
00051         default:
00052             if ( isRootDomNode( node )
00053                     || node->nodeType() == Node::ELEMENT_NODE ) {
00054                 QString str;
00055 
00056                 for ( NodeImpl *cur = node->firstChild(); cur; cur = cur->traverseNextNode(node) ) {
00057                     // We only include the value of text kids here.
00058                     int type = cur->nodeType();
00059                     if (type == Node::TEXT_NODE || type == Node::CDATA_SECTION_NODE)
00060                         str.append( stringValueImpl( cur ) );
00061                 }
00062                 return str;
00063             }
00064     }
00065     return QString();
00066 }
00067 
00068 DOMString stringValue( NodeImpl *node )
00069 {
00070     return stringValueImpl( node );
00071 }
00072 
00073 void collectChildrenRecursively( SharedPtr<DOM::StaticNodeListImpl> out,
00074                                  DOM::NodeImpl *root )
00075 {
00076     // ### probably beter to use traverseNext and the like
00077     
00078     NodeImpl *n = xpathFirstChild( root );
00079     while ( n ) {
00080         out->append( n );
00081         collectChildrenRecursively( out, n );
00082         n = n->nextSibling();
00083     }
00084 }
00085 
00086 void collectChildrenReverse( SharedPtr<DOM::StaticNodeListImpl> out,
00087                              DOM::NodeImpl *root )
00088 {
00089     // ### probably beter to use traverseNext and the like
00090 
00091     NodeImpl *n = xpathLastChild( root );
00092     while ( n ) {
00093         collectChildrenReverse( out, n );
00094         out->append( n );
00095         n = n->previousSibling();
00096     }
00097 }
00098 
00099 bool isValidContextNode( NodeImpl *node )
00100 {
00101     return node && ( 
00102            node->nodeType() == Node::ELEMENT_NODE ||
00103            node->nodeType() == Node::ATTRIBUTE_NODE ||
00104            node->nodeType() == Node::TEXT_NODE ||
00105            node->nodeType() == Node::CDATA_SECTION_NODE ||
00106            node->nodeType() == Node::PROCESSING_INSTRUCTION_NODE ||
00107            node->nodeType() == Node::COMMENT_NODE ||
00108            node->nodeType() == Node::DOCUMENT_NODE ||
00109            node->nodeType() == Node::XPATH_NAMESPACE_NODE );
00110 }
00111 
00112 DOM::NodeImpl *xpathParentNode( DOM::NodeImpl *node )
00113 {
00114     DOM::NodeImpl *res = 0;
00115     if ( node ) {
00116         if ( node->nodeType() == Node::ATTRIBUTE_NODE )
00117             res = static_cast<DOM::AttrImpl*>(node)->ownerElement();
00118     else
00119             res = node->parentNode();
00120     }
00121     return res;
00122 }
00123 
00124 DOM::NodeImpl *xpathFirstChild( DOM::NodeImpl *node )
00125 {
00126     DOM::NodeImpl *res = 0;
00127     if ( node && node->nodeType() != Node::ATTRIBUTE_NODE )
00128         res = node->firstChild();
00129     return res;
00130 }
00131 
00132 DOM::NodeImpl *xpathLastChild( DOM::NodeImpl *node )
00133 {
00134     DOM::NodeImpl *res = 0;
00135     if ( node && node->nodeType() != Node::ATTRIBUTE_NODE )
00136         res = node->lastChild();
00137     return res;
00138 }
00139 
00140 DOM::NodeImpl *nextSiblingForFollowing( DOM::NodeImpl *node )
00141 {
00142     DOM::NodeImpl *res = 0;
00143     if ( node ) {
00144         if ( node->nodeType() == Node::ATTRIBUTE_NODE )
00145             res = static_cast<DOM::AttrImpl*>(node)->ownerElement()->firstChild();
00146     else
00147             res = node->nextSibling();
00148     }
00149     return res;
00150 }
00151 
00152 } // namespace khtml
00153 } // namespace XPath
00154 
00155 // kate: indent-width 4; replace-tabs off; tab-width 4; space-indent off;
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 18:53:28 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.8.3 API Reference

Skip menu "kdelibs-4.8.3 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal