/src/ScriptingCore/DOM/Element.h

https://github.com/GordonSmith/FireBreath · C Header · 252 lines · 50 code · 28 blank · 174 comment · 1 complexity · e818e79fbb1a2a9e785fd31fd8e2b623 MD5 · raw file

  1. /**********************************************************\
  2. Original Author: Richard Bateman (taxilian)
  3. Created: Dec 9, 2009
  4. License: Dual license model; choose one of two:
  5. New BSD License
  6. http://www.opensource.org/licenses/bsd-license.php
  7. - or -
  8. GNU Lesser General Public License, version 2.1
  9. http://www.gnu.org/licenses/lgpl-2.1.html
  10. Copyright 2009 PacketPass, Inc and the Firebreath development team
  11. \**********************************************************/
  12. #pragma once
  13. #ifndef H_FB_DOM_ELEMENT
  14. #define H_FB_DOM_ELEMENT
  15. #include <string>
  16. #include "JSObject.h"
  17. #include "Node.h"
  18. namespace FB { namespace DOM {
  19. class Element;
  20. typedef boost::shared_ptr<Element> ElementPtr;
  21. ////////////////////////////////////////////////////////////////////////////////////////////////////
  22. /// @class Element
  23. ///
  24. /// @brief DOM Element wrapper
  25. ///
  26. /// This class should not be created directly; instead, use the Element::create method
  27. ////////////////////////////////////////////////////////////////////////////////////////////////////
  28. class Element : public virtual Node
  29. {
  30. public:
  31. Element(const FB::JSObjectPtr& element);
  32. virtual ~Element();
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////
  34. /// @fn ElementPtr Element::element()
  35. ///
  36. /// @brief Gets a FB::DOM::ElementPtr pointing to the current object
  37. ///
  38. /// @return FB::DOM::ElementPtr point to the current object
  39. ////////////////////////////////////////////////////////////////////////////////////////////////////
  40. ElementPtr element() { return boost::dynamic_pointer_cast<Element>(node()); }
  41. ////////////////////////////////////////////////////////////////////////////////////////////////////
  42. /// @fn static ElementPtr Element::create(const FB::JSObjectPtr &api)
  43. ///
  44. /// @brief Creates a FB::DOM::Element object from a JSObjectPtr representing a DOM object. This
  45. /// will probably throw an exception and definitely not work right if the DOM object
  46. /// represented is not a Element
  47. ///
  48. /// @param api The FB::JSObjectAPI object containing the element to wrap
  49. ///
  50. /// @return FB::DOM::ElementPtr to the created Element object
  51. ////////////////////////////////////////////////////////////////////////////////////////////////////
  52. static ElementPtr create(const FB::JSObjectPtr &api) {
  53. if (!api) { return ElementPtr(); } // If the API is null, return a NULL Element
  54. return api->getHost()->_createElement(api);
  55. }
  56. public:
  57. ////////////////////////////////////////////////////////////////////////////////////////////////////
  58. /// @fn virtual std::string Element::getInnerHTML() const
  59. ///
  60. /// @brief Gets the inner html for the element.
  61. ///
  62. /// @return The inner html.
  63. ////////////////////////////////////////////////////////////////////////////////////////////////////
  64. virtual std::string getInnerHTML() const;
  65. ////////////////////////////////////////////////////////////////////////////////////////////////////
  66. /// @fn virtual void Element::setInnerHTML(const std::string&) const
  67. ///
  68. /// @brief Sets an inner html for the element
  69. ///
  70. /// @param html The html to set
  71. ////////////////////////////////////////////////////////////////////////////////////////////////////
  72. virtual void setInnerHTML(const std::string& html) const;
  73. ////////////////////////////////////////////////////////////////////////////////////////////////////
  74. /// @fn virtual int Element::getWidth() const
  75. ///
  76. /// @brief Gets the width property of the element.
  77. ///
  78. /// @return The width.
  79. ////////////////////////////////////////////////////////////////////////////////////////////////////
  80. virtual int getWidth() const;
  81. ////////////////////////////////////////////////////////////////////////////////////////////////////
  82. /// @fn virtual void Element::setWidth(int) const
  83. ///
  84. /// @brief Sets the width property of the element.
  85. ///
  86. /// @param width The new width of the element.
  87. ////////////////////////////////////////////////////////////////////////////////////////////////////
  88. virtual void setWidth(const int width) const;
  89. ////////////////////////////////////////////////////////////////////////////////////////////////////
  90. /// @fn virtual int Element::getScrollWidth() const
  91. ///
  92. /// @brief Gets the scroll width property (read-only) of the element.
  93. ///
  94. /// @return The scroll width.
  95. ////////////////////////////////////////////////////////////////////////////////////////////////////
  96. virtual int getScrollWidth() const;
  97. ////////////////////////////////////////////////////////////////////////////////////////////////////
  98. /// @fn virtual int Element::getHeight() const
  99. ///
  100. /// @brief Gets the height property of the element.
  101. ///
  102. /// @return The height.
  103. ////////////////////////////////////////////////////////////////////////////////////////////////////
  104. virtual int getHeight() const;
  105. ////////////////////////////////////////////////////////////////////////////////////////////////////
  106. /// @fn virtual void Element::setHeight(int) const
  107. ///
  108. /// @brief Sets the height property of the element
  109. ///
  110. /// @param height The new height of the element
  111. ////////////////////////////////////////////////////////////////////////////////////////////////////
  112. virtual void setHeight(const int height) const;
  113. ////////////////////////////////////////////////////////////////////////////////////////////////////
  114. /// @fn virtual int Element::getScrollHeight() const
  115. ///
  116. /// @brief Gets the scroll height property (read-only) of the element.
  117. ///
  118. /// @return The scroll height.
  119. ////////////////////////////////////////////////////////////////////////////////////////////////////
  120. virtual int getScrollHeight() const;
  121. ////////////////////////////////////////////////////////////////////////////////////////////////////
  122. /// @fn virtual int Element::getChildNodeCount() const
  123. ///
  124. /// @brief Gets a child node count.
  125. ///
  126. /// @return The child node count.
  127. ////////////////////////////////////////////////////////////////////////////////////////////////////
  128. virtual int getChildNodeCount() const;
  129. ////////////////////////////////////////////////////////////////////////////////////////////////////
  130. /// @fn virtual ElementPtr Element::getChildNode(int idx) const
  131. ///
  132. /// @brief Gets a child node.
  133. ///
  134. /// @author Richard Bateman
  135. /// @date 10/15/2010
  136. ///
  137. /// @param idx Zero-based index of the node to fetch.
  138. ///
  139. /// @return The child node.
  140. ////////////////////////////////////////////////////////////////////////////////////////////////////
  141. virtual ElementPtr getChildNode(const int idx) const;
  142. ////////////////////////////////////////////////////////////////////////////////////////////////////
  143. /// @fn virtual ElementPtr Element::getParentNode() const
  144. ///
  145. /// @brief Gets the parent node of the current Element
  146. ///
  147. /// @return The parent node.
  148. ////////////////////////////////////////////////////////////////////////////////////////////////////
  149. virtual ElementPtr getParentNode() const;
  150. ////////////////////////////////////////////////////////////////////////////////////////////////////
  151. /// @fn virtual ElementPtr Element::getElement(const std::string& name) const
  152. ///
  153. /// @brief Gets a child element of the specified name
  154. ///
  155. /// This is the rough equivilent of the following javascript command
  156. ///
  157. /// @code
  158. /// // if name = child
  159. /// var el2 = element.child;
  160. /// // or
  161. /// var el2 = element[name];
  162. /// @endcode
  163. ///
  164. /// @param name The name of the subelement.
  165. ///
  166. /// @return The child element.
  167. ////////////////////////////////////////////////////////////////////////////////////////////////////
  168. virtual ElementPtr getElement(const std::string& name) const
  169. {
  170. JSObjectPtr api = getProperty<FB::JSObjectPtr>(name);
  171. ElementPtr retVal((api) ? new Element(api) : NULL);
  172. return retVal;
  173. }
  174. ////////////////////////////////////////////////////////////////////////////////////////////////////
  175. /// @fn virtual ElementPtr Element::getElement(int idx) const
  176. ///
  177. /// @brief Gets a child element of the specified name
  178. ///
  179. /// This is the rough equivilent of the following javascript command
  180. ///
  181. /// @code
  182. /// var el2 = element[idx];
  183. /// @endcode
  184. ///
  185. /// @param idx Zero-based index of the element to fetch.
  186. ///
  187. /// @return The child element.
  188. ////////////////////////////////////////////////////////////////////////////////////////////////////
  189. virtual ElementPtr getElement(const int idx) const
  190. {
  191. JSObjectPtr api = getProperty<FB::JSObjectPtr>(idx);
  192. ElementPtr retVal((api) ? new Element(api) : NULL);
  193. return retVal;
  194. }
  195. virtual std::string getStringAttribute(const std::string& attr) const;
  196. ////////////////////////////////////////////////////////////////////////////////////////////////////
  197. /// @fn virtual ElementPtr getElementById(const std::string& elem_id) const
  198. ///
  199. /// @brief Gets an element from the DOM with the specified id
  200. ///
  201. /// @param elem_id Identifier for the element.
  202. ///
  203. /// @return The element by identifier.
  204. ////////////////////////////////////////////////////////////////////////////////////////////////////
  205. virtual ElementPtr getElementById(const std::string& elem_id) const;
  206. ////////////////////////////////////////////////////////////////////////////////////////////////////
  207. /// @fn virtual std::vector<ElementPtr> getElementsByTagName(const std::string& tagName) const
  208. ///
  209. /// @brief Gets a list of all elements in the document with the specified tag name
  210. ///
  211. /// @param tagName Name of the tag.
  212. ///
  213. /// @return The elements by tag name.
  214. ////////////////////////////////////////////////////////////////////////////////////////////////////
  215. virtual std::vector<ElementPtr> getElementsByTagName(const std::string& tagName) const;
  216. ////////////////////////////////////////////////////////////////////////////////////////////////////
  217. /// @overload virtual std::vector<ElementPtr> getElementsByTagName(const std::wstring& tagName) const
  218. ////////////////////////////////////////////////////////////////////////////////////////////////////
  219. virtual std::vector<ElementPtr> getElementsByTagName(const std::wstring& tagName) const;
  220. };
  221. }; };
  222. #endif // H_FB_DOM_ELEMENT