StyleSheetNode.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 "precompiled.h"
  29. #include "StyleSheetNode.h"
  30. #include <algorithm>
  31. #include "../../Include/RmlUi/Core/Element.h"
  32. #include "StyleSheetFactory.h"
  33. #include "StyleSheetNodeSelector.h"
  34. namespace Rml {
  35. namespace Core {
  36. StyleSheetNode::StyleSheetNode(const String& name, NodeType _type, StyleSheetNode* _parent) : name(name)
  37. {
  38. type = _type;
  39. parent = _parent;
  40. specificity = CalculateSpecificity();
  41. selector = NULL;
  42. a = 0;
  43. b = 0;
  44. }
  45. // Constructs a structural style-sheet node.
  46. StyleSheetNode::StyleSheetNode(const String& name, StyleSheetNode* _parent, StyleSheetNodeSelector* _selector, int _a, int _b) : name(name)
  47. {
  48. type = STRUCTURAL_PSEUDO_CLASS;
  49. parent = _parent;
  50. specificity = CalculateSpecificity();
  51. selector = _selector;
  52. a = _a;
  53. b = _b;
  54. }
  55. StyleSheetNode::~StyleSheetNode()
  56. {
  57. for (int i = 0; i < NUM_NODE_TYPES; i++)
  58. {
  59. for (NodeMap::iterator j = children[i].begin(); j != children[i].end(); j++)
  60. delete (*j).second;
  61. }
  62. }
  63. // Writes the style sheet node (and all ancestors) into the stream.
  64. void StyleSheetNode::Write(Stream* stream)
  65. {
  66. if (properties.GetNumProperties() > 0)
  67. {
  68. String rule;
  69. StyleSheetNode* hierarchy = this;
  70. while (hierarchy != NULL)
  71. {
  72. switch (hierarchy->type)
  73. {
  74. case TAG:
  75. rule = " " + hierarchy->name + rule;
  76. break;
  77. case CLASS:
  78. rule = "." + hierarchy->name + rule;
  79. break;
  80. case ID:
  81. rule = "#" + hierarchy->name + rule;
  82. break;
  83. case PSEUDO_CLASS:
  84. rule = ":" + hierarchy->name + rule;
  85. break;
  86. case STRUCTURAL_PSEUDO_CLASS:
  87. rule = ":" + hierarchy->name + rule;
  88. break;
  89. default:
  90. break;
  91. }
  92. hierarchy = hierarchy->parent;
  93. }
  94. stream->Write(String(1024, "%s /* specificity: %d */\n", StringUtilities::StripWhitespace(rule).CString(), specificity));
  95. stream->Write("{\n");
  96. const Rml::Core::PropertyMap& property_map = properties.GetProperties();
  97. for (Rml::Core::PropertyMap::const_iterator i = property_map.begin(); i != property_map.end(); ++i)
  98. {
  99. const String& name = i->first;
  100. const Rml::Core::Property& property = i->second;
  101. stream->Write(String(1024, "\t%s: %s; /* specificity: %d */\n", name.CString(), property.value.Get< String >().CString(), property.specificity));
  102. }
  103. stream->Write("}\n\n");
  104. }
  105. for (size_t i = 0; i < NUM_NODE_TYPES; ++i)
  106. {
  107. for (NodeMap::iterator j = children[i].begin(); j != children[i].end(); ++j)
  108. (*j).second->Write(stream);
  109. }
  110. }
  111. // Merges an entire tree hierarchy into our hierarchy.
  112. bool StyleSheetNode::MergeHierarchy(StyleSheetNode* node, int specificity_offset)
  113. {
  114. // Merge the other node's properties into ours.
  115. MergeProperties(node->properties, specificity_offset);
  116. selector = node->selector;
  117. a = node->a;
  118. b = node->b;
  119. for (int i = 0; i < NUM_NODE_TYPES; i++)
  120. {
  121. for (NodeMap::iterator iterator = node->children[i].begin(); iterator != node->children[i].end(); iterator++)
  122. {
  123. StyleSheetNode* local_node = GetChildNode((*iterator).second->name, (NodeType) i);
  124. local_node->MergeHierarchy((*iterator).second, specificity_offset);
  125. }
  126. }
  127. return true;
  128. }
  129. // Builds up a style sheet's index recursively.
  130. void StyleSheetNode::BuildIndex(StyleSheet::NodeIndex& styled_index, StyleSheet::NodeIndex& complete_index)
  131. {
  132. // If this is a tag node, then we insert it into the list of all tag nodes. Makes sense, neh?
  133. if (type == TAG)
  134. {
  135. StyleSheet::NodeIndex::iterator iterator = complete_index.find(name);
  136. if (iterator == complete_index.end())
  137. (*complete_index.insert(StyleSheet::NodeIndex::value_type(name, StyleSheet::NodeList())).first).second.insert(this);
  138. else
  139. (*iterator).second.insert(this);
  140. }
  141. // If we are a styled node (ie, have some style attributes attached), then we insert our closest parent tag node
  142. // into the list of styled tag nodes.
  143. if (properties.GetNumProperties() > 0)
  144. {
  145. StyleSheetNode* tag_node = this;
  146. while (tag_node != NULL &&
  147. tag_node->type != TAG)
  148. tag_node = tag_node->parent;
  149. if (tag_node != NULL)
  150. {
  151. StyleSheet::NodeIndex::iterator iterator = styled_index.find(tag_node->name);
  152. if (iterator == styled_index.end())
  153. (*styled_index.insert(StyleSheet::NodeIndex::value_type(tag_node->name, StyleSheet::NodeList())).first).second.insert(tag_node);
  154. else
  155. (*iterator).second.insert(tag_node);
  156. }
  157. }
  158. for (int i = 0; i < NUM_NODE_TYPES; i++)
  159. {
  160. for (NodeMap::iterator j = children[i].begin(); j != children[i].end(); ++j)
  161. (*j).second->BuildIndex(styled_index, complete_index);
  162. }
  163. }
  164. // Returns the name of this node.
  165. const String& StyleSheetNode::GetName() const
  166. {
  167. return name;
  168. }
  169. // Returns the specificity of this node.
  170. int StyleSheetNode::GetSpecificity() const
  171. {
  172. return specificity;
  173. }
  174. // Imports properties from a single rule definition (ie, with a shared specificity) into the node's
  175. // properties.
  176. void StyleSheetNode::ImportProperties(const PropertyDictionary& _properties, int rule_specificity)
  177. {
  178. properties.Import(_properties, specificity + rule_specificity);
  179. }
  180. // Merges properties from another node (ie, with potentially differing specificities) into the
  181. // node's properties.
  182. void StyleSheetNode::MergeProperties(const PropertyDictionary& _properties, int rule_specificity_offset)
  183. {
  184. properties.Merge(_properties, rule_specificity_offset);
  185. }
  186. // Returns the node's default properties.
  187. const PropertyDictionary& StyleSheetNode::GetProperties() const
  188. {
  189. return properties;
  190. }
  191. // Builds the properties of all of the pseudo-classes of this style sheet node into a single map.
  192. void StyleSheetNode::GetPseudoClassProperties(PseudoClassPropertyMap& pseudo_class_properties) const
  193. {
  194. for (NodeMap::const_iterator i = children[PSEUDO_CLASS].begin(); i != children[PSEUDO_CLASS].end(); ++i)
  195. (*i).second->GetPseudoClassProperties(pseudo_class_properties, StringList());
  196. }
  197. // Adds to a list the names of this node's pseudo-classes which are deemed volatile.
  198. bool StyleSheetNode::GetVolatilePseudoClasses(PseudoClassList& volatile_pseudo_classes) const
  199. {
  200. if (type == PSEUDO_CLASS)
  201. {
  202. bool self_volatile = !children[TAG].empty();
  203. for (NodeMap::const_iterator i = children[PSEUDO_CLASS].begin(); i != children[PSEUDO_CLASS].end(); ++i)
  204. self_volatile = (*i).second->GetVolatilePseudoClasses(volatile_pseudo_classes) | self_volatile;
  205. if (self_volatile)
  206. volatile_pseudo_classes.insert(name);
  207. return self_volatile;
  208. }
  209. else
  210. {
  211. for (NodeMap::const_iterator i = children[PSEUDO_CLASS].begin(); i != children[PSEUDO_CLASS].end(); ++i)
  212. (*i).second->GetVolatilePseudoClasses(volatile_pseudo_classes);
  213. }
  214. return false;
  215. }
  216. // Returns a direct child node of this node of the requested type.
  217. StyleSheetNode* StyleSheetNode::GetChildNode(const String& child_name, NodeType child_type, bool create)
  218. {
  219. // Look for a node with given name.
  220. NodeMap::iterator iterator = children[child_type].find(child_name);
  221. if (iterator != children[child_type].end())
  222. {
  223. // Traverse into node.
  224. return (*iterator).second;
  225. }
  226. else
  227. {
  228. if (create)
  229. {
  230. StyleSheetNode* new_node = NULL;
  231. // Create the node; structural pseudo-classes require a little extra leg-work.
  232. if (child_type == STRUCTURAL_PSEUDO_CLASS)
  233. new_node = CreateStructuralChild(child_name);
  234. else
  235. new_node = new StyleSheetNode(child_name, child_type, this);
  236. if (new_node != NULL)
  237. {
  238. children[child_type][child_name] = new_node;
  239. return new_node;
  240. }
  241. }
  242. return NULL;
  243. }
  244. }
  245. // Returns true if this node is applicable to the given element, given its IDs, classes and heritage.
  246. bool StyleSheetNode::IsApplicable(const Element* element) const
  247. {
  248. // This function is called with an element that matches a style node only with the tag name. We have to determine
  249. // here whether or not it also matches the required hierarchy.
  250. // We must have a parent; if not, something's amok with the style tree.
  251. if (parent == NULL)
  252. {
  253. RMLUI_ERRORMSG("Invalid RCSS hierarchy.");
  254. return false;
  255. }
  256. // If we've hit a child of the root of the style sheet tree, then we're done; no more lineage to resolve.
  257. if (parent->type == ROOT)
  258. return true;
  259. // Determine the tag (and possibly id / class as well) of the next required parent in the RCSS hierarchy.
  260. const StyleSheetNode* parent_node = parent;
  261. String ancestor_id;
  262. StringList ancestor_classes;
  263. StringList ancestor_pseudo_classes;
  264. std::vector< const StyleSheetNode* > ancestor_structural_pseudo_classes;
  265. while (parent_node != NULL && parent_node->type != TAG)
  266. {
  267. switch (parent_node->type)
  268. {
  269. case ID: ancestor_id = parent_node->name; break;
  270. case CLASS: ancestor_classes.push_back(parent_node->name); break;
  271. case PSEUDO_CLASS: ancestor_pseudo_classes.push_back(parent_node->name); break;
  272. case STRUCTURAL_PSEUDO_CLASS: ancestor_structural_pseudo_classes.push_back(parent_node); break;
  273. default: RMLUI_ERRORMSG("Invalid RCSS hierarchy."); return false;
  274. }
  275. parent_node = parent_node->parent;
  276. }
  277. // Check for an invalid RCSS hierarchy.
  278. if (parent_node == NULL)
  279. {
  280. RMLUI_ERRORMSG("Invalid RCSS hierarchy.");
  281. return false;
  282. }
  283. // Now we know the name / class / ID / pseudo-class / structural requirements for the next ancestor requirement of
  284. // the element. So we look back through the element's ancestors to find one that matches.
  285. for (const Element* ancestor_element = element->GetParentNode(); ancestor_element != NULL; ancestor_element = ancestor_element->GetParentNode())
  286. {
  287. // Skip this ancestor if the name of the next style node doesn't match its tag name, and one was specified.
  288. if (!parent_node->name.Empty()
  289. && parent_node->name != ancestor_element->GetTagName())
  290. continue;
  291. // Skip this ancestor if the ID of the next style node doesn't match its ID, and one was specified.
  292. if (!ancestor_id.Empty() &&
  293. ancestor_id != ancestor_element->GetId())
  294. continue;
  295. // Skip this ancestor if the class of the next style node don't match its classes.
  296. bool resolved_requirements = true;
  297. for (size_t i = 0; i < ancestor_classes.size(); ++i)
  298. {
  299. if (!ancestor_element->IsClassSet(ancestor_classes[i]))
  300. {
  301. resolved_requirements = false;
  302. break;
  303. }
  304. }
  305. if (!resolved_requirements)
  306. continue;
  307. // Skip this ancestor if the required pseudo-classes of the style node aren't set on it.
  308. resolved_requirements = true;
  309. for (size_t i = 0; i < ancestor_pseudo_classes.size(); ++i)
  310. {
  311. if (!ancestor_element->IsPseudoClassSet(ancestor_pseudo_classes[i]))
  312. {
  313. resolved_requirements = false;
  314. break;
  315. }
  316. }
  317. if (!resolved_requirements)
  318. continue;
  319. // Skip this ancestor if the required structural pseudo-classes of the style node aren't applicable to it.
  320. resolved_requirements = true;
  321. for (size_t i = 0; i < ancestor_structural_pseudo_classes.size(); ++i)
  322. {
  323. if (!ancestor_structural_pseudo_classes[i]->selector->IsApplicable(ancestor_element, ancestor_structural_pseudo_classes[i]->a, ancestor_structural_pseudo_classes[i]->b))
  324. {
  325. resolved_requirements = false;
  326. break;
  327. }
  328. }
  329. if (!resolved_requirements)
  330. continue;
  331. return parent_node->IsApplicable(ancestor_element);
  332. }
  333. // We hit the end of the hierarchy before matching the required ancestor, so bail.
  334. return false;
  335. }
  336. // Appends all applicable non-tag descendants of this node into the given element list.
  337. void StyleSheetNode::GetApplicableDescendants(std::vector< const StyleSheetNode* >& applicable_nodes, const Element* element) const
  338. {
  339. // Check if this node matches this element.
  340. switch (type)
  341. {
  342. RMLUI_UNUSED_SWITCH_ENUM(NUM_NODE_TYPES);
  343. case ROOT:
  344. case TAG:
  345. {
  346. // These nodes always match.
  347. }
  348. break;
  349. case CLASS:
  350. {
  351. if (!element->IsClassSet(name))
  352. return;
  353. }
  354. break;
  355. case ID:
  356. {
  357. if (name != element->GetId())
  358. return;
  359. }
  360. break;
  361. case PSEUDO_CLASS:
  362. {
  363. if (!element->IsPseudoClassSet(name))
  364. return;
  365. }
  366. break;
  367. case STRUCTURAL_PSEUDO_CLASS:
  368. {
  369. if (selector == NULL)
  370. return;
  371. if (!selector->IsApplicable(element, a, b))
  372. return;
  373. }
  374. break;
  375. }
  376. if (properties.GetNumProperties() > 0 ||
  377. !children[PSEUDO_CLASS].empty())
  378. applicable_nodes.push_back(this);
  379. for (int i = CLASS; i < NUM_NODE_TYPES; i++)
  380. {
  381. // Don't recurse into pseudo-classes; they can't be built into the root definition.
  382. if (i == PSEUDO_CLASS)
  383. continue;
  384. for (NodeMap::const_iterator j = children[i].begin(); j != children[i].end(); ++j)
  385. (*j).second->GetApplicableDescendants(applicable_nodes, element);
  386. }
  387. }
  388. // Returns true if this node employs a structural selector, and therefore generates element definitions that are
  389. // sensitive to sibling changes.
  390. bool StyleSheetNode::IsStructurallyVolatile(bool check_ancestors) const
  391. {
  392. if (type == STRUCTURAL_PSEUDO_CLASS)
  393. return true;
  394. if (!children[STRUCTURAL_PSEUDO_CLASS].empty())
  395. return true;
  396. // Check our children for structural pseudo-classes.
  397. for (int i = 0; i < NUM_NODE_TYPES; ++i)
  398. {
  399. if (i == STRUCTURAL_PSEUDO_CLASS)
  400. continue;
  401. for (NodeMap::const_iterator j = children[i].begin(); j != children[i].end(); ++j)
  402. {
  403. if ((*j).second->IsStructurallyVolatile(false))
  404. return true;
  405. }
  406. }
  407. if (check_ancestors)
  408. {
  409. StyleSheetNode* ancestor = parent;
  410. while (ancestor != NULL)
  411. {
  412. if (ancestor->type == STRUCTURAL_PSEUDO_CLASS)
  413. return true;
  414. ancestor = ancestor->parent;
  415. }
  416. }
  417. return false;
  418. }
  419. // Constructs a structural pseudo-class child node.
  420. StyleSheetNode* StyleSheetNode::CreateStructuralChild(const String& child_name)
  421. {
  422. StyleSheetNodeSelector* child_selector = StyleSheetFactory::GetSelector(child_name);
  423. if (child_selector == NULL)
  424. return NULL;
  425. // Parse the 'a' and 'b' values.
  426. int child_a = 1;
  427. int child_b = 0;
  428. size_t parameter_start = child_name.Find("(");
  429. size_t parameter_end = child_name.Find(")");
  430. if (parameter_start != String::npos &&
  431. parameter_end != String::npos)
  432. {
  433. String parameters = child_name.Substring(parameter_start + 1, parameter_end - (parameter_start + 1));
  434. // Check for 'even' or 'odd' first.
  435. if (parameters == "even")
  436. {
  437. child_a = 2;
  438. child_b = 0;
  439. }
  440. else if (parameters == "odd")
  441. {
  442. child_a = 2;
  443. child_b = 1;
  444. }
  445. else
  446. {
  447. // Alrighty; we've got an equation in the form of [[+/-]an][(+/-)b]. So, foist up, we split on 'n'.
  448. size_t n_index = parameters.Find("n");
  449. if (n_index != String::npos)
  450. {
  451. // The equation is 0n + b. So a = 0, and we only have to parse b.
  452. child_a = 0;
  453. child_b = atoi(parameters.CString());
  454. }
  455. else
  456. {
  457. if (n_index == 0)
  458. child_a = 1;
  459. else
  460. {
  461. String a_parameter = parameters.Substring(0, n_index);
  462. if (StringUtilities::StripWhitespace(a_parameter) == "-")
  463. child_a = -1;
  464. else
  465. child_a = atoi(a_parameter.CString());
  466. }
  467. if (n_index == parameters.Length() - 1)
  468. child_b = 0;
  469. else
  470. child_b = atoi(parameters.Substring(n_index + 1).CString());
  471. }
  472. }
  473. }
  474. return new StyleSheetNode(child_name, this, child_selector, child_a, child_b);
  475. }
  476. // Recursively builds up a list of all pseudo-classes branching from a single node.
  477. void StyleSheetNode::GetPseudoClassProperties(PseudoClassPropertyMap& pseudo_class_properties, const StringList& ancestor_pseudo_classes)
  478. {
  479. StringList pseudo_classes(ancestor_pseudo_classes);
  480. pseudo_classes.push_back(name);
  481. if (properties.GetNumProperties() > 0)
  482. pseudo_class_properties[pseudo_classes] = properties;
  483. for (NodeMap::const_iterator i = children[PSEUDO_CLASS].begin(); i != children[PSEUDO_CLASS].end(); ++i)
  484. (*i).second->GetPseudoClassProperties(pseudo_class_properties, pseudo_classes);
  485. }
  486. int StyleSheetNode::CalculateSpecificity()
  487. {
  488. // Calculate the specificity of just this node; tags are worth 10,000, IDs 1,000,000 and other specifiers (classes
  489. // and pseudo-classes) 100,000.
  490. int specificity = 0;
  491. switch (type)
  492. {
  493. case TAG:
  494. {
  495. if (!name.Empty())
  496. specificity = 10000;
  497. }
  498. break;
  499. case CLASS:
  500. case PSEUDO_CLASS:
  501. case STRUCTURAL_PSEUDO_CLASS:
  502. {
  503. specificity = 100000;
  504. }
  505. break;
  506. case ID:
  507. {
  508. specificity = 1000000;
  509. }
  510. break;
  511. default:
  512. {
  513. specificity = 0;
  514. }
  515. break;
  516. }
  517. // Add our parent's specificity onto ours.
  518. if (parent != NULL)
  519. specificity += parent->CalculateSpecificity();
  520. return specificity;
  521. }
  522. }
  523. }