ElementInfo.cpp 25 KB

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