2
0

ElementInfo.cpp 24 KB

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