ElementInfo.cpp 23 KB

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