ElementInfo.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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-2023 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. #include "ElementInfo.h"
  29. #include "../../Include/RmlUi/Core/Context.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "../../Include/RmlUi/Core/ElementText.h"
  32. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  33. #include "../../Include/RmlUi/Core/Factory.h"
  34. #include "../../Include/RmlUi/Core/PropertiesIteratorView.h"
  35. #include "../../Include/RmlUi/Core/Property.h"
  36. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  37. #include "../../Include/RmlUi/Core/RenderManager.h"
  38. #include "../../Include/RmlUi/Core/StyleSheet.h"
  39. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  40. #include "../../Include/RmlUi/Core/SystemInterface.h"
  41. #include "CommonSource.h"
  42. #include "Geometry.h"
  43. #include "InfoSource.h"
  44. #include <algorithm>
  45. namespace Rml {
  46. namespace Debugger {
  47. ElementInfo::ElementInfo(const String& tag) : ElementDocument(tag)
  48. {
  49. hover_element = nullptr;
  50. source_element = nullptr;
  51. enable_element_select = true;
  52. show_source_element = true;
  53. update_source_element = true;
  54. force_update_once = false;
  55. title_dirty = true;
  56. previous_update_time = 0.0;
  57. }
  58. ElementInfo::~ElementInfo()
  59. {
  60. RemoveEventListener(EventId::Click, this);
  61. RemoveEventListener(EventId::Mouseover, this);
  62. RemoveEventListener(EventId::Mouseout, this);
  63. }
  64. bool ElementInfo::Initialise()
  65. {
  66. SetInnerRML(info_rml);
  67. SetId("rmlui-debug-info");
  68. AddEventListener(EventId::Click, this);
  69. AddEventListener(EventId::Mouseover, this);
  70. AddEventListener(EventId::Mouseout, this);
  71. SharedPtr<StyleSheetContainer> style_sheet = Factory::InstanceStyleSheetString(String(common_rcss) + String(info_rcss));
  72. if (!style_sheet)
  73. return false;
  74. SetStyleSheetContainer(std::move(style_sheet));
  75. return true;
  76. }
  77. void ElementInfo::Reset()
  78. {
  79. hover_element = nullptr;
  80. show_source_element = true;
  81. update_source_element = true;
  82. SetSourceElement(nullptr);
  83. }
  84. void ElementInfo::OnUpdate()
  85. {
  86. if (source_element && (update_source_element || force_update_once) && IsVisible())
  87. {
  88. const double t = GetSystemInterface()->GetElapsedTime();
  89. const float dt = (float)(t - previous_update_time);
  90. constexpr float update_interval = 0.3f;
  91. if (dt > update_interval || (force_update_once))
  92. {
  93. if (force_update_once && source_element)
  94. {
  95. // Since an update is being forced, it is possibly because we are reacting to an event and made some changes.
  96. // Update the source element's document to reflect any recent changes.
  97. if (auto document = source_element->GetOwnerDocument())
  98. document->UpdateDocument();
  99. }
  100. force_update_once = false;
  101. UpdateSourceElement();
  102. }
  103. }
  104. if (title_dirty)
  105. {
  106. UpdateTitle();
  107. title_dirty = false;
  108. }
  109. }
  110. void ElementInfo::OnElementDestroy(Element* element)
  111. {
  112. if (hover_element == element)
  113. hover_element = nullptr;
  114. if (source_element == element)
  115. source_element = nullptr;
  116. }
  117. void ElementInfo::RenderHoverElement()
  118. {
  119. if (hover_element)
  120. {
  121. ElementUtilities::ApplyTransform(*hover_element);
  122. for (int i = 0; i < hover_element->GetNumBoxes(); i++)
  123. {
  124. // Render the content area.
  125. Vector2f box_offset;
  126. const Box& element_box = hover_element->GetBox(i, box_offset);
  127. Vector2f size = element_box.GetSize(BoxArea::Border);
  128. size = Vector2f(std::max(size.x, 2.0f), std::max(size.y, 2.0f));
  129. Geometry::RenderOutline(hover_element->GetAbsoluteOffset(BoxArea::Border) + box_offset, size, Colourb(255, 0, 0, 255), 1);
  130. }
  131. }
  132. }
  133. void ElementInfo::RenderSourceElement()
  134. {
  135. if (source_element && show_source_element)
  136. {
  137. ElementUtilities::ApplyTransform(*source_element);
  138. for (int i = 0; i < source_element->GetNumBoxes(); i++)
  139. {
  140. Vector2f box_offset;
  141. const Box& element_box = source_element->GetBox(i, box_offset);
  142. const Vector2f border_offset = box_offset + source_element->GetAbsoluteOffset(BoxArea::Border);
  143. // Content area:
  144. Geometry::RenderBox(border_offset + element_box.GetPosition(BoxArea::Content), element_box.GetSize(), Colourb(158, 214, 237, 128));
  145. // Padding area:
  146. Geometry::RenderBox(border_offset + element_box.GetPosition(BoxArea::Padding), element_box.GetSize(BoxArea::Padding),
  147. border_offset + element_box.GetPosition(BoxArea::Content), element_box.GetSize(), Colourb(135, 122, 214, 128));
  148. // Border area:
  149. Geometry::RenderBox(border_offset + element_box.GetPosition(BoxArea::Border), element_box.GetSize(BoxArea::Border),
  150. border_offset + element_box.GetPosition(BoxArea::Padding), element_box.GetSize(BoxArea::Padding), Colourb(133, 133, 133, 128));
  151. // Border area:
  152. Geometry::RenderBox(border_offset + element_box.GetPosition(BoxArea::Margin), element_box.GetSize(BoxArea::Margin),
  153. border_offset + element_box.GetPosition(BoxArea::Border), element_box.GetSize(BoxArea::Border), Colourb(240, 255, 131, 128));
  154. }
  155. if (Context* context = source_element->GetContext())
  156. {
  157. context->GetRenderManager().SetTransform(nullptr);
  158. Rectanglef bounding_box;
  159. if (ElementUtilities::GetBoundingBox(bounding_box, source_element, BoxArea::Auto))
  160. {
  161. bounding_box.Extend(1.f);
  162. Math::ExpandToPixelGrid(bounding_box);
  163. Geometry::RenderOutline(bounding_box.Position(), bounding_box.Size(), Colourb(255, 255, 255, 200), 1.f);
  164. }
  165. }
  166. }
  167. }
  168. void ElementInfo::ProcessEvent(Event& event)
  169. {
  170. // Only process events if we're visible
  171. if (IsVisible())
  172. {
  173. if (event == EventId::Click)
  174. {
  175. Element* target_element = event.GetTargetElement();
  176. // Deal with clicks on our own elements differently.
  177. if (target_element->GetOwnerDocument() == this)
  178. {
  179. const String& id = event.GetTargetElement()->GetId();
  180. if (id == "close_button")
  181. {
  182. if (IsVisible())
  183. SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  184. }
  185. else if (id == "update_source")
  186. {
  187. update_source_element = !update_source_element;
  188. target_element->SetClass("active", update_source_element);
  189. }
  190. else if (id == "show_source")
  191. {
  192. show_source_element = !target_element->IsClassSet("active");
  193. target_element->SetClass("active", show_source_element);
  194. }
  195. else if (id == "enable_element_select")
  196. {
  197. enable_element_select = !target_element->IsClassSet("active");
  198. target_element->SetClass("active", enable_element_select);
  199. }
  200. else if (target_element->GetTagName() == "pseudo" && source_element)
  201. {
  202. const String name = target_element->GetAttribute<String>("name", "");
  203. if (!name.empty())
  204. {
  205. bool pseudo_active = target_element->IsClassSet("active");
  206. if (name == "focus")
  207. {
  208. if (!pseudo_active)
  209. source_element->Focus();
  210. else if (auto document = source_element->GetOwnerDocument())
  211. document->Focus();
  212. }
  213. else
  214. {
  215. source_element->SetPseudoClass(name, !pseudo_active);
  216. }
  217. force_update_once = true;
  218. }
  219. }
  220. else if (id == "offset_parent")
  221. {
  222. if (source_element)
  223. {
  224. Element* offset_parent = source_element->GetOffsetParent();
  225. if (offset_parent)
  226. SetSourceElement(offset_parent);
  227. }
  228. }
  229. // Check if the id is in the form "a %d" or "c %d" - these are the ancestor or child labels.
  230. else
  231. {
  232. int element_index;
  233. if (sscanf(target_element->GetId().c_str(), "a %d", &element_index) == 1)
  234. {
  235. Element* new_source_element = source_element;
  236. for (int i = 0; i < element_index; i++)
  237. {
  238. if (new_source_element != nullptr)
  239. new_source_element = new_source_element->GetParentNode();
  240. }
  241. SetSourceElement(new_source_element);
  242. }
  243. else if (sscanf(target_element->GetId().c_str(), "c %d", &element_index) == 1)
  244. {
  245. if (source_element != nullptr)
  246. SetSourceElement(source_element->GetChild(element_index));
  247. }
  248. }
  249. event.StopPropagation();
  250. }
  251. // Otherwise we just want to focus on the clicked element (unless it's on a debug element)
  252. else if (enable_element_select && target_element->GetOwnerDocument() != nullptr && !IsDebuggerElement(target_element))
  253. {
  254. Element* new_source_element = target_element;
  255. if (new_source_element != source_element)
  256. {
  257. SetSourceElement(new_source_element);
  258. event.StopPropagation();
  259. }
  260. }
  261. }
  262. else if (event == EventId::Mouseover)
  263. {
  264. Element* target_element = event.GetTargetElement();
  265. ElementDocument* owner_document = target_element->GetOwnerDocument();
  266. if (owner_document == this)
  267. {
  268. // Check if the id is in the form "a %d" or "c %d" - these are the ancestor or child labels.
  269. const String& id = target_element->GetId();
  270. int element_index;
  271. if (sscanf(id.c_str(), "a %d", &element_index) == 1)
  272. {
  273. hover_element = source_element;
  274. for (int i = 0; i < element_index; i++)
  275. {
  276. if (hover_element != nullptr)
  277. hover_element = hover_element->GetParentNode();
  278. }
  279. }
  280. else if (sscanf(id.c_str(), "c %d", &element_index) == 1)
  281. {
  282. if (source_element != nullptr)
  283. hover_element = source_element->GetChild(element_index);
  284. }
  285. else if (id == "offset_parent")
  286. {
  287. if (source_element)
  288. hover_element = source_element->GetOffsetParent();
  289. }
  290. else
  291. {
  292. hover_element = nullptr;
  293. }
  294. if (id == "show_source" && !show_source_element)
  295. {
  296. // Preview the source element view while hovering
  297. show_source_element = true;
  298. }
  299. if (id == "show_source" || id == "update_source" || id == "enable_element_select")
  300. {
  301. title_dirty = true;
  302. }
  303. }
  304. // Otherwise we just want to focus on the clicked element (unless it's on a debug element)
  305. else if (enable_element_select && owner_document && owner_document->GetId().find("rmlui-debug-") != 0)
  306. {
  307. if (Context* context = owner_document->GetContext())
  308. hover_element = context->GetHoverElement();
  309. }
  310. }
  311. else if (event == EventId::Mouseout)
  312. {
  313. Element* target_element = event.GetTargetElement();
  314. ElementDocument* owner_document = target_element->GetOwnerDocument();
  315. if (owner_document == this)
  316. {
  317. const String& id = target_element->GetId();
  318. if (id == "show_source")
  319. {
  320. // Disable the preview of the source element view
  321. if (show_source_element && !target_element->IsClassSet("active"))
  322. show_source_element = false;
  323. }
  324. if (id == "show_source" || id == "update_source" || id == "enable_element_select")
  325. {
  326. title_dirty = true;
  327. }
  328. }
  329. else if (enable_element_select && owner_document && owner_document->GetId().find("rmlui-debug-") != 0)
  330. {
  331. // Update hover element also on MouseOut as we may not get a MouseOver event when moving the mouse to a parent element.
  332. if (Context* context = owner_document->GetContext())
  333. hover_element = context->GetHoverElement();
  334. }
  335. }
  336. }
  337. }
  338. void ElementInfo::SetSourceElement(Element* new_source_element)
  339. {
  340. source_element = new_source_element;
  341. force_update_once = true;
  342. }
  343. void ElementInfo::UpdateSourceElement()
  344. {
  345. previous_update_time = GetSystemInterface()->GetElapsedTime();
  346. title_dirty = true;
  347. // Set the pseudo classes
  348. if (Element* pseudo = GetElementById("pseudo"))
  349. {
  350. StringList list;
  351. if (source_element)
  352. list = source_element->GetActivePseudoClasses();
  353. auto EraseFromList = [](StringList& list, const String& value) {
  354. auto it = std::find(list.begin(), list.end(), value);
  355. if (it == list.end())
  356. return false;
  357. list.erase(it);
  358. return true;
  359. };
  360. // There are some fixed pseudo classes that we always show and iterate through to determine if they are set.
  361. // We also want to show other pseudo classes when they are set, they are added under the #extra element last.
  362. for (int i = 0; i < pseudo->GetNumChildren(); i++)
  363. {
  364. Element* child = pseudo->GetChild(i);
  365. const String name = child->GetAttribute<String>("name", "");
  366. if (!name.empty())
  367. {
  368. bool active = EraseFromList(list, name);
  369. child->SetClass("active", active);
  370. }
  371. else if (child->GetId() == "extra")
  372. {
  373. // First, we iterate through the extra elements and remove those that are no longer active.
  374. for (int j = 0; j < child->GetNumChildren(); j++)
  375. {
  376. Element* grandchild = child->GetChild(j);
  377. const String grandchild_name = grandchild->GetAttribute<String>("name", "");
  378. bool active = (EraseFromList(list, grandchild_name) == 1);
  379. if (!active)
  380. child->RemoveChild(grandchild);
  381. }
  382. // Finally, create new pseudo buttons for the rest of the active pseudo classes.
  383. for (auto& extra_pseudo : list)
  384. {
  385. Element* grandchild = child->AppendChild(CreateElement("pseudo"));
  386. grandchild->SetClass("active", true);
  387. grandchild->SetAttribute("name", extra_pseudo);
  388. grandchild->SetInnerRML(":" + extra_pseudo);
  389. }
  390. }
  391. }
  392. }
  393. // Set the attributes
  394. if (Element* attributes_content = GetElementById("attributes-content"))
  395. {
  396. String attributes;
  397. if (source_element != nullptr)
  398. {
  399. {
  400. String name;
  401. String value;
  402. // The element's attribute list is not always synchronized with its internal values, fetch
  403. // them manually here (see e.g. Element::OnAttributeChange for relevant attributes)
  404. {
  405. name = "id";
  406. value = source_element->GetId();
  407. if (!value.empty())
  408. attributes += CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  409. }
  410. {
  411. name = "class";
  412. value = source_element->GetClassNames();
  413. if (!value.empty())
  414. attributes += CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  415. }
  416. }
  417. for (const auto& pair : source_element->GetAttributes())
  418. {
  419. auto& name = pair.first;
  420. auto& variant = pair.second;
  421. String value = StringUtilities::EncodeRml(variant.Get<String>());
  422. if (name != "class" && name != "style" && name != "id")
  423. attributes += CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  424. }
  425. // Text is not an attribute but useful nonetheless
  426. if (auto text_element = rmlui_dynamic_cast<ElementText*>(source_element))
  427. {
  428. const String& text_content = text_element->GetText();
  429. attributes += CreateString(text_content.size() + 32, "Text: <em>%s</em><br />", text_content.c_str());
  430. }
  431. }
  432. if (attributes.empty())
  433. {
  434. while (attributes_content->HasChildNodes())
  435. attributes_content->RemoveChild(attributes_content->GetChild(0));
  436. attributes_rml.clear();
  437. }
  438. else if (attributes != attributes_rml)
  439. {
  440. attributes_content->SetInnerRML(attributes);
  441. attributes_rml = std::move(attributes);
  442. }
  443. }
  444. // Set the properties
  445. if (Element* properties_content = GetElementById("properties-content"))
  446. {
  447. String properties;
  448. if (source_element != nullptr)
  449. BuildElementPropertiesRML(properties, source_element, source_element);
  450. if (properties.empty())
  451. {
  452. while (properties_content->HasChildNodes())
  453. properties_content->RemoveChild(properties_content->GetChild(0));
  454. properties_rml.clear();
  455. }
  456. else if (properties != properties_rml)
  457. {
  458. properties_content->SetInnerRML(properties);
  459. properties_rml = std::move(properties);
  460. }
  461. }
  462. // Set the events
  463. if (Element* events_content = GetElementById("events-content"))
  464. {
  465. String events;
  466. if (source_element != nullptr)
  467. {
  468. events = source_element->GetEventDispatcherSummary();
  469. }
  470. if (events.empty())
  471. {
  472. while (events_content->HasChildNodes())
  473. events_content->RemoveChild(events_content->GetChild(0));
  474. events_rml.clear();
  475. }
  476. else if (events != events_rml)
  477. {
  478. events_content->SetInnerRML(events);
  479. events_rml = std::move(events);
  480. }
  481. }
  482. // Set the position
  483. if (Element* position_content = GetElementById("position-content"))
  484. {
  485. String position;
  486. if (source_element)
  487. {
  488. const Vector2f element_offset = source_element->GetRelativeOffset(BoxArea::Border);
  489. const auto& box = source_element->GetBox();
  490. const Vector2f element_size = source_element->GetBox().GetSize(BoxArea::Border);
  491. Element* offset_parent = source_element->GetOffsetParent();
  492. const String offset_parent_rml =
  493. (offset_parent ? StringUtilities::EncodeRml(offset_parent->GetAddress(false, false)) : String("<em>none</em>"));
  494. auto box_string = [&box](BoxDirection direction) {
  495. const BoxEdge edge1 = (direction == BoxDirection::Horizontal ? BoxEdge::Left : BoxEdge::Top);
  496. const BoxEdge edge2 = (direction == BoxDirection::Horizontal ? BoxEdge::Right : BoxEdge::Bottom);
  497. const float content_size = (direction == BoxDirection::Horizontal ? box.GetSize().x : box.GetSize().y);
  498. const String edge1_str = ToString(box.GetEdge(BoxArea::Margin, edge1)) + "|" + ToString(box.GetEdge(BoxArea::Border, edge1)) + "|" +
  499. ToString(box.GetEdge(BoxArea::Padding, edge1));
  500. const String edge2_str = ToString(box.GetEdge(BoxArea::Padding, edge2)) + "|" + ToString(box.GetEdge(BoxArea::Border, edge2)) + "|" +
  501. ToString(box.GetEdge(BoxArea::Margin, edge2));
  502. return CreateString(256, "%s &lt;%s&gt; %s", edge1_str.c_str(), ToString(content_size).c_str(), edge2_str.c_str());
  503. };
  504. position = "<span class='name'>left: </span><em>" + ToString(element_offset.x) + "px</em><br/>" + //
  505. "<span class='name'>top: </span><em>" + ToString(element_offset.y) + "px</em><br/>" + //
  506. "<span class='name'>width: </span><em>" + ToString(element_size.x) + "px</em><br/>" + //
  507. "<span class='name'>height: </span><em>" + ToString(element_size.y) + "px</em><br/>" + //
  508. "<span class='name'>offset parent: </span><p style='display: inline' id='offset_parent'>" + offset_parent_rml + "</p><br/>" + //
  509. "<span class='name'>box-x (px): </span>" + box_string(BoxDirection::Horizontal) + "<br/>" + //
  510. "<span class='name'>box-y (px): </span>" + box_string(BoxDirection::Vertical);
  511. }
  512. if (position != position_rml)
  513. {
  514. position_content->SetInnerRML(position);
  515. position_rml = std::move(position);
  516. }
  517. }
  518. // Set the ancestors
  519. if (Element* ancestors_content = GetElementById("ancestors-content"))
  520. {
  521. String ancestors;
  522. Element* element_ancestor = nullptr;
  523. if (source_element != nullptr)
  524. element_ancestor = source_element->GetParentNode();
  525. int ancestor_depth = 1;
  526. while (element_ancestor)
  527. {
  528. String ancestor_name = element_ancestor->GetAddress(false, false);
  529. ancestors += CreateString(ancestor_name.size() + 32, "<p id=\"a %d\">%s</p>", ancestor_depth, ancestor_name.c_str());
  530. element_ancestor = element_ancestor->GetParentNode();
  531. ancestor_depth++;
  532. }
  533. if (ancestors.empty())
  534. {
  535. while (ancestors_content->HasChildNodes())
  536. ancestors_content->RemoveChild(ancestors_content->GetFirstChild());
  537. ancestors_rml.clear();
  538. }
  539. else if (ancestors != ancestors_rml)
  540. {
  541. ancestors_content->SetInnerRML(ancestors);
  542. ancestors_rml = std::move(ancestors);
  543. }
  544. }
  545. // Set the children
  546. if (Element* children_content = GetElementById("children-content"))
  547. {
  548. String children;
  549. if (source_element != nullptr)
  550. {
  551. const int num_dom_children = (source_element->GetNumChildren(false));
  552. for (int i = 0; i < source_element->GetNumChildren(true); i++)
  553. {
  554. Element* child = source_element->GetChild(i);
  555. // If this is a debugger document, do not show it.
  556. if (IsDebuggerElement(child))
  557. continue;
  558. String child_name = child->GetAddress(false, false);
  559. auto document = rmlui_dynamic_cast<ElementDocument*>(child);
  560. if (document && !document->GetTitle().empty())
  561. child_name += " (" + document->GetTitle() + ')';
  562. const char* non_dom_string = (i >= num_dom_children ? " class=\"non_dom\"" : "");
  563. children += CreateString(child_name.size() + 40, "<p id=\"c %d\"%s>%s</p>", i, non_dom_string, child_name.c_str());
  564. }
  565. }
  566. if (children.empty())
  567. {
  568. while (children_content->HasChildNodes())
  569. children_content->RemoveChild(children_content->GetChild(0));
  570. children_rml.clear();
  571. }
  572. else if (children != children_rml)
  573. {
  574. children_content->SetInnerRML(children);
  575. children_rml = std::move(children);
  576. }
  577. }
  578. }
  579. void ElementInfo::BuildElementPropertiesRML(String& property_rml, Element* element, Element* primary_element)
  580. {
  581. NamedPropertyList property_list;
  582. for (auto it = element->IterateLocalProperties(); !it.AtEnd(); ++it)
  583. {
  584. PropertyId property_id = it.GetId();
  585. const String& property_name = it.GetName();
  586. const Property* prop = &it.GetProperty();
  587. // Check that this property isn't overridden or just not inherited.
  588. if (primary_element->GetProperty(property_id) != prop)
  589. continue;
  590. property_list.push_back(NamedProperty{property_name, prop});
  591. }
  592. std::sort(property_list.begin(), property_list.end(), [](const NamedProperty& a, const NamedProperty& b) {
  593. if (a.second->source && !b.second->source)
  594. return false;
  595. if (!a.second->source && b.second->source)
  596. return true;
  597. if (a.second->specificity < b.second->specificity)
  598. return false;
  599. if (a.second->specificity > b.second->specificity)
  600. return true;
  601. if (a.second->definition && !b.second->definition)
  602. return false;
  603. if (!a.second->definition && b.second->definition)
  604. return true;
  605. const String& a_name = StyleSheetSpecification::GetPropertyName(a.second->definition->GetId());
  606. const String& b_name = StyleSheetSpecification::GetPropertyName(b.second->definition->GetId());
  607. return a_name < b_name;
  608. });
  609. if (!property_list.empty())
  610. {
  611. // Print the 'inherited from ...' header if we're not the primary element.
  612. if (element != primary_element)
  613. {
  614. property_rml += "<h3 class='strong'>inherited from " + element->GetAddress(false, false) + "</h3>";
  615. }
  616. const PropertySource* previous_source = nullptr;
  617. bool first_iteration = true;
  618. for (auto& named_property : property_list)
  619. {
  620. auto& source = named_property.second->source;
  621. if (source.get() != previous_source || first_iteration)
  622. {
  623. previous_source = source.get();
  624. first_iteration = false;
  625. // Print the rule name header.
  626. if (source)
  627. {
  628. String str_line_number;
  629. TypeConverter<int, String>::Convert(source->line_number, str_line_number);
  630. property_rml += "<h3>" + source->rule_name + "</h3>";
  631. property_rml += "<h4>" + source->path + " : " + str_line_number + "</h4>";
  632. }
  633. else
  634. {
  635. property_rml += "<h3><em>inline</em></h3>";
  636. }
  637. }
  638. BuildPropertyRML(property_rml, named_property.first, named_property.second);
  639. }
  640. }
  641. if (element->GetParentNode() != nullptr)
  642. BuildElementPropertiesRML(property_rml, element->GetParentNode(), primary_element);
  643. }
  644. void ElementInfo::BuildPropertyRML(String& property_rml, const String& name, const Property* property)
  645. {
  646. const String property_value = property->ToString();
  647. property_rml += "<span class='name'>" + name + "</span>: " + property_value + "<br/>";
  648. }
  649. void ElementInfo::UpdateTitle()
  650. {
  651. auto title_content = GetElementById("title-content");
  652. auto enable_select = GetElementById("enable_element_select");
  653. auto show_source = GetElementById("show_source");
  654. auto update_source = GetElementById("update_source");
  655. if (title_content && enable_select && show_source && update_source)
  656. {
  657. if (enable_select->IsPseudoClassSet("hover"))
  658. title_content->SetInnerRML("<em>(select elements)</em>");
  659. else if (show_source->IsPseudoClassSet("hover"))
  660. title_content->SetInnerRML("<em>(draw element dimensions)</em>");
  661. else if (update_source->IsPseudoClassSet("hover"))
  662. title_content->SetInnerRML("<em>(update info continuously)</em>");
  663. else if (source_element)
  664. title_content->SetInnerRML(source_element->GetTagName());
  665. else
  666. title_content->SetInnerRML("Element Information");
  667. }
  668. }
  669. bool ElementInfo::IsDebuggerElement(Element* element)
  670. {
  671. return element->GetOwnerDocument()->GetId().find("rmlui-debug-") == 0;
  672. }
  673. } // namespace Debugger
  674. } // namespace Rml