KHtml

util.cpp
1 /*
2  * util.cc - Copyright 2005 Frerich Raabe <[email protected]>
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 {
34 namespace XPath
35 {
36 
37 bool isRootDomNode(NodeImpl *node)
38 {
39  return node && !xpathParentNode(node);
40 }
41 
42 static QString stringValueImpl(NodeImpl *node)
43 {
44  // ### how different is this from textContent?
45  // ### "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."
46  switch (node->nodeType()) {
47  case Node::ATTRIBUTE_NODE:
48  case Node::PROCESSING_INSTRUCTION_NODE:
49  case Node::COMMENT_NODE:
50  case Node::TEXT_NODE:
51  case Node::CDATA_SECTION_NODE:
52  return node->nodeValue().string();
53  default:
54  if (isRootDomNode(node)
55  || node->nodeType() == Node::ELEMENT_NODE) {
56  QString str;
57 
58  for (NodeImpl *cur = node->firstChild(); cur; cur = cur->traverseNextNode(node)) {
59  // We only include the value of text kids here.
60  int type = cur->nodeType();
61  if (type == Node::TEXT_NODE || type == Node::CDATA_SECTION_NODE) {
62  str.append(stringValueImpl(cur));
63  }
64  }
65  return str;
66  }
67  }
68  return QString();
69 }
70 
71 DOMString stringValue(NodeImpl *node)
72 {
73  return stringValueImpl(node);
74 }
75 
76 void collectChildrenRecursively(SharedPtr<DOM::StaticNodeListImpl> out,
77  DOM::NodeImpl *root)
78 {
79  // ### probably beter to use traverseNext and the like
80 
81  NodeImpl *n = xpathFirstChild(root);
82  while (n) {
83  out->append(n);
84  collectChildrenRecursively(out, n);
85  n = n->nextSibling();
86  }
87 }
88 
89 void collectChildrenReverse(SharedPtr<DOM::StaticNodeListImpl> out,
90  DOM::NodeImpl *root)
91 {
92  // ### probably beter to use traverseNext and the like
93 
94  NodeImpl *n = xpathLastChild(root);
95  while (n) {
96  collectChildrenReverse(out, n);
97  out->append(n);
98  n = n->previousSibling();
99  }
100 }
101 
102 bool isValidContextNode(NodeImpl *node)
103 {
104  return node && (
105  node->nodeType() == Node::ELEMENT_NODE ||
106  node->nodeType() == Node::ATTRIBUTE_NODE ||
107  node->nodeType() == Node::TEXT_NODE ||
108  node->nodeType() == Node::CDATA_SECTION_NODE ||
109  node->nodeType() == Node::PROCESSING_INSTRUCTION_NODE ||
110  node->nodeType() == Node::COMMENT_NODE ||
111  node->nodeType() == Node::DOCUMENT_NODE ||
112  node->nodeType() == Node::XPATH_NAMESPACE_NODE);
113 }
114 
115 DOM::NodeImpl *xpathParentNode(DOM::NodeImpl *node)
116 {
117  DOM::NodeImpl *res = nullptr;
118  if (node) {
119  if (node->nodeType() == Node::ATTRIBUTE_NODE) {
120  res = static_cast<DOM::AttrImpl *>(node)->ownerElement();
121  } else {
122  res = node->parentNode();
123  }
124  }
125  return res;
126 }
127 
128 DOM::NodeImpl *xpathFirstChild(DOM::NodeImpl *node)
129 {
130  DOM::NodeImpl *res = nullptr;
131  if (node && node->nodeType() != Node::ATTRIBUTE_NODE) {
132  res = node->firstChild();
133  }
134  return res;
135 }
136 
137 DOM::NodeImpl *xpathLastChild(DOM::NodeImpl *node)
138 {
139  DOM::NodeImpl *res = nullptr;
140  if (node && node->nodeType() != Node::ATTRIBUTE_NODE) {
141  res = node->lastChild();
142  }
143  return res;
144 }
145 
146 DOM::NodeImpl *nextSiblingForFollowing(DOM::NodeImpl *node)
147 {
148  DOM::NodeImpl *res = nullptr;
149  if (node) {
150  if (node->nodeType() == Node::ATTRIBUTE_NODE) {
151  res = static_cast<DOM::AttrImpl *>(node)->ownerElement()->firstChild();
152  } else {
153  res = node->nextSibling();
154  }
155  }
156  return res;
157 }
158 
159 } // namespace khtml
160 } // namespace XPath
161 
QString & append(QChar ch)
This file is part of the HTML rendering engine for KDE.
Type type(const QSqlDatabase &db)
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
This library provides a full-featured HTML parser and widget.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:10 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.