ElementInfo.cpp 23 KB

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