ElementInfo.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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) : ElementDebugDocument(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 = 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. Hide();
  183. }
  184. else if (id == "update_source")
  185. {
  186. update_source_element = !update_source_element;
  187. target_element->SetClass("active", update_source_element);
  188. }
  189. else if (id == "show_source")
  190. {
  191. show_source_element = !target_element->IsClassSet("active");
  192. target_element->SetClass("active", show_source_element);
  193. }
  194. else if (id == "enable_element_select")
  195. {
  196. enable_element_select = !target_element->IsClassSet("active");
  197. target_element->SetClass("active", enable_element_select);
  198. }
  199. else if (target_element->GetTagName() == "pseudo" && source_element)
  200. {
  201. const String name = target_element->GetAttribute<String>("name", "");
  202. if (!name.empty())
  203. {
  204. bool pseudo_active = target_element->IsClassSet("active");
  205. if (name == "focus")
  206. {
  207. if (!pseudo_active)
  208. source_element->Focus();
  209. else if (auto document = source_element->GetOwnerDocument())
  210. document->Focus();
  211. }
  212. else
  213. {
  214. source_element->SetPseudoClass(name, !pseudo_active);
  215. }
  216. force_update_once = true;
  217. }
  218. }
  219. else if (id == "offset_parent")
  220. {
  221. if (source_element)
  222. {
  223. Element* offset_parent = source_element->GetOffsetParent();
  224. if (offset_parent)
  225. SetSourceElement(offset_parent);
  226. }
  227. }
  228. // Check if the id is in the form "a %d" or "c %d" - these are the ancestor or child labels.
  229. else
  230. {
  231. int element_index;
  232. if (sscanf(target_element->GetId().c_str(), "a %d", &element_index) == 1)
  233. {
  234. Element* new_source_element = source_element;
  235. for (int i = 0; i < element_index; i++)
  236. {
  237. if (new_source_element != nullptr)
  238. new_source_element = new_source_element->GetParentNode();
  239. }
  240. SetSourceElement(new_source_element);
  241. }
  242. else if (sscanf(target_element->GetId().c_str(), "c %d", &element_index) == 1)
  243. {
  244. if (source_element != nullptr)
  245. SetSourceElement(source_element->GetChild(element_index));
  246. }
  247. }
  248. event.StopPropagation();
  249. }
  250. // Otherwise we just want to focus on the clicked element (unless it's on a debug element)
  251. else if (enable_element_select && target_element->GetOwnerDocument() != nullptr && !IsDebuggerElement(target_element))
  252. {
  253. Element* new_source_element = target_element;
  254. if (new_source_element != source_element)
  255. {
  256. SetSourceElement(new_source_element);
  257. event.StopPropagation();
  258. }
  259. }
  260. }
  261. else if (event == EventId::Mouseover)
  262. {
  263. Element* target_element = event.GetTargetElement();
  264. ElementDocument* owner_document = target_element->GetOwnerDocument();
  265. if (owner_document == this)
  266. {
  267. // Check if the id is in the form "a %d" or "c %d" - these are the ancestor or child labels.
  268. const String& id = target_element->GetId();
  269. int element_index;
  270. if (sscanf(id.c_str(), "a %d", &element_index) == 1)
  271. {
  272. hover_element = source_element;
  273. for (int i = 0; i < element_index; i++)
  274. {
  275. if (hover_element != nullptr)
  276. hover_element = hover_element->GetParentNode();
  277. }
  278. }
  279. else if (sscanf(id.c_str(), "c %d", &element_index) == 1)
  280. {
  281. if (source_element != nullptr)
  282. hover_element = source_element->GetChild(element_index);
  283. }
  284. else if (id == "offset_parent")
  285. {
  286. if (source_element)
  287. hover_element = source_element->GetOffsetParent();
  288. }
  289. else
  290. {
  291. hover_element = nullptr;
  292. }
  293. if (id == "show_source" && !show_source_element)
  294. {
  295. // Preview the source element view while hovering
  296. show_source_element = true;
  297. }
  298. if (id == "show_source" || id == "update_source" || id == "enable_element_select")
  299. {
  300. title_dirty = true;
  301. }
  302. }
  303. // Otherwise we just want to focus on the clicked element (unless it's on a debug element)
  304. else if (enable_element_select && owner_document && owner_document->GetId().find("rmlui-debug-") != 0)
  305. {
  306. if (Context* context = owner_document->GetContext())
  307. hover_element = context->GetHoverElement();
  308. }
  309. }
  310. else if (event == EventId::Mouseout)
  311. {
  312. Element* target_element = event.GetTargetElement();
  313. ElementDocument* owner_document = target_element->GetOwnerDocument();
  314. if (owner_document == this)
  315. {
  316. const String& id = target_element->GetId();
  317. if (id == "show_source")
  318. {
  319. // Disable the preview of the source element view
  320. if (show_source_element && !target_element->IsClassSet("active"))
  321. show_source_element = false;
  322. }
  323. if (id == "show_source" || id == "update_source" || id == "enable_element_select")
  324. {
  325. title_dirty = true;
  326. }
  327. }
  328. else if (enable_element_select && owner_document && owner_document->GetId().find("rmlui-debug-") != 0)
  329. {
  330. // Update hover element also on MouseOut as we may not get a MouseOver event when moving the mouse to a parent element.
  331. if (Context* context = owner_document->GetContext())
  332. hover_element = context->GetHoverElement();
  333. }
  334. }
  335. }
  336. }
  337. void ElementInfo::SetSourceElement(Element* new_source_element)
  338. {
  339. source_element = new_source_element;
  340. force_update_once = true;
  341. }
  342. void ElementInfo::UpdateSourceElement()
  343. {
  344. previous_update_time = GetSystemInterface()->GetElapsedTime();
  345. title_dirty = true;
  346. // Set the pseudo classes
  347. if (Element* pseudo = GetElementById("pseudo"))
  348. {
  349. StringList list;
  350. if (source_element)
  351. list = source_element->GetActivePseudoClasses();
  352. auto EraseFromList = [](StringList& list, const String& value) {
  353. auto it = std::find(list.begin(), list.end(), value);
  354. if (it == list.end())
  355. return false;
  356. list.erase(it);
  357. return true;
  358. };
  359. // There are some fixed pseudo classes that we always show and iterate through to determine if they are set.
  360. // We also want to show other pseudo classes when they are set, they are added under the #extra element last.
  361. for (int i = 0; i < pseudo->GetNumChildren(); i++)
  362. {
  363. Element* child = pseudo->GetChild(i);
  364. const String name = child->GetAttribute<String>("name", "");
  365. if (!name.empty())
  366. {
  367. bool active = EraseFromList(list, name);
  368. child->SetClass("active", active);
  369. }
  370. else if (child->GetId() == "extra")
  371. {
  372. // First, we iterate through the extra elements and remove those that are no longer active.
  373. for (int j = 0; j < child->GetNumChildren(); j++)
  374. {
  375. Element* grandchild = child->GetChild(j);
  376. const String grandchild_name = grandchild->GetAttribute<String>("name", "");
  377. bool active = (EraseFromList(list, grandchild_name) == 1);
  378. if (!active)
  379. child->RemoveChild(grandchild);
  380. }
  381. // Finally, create new pseudo buttons for the rest of the active pseudo classes.
  382. for (auto& extra_pseudo : list)
  383. {
  384. Element* grandchild = child->AppendChild(CreateElement("pseudo"));
  385. grandchild->SetClass("active", true);
  386. grandchild->SetAttribute("name", extra_pseudo);
  387. grandchild->SetInnerRML(":" + extra_pseudo);
  388. }
  389. }
  390. }
  391. }
  392. // Set the attributes
  393. if (Element* attributes_content = GetElementById("attributes-content"))
  394. {
  395. String attributes;
  396. if (source_element != nullptr)
  397. {
  398. {
  399. String name;
  400. String value;
  401. // The element's attribute list is not always synchronized with its internal values, fetch
  402. // them manually here (see e.g. Element::OnAttributeChange for relevant attributes)
  403. {
  404. name = "id";
  405. value = source_element->GetId();
  406. if (!value.empty())
  407. attributes += CreateString("%s: <em>%s</em><br />", name.c_str(), value.c_str());
  408. }
  409. {
  410. name = "class";
  411. value = source_element->GetClassNames();
  412. if (!value.empty())
  413. attributes += CreateString("%s: <em>%s</em><br />", name.c_str(), value.c_str());
  414. }
  415. }
  416. for (const auto& pair : source_element->GetAttributes())
  417. {
  418. auto& name = pair.first;
  419. auto& variant = pair.second;
  420. String value = StringUtilities::EncodeRml(variant.Get<String>());
  421. if (name != "class" && name != "style" && name != "id")
  422. attributes += CreateString("%s: <em>%s</em><br />", name.c_str(), value.c_str());
  423. }
  424. // Text is not an attribute but useful nonetheless
  425. if (auto text_element = rmlui_dynamic_cast<ElementText*>(source_element))
  426. {
  427. const String& text_content = text_element->GetText();
  428. attributes += CreateString("Text: <em>%s</em><br />", text_content.c_str());
  429. }
  430. }
  431. if (attributes.empty())
  432. {
  433. while (attributes_content->HasChildNodes())
  434. attributes_content->RemoveChild(attributes_content->GetChild(0));
  435. attributes_rml.clear();
  436. }
  437. else if (attributes != attributes_rml)
  438. {
  439. attributes_content->SetInnerRML(attributes);
  440. attributes_rml = std::move(attributes);
  441. }
  442. }
  443. // Set the properties
  444. if (Element* properties_content = GetElementById("properties-content"))
  445. {
  446. String properties;
  447. if (source_element != nullptr)
  448. BuildElementPropertiesRML(properties, source_element, source_element);
  449. if (properties != properties_rml)
  450. {
  451. properties_content->SetInnerRML(properties);
  452. properties_rml = std::move(properties);
  453. }
  454. }
  455. // Set the events
  456. if (Element* events_content = GetElementById("events-content"))
  457. {
  458. String events;
  459. if (source_element != nullptr)
  460. {
  461. events = source_element->GetEventDispatcherSummary();
  462. }
  463. if (events.empty())
  464. {
  465. while (events_content->HasChildNodes())
  466. events_content->RemoveChild(events_content->GetChild(0));
  467. events_rml.clear();
  468. }
  469. else if (events != events_rml)
  470. {
  471. events_content->SetInnerRML(events);
  472. events_rml = std::move(events);
  473. }
  474. }
  475. // Set the position
  476. if (Element* position_content = GetElementById("position-content"))
  477. {
  478. String position;
  479. if (source_element)
  480. {
  481. const Vector2f element_offset = source_element->GetRelativeOffset(BoxArea::Border);
  482. const auto& box = source_element->GetBox();
  483. const Vector2f element_size = source_element->GetBox().GetSize(BoxArea::Border);
  484. Element* offset_parent = source_element->GetOffsetParent();
  485. const String offset_parent_rml =
  486. (offset_parent ? StringUtilities::EncodeRml(offset_parent->GetAddress(false, false)) : String("<em>none</em>"));
  487. auto box_string = [&box](BoxDirection direction) {
  488. const BoxEdge edge1 = (direction == BoxDirection::Horizontal ? BoxEdge::Left : BoxEdge::Top);
  489. const BoxEdge edge2 = (direction == BoxDirection::Horizontal ? BoxEdge::Right : BoxEdge::Bottom);
  490. const float content_size = (direction == BoxDirection::Horizontal ? box.GetSize().x : box.GetSize().y);
  491. const String edge1_str = ToString(box.GetEdge(BoxArea::Margin, edge1)) + "|" + ToString(box.GetEdge(BoxArea::Border, edge1)) + "|" +
  492. ToString(box.GetEdge(BoxArea::Padding, edge1));
  493. const String edge2_str = ToString(box.GetEdge(BoxArea::Padding, edge2)) + "|" + ToString(box.GetEdge(BoxArea::Border, edge2)) + "|" +
  494. ToString(box.GetEdge(BoxArea::Margin, edge2));
  495. return CreateString("%s &lt;%s&gt; %s", edge1_str.c_str(), ToString(content_size).c_str(), edge2_str.c_str());
  496. };
  497. position = "<span class='name'>left: </span><em>" + ToString(element_offset.x) + "px</em><br/>" + //
  498. "<span class='name'>top: </span><em>" + ToString(element_offset.y) + "px</em><br/>" + //
  499. "<span class='name'>width: </span><em>" + ToString(element_size.x) + "px</em><br/>" + //
  500. "<span class='name'>height: </span><em>" + ToString(element_size.y) + "px</em><br/>" + //
  501. "<span class='name'>offset parent: </span><p style='display: inline' id='offset_parent'>" + offset_parent_rml + "</p><br/>" + //
  502. "<span class='name'>box-x (px): </span>" + box_string(BoxDirection::Horizontal) + "<br/>" + //
  503. "<span class='name'>box-y (px): </span>" + box_string(BoxDirection::Vertical);
  504. }
  505. if (position != position_rml)
  506. {
  507. position_content->SetInnerRML(position);
  508. position_rml = std::move(position);
  509. }
  510. }
  511. // Set the ancestors
  512. if (Element* ancestors_content = GetElementById("ancestors-content"))
  513. {
  514. String ancestors;
  515. Element* element_ancestor = nullptr;
  516. if (source_element != nullptr)
  517. element_ancestor = source_element->GetParentNode();
  518. int ancestor_depth = 1;
  519. while (element_ancestor)
  520. {
  521. String ancestor_name = element_ancestor->GetAddress(false, false);
  522. ancestors += CreateString("<p id=\"a %d\">%s</p>", ancestor_depth, ancestor_name.c_str());
  523. element_ancestor = element_ancestor->GetParentNode();
  524. ancestor_depth++;
  525. }
  526. if (ancestors.empty())
  527. {
  528. while (ancestors_content->HasChildNodes())
  529. ancestors_content->RemoveChild(ancestors_content->GetFirstChild());
  530. ancestors_rml.clear();
  531. }
  532. else if (ancestors != ancestors_rml)
  533. {
  534. ancestors_content->SetInnerRML(ancestors);
  535. ancestors_rml = std::move(ancestors);
  536. }
  537. }
  538. // Set the children
  539. if (Element* children_content = GetElementById("children-content"))
  540. {
  541. String children;
  542. if (source_element != nullptr)
  543. {
  544. const int num_dom_children = (source_element->GetNumChildren(false));
  545. for (int i = 0; i < source_element->GetNumChildren(true); i++)
  546. {
  547. Element* child = source_element->GetChild(i);
  548. // If this is a debugger document, do not show it.
  549. if (IsDebuggerElement(child))
  550. continue;
  551. String child_name = child->GetAddress(false, false);
  552. auto document = rmlui_dynamic_cast<ElementDocument*>(child);
  553. if (document && !document->GetTitle().empty())
  554. child_name += " (" + document->GetTitle() + ')';
  555. const char* non_dom_string = (i >= num_dom_children ? " class=\"non_dom\"" : "");
  556. children += CreateString("<p id=\"c %d\"%s>%s</p>", i, non_dom_string, child_name.c_str());
  557. }
  558. }
  559. if (children.empty())
  560. {
  561. while (children_content->HasChildNodes())
  562. children_content->RemoveChild(children_content->GetChild(0));
  563. children_rml.clear();
  564. }
  565. else if (children != children_rml)
  566. {
  567. children_content->SetInnerRML(children);
  568. children_rml = std::move(children);
  569. }
  570. }
  571. }
  572. void ElementInfo::BuildElementPropertiesRML(String& property_rml, Element* element, Element* primary_element)
  573. {
  574. NamedPropertyList property_list;
  575. for (auto it = element->IterateLocalProperties(); !it.AtEnd(); ++it)
  576. {
  577. PropertyId property_id = it.GetId();
  578. const String& property_name = it.GetName();
  579. const Property* prop = &it.GetProperty();
  580. // Check that this property isn't overridden or just not inherited.
  581. if (primary_element->GetProperty(property_id) != prop)
  582. continue;
  583. property_list.push_back(NamedProperty{property_name, prop});
  584. }
  585. std::sort(property_list.begin(), property_list.end(), [](const NamedProperty& a, const NamedProperty& b) {
  586. if (a.second->source && !b.second->source)
  587. return false;
  588. if (!a.second->source && b.second->source)
  589. return true;
  590. if (a.second->specificity < b.second->specificity)
  591. return false;
  592. if (a.second->specificity > b.second->specificity)
  593. return true;
  594. if (a.second->definition && !b.second->definition)
  595. return false;
  596. if (!a.second->definition && b.second->definition)
  597. return true;
  598. const String& a_name = StyleSheetSpecification::GetPropertyName(a.second->definition->GetId());
  599. const String& b_name = StyleSheetSpecification::GetPropertyName(b.second->definition->GetId());
  600. return a_name < b_name;
  601. });
  602. if (!property_list.empty())
  603. {
  604. // Print the 'inherited from ...' header if we're not the primary element.
  605. if (element != primary_element)
  606. {
  607. property_rml += "<h3 class='strong'>inherited from " + element->GetAddress(false, false) + "</h3>";
  608. }
  609. const PropertySource* previous_source = nullptr;
  610. bool first_iteration = true;
  611. for (auto& named_property : property_list)
  612. {
  613. auto& source = named_property.second->source;
  614. if (source.get() != previous_source || first_iteration)
  615. {
  616. previous_source = source.get();
  617. first_iteration = false;
  618. // Print the rule name header.
  619. if (source)
  620. {
  621. String str_line_number;
  622. TypeConverter<int, String>::Convert(source->line_number, str_line_number);
  623. property_rml += "<h3>" + source->rule_name + "</h3>";
  624. property_rml += "<h4><span class='break-all'>" + source->path + "</span> : " + str_line_number + "</h4>";
  625. }
  626. else
  627. {
  628. property_rml += "<h3><em>inline</em></h3>";
  629. }
  630. }
  631. BuildPropertyRML(property_rml, named_property.first, named_property.second);
  632. }
  633. }
  634. if (element->GetParentNode() != nullptr)
  635. BuildElementPropertiesRML(property_rml, element->GetParentNode(), primary_element);
  636. }
  637. void ElementInfo::BuildPropertyRML(String& property_rml, const String& name, const Property* property)
  638. {
  639. const String property_value = property->ToString();
  640. property_rml += "<span class='name'>" + name + "</span>: " + property_value + "<br/>";
  641. }
  642. void ElementInfo::UpdateTitle()
  643. {
  644. auto title_content = GetElementById("title-content");
  645. auto enable_select = GetElementById("enable_element_select");
  646. auto show_source = GetElementById("show_source");
  647. auto update_source = GetElementById("update_source");
  648. if (title_content && enable_select && show_source && update_source)
  649. {
  650. if (enable_select->IsPseudoClassSet("hover"))
  651. title_content->SetInnerRML("<em>(select elements)</em>");
  652. else if (show_source->IsPseudoClassSet("hover"))
  653. title_content->SetInnerRML("<em>(draw element dimensions)</em>");
  654. else if (update_source->IsPseudoClassSet("hover"))
  655. title_content->SetInnerRML("<em>(update info continuously)</em>");
  656. else if (source_element)
  657. title_content->SetInnerRML(source_element->GetTagName());
  658. else
  659. title_content->SetInnerRML("Element Information");
  660. }
  661. }
  662. bool ElementInfo::IsDebuggerElement(Element* element)
  663. {
  664. return element->GetOwnerDocument()->GetId().find("rmlui-debug-") == 0;
  665. }
  666. } // namespace Debugger
  667. } // namespace Rml