ElementInfo.cpp 24 KB

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