ElementStyle.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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 "precompiled.h"
  28. #include "ElementStyle.h"
  29. #include <algorithm>
  30. #include "../../Include/Rocket/Core/ElementDocument.h"
  31. #include "../../Include/Rocket/Core/ElementUtilities.h"
  32. #include "../../Include/Rocket/Core/Log.h"
  33. #include "../../Include/Rocket/Core/Math.h"
  34. #include "../../Include/Rocket/Core/Property.h"
  35. #include "../../Include/Rocket/Core/PropertyDefinition.h"
  36. #include "../../Include/Rocket/Core/PropertyDictionary.h"
  37. #include "../../Include/Rocket/Core/StyleSheetSpecification.h"
  38. #include "../../Include/Rocket/Core/TransformPrimitive.h"
  39. #include "ElementBackground.h"
  40. #include "ElementBorder.h"
  41. #include "ElementDecoration.h"
  42. #include "ElementDefinition.h"
  43. #include "FontFaceHandle.h"
  44. #include "ComputeProperty.h"
  45. namespace Rocket {
  46. namespace Core {
  47. const Style::ComputedValues DefaultComputedValues;
  48. ElementStyle::ElementStyle(Element* _element)
  49. {
  50. local_properties = NULL;
  51. em_properties = NULL;
  52. definition = NULL;
  53. element = _element;
  54. definition_dirty = true;
  55. }
  56. ElementStyle::~ElementStyle()
  57. {
  58. if (local_properties != NULL)
  59. delete local_properties;
  60. if (em_properties != NULL)
  61. delete em_properties;
  62. if (definition != NULL)
  63. definition->RemoveReference();
  64. }
  65. // Returns the element's definition, updating if necessary.
  66. const ElementDefinition* ElementStyle::GetDefinition()
  67. {
  68. return definition;
  69. }
  70. // Returns one of this element's properties.
  71. const Property* ElementStyle::GetLocalProperty(const String& name, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  72. {
  73. // Check for overriding local properties.
  74. if (local_properties != NULL)
  75. {
  76. const Property* property = local_properties->GetProperty(name);
  77. if (property != NULL)
  78. return property;
  79. }
  80. // Check for a property defined in an RCSS rule.
  81. if (definition != NULL)
  82. return definition->GetProperty(name, pseudo_classes);
  83. return NULL;
  84. }
  85. // Returns one of this element's properties.
  86. const Property* ElementStyle::GetProperty(const String& name, Element* element, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  87. {
  88. const Property* local_property = GetLocalProperty(name, local_properties, definition, pseudo_classes);
  89. if (local_property != NULL)
  90. return local_property;
  91. // Fetch the property specification.
  92. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(name);
  93. if (property == NULL)
  94. return NULL;
  95. // If we can inherit this property, return our parent's property.
  96. if (property->IsInherited())
  97. {
  98. Element* parent = element->GetParentNode();
  99. while (parent != NULL)
  100. {
  101. const Property* parent_property = parent->GetStyle()->GetLocalProperty(name);
  102. if (parent_property)
  103. return parent_property;
  104. parent = parent->GetParentNode();
  105. }
  106. }
  107. // No property available! Return the default value.
  108. return property->GetDefaultValue();
  109. }
  110. // Apply transition to relevant properties if a transition is defined on element.
  111. // Properties that are part of a transition are removed from the properties list.
  112. void ElementStyle::TransitionPropertyChanges(Element* element, PropertyNameList& properties, PropertyDictionary* local_properties, ElementDefinition* old_definition, ElementDefinition* new_definition,
  113. const PseudoClassList& pseudo_classes_before, const PseudoClassList& pseudo_classes_after)
  114. {
  115. ROCKET_ASSERT(element);
  116. if (!old_definition || !new_definition || properties.empty())
  117. return;
  118. if (const Property* transition_property = GetLocalProperty(TRANSITION, local_properties, new_definition, pseudo_classes_after))
  119. {
  120. auto transition_list = transition_property->Get<TransitionList>();
  121. if (!transition_list.none)
  122. {
  123. auto add_transition = [&](const Transition& transition) {
  124. bool transition_added = false;
  125. const Property* start_value = GetProperty(transition.name, element, local_properties, old_definition, pseudo_classes_before);
  126. const Property* target_value = GetProperty(transition.name, element, nullptr, new_definition, pseudo_classes_after);
  127. if (start_value && target_value && (*start_value != *target_value))
  128. transition_added = element->StartTransition(transition, *start_value, *target_value);
  129. return transition_added;
  130. };
  131. if (transition_list.all)
  132. {
  133. Transition transition = transition_list.transitions[0];
  134. for (auto it = properties.begin(); it != properties.end(); )
  135. {
  136. transition.name = *it;
  137. if (add_transition(transition))
  138. it = properties.erase(it);
  139. else
  140. ++it;
  141. }
  142. }
  143. else
  144. {
  145. for (auto& transition : transition_list.transitions)
  146. {
  147. if (auto it = properties.find(transition.name); it != properties.end())
  148. {
  149. if (add_transition(transition))
  150. properties.erase(it);
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. void ElementStyle::UpdateDefinition()
  158. {
  159. if (definition_dirty)
  160. {
  161. definition_dirty = false;
  162. ElementDefinition* new_definition = NULL;
  163. const StyleSheet* style_sheet = GetStyleSheet();
  164. if (style_sheet != NULL)
  165. {
  166. new_definition = style_sheet->GetElementDefinition(element);
  167. }
  168. // Switch the property definitions if the definition has changed.
  169. if (new_definition != definition || new_definition == NULL)
  170. {
  171. PropertyNameList properties;
  172. if (definition != NULL)
  173. definition->GetDefinedProperties(properties, pseudo_classes);
  174. if (new_definition != NULL)
  175. new_definition->GetDefinedProperties(properties, pseudo_classes);
  176. TransitionPropertyChanges(element, properties, local_properties, definition, new_definition, pseudo_classes, pseudo_classes);
  177. if (definition != NULL)
  178. definition->RemoveReference();
  179. definition = new_definition;
  180. DirtyProperties(properties);
  181. element->GetElementDecoration()->DirtyDecorators(true);
  182. }
  183. else if (new_definition != NULL)
  184. {
  185. new_definition->RemoveReference();
  186. }
  187. }
  188. }
  189. // Sets or removes a pseudo-class on the element.
  190. void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
  191. {
  192. size_t num_pseudo_classes = pseudo_classes.size();
  193. auto pseudo_classes_before = pseudo_classes;
  194. if (activate)
  195. pseudo_classes.push_back(pseudo_class);
  196. else
  197. {
  198. // In case of duplicates, we do a loop here. We could do a sort and unique instead,
  199. // but that might even be slower for a small list with few duplicates, which
  200. // is probably the most common case.
  201. auto it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
  202. while(it != pseudo_classes.end())
  203. {
  204. pseudo_classes.erase(it);
  205. it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
  206. }
  207. }
  208. if (pseudo_classes.size() != num_pseudo_classes)
  209. {
  210. element->GetElementDecoration()->DirtyDecorators(false);
  211. if (definition != NULL)
  212. {
  213. PropertyNameList properties;
  214. definition->GetDefinedProperties(properties, pseudo_classes, pseudo_class);
  215. TransitionPropertyChanges(element, properties, local_properties, definition, definition, pseudo_classes_before, pseudo_classes);
  216. DirtyProperties(properties);
  217. switch (definition->GetPseudoClassVolatility(pseudo_class))
  218. {
  219. case ElementDefinition::FONT_VOLATILE:
  220. element->DirtyFont();
  221. break;
  222. case ElementDefinition::STRUCTURE_VOLATILE:
  223. DirtyChildDefinitions();
  224. break;
  225. default:
  226. break;
  227. }
  228. }
  229. }
  230. }
  231. // Checks if a specific pseudo-class has been set on the element.
  232. bool ElementStyle::IsPseudoClassSet(const String& pseudo_class) const
  233. {
  234. return (std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class) != pseudo_classes.end());
  235. }
  236. const PseudoClassList& ElementStyle::GetActivePseudoClasses() const
  237. {
  238. return pseudo_classes;
  239. }
  240. // Sets or removes a class on the element.
  241. void ElementStyle::SetClass(const String& class_name, bool activate)
  242. {
  243. StringList::iterator class_location = std::find(classes.begin(), classes.end(), class_name);
  244. if (activate)
  245. {
  246. if (class_location == classes.end())
  247. {
  248. classes.push_back(class_name);
  249. DirtyDefinition();
  250. }
  251. }
  252. else
  253. {
  254. if (class_location != classes.end())
  255. {
  256. classes.erase(class_location);
  257. DirtyDefinition();
  258. }
  259. }
  260. }
  261. // Checks if a class is set on the element.
  262. bool ElementStyle::IsClassSet(const String& class_name) const
  263. {
  264. return std::find(classes.begin(), classes.end(), class_name) != classes.end();
  265. }
  266. // Specifies the entire list of classes for this element. This will replace any others specified.
  267. void ElementStyle::SetClassNames(const String& class_names)
  268. {
  269. classes.clear();
  270. StringUtilities::ExpandString(classes, class_names, ' ');
  271. DirtyDefinition();
  272. }
  273. // Returns the list of classes specified for this element.
  274. String ElementStyle::GetClassNames() const
  275. {
  276. String class_names;
  277. for (size_t i = 0; i < classes.size(); i++)
  278. {
  279. if (i != 0)
  280. {
  281. class_names += " ";
  282. }
  283. class_names += classes[i];
  284. }
  285. return class_names;
  286. }
  287. // Sets a local property override on the element.
  288. bool ElementStyle::SetProperty(const String& name, const String& value)
  289. {
  290. if (local_properties == NULL)
  291. local_properties = new PropertyDictionary();
  292. if (StyleSheetSpecification::ParsePropertyDeclaration(*local_properties, name, value))
  293. {
  294. DirtyProperty(name);
  295. return true;
  296. }
  297. else
  298. {
  299. Log::Message(Log::LT_WARNING, "Syntax error parsing inline property declaration '%s: %s;'.", name.c_str(), value.c_str());
  300. return false;
  301. }
  302. }
  303. // Sets a local property override on the element to a pre-parsed value.
  304. bool ElementStyle::SetProperty(const String& name, const Property& property)
  305. {
  306. Property new_property = property;
  307. new_property.definition = StyleSheetSpecification::GetProperty(name);
  308. if (new_property.definition == NULL)
  309. return false;
  310. if (local_properties == NULL)
  311. local_properties = new PropertyDictionary();
  312. local_properties->SetProperty(name, new_property);
  313. DirtyProperty(name);
  314. return true;
  315. }
  316. // Removes a local property override on the element.
  317. void ElementStyle::RemoveProperty(const String& name)
  318. {
  319. if (local_properties == NULL)
  320. return;
  321. if (local_properties->GetProperty(name) != NULL)
  322. {
  323. local_properties->RemoveProperty(name);
  324. DirtyProperty(name);
  325. }
  326. }
  327. // Returns one of this element's properties.
  328. const Property* ElementStyle::GetProperty(const String& name)
  329. {
  330. return GetProperty(name, element, local_properties, definition, pseudo_classes);
  331. }
  332. // Returns one of this element's properties.
  333. const Property* ElementStyle::GetLocalProperty(const String& name)
  334. {
  335. return GetLocalProperty(name, local_properties, definition, pseudo_classes);
  336. }
  337. const PropertyMap * ElementStyle::GetLocalProperties() const
  338. {
  339. if (local_properties)
  340. return &local_properties->GetProperties();
  341. return NULL;
  342. }
  343. float ElementStyle::ResolveNumberLengthPercentage(const Property * property, RelativeTarget relative_target)
  344. {
  345. // There is an exception on font-size properties, as 'em' units here refer to parent font size instead
  346. if ((property->unit & Property::LENGTH) && !(property->unit == Property::EM && relative_target == RelativeTarget::ParentFontSize))
  347. {
  348. float result = ComputeLength(property, element->GetComputedValues().font_size, element->GetOwnerDocument()->GetComputedValues().font_size, ElementUtilities::GetDensityIndependentPixelRatio(element));
  349. return result;
  350. }
  351. float base_value = 0.0f;
  352. switch (relative_target)
  353. {
  354. case RelativeTarget::None:
  355. base_value = 1.0f;
  356. break;
  357. case RelativeTarget::ContainingBlockWidth:
  358. base_value = element->GetContainingBlock().x;
  359. break;
  360. case RelativeTarget::ContainingBlockHeight:
  361. base_value = element->GetContainingBlock().y;
  362. break;
  363. case RelativeTarget::FontSize:
  364. base_value = element->GetComputedValues().font_size;
  365. break;
  366. case RelativeTarget::ParentFontSize:
  367. base_value = element->GetParentNode()->GetComputedValues().font_size;
  368. break;
  369. case RelativeTarget::LineHeight:
  370. base_value = element->GetLineHeight();
  371. break;
  372. default:
  373. break;
  374. }
  375. float scale_value = 0.0f;
  376. switch (property->unit)
  377. {
  378. case Property::EM:
  379. case Property::NUMBER:
  380. scale_value = property->value.Get< float >();
  381. break;
  382. case Property::PERCENT:
  383. scale_value = property->value.Get< float >() * 0.01f;
  384. break;
  385. }
  386. return base_value * scale_value;
  387. }
  388. // Resolves one of this element's properties.
  389. float ElementStyle::ResolveLengthPercentage(const Property* property, float base_value)
  390. {
  391. if (!property)
  392. {
  393. ROCKET_ERROR;
  394. return 0.0f;
  395. }
  396. ROCKET_ASSERT(property->unit & Property::LENGTH_PERCENT);
  397. const float font_size = element->GetComputedValues().font_size;
  398. const float doc_font_size = element->GetOwnerDocument()->GetComputedValues().font_size;
  399. const float dp_ratio = ElementUtilities::GetDensityIndependentPixelRatio(element);
  400. Style::LengthPercentage computed = ComputeLengthPercentage(property, font_size, doc_font_size, dp_ratio);
  401. float result = ResolveValue(computed, base_value);
  402. return result;
  403. }
  404. // Iterates over the properties defined on the element.
  405. bool ElementStyle::IterateProperties(int& index, String& name, const Property*& property, const PseudoClassList** property_pseudo_classes)
  406. {
  407. // First check for locally defined properties.
  408. if (local_properties != NULL)
  409. {
  410. if (index < local_properties->GetNumProperties())
  411. {
  412. PropertyMap::const_iterator i = local_properties->GetProperties().begin();
  413. for (int count = 0; count < index; ++count)
  414. ++i;
  415. name = (*i).first;
  416. property = &((*i).second);
  417. if (property_pseudo_classes)
  418. * property_pseudo_classes = nullptr;
  419. ++index;
  420. return true;
  421. }
  422. }
  423. const ElementDefinition* definition = GetDefinition();
  424. if (definition != NULL)
  425. {
  426. int index_offset = 0;
  427. if (local_properties != NULL)
  428. index_offset = local_properties->GetNumProperties();
  429. // Offset the index to be relative to the definition before we start indexing. When we do get a property back,
  430. // check that it hasn't been overridden by the element's local properties; if so, continue on to the next one.
  431. index -= index_offset;
  432. while (definition->IterateProperties(index, pseudo_classes, name, property, property_pseudo_classes))
  433. {
  434. if (local_properties == NULL ||
  435. local_properties->GetProperty(name) == NULL)
  436. {
  437. index += index_offset;
  438. return true;
  439. }
  440. }
  441. return false;
  442. }
  443. return false;
  444. }
  445. // Returns the active style sheet for this element. This may be NULL.
  446. StyleSheet* ElementStyle::GetStyleSheet() const
  447. {
  448. ElementDocument* document = element->GetOwnerDocument();
  449. if (document != NULL)
  450. return document->GetStyleSheet();
  451. return NULL;
  452. }
  453. void ElementStyle::DirtyDefinition()
  454. {
  455. definition_dirty = true;
  456. DirtyChildDefinitions();
  457. }
  458. void ElementStyle::DirtyChildDefinitions()
  459. {
  460. for (int i = 0; i < element->GetNumChildren(true); i++)
  461. element->GetChild(i)->GetStyle()->DirtyDefinition();
  462. }
  463. // Dirties every property.
  464. void ElementStyle::DirtyProperties()
  465. {
  466. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  467. DirtyProperties(properties);
  468. }
  469. // Dirties em-relative properties.
  470. void ElementStyle::DirtyEmProperties()
  471. {
  472. if (!em_properties)
  473. {
  474. // Check if any of these are currently em-relative. If so, dirty them.
  475. em_properties = new PropertyNameList;
  476. for (auto& property : StyleSheetSpecification::GetRegisteredProperties())
  477. {
  478. // Skip font-size; this is relative to our parent's em, not ours.
  479. if (property == FONT_SIZE)
  480. continue;
  481. // Get this property from this element. If this is em-relative, then add it to the list to
  482. // dirty.
  483. if (element->GetProperty(property)->unit == Property::EM)
  484. em_properties->insert(property);
  485. }
  486. }
  487. if (!em_properties->empty())
  488. DirtyProperties(*em_properties, false);
  489. // Now dirty all of our descendant's font-size properties that are relative to ems.
  490. int num_children = element->GetNumChildren(true);
  491. for (int i = 0; i < num_children; ++i)
  492. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  493. }
  494. // Dirties font-size on child elements if appropriate.
  495. void ElementStyle::DirtyInheritedEmProperties()
  496. {
  497. const Property* font_size = element->GetLocalProperty(FONT_SIZE);
  498. if (font_size == NULL)
  499. {
  500. int num_children = element->GetNumChildren(true);
  501. for (int i = 0; i < num_children; ++i)
  502. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  503. }
  504. else
  505. {
  506. if (font_size->unit & Property::RELATIVE_UNIT)
  507. DirtyProperty(FONT_SIZE);
  508. }
  509. }
  510. // Dirties rem properties.
  511. void ElementStyle::DirtyRemProperties()
  512. {
  513. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  514. PropertyNameList rem_properties;
  515. // Dirty all the properties of this element that use the rem unit.
  516. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  517. {
  518. if (element->GetProperty(*list_iterator)->unit == Property::REM)
  519. rem_properties.insert(*list_iterator);
  520. }
  521. if (!rem_properties.empty())
  522. DirtyProperties(rem_properties, false);
  523. // Now dirty all of our descendant's properties that use the rem unit.
  524. int num_children = element->GetNumChildren(true);
  525. for (int i = 0; i < num_children; ++i)
  526. element->GetChild(i)->GetStyle()->DirtyRemProperties();
  527. }
  528. void ElementStyle::DirtyDpProperties()
  529. {
  530. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  531. PropertyNameList dp_properties;
  532. // Dirty all the properties of this element that use the dp unit.
  533. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  534. {
  535. if (element->GetProperty(*list_iterator)->unit == Property::DP)
  536. dp_properties.insert(*list_iterator);
  537. }
  538. if (!dp_properties.empty())
  539. DirtyProperties(dp_properties, false);
  540. // Now dirty all of our descendant's properties that use the dp unit.
  541. int num_children = element->GetNumChildren(true);
  542. for (int i = 0; i < num_children; ++i)
  543. element->GetChild(i)->GetStyle()->DirtyDpProperties();
  544. }
  545. // Sets a single property as dirty.
  546. void ElementStyle::DirtyProperty(const String& property)
  547. {
  548. PropertyNameList properties;
  549. properties.insert(String(property));
  550. DirtyProperties(properties);
  551. }
  552. // Sets a list of properties as dirty.
  553. void ElementStyle::DirtyProperties(const PropertyNameList& properties, bool clear_em_properties)
  554. {
  555. if (properties.empty())
  556. return;
  557. bool all_inherited_dirty =
  558. &properties == &StyleSheetSpecification::GetRegisteredProperties() ||
  559. StyleSheetSpecification::GetRegisteredProperties() == properties ||
  560. StyleSheetSpecification::GetRegisteredInheritedProperties() == properties;
  561. if (all_inherited_dirty)
  562. {
  563. const PropertyNameList &all_inherited_properties = StyleSheetSpecification::GetRegisteredInheritedProperties();
  564. for (int i = 0; i < element->GetNumChildren(true); i++)
  565. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(all_inherited_properties);
  566. }
  567. else
  568. {
  569. PropertyNameList inherited_properties;
  570. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  571. {
  572. // If this property is an inherited property, then push it into the list to be passed onto our children.
  573. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(*i);
  574. if (property != NULL &&
  575. property->IsInherited())
  576. inherited_properties.insert(*i);
  577. }
  578. // Pass the list of those properties that are inherited onto our children.
  579. if (!inherited_properties.empty())
  580. {
  581. for (int i = 0; i < element->GetNumChildren(true); i++)
  582. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  583. }
  584. }
  585. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  586. if (clear_em_properties && em_properties != NULL)
  587. {
  588. delete em_properties;
  589. em_properties = NULL;
  590. }
  591. // And send the event.
  592. element->DirtyProperties(properties);
  593. }
  594. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  595. void ElementStyle::DirtyInheritedProperties(const PropertyNameList& properties)
  596. {
  597. bool clear_em_properties = em_properties != NULL;
  598. PropertyNameList inherited_properties;
  599. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  600. {
  601. const Property *property = GetLocalProperty((*i));
  602. if (property == NULL)
  603. {
  604. inherited_properties.insert(*i);
  605. if (!clear_em_properties && em_properties != NULL && em_properties->find((*i)) != em_properties->end()) {
  606. clear_em_properties = true;
  607. }
  608. }
  609. }
  610. if (inherited_properties.empty())
  611. return;
  612. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  613. if (clear_em_properties && em_properties != NULL)
  614. {
  615. delete em_properties;
  616. em_properties = NULL;
  617. }
  618. // Pass the list of those properties that this element doesn't override onto our children.
  619. for (int i = 0; i < element->GetNumChildren(true); i++)
  620. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  621. element->DirtyProperties(properties);
  622. }
  623. // Must be called in correct order, from document root to children elements.
  624. void ElementStyle::ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, bool values_are_default_initialized, float dp_ratio)
  625. {
  626. // Generally, this is how it works (for now, we can probably be smarter about this):
  627. // 1. Assign default values (clears any newly dirtied properties)
  628. // 2. Inherit inheritable values from parent
  629. // 3. Assign any local properties (from inline style or stylesheet)
  630. // The next flag is just a small optimization, if the element was just created we don't need to copy all the default values.
  631. if (!values_are_default_initialized)
  632. {
  633. values = DefaultComputedValues;
  634. }
  635. // Always do font-size first if dirty, because of em-relative values
  636. if (auto p = GetLocalProperty(FONT_SIZE))
  637. values.font_size = ComputeFontsize(*p, values, parent_values, document_values, dp_ratio);
  638. else if (parent_values)
  639. values.font_size = parent_values->font_size;
  640. const float font_size = values.font_size;
  641. const float document_font_size = (document_values ? document_values->font_size : DefaultComputedValues.font_size);
  642. // Since vertical-align depends on line-height we compute this before iteration
  643. if (auto p = GetLocalProperty(LINE_HEIGHT))
  644. {
  645. values.line_height = ComputeLineHeight(p, font_size, document_font_size, dp_ratio);
  646. }
  647. else if (parent_values)
  648. {
  649. // Line height has a special inheritance case for numbers/percent: they inherit them directly instead of computed length, but for lengths, they inherit the length.
  650. // See CSS specs for details. Percent is already converted to number.
  651. if (parent_values->line_height.inherit_type == Style::LineHeight::Number)
  652. values.line_height = Style::LineHeight(font_size * parent_values->line_height.inherit_value, Style::LineHeight::Number, parent_values->line_height.inherit_value);
  653. else
  654. values.line_height = parent_values->line_height;
  655. }
  656. if (parent_values)
  657. {
  658. // Inherited properties are copied here, but may be overwritten below by locally defined properties
  659. // Line-height and font-size are computed above
  660. values.clip = parent_values->clip;
  661. values.color = parent_values->color;
  662. values.opacity = parent_values->opacity;
  663. values.font_family = parent_values->font_family;
  664. values.font_charset = parent_values->font_charset;
  665. values.font_style = parent_values->font_style;
  666. values.font_weight = parent_values->font_weight;
  667. values.text_align = parent_values->text_align;
  668. values.text_decoration = parent_values->text_decoration;
  669. values.text_transform = parent_values->text_transform;
  670. values.white_space = parent_values->white_space;
  671. values.cursor = parent_values->cursor;
  672. values.focus = parent_values->focus;
  673. values.pointer_events = parent_values->pointer_events;
  674. }
  675. int index = 0;
  676. String name;
  677. const Property* p = nullptr;
  678. while (IterateProperties(index, name, p))
  679. {
  680. using namespace Style;
  681. // @performance: Can use a switch-case with constexpr hashing function
  682. // Or even better: a PropertyId enum, but the problem is custom properties such as decorators. We may want to redo the decleration of these perhaps...
  683. // @performance: Compare to the list of actually changed properties, skip if not inside it
  684. if (name == MARGIN_TOP)
  685. values.margin_top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  686. else if (name == MARGIN_RIGHT)
  687. values.margin_right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  688. else if (name == MARGIN_BOTTOM)
  689. values.margin_bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  690. else if (name == MARGIN_LEFT)
  691. values.margin_left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  692. else if (name == PADDING_TOP)
  693. values.padding_top = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  694. else if (name == PADDING_RIGHT)
  695. values.padding_right = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  696. else if (name == PADDING_BOTTOM)
  697. values.padding_bottom = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  698. else if (name == PADDING_LEFT)
  699. values.padding_left = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  700. else if (name == BORDER_TOP_WIDTH)
  701. values.border_top_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  702. else if (name == BORDER_RIGHT_WIDTH)
  703. values.border_right_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  704. else if (name == BORDER_BOTTOM_WIDTH)
  705. values.border_bottom_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  706. else if (name == BORDER_LEFT_WIDTH)
  707. values.border_left_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  708. else if (name == BORDER_TOP_COLOR)
  709. values.border_top_color = p->Get<Colourb>();
  710. else if (name == BORDER_RIGHT_COLOR)
  711. values.border_right_color = p->Get<Colourb>();
  712. else if (name == BORDER_BOTTOM_COLOR)
  713. values.border_bottom_color = p->Get<Colourb>();
  714. else if (name == BORDER_LEFT_COLOR)
  715. values.border_left_color = p->Get<Colourb>();
  716. else if (name == DISPLAY)
  717. values.display = (Display)p->Get<int>();
  718. else if (name == POSITION)
  719. values.position = (Position)p->Get<int>();
  720. else if (name == TOP)
  721. values.top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  722. else if (name == RIGHT)
  723. values.right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  724. else if (name == BOTTOM)
  725. values.bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  726. else if (name == LEFT)
  727. values.left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  728. else if (name == FLOAT)
  729. values.float_ = (Float)p->Get<int>();
  730. else if (name == CLEAR)
  731. values.clear = (Clear)p->Get<int>();
  732. else if (name == Z_INDEX)
  733. values.z_index = (p->unit == Property::KEYWORD ? ZIndex(ZIndex::Auto) : ZIndex(ZIndex::Number, p->Get<float>()));
  734. else if (name == WIDTH)
  735. values.width = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  736. else if (name == MIN_WIDTH)
  737. values.min_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  738. else if (name == MAX_WIDTH)
  739. values.max_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  740. else if (name == HEIGHT)
  741. values.height = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  742. else if (name == MIN_HEIGHT)
  743. values.min_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  744. else if (name == MAX_HEIGHT)
  745. values.max_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  746. // (Line-height computed above)
  747. else if (name == VERTICAL_ALIGN)
  748. values.vertical_align = ComputeVerticalAlign(p, values.line_height.value, font_size, document_font_size, dp_ratio);
  749. else if (name == OVERFLOW_X)
  750. values.overflow_x = (Overflow)p->Get< int >();
  751. else if (name == OVERFLOW_Y)
  752. values.overflow_y = (Overflow)p->Get< int >();
  753. else if (name == CLIP)
  754. values.clip = ComputeClip(p);
  755. else if (name == VISIBILITY)
  756. values.visibility = (Visibility)p->Get< int >();
  757. else if (name == BACKGROUND_COLOR)
  758. values.background_color = p->Get<Colourb>();
  759. else if (name == COLOR)
  760. values.color = p->Get<Colourb>();
  761. else if (name == IMAGE_COLOR)
  762. values.image_color = p->Get<Colourb>();
  763. else if (name == OPACITY)
  764. values.opacity = p->Get<float>();
  765. else if (name == FONT_FAMILY)
  766. values.font_family = ToLower(p->Get<String>());
  767. else if (name == FONT_CHARSET)
  768. values.font_charset = p->Get<String>();
  769. else if (name == FONT_STYLE)
  770. values.font_style = (FontStyle)p->Get< int >();
  771. else if (name == FONT_WEIGHT)
  772. values.font_weight = (FontWeight)p->Get< int >();
  773. // (font-size computed above)
  774. else if (name == TEXT_ALIGN)
  775. values.text_align = (TextAlign)p->Get< int >();
  776. else if (name == TEXT_DECORATION)
  777. values.text_decoration = (TextDecoration)p->Get< int >();
  778. else if (name == TEXT_TRANSFORM)
  779. values.text_transform = (TextTransform)p->Get< int >();
  780. else if (name == WHITE_SPACE)
  781. values.white_space = (WhiteSpace)p->Get< int >();
  782. else if (name == CURSOR)
  783. values.cursor = p->Get< String >();
  784. else if (name == DRAG)
  785. values.drag = (Drag)p->Get< int >();
  786. else if (name == TAB_INDEX)
  787. values.tab_index = (TabIndex)p->Get< int >();
  788. else if (name == FOCUS)
  789. values.focus = (Focus)p->Get<int>();
  790. else if (name == SCROLLBAR_MARGIN)
  791. values.scrollbar_margin = ComputeLength(p, font_size, document_font_size, dp_ratio);
  792. else if (name == POINTER_EVENTS)
  793. values.pointer_events = (PointerEvents)p->Get<int>();
  794. else if (name == PERSPECTIVE)
  795. values.perspective = ComputeLength(p, font_size, document_font_size, dp_ratio);
  796. else if (name == PERSPECTIVE_ORIGIN_X)
  797. values.perspective_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  798. else if (name == PERSPECTIVE_ORIGIN_Y)
  799. values.perspective_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  800. else if (name == TRANSFORM)
  801. values.transform = p->Get<TransformRef>();
  802. else if(name == TRANSFORM_ORIGIN_X)
  803. values.transform_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  804. else if (name == TRANSFORM_ORIGIN_Y)
  805. values.transform_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  806. else if (name == TRANSFORM_ORIGIN_Z)
  807. values.transform_origin_z = ComputeLength(p, font_size, document_font_size, dp_ratio);
  808. else if (name == TRANSITION)
  809. values.transition = p->Get<TransitionList>();
  810. else if (name == ANIMATION)
  811. values.animation = p->Get<AnimationList>();
  812. }
  813. }
  814. }
  815. }