Node.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2025 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_CORE_NODE_H
  29. #define RMLUI_CORE_NODE_H
  30. #include "ScriptInterface.h"
  31. #include "Traits.h"
  32. #include "Types.h"
  33. namespace Rml {
  34. class Context;
  35. class Element;
  36. class ElementDocument;
  37. using OwnedNodeList = Vector<NodePtr>;
  38. /**
  39. A generic node in the DOM tree.
  40. */
  41. class RMLUICORE_API Node : public ScriptInterface {
  42. public:
  43. RMLUI_RTTI_DefineWithParent(Node, ScriptInterface)
  44. virtual ~Node();
  45. /// Get the child node at the given index.
  46. /// @param[in] index Index of child to get.
  47. /// @return The child node at the given index.
  48. Node* GetChildNode(int index) const;
  49. /// Get the number of children of this node.
  50. /// @param[in] include_non_dom_elements True if the caller wants to include the non-DOM children. Only set this to true if you know what you're
  51. /// doing!
  52. /// @return The number of children.
  53. int GetNumChildNodes(bool include_non_dom_elements = false) const;
  54. /// Returns whether or not this node has any DOM children.
  55. /// @return True if the node has at least one DOM child, false otherwise.
  56. bool HasChildNodes() const;
  57. /// Append a child to this node.
  58. /// @param[in] node The node to append as a child.
  59. /// @param[in] dom_node True if the node is to be part of the DOM, false otherwise. Only set this to false if you know what you're doing!
  60. /// @return A pointer to the just inserted node.
  61. Node* AppendChild(NodePtr node, bool dom_node = true);
  62. [[deprecated("Use the NodePtr version of this function")]] Element* AppendChild(ElementPtr element, bool dom_element = true);
  63. /// Adds a child to this node directly before the adjacent node. The new node inherits the DOM/non-DOM
  64. /// status from the adjacent node.
  65. /// @param[in] node Node to be inserted.
  66. /// @param[in] adjacent_node The reference node which the new node will be inserted before.
  67. /// @return A pointer to the just inserted node.
  68. Node* InsertBefore(NodePtr node, Node* adjacent_node);
  69. [[deprecated("Use the NodePtr version of this function")]] Element* InsertBefore(ElementPtr element, Element* adjacent_element);
  70. /// Replaces the second node with the first node.
  71. /// @param[in] insert_node The node to insert and replace the other node.
  72. /// @param[in] replace_node The existing child node to replace. If this doesn't exist, insert_node will be appended.
  73. /// @return A unique pointer to the replaced node if found, discard the result to immediately destroy.
  74. NodePtr ReplaceChild(NodePtr insert_node, Node* replace_node);
  75. [[deprecated("Use the NodePtr version of this function")]] ElementPtr ReplaceChild(ElementPtr inserted_element, Element* replaced_element);
  76. /// Remove a child node from this node.
  77. /// @param[in] node The node to remove.
  78. /// @return A unique pointer to the node if found, discard the result to immediately destroy.
  79. NodePtr RemoveChild(Node* node);
  80. [[deprecated("Use the NodePtr version of this function")]] ElementPtr RemoveChild(Element* element);
  81. /// Gets this nodes's parent node.
  82. /// @return This node's parent.
  83. [[deprecated("Use GetParentElement")]] Element* GetParentNode() const;
  84. /// Gets this nodes's parent element.
  85. /// @return This node's parent if it is an element, otherwise false.
  86. Element* GetParentElement() const;
  87. /// Gets the document this node belongs to.
  88. /// @return This node's document.
  89. ElementDocument* GetOwnerDocument() const;
  90. /// Returns the element's context.
  91. /// @return The context this element's document exists within.
  92. Context* GetContext() const;
  93. /// Returns the element's render manager.
  94. /// @return The render manager responsible for this element.
  95. RenderManager* GetRenderManager() const;
  96. template <typename T>
  97. class NodeChildIterator {
  98. public:
  99. using iterator_category = std::forward_iterator_tag;
  100. using value_type = T*;
  101. using difference_type = std::ptrdiff_t;
  102. using pointer = T**;
  103. using reference = T*&;
  104. struct Stop {};
  105. NodeChildIterator() = default;
  106. NodeChildIterator(const Node* host, int index, bool include_non_dom_elements) :
  107. host(host), index(index), include_non_dom_elements(include_non_dom_elements)
  108. {
  109. Advance();
  110. }
  111. T* operator*() const
  112. {
  113. RMLUI_ASSERT(host && index < (int)host->children.size());
  114. RMLUI_ASSERTMSG(include_non_dom_elements || index < (int)host->children.size() - host->num_non_dom_children,
  115. "Attempting to access a non-DOM child, but configured to exclude them.");
  116. return rmlui_static_cast<T*>(host->children[index].get());
  117. }
  118. NodeChildIterator& operator++()
  119. {
  120. ++index;
  121. Advance();
  122. return *this;
  123. }
  124. NodeChildIterator operator++(int)
  125. {
  126. NodeChildIterator it = *this;
  127. ++*this;
  128. return it;
  129. }
  130. bool operator==(const Stop& /*stop*/) const
  131. {
  132. return !host || index >= (int)host->children.size() - (include_non_dom_elements ? 0 : host->num_non_dom_children);
  133. }
  134. bool operator!=(const Stop& stop) const { return !(*this == stop); }
  135. bool operator==(const NodeChildIterator& other) const { return host == other.host && index == other.index; }
  136. bool operator!=(const NodeChildIterator& other) const { return !(*this == other); }
  137. private:
  138. const Node* host = nullptr;
  139. int index = 0;
  140. bool include_non_dom_elements = false;
  141. void Advance()
  142. {
  143. const int size = (int)host->children.size();
  144. while (index < size)
  145. {
  146. if (rmlui_dynamic_cast<T*>(host->children[index].get()))
  147. return;
  148. ++index;
  149. }
  150. if (!include_non_dom_elements && index >= size - host->num_non_dom_children)
  151. {
  152. index = size;
  153. }
  154. }
  155. };
  156. template <typename T>
  157. class NodeChildReverseIterator {
  158. public:
  159. using iterator_category = std::forward_iterator_tag;
  160. using value_type = T*;
  161. using difference_type = std::ptrdiff_t;
  162. using pointer = T**;
  163. using reference = T*&;
  164. struct Stop {};
  165. NodeChildReverseIterator() = default;
  166. NodeChildReverseIterator(const Node* host, int index, bool include_non_dom_elements) :
  167. host(host), index(index), include_non_dom_elements(include_non_dom_elements)
  168. {
  169. Advance();
  170. }
  171. T* operator*() const
  172. {
  173. RMLUI_ASSERT(host && index >= 0 && index < (int)host->children.size());
  174. RMLUI_ASSERTMSG(include_non_dom_elements || index < (int)host->children.size() - host->num_non_dom_children,
  175. "Attempting to access a non-DOM child, but configured to exclude them.");
  176. return rmlui_static_cast<T*>(host->children[index].get());
  177. }
  178. NodeChildReverseIterator& operator++()
  179. {
  180. --index;
  181. Advance();
  182. return *this;
  183. }
  184. NodeChildReverseIterator operator++(int)
  185. {
  186. NodeChildIterator it = *this;
  187. --*this;
  188. return it;
  189. }
  190. bool operator==(const Stop& /*stop*/) const { return !host || index < 0; }
  191. bool operator!=(const Stop& stop) const { return !(*this == stop); }
  192. bool operator==(const NodeChildReverseIterator& other) const { return host == other.host && index == other.index; }
  193. bool operator!=(const NodeChildReverseIterator& other) const { return !(*this == other); }
  194. private:
  195. const Node* host = nullptr;
  196. int index = 0;
  197. bool include_non_dom_elements = false;
  198. void Advance()
  199. {
  200. while (index >= 0)
  201. {
  202. if (rmlui_dynamic_cast<T*>(host->children[index].get()))
  203. return;
  204. --index;
  205. }
  206. }
  207. };
  208. template <typename T>
  209. class NodeChildRange {
  210. public:
  211. NodeChildRange() = default;
  212. NodeChildRange(const Node* node, bool include_non_dom_elements) : node(node), include_non_dom_elements(include_non_dom_elements) {}
  213. NodeChildIterator<T> begin() { return {node, 0, include_non_dom_elements}; }
  214. typename NodeChildIterator<T>::Stop end() { return {}; }
  215. private:
  216. const Node* node = nullptr;
  217. bool include_non_dom_elements = false;
  218. };
  219. template <typename T>
  220. class NodeChildReverseRange {
  221. public:
  222. NodeChildReverseRange() = default;
  223. NodeChildReverseRange(const Node* node, bool include_non_dom_elements) : node(node), include_non_dom_elements(include_non_dom_elements) {}
  224. NodeChildReverseIterator<T> begin()
  225. {
  226. return {node, (int)node->children.size() - 1 - (include_non_dom_elements ? 0 : node->num_non_dom_children), include_non_dom_elements};
  227. }
  228. typename NodeChildReverseIterator<T>::Stop end() { return {}; }
  229. private:
  230. const Node* node = nullptr;
  231. bool include_non_dom_elements = false;
  232. };
  233. template <typename T>
  234. NodeChildRange<T> IterateChildren(bool include_non_dom_elements = false) const
  235. {
  236. return NodeChildRange<T>(this, include_non_dom_elements);
  237. }
  238. template <typename T>
  239. NodeChildReverseRange<T> IterateChildrenReverse(bool include_non_dom_elements = false) const
  240. {
  241. return NodeChildReverseRange<T>(this, include_non_dom_elements);
  242. }
  243. protected:
  244. Node();
  245. void SetOwnerDocument(ElementDocument* document);
  246. virtual void OnChildNodeAdd(Node* child, bool dom_node);
  247. virtual void OnChildNodeRemove(Node* child, bool dom_node);
  248. virtual void OnParentChange(Node* parent);
  249. private:
  250. void SetParent(Node* new_parent);
  251. Node* parent = nullptr;
  252. ElementDocument* owner_document = nullptr;
  253. OwnedNodeList children;
  254. int num_non_dom_children = 0;
  255. friend class Rml::Context;
  256. };
  257. } // namespace Rml
  258. #endif