ElementInfo.cpp 23 KB

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