ElementInfo.cpp 24 KB

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