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

KHTML

  • sources
  • kde-4.14
  • kdelibs
  • khtml
  • xpath
util.cpp
Go to the documentation of this file.
1 /*
2  * util.cc - Copyright 2005 Frerich Raabe <raabe@kde.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "util.h"
26 #include "xml/dom_nodeimpl.h"
27 #include "xml/dom_elementimpl.h"
28 #include "xml/dom_nodelistimpl.h"
29 
30 using namespace DOM;
31 
32 namespace khtml {
33 namespace XPath {
34 
35 bool isRootDomNode( NodeImpl *node )
36 {
37  return node && !xpathParentNode(node);
38 }
39 
40 static QString stringValueImpl( NodeImpl *node )
41 {
42  // ### how different is this from textContent?
43  // ### "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."
44  switch ( node->nodeType() ) {
45  case Node::ATTRIBUTE_NODE:
46  case Node::PROCESSING_INSTRUCTION_NODE:
47  case Node::COMMENT_NODE:
48  case Node::TEXT_NODE:
49  case Node::CDATA_SECTION_NODE:
50  return node->nodeValue().string();
51  default:
52  if ( isRootDomNode( node )
53  || node->nodeType() == Node::ELEMENT_NODE ) {
54  QString str;
55 
56  for ( NodeImpl *cur = node->firstChild(); cur; cur = cur->traverseNextNode(node) ) {
57  // We only include the value of text kids here.
58  int type = cur->nodeType();
59  if (type == Node::TEXT_NODE || type == Node::CDATA_SECTION_NODE)
60  str.append( stringValueImpl( cur ) );
61  }
62  return str;
63  }
64  }
65  return QString();
66 }
67 
68 DOMString stringValue( NodeImpl *node )
69 {
70  return stringValueImpl( node );
71 }
72 
73 void collectChildrenRecursively( SharedPtr<DOM::StaticNodeListImpl> out,
74  DOM::NodeImpl *root )
75 {
76  // ### probably beter to use traverseNext and the like
77 
78  NodeImpl *n = xpathFirstChild( root );
79  while ( n ) {
80  out->append( n );
81  collectChildrenRecursively( out, n );
82  n = n->nextSibling();
83  }
84 }
85 
86 void collectChildrenReverse( SharedPtr<DOM::StaticNodeListImpl> out,
87  DOM::NodeImpl *root )
88 {
89  // ### probably beter to use traverseNext and the like
90 
91  NodeImpl *n = xpathLastChild( root );
92  while ( n ) {
93  collectChildrenReverse( out, n );
94  out->append( n );
95  n = n->previousSibling();
96  }
97 }
98 
99 bool isValidContextNode( NodeImpl *node )
100 {
101  return node && (
102  node->nodeType() == Node::ELEMENT_NODE ||
103  node->nodeType() == Node::ATTRIBUTE_NODE ||
104  node->nodeType() == Node::TEXT_NODE ||
105  node->nodeType() == Node::CDATA_SECTION_NODE ||
106  node->nodeType() == Node::PROCESSING_INSTRUCTION_NODE ||
107  node->nodeType() == Node::COMMENT_NODE ||
108  node->nodeType() == Node::DOCUMENT_NODE ||
109  node->nodeType() == Node::XPATH_NAMESPACE_NODE );
110 }
111 
112 DOM::NodeImpl *xpathParentNode( DOM::NodeImpl *node )
113 {
114  DOM::NodeImpl *res = 0;
115  if ( node ) {
116  if ( node->nodeType() == Node::ATTRIBUTE_NODE )
117  res = static_cast<DOM::AttrImpl*>(node)->ownerElement();
118  else
119  res = node->parentNode();
120  }
121  return res;
122 }
123 
124 DOM::NodeImpl *xpathFirstChild( DOM::NodeImpl *node )
125 {
126  DOM::NodeImpl *res = 0;
127  if ( node && node->nodeType() != Node::ATTRIBUTE_NODE )
128  res = node->firstChild();
129  return res;
130 }
131 
132 DOM::NodeImpl *xpathLastChild( DOM::NodeImpl *node )
133 {
134  DOM::NodeImpl *res = 0;
135  if ( node && node->nodeType() != Node::ATTRIBUTE_NODE )
136  res = node->lastChild();
137  return res;
138 }
139 
140 DOM::NodeImpl *nextSiblingForFollowing( DOM::NodeImpl *node )
141 {
142  DOM::NodeImpl *res = 0;
143  if ( node ) {
144  if ( node->nodeType() == Node::ATTRIBUTE_NODE )
145  res = static_cast<DOM::AttrImpl*>(node)->ownerElement()->firstChild();
146  else
147  res = node->nextSibling();
148  }
149  return res;
150 }
151 
152 } // namespace khtml
153 } // namespace XPath
154 
155 // kate: indent-width 4; replace-tabs off; tab-width 4; space-indent off;
QString::append
QString & append(QChar ch)
khtml::XPath::nextSiblingForFollowing
DOM::NodeImpl * nextSiblingForFollowing(DOM::NodeImpl *node)
Definition: util.cpp:140
DOM::Node::ATTRIBUTE_NODE
Definition: dom_node.h:383
DOM::Node::XPATH_NAMESPACE_NODE
Definition: dom_node.h:394
khtml::XPath::stringValueImpl
static QString stringValueImpl(NodeImpl *node)
Definition: util.cpp:40
khtml::XPath::isRootDomNode
bool isRootDomNode(NodeImpl *node)
Definition: util.cpp:35
DOM::Node::CDATA_SECTION_NODE
Definition: dom_node.h:385
khtml::XPath::isValidContextNode
bool isValidContextNode(NodeImpl *node)
Definition: util.cpp:99
khtml::XPath::stringValue
DOMString stringValue(NodeImpl *node)
Definition: util.cpp:68
DOM::Node::PROCESSING_INSTRUCTION_NODE
Definition: dom_node.h:388
DOM::DOMString
This class implements the basic string we use in the DOM.
Definition: dom_string.h:43
khtml::XPath::xpathFirstChild
DOM::NodeImpl * xpathFirstChild(DOM::NodeImpl *node)
Definition: util.cpp:124
khtml::XPath::collectChildrenReverse
void collectChildrenReverse(SharedPtr< DOM::StaticNodeListImpl > out, DOM::NodeImpl *root)
Definition: util.cpp:86
QString
util.h
DOM::Node::DOCUMENT_NODE
Definition: dom_node.h:390
DOM::Node::ELEMENT_NODE
Definition: dom_node.h:382
khtml::XPath::xpathParentNode
DOM::NodeImpl * xpathParentNode(DOM::NodeImpl *node)
Definition: util.cpp:112
khtml::XPath::collectChildrenRecursively
void collectChildrenRecursively(SharedPtr< DOM::StaticNodeListImpl > out, DOM::NodeImpl *root)
Definition: util.cpp:73
DOM::Node::TEXT_NODE
Definition: dom_node.h:384
khtml::XPath::xpathLastChild
DOM::NodeImpl * xpathLastChild(DOM::NodeImpl *node)
Definition: util.cpp:132
DOM::Node::COMMENT_NODE
Definition: dom_node.h:389
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:26:19 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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