2
0

ElementInfo.cpp 25 KB

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