web_node.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2009 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_NODE_H_
  31. #define THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_NODE_H_
  32. #include "third_party/blink/public/platform/web_common.h"
  33. #include "third_party/blink/public/platform/web_private_ptr.h"
  34. #include "third_party/blink/public/platform/web_string.h"
  35. #include "third_party/blink/public/platform/web_vector.h"
  36. #include "v8/include/v8.h"
  37. namespace blink {
  38. class Node;
  39. class WebDocument;
  40. class WebElement;
  41. class WebElementCollection;
  42. class WebPluginContainer;
  43. // Provides access to some properties of a DOM node.
  44. // Note that the class design requires that neither this class nor any of its
  45. // subclasses have any virtual methods (other than the destructor), so that it
  46. // is possible to safely static_cast an instance of one class to the appropriate
  47. // subclass based on the actual type of the wrapped blink::Node. For the same
  48. // reason, subclasses must not add any additional data members.
  49. class BLINK_EXPORT WebNode {
  50. public:
  51. virtual ~WebNode();
  52. WebNode();
  53. WebNode(const WebNode&);
  54. WebNode& operator=(const WebNode&);
  55. void Reset();
  56. void Assign(const WebNode&);
  57. bool Equals(const WebNode&) const;
  58. // Required for using WebNodes in std maps. Note the order used is
  59. // arbitrary and should not be expected to have any specific meaning.
  60. bool LessThan(const WebNode&) const;
  61. bool IsNull() const;
  62. WebNode ParentNode() const;
  63. WebString NodeValue() const;
  64. WebDocument GetDocument() const;
  65. WebNode FirstChild() const;
  66. WebNode LastChild() const;
  67. WebNode PreviousSibling() const;
  68. WebNode NextSibling() const;
  69. bool IsLink() const;
  70. bool IsDocumentNode() const;
  71. bool IsDocumentTypeNode() const;
  72. bool IsCommentNode() const;
  73. bool IsTextNode() const;
  74. bool IsFocusable() const;
  75. bool IsContentEditable() const;
  76. bool IsElementNode() const;
  77. void SimulateClick();
  78. // See cc/paint/element_id.h for the definition of these ids.
  79. uint64_t ScrollingElementIdForTesting() const;
  80. // The argument should be lower-cased.
  81. WebElementCollection GetElementsByHTMLTagName(const WebString&) const;
  82. // https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
  83. // If the JS API would have thrown this returns null instead.
  84. WebElement QuerySelector(const WebString& selector) const;
  85. WebVector<WebElement> QuerySelectorAll(const WebString& selector) const;
  86. bool Focused() const;
  87. WebPluginContainer* PluginContainer() const;
  88. bool IsInsideFocusableElementOrARIAWidget() const;
  89. v8::Local<v8::Value> ToV8Value(v8::Local<v8::Object> creation_context,
  90. v8::Isolate*);
  91. template <typename T>
  92. T To();
  93. template <typename T>
  94. const T ToConst() const;
  95. #if INSIDE_BLINK
  96. WebNode(Node*);
  97. WebNode& operator=(Node*);
  98. operator Node*() const;
  99. template <typename T>
  100. T* Unwrap() {
  101. return static_cast<T*>(private_.Get());
  102. }
  103. template <typename T>
  104. const T* ConstUnwrap() const {
  105. return static_cast<const T*>(private_.Get());
  106. }
  107. #endif
  108. protected:
  109. WebPrivatePtr<Node> private_;
  110. };
  111. #define DECLARE_WEB_NODE_TYPE_CASTS(type) \
  112. template <> \
  113. BLINK_EXPORT type WebNode::To<type>(); \
  114. template <> \
  115. BLINK_EXPORT const type WebNode::ToConst<type>() const
  116. #if INSIDE_BLINK
  117. #define DEFINE_WEB_NODE_TYPE_CASTS(type, predicate) \
  118. template <> \
  119. BLINK_EXPORT type WebNode::To<type>() { \
  120. SECURITY_DCHECK(IsNull() || (predicate)); \
  121. type result; \
  122. result.WebNode::Assign(*this); \
  123. return result; \
  124. } \
  125. template <> \
  126. BLINK_EXPORT const type WebNode::ToConst<type>() const { \
  127. SECURITY_DCHECK(IsNull() || (predicate)); \
  128. type result; \
  129. result.WebNode::Assign(*this); \
  130. return result; \
  131. }
  132. #endif
  133. inline bool operator==(const WebNode& a, const WebNode& b) {
  134. return a.Equals(b);
  135. }
  136. inline bool operator!=(const WebNode& a, const WebNode& b) {
  137. return !(a == b);
  138. }
  139. inline bool operator<(const WebNode& a, const WebNode& b) {
  140. return a.LessThan(b);
  141. }
  142. } // namespace blink
  143. #endif