ElementInfo.cpp 23 KB

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