ElementInfo.cpp 19 KB

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