ElementInfo.cpp 19 KB

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