ElementInfo.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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/Property.h"
  30. #include "../../Include/RmlUi/Core/PropertiesIteratorView.h"
  31. #include "../../Include/RmlUi/Core/Factory.h"
  32. #include "../../Include/RmlUi/Core/StyleSheet.h"
  33. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  34. #include "Geometry.h"
  35. #include "CommonSource.h"
  36. #include "InfoSource.h"
  37. #include <map>
  38. namespace Rml {
  39. namespace Debugger {
  40. ElementInfo::ElementInfo(const Core::String& tag) : Core::ElementDocument(tag)
  41. {
  42. hover_element = NULL;
  43. source_element = NULL;
  44. }
  45. ElementInfo::~ElementInfo()
  46. {
  47. }
  48. // Initialises the info element.
  49. bool ElementInfo::Initialise()
  50. {
  51. SetInnerRML(info_rml);
  52. SetId("rmlui-debug-info");
  53. AddEventListener(Core::EventId::Click, this);
  54. AddEventListener(Core::EventId::Mouseover, this);
  55. Core::StyleSheet* style_sheet = Core::Factory::InstanceStyleSheetString(Core::String(common_rcss) + Core::String(info_rcss));
  56. if (style_sheet == NULL)
  57. return false;
  58. SetStyleSheet(style_sheet);
  59. style_sheet->RemoveReference();
  60. return true;
  61. }
  62. // Clears the element references.
  63. void ElementInfo::Reset()
  64. {
  65. hover_element = NULL;
  66. SetSourceElement(NULL);
  67. }
  68. // Called when an element is destroyed.
  69. void ElementInfo::OnElementDestroy(Core::Element* element)
  70. {
  71. if (hover_element == element)
  72. hover_element = NULL;
  73. if (source_element == element)
  74. source_element = NULL;
  75. }
  76. void ElementInfo::RenderHoverElement()
  77. {
  78. if (hover_element)
  79. {
  80. for (int i = 0; i < hover_element->GetNumBoxes(); i++)
  81. {
  82. // Render the content area.
  83. const Core::Box element_box = hover_element->GetBox(i);
  84. Geometry::RenderOutline(hover_element->GetAbsoluteOffset(Core::Box::BORDER) + element_box.GetPosition(Core::Box::BORDER), element_box.GetSize(Core::Box::BORDER), Core::Colourb(255, 0, 0, 255), 1);
  85. }
  86. }
  87. }
  88. void ElementInfo::RenderSourceElement()
  89. {
  90. if (source_element)
  91. {
  92. for (int i = 0; i < source_element->GetNumBoxes(); i++)
  93. {
  94. const Core::Box element_box = source_element->GetBox(i);
  95. // Content area:
  96. Geometry::RenderBox(source_element->GetAbsoluteOffset(Core::Box::BORDER) + element_box.GetPosition(Core::Box::CONTENT), element_box.GetSize(), Core::Colourb(158, 214, 237, 128));
  97. // Padding area:
  98. 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));
  99. // Border area:
  100. 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));
  101. // Border area:
  102. 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));
  103. }
  104. }
  105. }
  106. void ElementInfo::ProcessEvent(Core::Event& event)
  107. {
  108. // Only process events if we're visible
  109. if (IsVisible())
  110. {
  111. if (event == Core::EventId::Click)
  112. {
  113. Core::Element* target_element = event.GetTargetElement();
  114. // Deal with clicks on our own elements differently.
  115. if (target_element->GetOwnerDocument() == this)
  116. {
  117. // If it's a pane title, then we need to toggle the visibility of its sibling (the contents pane underneath it).
  118. if (target_element->GetTagName() == "h2")
  119. {
  120. Core::Element* panel = target_element->GetNextSibling();
  121. if (panel->IsVisible())
  122. panel->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::None));
  123. else
  124. panel->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::Block));
  125. event.StopPropagation();
  126. }
  127. else if (event.GetTargetElement()->GetId() == "close_button")
  128. {
  129. if (IsVisible())
  130. SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  131. }
  132. // Check if the id is in the form "a %d" or "c %d" - these are the ancestor or child labels.
  133. else
  134. {
  135. int element_index;
  136. if (sscanf(target_element->GetId().c_str(), "a %d", &element_index) == 1)
  137. {
  138. Core::Element* new_source_element = source_element;
  139. for (int i = 0; i < element_index; i++)
  140. {
  141. if (new_source_element != NULL)
  142. new_source_element = new_source_element->GetParentNode();
  143. }
  144. SetSourceElement(new_source_element);
  145. }
  146. else if (sscanf(target_element->GetId().c_str(), "c %d", &element_index) == 1)
  147. {
  148. if (source_element != NULL)
  149. SetSourceElement(source_element->GetChild(element_index));
  150. }
  151. event.StopPropagation();
  152. }
  153. }
  154. // Otherwise we just want to focus on the clicked element (unless it's on a debug element)
  155. else if (target_element->GetOwnerDocument() != NULL && !IsDebuggerElement(target_element))
  156. {
  157. Core::Element* new_source_element = target_element;
  158. if (new_source_element != source_element)
  159. {
  160. SetSourceElement(new_source_element);
  161. event.StopPropagation();
  162. }
  163. }
  164. }
  165. else if (event == Core::EventId::Mouseover)
  166. {
  167. Core::Element* target_element = event.GetTargetElement();
  168. // Deal with clicks on our own elements differently.
  169. Core::ElementDocument* owner_document = target_element->GetOwnerDocument();
  170. if (owner_document == this)
  171. {
  172. // Check if the id is in the form "a %d" or "c %d" - these are the ancestor or child labels.
  173. int element_index;
  174. if (sscanf(target_element->GetId().c_str(), "a %d", &element_index) == 1)
  175. {
  176. hover_element = source_element;
  177. for (int i = 0; i < element_index; i++)
  178. {
  179. if (hover_element != NULL)
  180. hover_element = hover_element->GetParentNode();
  181. }
  182. }
  183. else if (sscanf(target_element->GetId().c_str(), "c %d", &element_index) == 1)
  184. {
  185. if (source_element != NULL)
  186. hover_element = source_element->GetChild(element_index);
  187. }
  188. }
  189. // Otherwise we just want to focus on the clicked element (unless it's on a debug element)
  190. else if (owner_document != NULL && owner_document->GetId().find("rmlui-debug-") != 0)
  191. {
  192. hover_element = target_element;
  193. }
  194. }
  195. }
  196. }
  197. void ElementInfo::SetSourceElement(Core::Element* new_source_element)
  198. {
  199. source_element = new_source_element;
  200. UpdateSourceElement();
  201. }
  202. void ElementInfo::UpdateSourceElement()
  203. {
  204. // Set the title:
  205. Core::Element* title_content = GetElementById("title-content");
  206. if (title_content != NULL)
  207. {
  208. if (source_element != NULL)
  209. title_content->SetInnerRML(source_element->GetTagName());
  210. else
  211. title_content->SetInnerRML("Element Information");
  212. }
  213. // Set the attributes:
  214. Core::Element* attributes_content = GetElementById("attributes-content");
  215. if (attributes_content)
  216. {
  217. Core::String attributes;
  218. if (source_element != NULL)
  219. {
  220. {
  221. Core::String name;
  222. Core::String value;
  223. // The element's attribute list is not always synchronized with its internal values, fetch
  224. // them manually here (see e.g. Element::OnAttributeChange for relevant attributes)
  225. {
  226. name = "id";
  227. value = source_element->GetId();
  228. if (!value.empty())
  229. attributes += Core::CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  230. }
  231. {
  232. name = "class";
  233. value = source_element->GetClassNames();
  234. if (!value.empty())
  235. attributes += Core::CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  236. }
  237. {
  238. // Not actually an attribute, but may be useful
  239. name = "pseudo";
  240. value.clear();
  241. for (auto str : source_element->GetActivePseudoClasses())
  242. value += " :" + str;
  243. if (!value.empty())
  244. attributes += Core::CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  245. }
  246. {
  247. name = "style";
  248. value = "";
  249. const Core::PropertyMap& style_properties = source_element->GetLocalStyleProperties();
  250. for (auto nvp : style_properties)
  251. {
  252. auto& prop_name = Core::StyleSheetSpecification::GetPropertyName(nvp.first);
  253. auto prop_value = nvp.second.ToString();
  254. value += Core::CreateString(prop_name.size() + prop_value.size() + 12, "%s: %s; ", prop_name.c_str(), prop_value.c_str());
  255. }
  256. if (!value.empty())
  257. attributes += Core::CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  258. }
  259. }
  260. for(const auto& pair : source_element->GetAttributes())
  261. {
  262. auto& name = pair.first;
  263. auto& variant = pair.second;
  264. Core::String value = variant.Get<Core::String>();
  265. if(name != "class" && name != "style" && name != "id")
  266. attributes += Core::CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  267. }
  268. }
  269. if (attributes.empty())
  270. {
  271. while (attributes_content->HasChildNodes())
  272. attributes_content->RemoveChild(attributes_content->GetChild(0));
  273. }
  274. else
  275. attributes_content->SetInnerRML(attributes);
  276. }
  277. // Set the properties:
  278. Core::Element* properties_content = GetElementById("properties-content");
  279. if (properties_content)
  280. {
  281. Core::String properties;
  282. if (source_element != NULL)
  283. BuildElementPropertiesRML(properties, source_element, source_element);
  284. if (properties.empty())
  285. {
  286. while (properties_content->HasChildNodes())
  287. properties_content->RemoveChild(properties_content->GetChild(0));
  288. }
  289. else
  290. properties_content->SetInnerRML(properties);
  291. }
  292. // Set the events:
  293. Core::Element* events_content = GetElementById("events-content");
  294. if (events_content)
  295. {
  296. Core::String events;
  297. if (source_element != NULL)
  298. {
  299. events = source_element->GetEventDispatcherSummary();
  300. }
  301. if (events.empty())
  302. {
  303. while (events_content->HasChildNodes())
  304. events_content->RemoveChild(events_content->GetChild(0));
  305. }
  306. else
  307. events_content->SetInnerRML(events);
  308. }
  309. // Set the position:
  310. Core::Element* position_content = GetElementById("position-content");
  311. if (position_content)
  312. {
  313. // left, top, width, height.
  314. if (source_element != NULL)
  315. {
  316. Core::Vector2f element_offset = source_element->GetRelativeOffset(Core::Box::BORDER);
  317. Core::Vector2f element_size = source_element->GetBox().GetSize(Core::Box::BORDER);
  318. Core::String positions;
  319. positions += Core::CreateString(64, "left: <em>%.0fpx</em><br />", element_offset.x);
  320. positions += Core::CreateString(64, "top: <em>%.0fpx</em><br />", element_offset.y);
  321. positions += Core::CreateString(64, "width: <em>%.0fpx</em><br />", element_size.x);
  322. positions += Core::CreateString(64, "height: <em>%.0fpx</em><br />", element_size.y);
  323. position_content->SetInnerRML(positions);
  324. }
  325. else
  326. {
  327. while (position_content->HasChildNodes())
  328. position_content->RemoveChild(position_content->GetFirstChild());
  329. }
  330. }
  331. // Set the ancestors:
  332. Core::Element* ancestors_content = GetElementById("ancestors-content");
  333. if (ancestors_content)
  334. {
  335. Core::String ancestors;
  336. Core::Element* element_ancestor = NULL;
  337. if (source_element != NULL)
  338. element_ancestor = source_element->GetParentNode();
  339. int ancestor_depth = 1;
  340. while (element_ancestor)
  341. {
  342. Core::String ancestor_name = element_ancestor->GetTagName();
  343. const Core::String ancestor_id = element_ancestor->GetId();
  344. if (!ancestor_id.empty())
  345. {
  346. ancestor_name += "#";
  347. ancestor_name += ancestor_id;
  348. }
  349. ancestors += Core::CreateString(ancestor_name.size() + 32, "<p id=\"a %d\">%s</p>", ancestor_depth, ancestor_name.c_str());
  350. element_ancestor = element_ancestor->GetParentNode();
  351. ancestor_depth++;
  352. }
  353. if (ancestors.empty())
  354. {
  355. while (ancestors_content->HasChildNodes())
  356. ancestors_content->RemoveChild(ancestors_content->GetFirstChild());
  357. }
  358. else
  359. ancestors_content->SetInnerRML(ancestors);
  360. }
  361. // Set the children:
  362. Core::Element* children_content = GetElementById("children-content");
  363. if (children_content)
  364. {
  365. Core::String children;
  366. if (source_element != NULL)
  367. {
  368. for (int i = 0; i < source_element->GetNumChildren(); i++)
  369. {
  370. Core::Element* child = source_element->GetChild(i);
  371. // If this is a debugger document, do not show it.
  372. if (IsDebuggerElement(child))
  373. continue;
  374. Core::String child_name = child->GetTagName();
  375. const Core::String child_id = child->GetId();
  376. if (!child_id.empty())
  377. {
  378. child_name += "#";
  379. child_name += child_id;
  380. }
  381. children += Core::CreateString(child_name.size() + 32, "<p id=\"c %d\">%s</p>", i, child_name.c_str());
  382. }
  383. }
  384. if (children.empty())
  385. {
  386. while (children_content->HasChildNodes())
  387. children_content->RemoveChild(children_content->GetChild(0));
  388. }
  389. else
  390. children_content->SetInnerRML(children);
  391. }
  392. }
  393. void ElementInfo::BuildElementPropertiesRML(Core::String& property_rml, Core::Element* element, Core::Element* primary_element)
  394. {
  395. NamedPropertyMap property_map;
  396. for(auto it = element->IterateLocalProperties(); !it.AtEnd(); ++it)
  397. {
  398. const Core::PropertyId property_id = it.GetId();
  399. const Core::String& property_name = it.GetName();
  400. const Core::Property* property = &it.GetProperty();
  401. const Core::PseudoClassList& property_pseudo_classes = it.GetPseudoClassList();
  402. // Check that this property isn't overridden or just not inherited.
  403. if (primary_element->GetProperty(property_id) != property)
  404. continue;
  405. NamedPropertyMap::iterator i = property_map.find(property_pseudo_classes);
  406. if (i == property_map.end())
  407. property_map[property_pseudo_classes] = NamedPropertyList(1, NamedProperty(property_name, property));
  408. else
  409. {
  410. // Find a place in this list of properties to insert the new one.
  411. NamedPropertyList& properties = (*i).second;
  412. NamedPropertyList::iterator insert_iterator = properties.begin();
  413. while (insert_iterator != properties.end())
  414. {
  415. int source_cmp = strcasecmp((*insert_iterator).second->source.c_str(), property->source.c_str());
  416. if (source_cmp > 0 ||
  417. (source_cmp == 0 && (*insert_iterator).second->source_line_number >= property->source_line_number))
  418. break;
  419. ++insert_iterator;
  420. }
  421. (*i).second.insert(insert_iterator, NamedProperty(property_name, property));
  422. }
  423. }
  424. if (!property_map.empty())
  425. {
  426. // Print the 'inherited from ...' header if we're not the primary element.
  427. if (element != primary_element)
  428. property_rml += Core::CreateString(element->GetTagName().size() + 32, "<h3>inherited from %s</h3>", element->GetTagName().c_str());
  429. NamedPropertyMap::iterator base_properties = property_map.find(Core::PseudoClassList());
  430. if (base_properties != property_map.end())
  431. BuildPropertiesRML(property_rml, (*base_properties).second);
  432. for (NamedPropertyMap::iterator i = property_map.begin(); i != property_map.end(); ++i)
  433. {
  434. // Skip the base property list, we've already printed it.
  435. if (i == base_properties)
  436. continue;
  437. // Print the pseudo-class header.
  438. property_rml += "<h3>";
  439. for (Core::PseudoClassList::const_iterator j = (*i).first.begin(); j != (*i).first.end(); ++j)
  440. {
  441. property_rml += " :";
  442. property_rml += *j;
  443. }
  444. property_rml += "</h3>";
  445. BuildPropertiesRML(property_rml, (*i).second);
  446. }
  447. }
  448. if (element->GetParentNode() != NULL)
  449. BuildElementPropertiesRML(property_rml, element->GetParentNode(), primary_element);
  450. }
  451. void ElementInfo::BuildPropertiesRML(Core::String& property_rml, const NamedPropertyList& properties)
  452. {
  453. Core::String last_source;
  454. int last_source_line = -1;
  455. for (size_t i = 0; i < properties.size(); ++i)
  456. {
  457. if (i == 0 ||
  458. last_source != properties[i].second->source ||
  459. last_source_line != properties[i].second->source_line_number)
  460. {
  461. last_source = properties[i].second->source;
  462. last_source_line = properties[i].second->source_line_number;
  463. property_rml += "<h4>";
  464. if (last_source.empty() &&
  465. last_source_line == 0)
  466. property_rml += "<em>inline</em>";
  467. else
  468. property_rml += Core::CreateString(last_source.size() + 32, "<em>%s</em>: %d", last_source.c_str(), last_source_line);
  469. property_rml += "</h4>";
  470. }
  471. BuildPropertyRML(property_rml, properties[i].first, properties[i].second);
  472. }
  473. }
  474. void ElementInfo::BuildPropertyRML(Core::String& property_rml, const Core::String& name, const Core::Property* property)
  475. {
  476. Core::String property_value = property->ToString();
  477. RemoveTrailingZeroes(property_value);
  478. property_rml += Core::CreateString(name.size() + property_value.size() + 32, "%s: <em>%s;</em><br />", name.c_str(), property_value.c_str());
  479. }
  480. void ElementInfo::RemoveTrailingZeroes(Core::String& string)
  481. {
  482. if (string.empty())
  483. {
  484. return;
  485. }
  486. // First, check for a decimal point. No point, no chance of trailing zeroes!
  487. size_t decimal_point_position = string.find(".");
  488. if (decimal_point_position != Core::String::npos)
  489. {
  490. // Ok, so now we start at the back of the string and find the first
  491. // numeral. If the character we find is a zero, then we start counting
  492. // back till we find something that isn't a zero or a decimal point -
  493. // and then remove all that we've counted.
  494. size_t last_zero = string.size() - 1;
  495. while ((string[last_zero] < '0' || string[last_zero] > '9') && string[last_zero] != '.')
  496. {
  497. if (last_zero == 0)
  498. {
  499. return;
  500. }
  501. if (string[last_zero] == '.')
  502. {
  503. break;
  504. }
  505. last_zero--;
  506. }
  507. if (!(string[last_zero] == '0' || string[last_zero] == '.'))
  508. {
  509. return;
  510. }
  511. // Now find the first character that isn't a zero (unless we're just
  512. // chopping off the dangling decimal point)
  513. size_t first_zero = last_zero;
  514. if (string[last_zero] == '0')
  515. {
  516. while (first_zero > 0 && string[first_zero - 1] == '0')
  517. {
  518. first_zero--;
  519. }
  520. // Check for a preceeding decimal point - if it's all zeroes until the
  521. // decimal, then we should remove it too.
  522. if (string[first_zero - 1] == '.')
  523. {
  524. first_zero--;
  525. }
  526. }
  527. // Now remove everything between first_zero and last_zero, inclusive.
  528. if (last_zero > first_zero)
  529. string.erase(first_zero, (last_zero - first_zero) + 1);
  530. }
  531. }
  532. bool ElementInfo::IsDebuggerElement(Core::Element* element)
  533. {
  534. return element->GetOwnerDocument()->GetId().find("rmlui-debug-") == 0;
  535. }
  536. }
  537. }