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 if ( node && node->nodeType() == Node::ATTRIBUTE_NODE ) 00115 return static_cast<DOM::AttrImpl*>(node)->ownerElement(); 00116 else 00117 return node->parentNode(); 00118 } 00119 00120 DOM::NodeImpl *xpathFirstChild( DOM::NodeImpl *node ) 00121 { 00122 if ( node && node->nodeType() == Node::ATTRIBUTE_NODE ) 00123 return 0; 00124 else 00125 return node->firstChild(); 00126 } 00127 00128 DOM::NodeImpl *xpathLastChild( DOM::NodeImpl *node ) 00129 { 00130 if ( node && node->nodeType() == Node::ATTRIBUTE_NODE ) 00131 return 0; 00132 else 00133 return node->lastChild(); 00134 } 00135 00136 DOM::NodeImpl *nextSiblingForFollowing( DOM::NodeImpl *node ) 00137 { 00138 if ( node && node->nodeType() == Node::ATTRIBUTE_NODE ) 00139 return static_cast<DOM::AttrImpl*>(node)->ownerElement()->firstChild(); 00140 else 00141 return node->nextSibling(); 00142 } 00143 00144 } // namespace khtml 00145 } // namespace XPath 00146 00147 // kate: indent-width 4; replace-tabs off; tab-width 4; space-indent off;
KDE 4.6 API Reference