ElementInfo.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. RemoveEventListener(EventId::Click, this);
  59. RemoveEventListener(EventId::Mouseover, this);
  60. RemoveEventListener(EventId::Mouseout, this);
  61. }
  62. // Initialises the info element.
  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. // Clears the element references.
  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. // Called when an element is destroyed.
  111. void ElementInfo::OnElementDestroy(Element* element)
  112. {
  113. if (hover_element == element)
  114. hover_element = nullptr;
  115. if (source_element == element)
  116. source_element = nullptr;
  117. }
  118. void ElementInfo::RenderHoverElement()
  119. {
  120. if (hover_element)
  121. {
  122. ElementUtilities::ApplyTransform(*hover_element);
  123. for (int i = 0; i < hover_element->GetNumBoxes(); i++)
  124. {
  125. // Render the content area.
  126. Vector2f box_offset;
  127. const Box& element_box = hover_element->GetBox(i, box_offset);
  128. Vector2f size = element_box.GetSize(Box::BORDER);
  129. size = Vector2f(std::max(size.x, 2.0f), std::max(size.y, 2.0f));
  130. Geometry::RenderOutline(
  131. hover_element->GetAbsoluteOffset(Box::BORDER) + box_offset,
  132. size,
  133. Colourb(255, 0, 0, 255),
  134. 1
  135. );
  136. }
  137. }
  138. }
  139. void ElementInfo::RenderSourceElement()
  140. {
  141. if (source_element && show_source_element)
  142. {
  143. ElementUtilities::ApplyTransform(*source_element);
  144. for (int i = 0; i < source_element->GetNumBoxes(); i++)
  145. {
  146. Vector2f box_offset;
  147. const Box element_box = source_element->GetBox(i, box_offset);
  148. const Vector2f border_offset = box_offset + source_element->GetAbsoluteOffset(Box::BORDER);
  149. // Content area:
  150. Geometry::RenderBox(border_offset + element_box.GetPosition(Box::CONTENT), element_box.GetSize(), Colourb(158, 214, 237, 128));
  151. // Padding area:
  152. 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));
  153. // Border area:
  154. 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));
  155. // Border area:
  156. 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));
  157. }
  158. }
  159. }
  160. void ElementInfo::ProcessEvent(Event& event)
  161. {
  162. // Only process events if we're visible
  163. if (IsVisible())
  164. {
  165. if (event == EventId::Click)
  166. {
  167. Element* target_element = event.GetTargetElement();
  168. // Deal with clicks on our own elements differently.
  169. if (target_element->GetOwnerDocument() == this)
  170. {
  171. const String& id = event.GetTargetElement()->GetId();
  172. if (id == "close_button")
  173. {
  174. if (IsVisible())
  175. SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  176. }
  177. else if (id == "update_source")
  178. {
  179. update_source_element = !update_source_element;
  180. target_element->SetClass("active", update_source_element);
  181. }
  182. else if (id == "show_source")
  183. {
  184. show_source_element = !target_element->IsClassSet("active");;
  185. target_element->SetClass("active", show_source_element);
  186. }
  187. else if (id == "enable_element_select")
  188. {
  189. enable_element_select = !target_element->IsClassSet("active");;
  190. target_element->SetClass("active", enable_element_select);
  191. }
  192. else if (target_element->GetTagName() == "pseudo" && source_element)
  193. {
  194. const String name = target_element->GetAttribute<String>("name", "");
  195. if (!name.empty())
  196. {
  197. bool pseudo_active = target_element->IsClassSet("active");
  198. if (name == "focus")
  199. {
  200. if (!pseudo_active)
  201. source_element->Focus();
  202. else if (auto document = source_element->GetOwnerDocument())
  203. document->Focus();
  204. }
  205. else
  206. {
  207. source_element->SetPseudoClass(name, !pseudo_active);
  208. }
  209. force_update_once = true;
  210. }
  211. }
  212. else if (id == "offset_parent")
  213. {
  214. if (source_element)
  215. {
  216. Element* offset_parent = source_element->GetOffsetParent();
  217. if (offset_parent)
  218. SetSourceElement(offset_parent);
  219. }
  220. }
  221. // Check if the id is in the form "a %d" or "c %d" - these are the ancestor or child labels.
  222. else
  223. {
  224. int element_index;
  225. if (sscanf(target_element->GetId().c_str(), "a %d", &element_index) == 1)
  226. {
  227. Element* new_source_element = source_element;
  228. for (int i = 0; i < element_index; i++)
  229. {
  230. if (new_source_element != nullptr)
  231. new_source_element = new_source_element->GetParentNode();
  232. }
  233. SetSourceElement(new_source_element);
  234. }
  235. else if (sscanf(target_element->GetId().c_str(), "c %d", &element_index) == 1)
  236. {
  237. if (source_element != nullptr)
  238. SetSourceElement(source_element->GetChild(element_index));
  239. }
  240. }
  241. event.StopPropagation();
  242. }
  243. // Otherwise we just want to focus on the clicked element (unless it's on a debug element)
  244. else if (enable_element_select && target_element->GetOwnerDocument() != nullptr && !IsDebuggerElement(target_element))
  245. {
  246. Element* new_source_element = target_element;
  247. if (new_source_element != source_element)
  248. {
  249. SetSourceElement(new_source_element);
  250. event.StopPropagation();
  251. }
  252. }
  253. }
  254. else if (event == EventId::Mouseover)
  255. {
  256. Element* target_element = event.GetTargetElement();
  257. ElementDocument* owner_document = target_element->GetOwnerDocument();
  258. if (owner_document == this)
  259. {
  260. // Check if the id is in the form "a %d" or "c %d" - these are the ancestor or child labels.
  261. const String& id = target_element->GetId();
  262. int element_index;
  263. if (sscanf(id.c_str(), "a %d", &element_index) == 1)
  264. {
  265. hover_element = source_element;
  266. for (int i = 0; i < element_index; i++)
  267. {
  268. if (hover_element != nullptr)
  269. hover_element = hover_element->GetParentNode();
  270. }
  271. }
  272. else if (sscanf(id.c_str(), "c %d", &element_index) == 1)
  273. {
  274. if (source_element != nullptr)
  275. hover_element = source_element->GetChild(element_index);
  276. }
  277. else if (id == "offset_parent")
  278. {
  279. if (source_element)
  280. hover_element = source_element->GetOffsetParent();
  281. }
  282. else
  283. {
  284. hover_element = nullptr;
  285. }
  286. if (id == "show_source" && !show_source_element)
  287. {
  288. // Preview the source element view while hovering
  289. show_source_element = true;
  290. }
  291. if (id == "show_source" || id == "update_source" || id == "enable_element_select")
  292. {
  293. title_dirty = true;
  294. }
  295. }
  296. // Otherwise we just want to focus on the clicked element (unless it's on a debug element)
  297. else if (enable_element_select && owner_document != nullptr && owner_document->GetId().find("rmlui-debug-") != 0)
  298. {
  299. hover_element = target_element;
  300. }
  301. }
  302. else if (event == EventId::Mouseout)
  303. {
  304. Element* target_element = event.GetTargetElement();
  305. ElementDocument* owner_document = target_element->GetOwnerDocument();
  306. if (owner_document == this)
  307. {
  308. const String& id = target_element->GetId();
  309. if (id == "show_source")
  310. {
  311. // Disable the preview of the source element view
  312. if (show_source_element && !target_element->IsClassSet("active"))
  313. show_source_element = false;
  314. }
  315. if (id == "show_source" || id == "update_source" || id == "enable_element_select")
  316. {
  317. title_dirty = true;
  318. }
  319. }
  320. }
  321. }
  322. }
  323. void ElementInfo::SetSourceElement(Element* new_source_element)
  324. {
  325. source_element = new_source_element;
  326. force_update_once = true;
  327. }
  328. void ElementInfo::UpdateSourceElement()
  329. {
  330. previous_update_time = GetSystemInterface()->GetElapsedTime();
  331. title_dirty = true;
  332. // Set the pseudo classes
  333. if (Element* pseudo = GetElementById("pseudo"))
  334. {
  335. StringList list;
  336. if (source_element)
  337. list = source_element->GetActivePseudoClasses();
  338. auto EraseFromList = [](StringList& list, const String& value) {
  339. auto it = std::find(list.begin(), list.end(), value);
  340. if (it == list.end())
  341. return false;
  342. list.erase(it);
  343. return true;
  344. };
  345. // There are some fixed pseudo classes that we always show and iterate through to determine if they are set.
  346. // We also want to show other pseudo classes when they are set, they are added under the #extra element last.
  347. for (int i = 0; i < pseudo->GetNumChildren(); i++)
  348. {
  349. Element* child = pseudo->GetChild(i);
  350. const String name = child->GetAttribute<String>("name", "");
  351. if (!name.empty())
  352. {
  353. bool active = EraseFromList(list, name);
  354. child->SetClass("active", active);
  355. }
  356. else if (child->GetId() == "extra")
  357. {
  358. // First, we iterate through the extra elements and remove those that are no longer active.
  359. for (int j = 0; j < child->GetNumChildren(); j++)
  360. {
  361. Element* grandchild = child->GetChild(j);
  362. const String grandchild_name = grandchild->GetAttribute<String>("name", "");
  363. bool active = (EraseFromList(list, grandchild_name) == 1);
  364. if(!active)
  365. child->RemoveChild(grandchild);
  366. }
  367. // Finally, create new pseudo buttons for the rest of the active pseudo classes.
  368. for (auto& extra_pseudo : list)
  369. {
  370. Element* grandchild = child->AppendChild(CreateElement("pseudo"));
  371. grandchild->SetClass("active", true);
  372. grandchild->SetAttribute("name", extra_pseudo);
  373. grandchild->SetInnerRML(":" + extra_pseudo);
  374. }
  375. }
  376. }
  377. }
  378. // Set the attributes
  379. if (Element* attributes_content = GetElementById("attributes-content"))
  380. {
  381. String attributes;
  382. if (source_element != nullptr)
  383. {
  384. {
  385. String name;
  386. String value;
  387. // The element's attribute list is not always synchronized with its internal values, fetch
  388. // them manually here (see e.g. Element::OnAttributeChange for relevant attributes)
  389. {
  390. name = "id";
  391. value = source_element->GetId();
  392. if (!value.empty())
  393. attributes += CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  394. }
  395. {
  396. name = "class";
  397. value = source_element->GetClassNames();
  398. if (!value.empty())
  399. attributes += CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  400. }
  401. }
  402. for(const auto& pair : source_element->GetAttributes())
  403. {
  404. auto& name = pair.first;
  405. auto& variant = pair.second;
  406. String value = StringUtilities::EncodeRml(variant.Get<String>());
  407. if(name != "class" && name != "style" && name != "id")
  408. attributes += CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  409. }
  410. // Text is not an attribute but useful nonetheless
  411. if (auto text_element = rmlui_dynamic_cast<ElementText*>(source_element))
  412. {
  413. const String& text_content = text_element->GetText();
  414. attributes += CreateString(text_content.size() + 32, "Text: <em>%s</em><br />", text_content.c_str());
  415. }
  416. }
  417. if (attributes.empty())
  418. {
  419. while (attributes_content->HasChildNodes())
  420. attributes_content->RemoveChild(attributes_content->GetChild(0));
  421. attributes_rml.clear();
  422. }
  423. else if (attributes != attributes_rml)
  424. {
  425. attributes_content->SetInnerRML(attributes);
  426. attributes_rml = std::move(attributes);
  427. }
  428. }
  429. // Set the properties
  430. if (Element* properties_content = GetElementById("properties-content"))
  431. {
  432. String properties;
  433. if (source_element != nullptr)
  434. BuildElementPropertiesRML(properties, source_element, source_element);
  435. if (properties.empty())
  436. {
  437. while (properties_content->HasChildNodes())
  438. properties_content->RemoveChild(properties_content->GetChild(0));
  439. properties_rml.clear();
  440. }
  441. else if (properties != properties_rml)
  442. {
  443. properties_content->SetInnerRML(properties);
  444. properties_rml = std::move(properties);
  445. }
  446. }
  447. // Set the events
  448. if (Element* events_content = GetElementById("events-content"))
  449. {
  450. String events;
  451. if (source_element != nullptr)
  452. {
  453. events = source_element->GetEventDispatcherSummary();
  454. }
  455. if (events.empty())
  456. {
  457. while (events_content->HasChildNodes())
  458. events_content->RemoveChild(events_content->GetChild(0));
  459. events_rml.clear();
  460. }
  461. else if (events != events_rml)
  462. {
  463. events_content->SetInnerRML(events);
  464. events_rml = std::move(events);
  465. }
  466. }
  467. // Set the position
  468. if (Element* position_content = GetElementById("position-content"))
  469. {
  470. String position;
  471. // left, top, width, height.
  472. if (source_element != nullptr)
  473. {
  474. const Vector2f element_offset = source_element->GetRelativeOffset(Box::BORDER);
  475. const Vector2f element_size = source_element->GetBox().GetSize(Box::BORDER);
  476. Element* offset_parent = source_element->GetOffsetParent();
  477. const String offset_parent_rml = (offset_parent ? StringUtilities::EncodeRml(offset_parent->GetAddress(false, false)) : String("<em>none</em>"));
  478. position =
  479. "<span class='name'>left: </span><em>" + ToString(element_offset.x) + "px</em><br/>" +
  480. "<span class='name'>top: </span><em>" + ToString(element_offset.y) + "px</em><br/>" +
  481. "<span class='name'>width: </span><em>" + ToString(element_size.x) + "px</em><br/>" +
  482. "<span class='name'>height: </span><em>" + ToString(element_size.y) + "px</em><br/>" +
  483. "<span class='name'>offset parent: </span><p style='display: inline' id='offset_parent'>" + offset_parent_rml + "</p>";
  484. }
  485. else
  486. {
  487. while (position_content->HasChildNodes())
  488. position_content->RemoveChild(position_content->GetFirstChild());
  489. }
  490. if (position.empty())
  491. {
  492. while (position_content->HasChildNodes())
  493. position_content->RemoveChild(position_content->GetFirstChild());
  494. position_rml.clear();
  495. }
  496. else if (position != position_rml)
  497. {
  498. position_content->SetInnerRML(position);
  499. position_rml = std::move(position);
  500. }
  501. }
  502. // Set the ancestors
  503. if (Element* ancestors_content = GetElementById("ancestors-content"))
  504. {
  505. String ancestors;
  506. Element* element_ancestor = nullptr;
  507. if (source_element != nullptr)
  508. element_ancestor = source_element->GetParentNode();
  509. int ancestor_depth = 1;
  510. while (element_ancestor)
  511. {
  512. String ancestor_name = element_ancestor->GetAddress(false, false);
  513. ancestors += CreateString(ancestor_name.size() + 32, "<p id=\"a %d\">%s</p>", ancestor_depth, ancestor_name.c_str());
  514. element_ancestor = element_ancestor->GetParentNode();
  515. ancestor_depth++;
  516. }
  517. if (ancestors.empty())
  518. {
  519. while (ancestors_content->HasChildNodes())
  520. ancestors_content->RemoveChild(ancestors_content->GetFirstChild());
  521. ancestors_rml.clear();
  522. }
  523. else if (ancestors != ancestors_rml)
  524. {
  525. ancestors_content->SetInnerRML(ancestors);
  526. ancestors_rml = std::move(ancestors);
  527. }
  528. }
  529. // Set the children
  530. if (Element* children_content = GetElementById("children-content"))
  531. {
  532. String children;
  533. if (source_element != nullptr)
  534. {
  535. const int num_dom_children = (source_element->GetNumChildren(false));
  536. for (int i = 0; i < source_element->GetNumChildren(true); i++)
  537. {
  538. Element* child = source_element->GetChild(i);
  539. // If this is a debugger document, do not show it.
  540. if (IsDebuggerElement(child))
  541. continue;
  542. String child_name = child->GetAddress(false, false);
  543. auto document = rmlui_dynamic_cast<ElementDocument*>(child);
  544. if (document && !document->GetTitle().empty())
  545. child_name += " (" + document->GetTitle() + ')';
  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. }