CefDomNode.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Class used to represent a DOM node. The methods of this class should only be
  10. /// called on the render process main thread.
  11. /// </summary>
  12. public sealed unsafe partial class CefDomNode
  13. {
  14. /// <summary>
  15. /// Returns the type for this node.
  16. /// </summary>
  17. public CefDomNodeType NodeType
  18. {
  19. get { return cef_domnode_t.get_type(_self); }
  20. }
  21. /// <summary>
  22. /// Returns true if this is a text node.
  23. /// </summary>
  24. public bool IsText
  25. {
  26. get { return cef_domnode_t.is_text(_self) != 0; }
  27. }
  28. /// <summary>
  29. /// Returns true if this is an element node.
  30. /// </summary>
  31. public bool IsElement
  32. {
  33. get { return cef_domnode_t.is_element(_self) != 0; }
  34. }
  35. /// <summary>
  36. /// Returns true if this is an editable node.
  37. /// </summary>
  38. public bool IsEditable
  39. {
  40. get { return cef_domnode_t.is_editable(_self) != 0; }
  41. }
  42. /// <summary>
  43. /// Returns true if this is a form control element node.
  44. /// </summary>
  45. public bool IsFormControlElement
  46. {
  47. get { return cef_domnode_t.is_form_control_element(_self) != 0; }
  48. }
  49. /// <summary>
  50. /// Returns the type of this form control element node.
  51. /// </summary>
  52. public string FormControlElementType
  53. {
  54. get
  55. {
  56. var n_result = cef_domnode_t.get_form_control_element_type(_self);
  57. return cef_string_userfree.ToString(n_result);
  58. }
  59. }
  60. /// <summary>
  61. /// Returns true if this object is pointing to the same handle as |that|
  62. /// object.
  63. /// </summary>
  64. public bool IsSame(CefDomNode that)
  65. {
  66. return cef_domnode_t.is_same(_self, that.ToNative()) != 0;
  67. }
  68. /// <summary>
  69. /// Returns the name of this node.
  70. /// </summary>
  71. public string Name
  72. {
  73. get
  74. {
  75. var n_result = cef_domnode_t.get_name(_self);
  76. return cef_string_userfree.ToString(n_result);
  77. }
  78. }
  79. /// <summary>
  80. /// Returns the value of this node.
  81. /// </summary>
  82. public string Value
  83. {
  84. get
  85. {
  86. var n_result = cef_domnode_t.get_value(_self);
  87. return cef_string_userfree.ToString(n_result);
  88. }
  89. }
  90. /// <summary>
  91. /// Set the value of this node. Returns true on success.
  92. /// </summary>
  93. public bool SetValue(string value)
  94. {
  95. fixed (char* value_str = value)
  96. {
  97. var n_value = new cef_string_t(value_str, value != null ? value.Length : 0);
  98. return cef_domnode_t.set_value(_self, &n_value) != 0;
  99. }
  100. }
  101. /// <summary>
  102. /// Returns the contents of this node as markup.
  103. /// </summary>
  104. public string GetAsMarkup()
  105. {
  106. var n_result = cef_domnode_t.get_as_markup(_self);
  107. return cef_string_userfree.ToString(n_result);
  108. }
  109. /// <summary>
  110. /// Returns the document associated with this node.
  111. /// </summary>
  112. public CefDomDocument Document
  113. {
  114. get
  115. {
  116. return CefDomDocument.FromNative(
  117. cef_domnode_t.get_document(_self)
  118. );
  119. }
  120. }
  121. /// <summary>
  122. /// Returns the parent node.
  123. /// </summary>
  124. public CefDomNode Parent
  125. {
  126. get
  127. {
  128. return CefDomNode.FromNativeOrNull(
  129. cef_domnode_t.get_parent(_self)
  130. );
  131. }
  132. }
  133. /// <summary>
  134. /// Returns the previous sibling node.
  135. /// </summary>
  136. public CefDomNode PreviousSibling
  137. {
  138. get
  139. {
  140. return CefDomNode.FromNativeOrNull(
  141. cef_domnode_t.get_previous_sibling(_self)
  142. );
  143. }
  144. }
  145. /// <summary>
  146. /// Returns the next sibling node.
  147. /// </summary>
  148. public CefDomNode NextSibling
  149. {
  150. get
  151. {
  152. return CefDomNode.FromNativeOrNull(
  153. cef_domnode_t.get_next_sibling(_self)
  154. );
  155. }
  156. }
  157. /// <summary>
  158. /// Returns true if this node has child nodes.
  159. /// </summary>
  160. public bool HasChildren
  161. {
  162. get { return cef_domnode_t.has_children(_self) != 0; }
  163. }
  164. /// <summary>
  165. /// Return the first child node.
  166. /// </summary>
  167. public CefDomNode FirstChild
  168. {
  169. get
  170. {
  171. return CefDomNode.FromNativeOrNull(
  172. cef_domnode_t.get_first_child(_self)
  173. );
  174. }
  175. }
  176. /// <summary>
  177. /// Returns the last child node.
  178. /// </summary>
  179. public CefDomNode LastChild
  180. {
  181. get
  182. {
  183. return CefDomNode.FromNativeOrNull(
  184. cef_domnode_t.get_last_child(_self)
  185. );
  186. }
  187. }
  188. /// <summary>
  189. /// The following methods are valid only for element nodes.
  190. /// Returns the tag name of this element.
  191. /// </summary>
  192. public string ElementTagName
  193. {
  194. get
  195. {
  196. var n_result = cef_domnode_t.get_element_tag_name(_self);
  197. return cef_string_userfree.ToString(n_result);
  198. }
  199. }
  200. /// <summary>
  201. /// Returns true if this element has attributes.
  202. /// </summary>
  203. public bool HasAttributes
  204. {
  205. get { return cef_domnode_t.has_element_attributes(_self) != 0; }
  206. }
  207. /// <summary>
  208. /// Returns true if this element has an attribute named |attrName|.
  209. /// </summary>
  210. public bool HasAttribute(string attrName)
  211. {
  212. fixed (char* attrName_str = attrName)
  213. {
  214. var n_attrName = new cef_string_t(attrName_str, attrName.Length);
  215. return cef_domnode_t.has_element_attribute(_self, &n_attrName) != 0;
  216. }
  217. }
  218. /// <summary>
  219. /// Returns the element attribute named |attrName|.
  220. /// </summary>
  221. public string GetAttribute(string attrName)
  222. {
  223. fixed (char* attrName_str = attrName)
  224. {
  225. var n_attrName = new cef_string_t(attrName_str, attrName.Length);
  226. var n_result = cef_domnode_t.get_element_attribute(_self, &n_attrName);
  227. return cef_string_userfree.ToString(n_result);
  228. }
  229. }
  230. /// <summary>
  231. /// Returns a map of all element attributes.
  232. /// </summary>
  233. public IDictionary<string, string> GetAttributes()
  234. {
  235. var attrMap = libcef.string_map_alloc();
  236. cef_domnode_t.get_element_attributes(_self, attrMap);
  237. var result = cef_string_map.ToDictionary(attrMap);
  238. libcef.string_map_free(attrMap);
  239. return result;
  240. }
  241. /// <summary>
  242. /// Set the value for the element attribute named |attrName|. Returns true on
  243. /// success.
  244. /// </summary>
  245. public bool SetAttribute(string attrName, string value)
  246. {
  247. fixed (char* attrName_str = attrName)
  248. fixed (char* value_str = value)
  249. {
  250. var n_attrName = new cef_string_t(attrName_str, attrName.Length);
  251. var n_value = new cef_string_t(value_str, value != null ? value.Length : 0);
  252. return cef_domnode_t.set_element_attribute(_self, &n_attrName, &n_value) != 0;
  253. }
  254. }
  255. /// <summary>
  256. /// Returns the inner text of the element.
  257. /// </summary>
  258. public string InnerText
  259. {
  260. get
  261. {
  262. var n_result = cef_domnode_t.get_element_inner_text(_self);
  263. return cef_string_userfree.ToString(n_result);
  264. }
  265. }
  266. /// <summary>
  267. /// Returns the bounds of the element.
  268. /// </summary>
  269. public CefRectangle GetElementBounds()
  270. {
  271. var n_result = cef_domnode_t.get_element_bounds(_self);
  272. return new CefRectangle(
  273. n_result.x,
  274. n_result.y,
  275. n_result.width,
  276. n_result.height
  277. );
  278. }
  279. }
  280. }