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

KHTML

  • 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
30using namespace DOM;
31
32namespace khtml {
33namespace XPath {
34
35bool isRootDomNode( NodeImpl *node )
36{
37 return node && !xpathParentNode(node);
38}
39
40static 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
68DOMString stringValue( NodeImpl *node )
69{
70 return stringValueImpl( node );
71}
72
73void 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
86void 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
99bool 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
112DOM::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
124DOM::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
132DOM::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
140DOM::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;
DOM::DOMString
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
DOM::Node::XPATH_NAMESPACE_NODE
@ XPATH_NAMESPACE_NODE
Definition: dom_node.h:394
DOM::Node::ELEMENT_NODE
@ ELEMENT_NODE
Definition: dom_node.h:382
DOM::Node::COMMENT_NODE
@ COMMENT_NODE
Definition: dom_node.h:389
DOM::Node::PROCESSING_INSTRUCTION_NODE
@ PROCESSING_INSTRUCTION_NODE
Definition: dom_node.h:388
DOM::Node::CDATA_SECTION_NODE
@ CDATA_SECTION_NODE
Definition: dom_node.h:385
DOM::Node::TEXT_NODE
@ TEXT_NODE
Definition: dom_node.h:384
DOM::Node::ATTRIBUTE_NODE
@ ATTRIBUTE_NODE
Definition: dom_node.h:383
DOM::Node::DOCUMENT_NODE
@ DOCUMENT_NODE
Definition: dom_node.h:390
DOM
This library provides a full-featured HTML parser and widget.
Definition: design.h:55
khtml::XPath::stringValueImpl
static QString stringValueImpl(NodeImpl *node)
Definition: util.cpp:40
khtml::XPath::collectChildrenReverse
void collectChildrenReverse(SharedPtr< DOM::StaticNodeListImpl > out, DOM::NodeImpl *root)
Definition: util.cpp:86
khtml::XPath::xpathFirstChild
DOM::NodeImpl * xpathFirstChild(DOM::NodeImpl *node)
Definition: util.cpp:124
khtml::XPath::isRootDomNode
bool isRootDomNode(NodeImpl *node)
Definition: util.cpp:35
khtml::XPath::xpathParentNode
DOM::NodeImpl * xpathParentNode(DOM::NodeImpl *node)
Definition: util.cpp:112
khtml::XPath::stringValue
DOMString stringValue(NodeImpl *node)
Definition: util.cpp:68
khtml::XPath::nextSiblingForFollowing
DOM::NodeImpl * nextSiblingForFollowing(DOM::NodeImpl *node)
Definition: util.cpp:140
khtml::XPath::xpathLastChild
DOM::NodeImpl * xpathLastChild(DOM::NodeImpl *node)
Definition: util.cpp:132
khtml::XPath::isValidContextNode
bool isValidContextNode(NodeImpl *node)
Definition: util.cpp:99
khtml::XPath::collectChildrenRecursively
void collectChildrenRecursively(SharedPtr< DOM::StaticNodeListImpl > out, DOM::NodeImpl *root)
Definition: util.cpp:73
khtml
util.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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.14.38 API Reference

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