CefDomDocument.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 document. The methods of this class should only
  10. /// be called on the render process main thread thread.
  11. /// </summary>
  12. public sealed unsafe partial class CefDomDocument
  13. {
  14. /// <summary>
  15. /// Returns the document type.
  16. /// </summary>
  17. public CefDomDocumentType DocumentType
  18. {
  19. get { return cef_domdocument_t.get_type(_self); }
  20. }
  21. /// <summary>
  22. /// Returns the root document node.
  23. /// </summary>
  24. public CefDomNode Root
  25. {
  26. get
  27. {
  28. return CefDomNode.FromNative(
  29. cef_domdocument_t.get_document(_self)
  30. );
  31. }
  32. }
  33. /// <summary>
  34. /// Returns the BODY node of an HTML document.
  35. /// </summary>
  36. public CefDomNode Body
  37. {
  38. get
  39. {
  40. return CefDomNode.FromNative(
  41. cef_domdocument_t.get_body(_self)
  42. );
  43. }
  44. }
  45. /// <summary>
  46. /// Returns the HEAD node of an HTML document.
  47. /// </summary>
  48. public CefDomNode Head
  49. {
  50. get
  51. {
  52. return CefDomNode.FromNative(
  53. cef_domdocument_t.get_head(_self)
  54. );
  55. }
  56. }
  57. /// <summary>
  58. /// Returns the title of an HTML document.
  59. /// </summary>
  60. public string Title
  61. {
  62. get
  63. {
  64. var n_result = cef_domdocument_t.get_title(_self);
  65. return cef_string_userfree.ToString(n_result);
  66. }
  67. }
  68. /// <summary>
  69. /// Returns the document element with the specified ID value.
  70. /// </summary>
  71. public CefDomNode GetElementById(string id)
  72. {
  73. fixed (char* id_str = id)
  74. {
  75. var n_id = new cef_string_t(id_str, id.Length);
  76. return CefDomNode.FromNativeOrNull(
  77. cef_domdocument_t.get_element_by_id(_self, &n_id)
  78. );
  79. }
  80. }
  81. /// <summary>
  82. /// Returns the node that currently has keyboard focus.
  83. /// </summary>
  84. public CefDomNode FocusedNode
  85. {
  86. get
  87. {
  88. return CefDomNode.FromNativeOrNull(
  89. cef_domdocument_t.get_focused_node(_self)
  90. );
  91. }
  92. }
  93. /// <summary>
  94. /// Returns true if a portion of the document is selected.
  95. /// </summary>
  96. public bool HasSelection
  97. {
  98. get { return cef_domdocument_t.has_selection(_self) != 0; }
  99. }
  100. /// <summary>
  101. /// Returns the selection offset within the start node.
  102. /// </summary>
  103. public int SelectionStartOffset
  104. {
  105. get { return cef_domdocument_t.get_selection_start_offset(_self); }
  106. }
  107. /// <summary>
  108. /// Returns the selection offset within the end node.
  109. /// </summary>
  110. public int GetSelectionEndOffset
  111. {
  112. get { return cef_domdocument_t.get_selection_end_offset(_self); }
  113. }
  114. /// <summary>
  115. /// Returns the contents of this selection as markup.
  116. /// </summary>
  117. public string SelectionAsMarkup
  118. {
  119. get
  120. {
  121. var n_result = cef_domdocument_t.get_selection_as_markup(_self);
  122. return cef_string_userfree.ToString(n_result);
  123. }
  124. }
  125. /// <summary>
  126. /// Returns the contents of this selection as text.
  127. /// </summary>
  128. public string GetSelectionAsText
  129. {
  130. get
  131. {
  132. var n_result = cef_domdocument_t.get_selection_as_text(_self);
  133. return cef_string_userfree.ToString(n_result);
  134. }
  135. }
  136. /// <summary>
  137. /// Returns the base URL for the document.
  138. /// </summary>
  139. public string BaseUrl
  140. {
  141. get
  142. {
  143. var n_result = cef_domdocument_t.get_base_url(_self);
  144. return cef_string_userfree.ToString(n_result);
  145. }
  146. }
  147. /// <summary>
  148. /// Returns a complete URL based on the document base URL and the specified
  149. /// partial URL.
  150. /// </summary>
  151. public string GetCompleteUrl(string partialUrl)
  152. {
  153. fixed (char* partialUrl_str = partialUrl)
  154. {
  155. var n_partialUrl = new cef_string_t(partialUrl_str, partialUrl.Length);
  156. var n_result = cef_domdocument_t.get_complete_url(_self, &n_partialUrl);
  157. return cef_string_userfree.ToString(n_result);
  158. }
  159. }
  160. }
  161. }