ElementStyle.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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 "ElementStyleCache.h"
  30. #include <algorithm>
  31. #include "../../Include/Rocket/Core/ElementDocument.h"
  32. #include "../../Include/Rocket/Core/ElementUtilities.h"
  33. #include "../../Include/Rocket/Core/Log.h"
  34. #include "../../Include/Rocket/Core/Math.h"
  35. #include "../../Include/Rocket/Core/Property.h"
  36. #include "../../Include/Rocket/Core/PropertyDefinition.h"
  37. #include "../../Include/Rocket/Core/PropertyDictionary.h"
  38. #include "../../Include/Rocket/Core/StyleSheetSpecification.h"
  39. #include "../../Include/Rocket/Core/TransformPrimitive.h"
  40. #include "ElementBackground.h"
  41. #include "ElementBorder.h"
  42. #include "ElementDecoration.h"
  43. #include "ElementDefinition.h"
  44. #include "FontFaceHandle.h"
  45. namespace Rocket {
  46. namespace Core {
  47. ElementStyle::ElementStyle(Element* _element)
  48. {
  49. local_properties = NULL;
  50. em_properties = NULL;
  51. definition = NULL;
  52. element = _element;
  53. cache = new ElementStyleCache(this);
  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. delete cache;
  65. }
  66. // Returns the element's definition, updating if necessary.
  67. const ElementDefinition* ElementStyle::GetDefinition()
  68. {
  69. return definition;
  70. }
  71. // Returns one of this element's properties.
  72. const Property* ElementStyle::GetLocalProperty(PropertyId id, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  73. {
  74. // Check for overriding local properties.
  75. if (local_properties != NULL)
  76. {
  77. if (auto it = local_properties->find(id); it != local_properties->end())
  78. return &it->second;
  79. }
  80. // Check for a property defined in an RCSS rule.
  81. if (definition != NULL)
  82. return definition->GetProperty(id, pseudo_classes);
  83. return NULL;
  84. }
  85. // Returns one of this element's properties.
  86. const Property* ElementStyle::GetProperty(PropertyId id, Element* element, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  87. {
  88. const Property* local_property = GetLocalProperty(id, 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(id);
  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(id);
  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, PropertyIdList& 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(PropertyId::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.property_id, element, local_properties, old_definition, pseudo_classes_before);
  126. const Property* target_value = GetProperty(transition.property_id, 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.property_id = *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.property_id); 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. PropertyIdList 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()->ReloadDecorators();
  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();
  211. if (definition != NULL)
  212. {
  213. PropertyIdList 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(PropertyId id, const String& value)
  289. {
  290. if (local_properties == NULL)
  291. local_properties = new PropertyDictionary();
  292. if (StyleSheetSpecification::ParsePropertyDeclaration(*local_properties, id, value))
  293. {
  294. DirtyProperty(id);
  295. return true;
  296. }
  297. else
  298. {
  299. const String& name = GetName(id);
  300. Log::Message(Log::LT_WARNING, "Syntax error parsing inline property declaration '%s: %s;'.", name.c_str(), value.c_str());
  301. return false;
  302. }
  303. }
  304. // Sets a local property override on the element to a pre-parsed value.
  305. bool ElementStyle::SetProperty(PropertyId property_id, const Property& property)
  306. {
  307. Property new_property = property;
  308. new_property.definition = StyleSheetSpecification::GetProperty(property_id);
  309. if (new_property.definition == NULL)
  310. return false;
  311. sizeof(ElementDefinition);
  312. if (local_properties == NULL)
  313. local_properties = new PropertyDictionary();
  314. (*local_properties)[property_id] = new_property;
  315. DirtyProperty(property_id);
  316. return true;
  317. }
  318. // Removes a local property override on the element.
  319. void ElementStyle::RemoveProperty(PropertyId property_id)
  320. {
  321. if (local_properties == NULL)
  322. return;
  323. if (local_properties->find(property_id) != local_properties->end())
  324. {
  325. local_properties->erase(property_id);
  326. DirtyProperty(property_id);
  327. }
  328. }
  329. // Returns one of this element's properties.
  330. const Property* ElementStyle::GetProperty(PropertyId property_id)
  331. {
  332. return GetProperty(property_id, element, local_properties, definition, pseudo_classes);
  333. }
  334. // Returns one of this element's properties.
  335. const Property* ElementStyle::GetLocalProperty(PropertyId property_id)
  336. {
  337. return GetLocalProperty(property_id, local_properties, definition, pseudo_classes);
  338. }
  339. const PropertyMap * ElementStyle::GetLocalProperties() const
  340. {
  341. if (local_properties)
  342. return local_properties;
  343. return NULL;
  344. }
  345. float ElementStyle::ResolveLength(const Property * property)
  346. {
  347. if (!property)
  348. {
  349. ROCKET_ERROR;
  350. return 0.0f;
  351. }
  352. if (!(property->unit & Property::LENGTH))
  353. {
  354. ROCKET_ERRORMSG("Trying to resolve length on a non-length property.");
  355. return 0.0f;
  356. }
  357. switch (property->unit)
  358. {
  359. case Property::NUMBER:
  360. case Property::PX:
  361. return property->value.Get< float >();
  362. case Property::EM:
  363. return property->value.Get< float >() * ElementUtilities::GetFontSize(element);
  364. case Property::REM:
  365. return property->value.Get< float >() * ElementUtilities::GetFontSize(element->GetOwnerDocument());
  366. case Property::DP:
  367. return property->value.Get< float >() * ElementUtilities::GetDensityIndependentPixelRatio(element);
  368. }
  369. // Values based on pixels-per-inch.
  370. if (property->unit & Property::PPI_UNIT)
  371. {
  372. float inch = property->value.Get< float >() * element->GetRenderInterface()->GetPixelsPerInch();
  373. switch (property->unit)
  374. {
  375. case Property::INCH: // inch
  376. return inch;
  377. case Property::CM: // centimeter
  378. return inch * (1.0f / 2.54f);
  379. case Property::MM: // millimeter
  380. return inch * (1.0f / 25.4f);
  381. case Property::PT: // point
  382. return inch * (1.0f / 72.0f);
  383. case Property::PC: // pica
  384. return inch * (1.0f / 6.0f);
  385. }
  386. }
  387. // We're not a numeric property; return 0.
  388. return 0.0f;
  389. }
  390. float ElementStyle::ResolveAngle(const Property * property)
  391. {
  392. switch (property->unit)
  393. {
  394. case Property::NUMBER:
  395. case Property::DEG:
  396. return Math::DegreesToRadians(property->value.Get< float >());
  397. case Property::RAD:
  398. return property->value.Get< float >();
  399. case Property::PERCENT:
  400. return property->value.Get< float >() * 0.01f * 2.0f * Math::ROCKET_PI;
  401. }
  402. ROCKET_ERRORMSG("Trying to resolve angle on a non-angle property.");
  403. return 0.0f;
  404. }
  405. float ElementStyle::ResolveNumericProperty(PropertyId property_id, const Property * property)
  406. {
  407. if ((property->unit & Property::LENGTH) && !(property->unit == Property::EM && property_id == PropertyId::FontSize))
  408. {
  409. return ResolveLength(property);
  410. }
  411. auto definition = property->definition;
  412. if (!definition) definition = StyleSheetSpecification::GetProperty(property_id);
  413. if (!definition) return 0.0f;
  414. auto relative_target = definition->GetRelativeTarget();
  415. return ResolveNumericProperty(property, relative_target);
  416. }
  417. float ElementStyle::ResolveNumericProperty(const Property * property, RelativeTarget relative_target)
  418. {
  419. // There is an exception on font-size properties, as 'em' units here refer to parent font size instead
  420. if ((property->unit & Property::LENGTH) && !(property->unit == Property::EM && relative_target == RelativeTarget::ParentFontSize))
  421. {
  422. return ResolveLength(property);
  423. }
  424. float base_value = 0.0f;
  425. switch (relative_target)
  426. {
  427. case RelativeTarget::None:
  428. base_value = 1.0f;
  429. break;
  430. case RelativeTarget::ContainingBlockWidth:
  431. base_value = element->GetContainingBlock().x;
  432. break;
  433. case RelativeTarget::ContainingBlockHeight:
  434. base_value = element->GetContainingBlock().y;
  435. break;
  436. case RelativeTarget::FontSize:
  437. base_value = (float)ElementUtilities::GetFontSize(element);
  438. break;
  439. case RelativeTarget::ParentFontSize:
  440. base_value = (float)ElementUtilities::GetFontSize(element->GetParentNode());
  441. break;
  442. case RelativeTarget::LineHeight:
  443. base_value = (float)ElementUtilities::GetLineHeight(element);
  444. break;
  445. default:
  446. break;
  447. }
  448. float scale_value = 0.0f;
  449. switch (property->unit)
  450. {
  451. case Property::EM:
  452. case Property::NUMBER:
  453. scale_value = property->value.Get< float >();
  454. break;
  455. case Property::PERCENT:
  456. scale_value = property->value.Get< float >() * 0.01f;
  457. break;
  458. }
  459. return base_value * scale_value;
  460. }
  461. // Resolves one of this element's properties.
  462. float ElementStyle::ResolveProperty(const Property* property, float base_value)
  463. {
  464. if (!property)
  465. {
  466. ROCKET_ERROR;
  467. return 0.0f;
  468. }
  469. switch (property->unit)
  470. {
  471. case Property::NUMBER:
  472. case Property::PX:
  473. case Property::RAD:
  474. return property->value.Get< float >();
  475. case Property::PERCENT:
  476. return base_value * property->value.Get< float >() * 0.01f;
  477. case Property::EM:
  478. return property->value.Get< float >() * (float)ElementUtilities::GetFontSize(element);
  479. case Property::REM:
  480. return property->value.Get< float >() * (float)ElementUtilities::GetFontSize(element->GetOwnerDocument());
  481. case Property::DP:
  482. return property->value.Get< float >() * ElementUtilities::GetDensityIndependentPixelRatio(element);
  483. case Property::DEG:
  484. return Math::DegreesToRadians(property->value.Get< float >());
  485. }
  486. // Values based on pixels-per-inch.
  487. if (property->unit & Property::PPI_UNIT)
  488. {
  489. float inch = property->value.Get< float >() * element->GetRenderInterface()->GetPixelsPerInch();
  490. switch (property->unit)
  491. {
  492. case Property::INCH: // inch
  493. return inch;
  494. case Property::CM: // centimeter
  495. return inch * (1.0f / 2.54f);
  496. case Property::MM: // millimeter
  497. return inch * (1.0f / 25.4f);
  498. case Property::PT: // point
  499. return inch * (1.0f / 72.0f);
  500. case Property::PC: // pica
  501. return inch * (1.0f / 6.0f);
  502. }
  503. }
  504. // We're not a numeric property; return 0.
  505. return 0.0f;
  506. }
  507. // Resolves one of this element's properties.
  508. float ElementStyle::ResolveProperty(PropertyId property_id, float base_value)
  509. {
  510. const Property* property = GetProperty(property_id);
  511. if (!property)
  512. {
  513. ROCKET_ERROR;
  514. return 0.0f;
  515. }
  516. // The calculated value of the font-size property is inherited, so we need to check if this
  517. // is an inherited property. If so, then we return our parent's font size instead.
  518. if (property_id == PropertyId::FontSize && property->unit & Property::RELATIVE_UNIT)
  519. {
  520. // If the rem unit is used, the font-size is inherited directly from the document,
  521. // otherwise we use the parent's font size.
  522. if (property->unit & Property::REM)
  523. {
  524. Rocket::Core::ElementDocument* owner_document = element->GetOwnerDocument();
  525. if (owner_document == NULL)
  526. return 0;
  527. base_value = element->GetOwnerDocument()->ResolveProperty(PropertyId::FontSize, 0);
  528. }
  529. else
  530. {
  531. Rocket::Core::Element* parent = element->GetParentNode();
  532. if (parent == NULL)
  533. return 0;
  534. if (GetLocalProperty(PropertyId::FontSize) == NULL)
  535. return parent->ResolveProperty(PropertyId::FontSize, 0);
  536. // The base value for font size is always the height of *this* element's parent's font.
  537. base_value = parent->ResolveProperty(PropertyId::FontSize, 0);
  538. }
  539. switch (property->unit)
  540. {
  541. case Property::PERCENT:
  542. return base_value * property->value.Get< float >() * 0.01f;
  543. case Property::EM:
  544. return property->value.Get< float >() * base_value;
  545. case Property::REM:
  546. // If an rem-relative font size is specified, it is expressed relative to the document's
  547. // font height.
  548. return property->value.Get< float >() * ElementUtilities::GetFontSize(element->GetOwnerDocument());
  549. }
  550. }
  551. return ResolveProperty(property, base_value);
  552. }
  553. // Iterates over the properties defined on the element.
  554. bool ElementStyle::IterateProperties(int& index, PseudoClassList& property_pseudo_classes, PropertyId& property_id, const Property*& property)
  555. {
  556. // First check for locally defined properties.
  557. if (local_properties != NULL)
  558. {
  559. if (index < (int)local_properties->size())
  560. {
  561. PropertyMap::const_iterator i = local_properties->cbegin();
  562. for (int count = 0; count < index; ++count)
  563. ++i;
  564. property_id = (*i).first;
  565. property = &((*i).second);
  566. property_pseudo_classes.clear();
  567. ++index;
  568. return true;
  569. }
  570. }
  571. const ElementDefinition* definition = GetDefinition();
  572. if (definition != NULL)
  573. {
  574. int index_offset = 0;
  575. if (local_properties != NULL)
  576. index_offset = (int)local_properties->size();
  577. // Offset the index to be relative to the definition before we start indexing. When we do get a property back,
  578. // check that it hasn't been overridden by the element's local properties; if so, continue on to the next one.
  579. index -= index_offset;
  580. while (definition->IterateProperties(index, pseudo_classes, property_pseudo_classes, property_id, property))
  581. {
  582. if (local_properties == NULL ||
  583. local_properties->find(property_id) == local_properties->end())
  584. {
  585. index += index_offset;
  586. return true;
  587. }
  588. }
  589. return false;
  590. }
  591. return false;
  592. }
  593. // Returns the active style sheet for this element. This may be NULL.
  594. StyleSheet* ElementStyle::GetStyleSheet() const
  595. {
  596. ElementDocument* document = element->GetOwnerDocument();
  597. if (document != NULL)
  598. return document->GetStyleSheet();
  599. return NULL;
  600. }
  601. void ElementStyle::DirtyDefinition()
  602. {
  603. definition_dirty = true;
  604. DirtyChildDefinitions();
  605. }
  606. void ElementStyle::DirtyChildDefinitions()
  607. {
  608. for (int i = 0; i < element->GetNumChildren(true); i++)
  609. element->GetChild(i)->GetStyle()->DirtyDefinition();
  610. }
  611. // Dirties every property.
  612. void ElementStyle::DirtyProperties()
  613. {
  614. const PropertyIdList &properties = StyleSheetSpecification::GetRegisteredProperties();
  615. DirtyProperties(properties);
  616. }
  617. // Dirties em-relative properties.
  618. void ElementStyle::DirtyEmProperties()
  619. {
  620. if (!em_properties)
  621. {
  622. // Check if any of these are currently em-relative. If so, dirty them.
  623. em_properties = new PropertyIdList;
  624. for (auto& property : StyleSheetSpecification::GetRegisteredProperties())
  625. {
  626. // Skip font-size; this is relative to our parent's em, not ours.
  627. if (property == PropertyId::FontSize)
  628. continue;
  629. // Get this property from this element. If this is em-relative, then add it to the list to
  630. // dirty.
  631. if (element->GetProperty(property)->unit == Property::EM)
  632. em_properties->insert(property);
  633. }
  634. }
  635. if (!em_properties->empty())
  636. DirtyProperties(*em_properties, false);
  637. // Now dirty all of our descendant's font-size properties that are relative to ems.
  638. int num_children = element->GetNumChildren(true);
  639. for (int i = 0; i < num_children; ++i)
  640. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  641. }
  642. // Dirties font-size on child elements if appropriate.
  643. void ElementStyle::DirtyInheritedEmProperties()
  644. {
  645. const Property* font_size = element->GetLocalProperty(PropertyId::FontSize);
  646. if (font_size == NULL)
  647. {
  648. int num_children = element->GetNumChildren(true);
  649. for (int i = 0; i < num_children; ++i)
  650. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  651. }
  652. else
  653. {
  654. if (font_size->unit & Property::RELATIVE_UNIT)
  655. DirtyProperty(PropertyId::FontSize);
  656. }
  657. }
  658. // Dirties rem properties.
  659. void ElementStyle::DirtyRemProperties()
  660. {
  661. const PropertyIdList& properties = StyleSheetSpecification::GetRegisteredProperties();
  662. PropertyIdList rem_properties;
  663. // Dirty all the properties of this element that use the rem unit.
  664. for (auto& p : properties)
  665. {
  666. if (element->GetProperty(p)->unit == Property::REM)
  667. rem_properties.insert(p);
  668. }
  669. if (!rem_properties.empty())
  670. DirtyProperties(rem_properties, false);
  671. // Now dirty all of our descendant's properties that use the rem unit.
  672. int num_children = element->GetNumChildren(true);
  673. for (int i = 0; i < num_children; ++i)
  674. element->GetChild(i)->GetStyle()->DirtyRemProperties();
  675. }
  676. void ElementStyle::DirtyDpProperties()
  677. {
  678. const PropertyIdList& properties = StyleSheetSpecification::GetRegisteredProperties();
  679. PropertyIdList dp_properties;
  680. // Dirty all the properties of this element that use the dp unit.
  681. for (auto& p : properties)
  682. {
  683. if (element->GetProperty(p)->unit == Property::DP)
  684. dp_properties.insert(p);
  685. }
  686. if (!dp_properties.empty())
  687. DirtyProperties(dp_properties, false);
  688. // Now dirty all of our descendant's properties that use the dp unit.
  689. int num_children = element->GetNumChildren(true);
  690. for (int i = 0; i < num_children; ++i)
  691. element->GetChild(i)->GetStyle()->DirtyDpProperties();
  692. }
  693. // Sets a single property as dirty.
  694. void ElementStyle::DirtyProperty(PropertyId property_id)
  695. {
  696. DirtyProperties({ property_id });
  697. }
  698. // Sets a list of properties as dirty.
  699. void ElementStyle::DirtyProperties(const PropertyIdList& properties, bool clear_em_properties)
  700. {
  701. if (properties.empty())
  702. return;
  703. bool all_inherited_dirty =
  704. &properties == &StyleSheetSpecification::GetRegisteredProperties() ||
  705. StyleSheetSpecification::GetRegisteredProperties() == properties ||
  706. StyleSheetSpecification::GetRegisteredInheritedProperties() == properties;
  707. if (all_inherited_dirty)
  708. {
  709. const PropertyIdList &all_inherited_properties = StyleSheetSpecification::GetRegisteredInheritedProperties();
  710. for (int i = 0; i < element->GetNumChildren(true); i++)
  711. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(all_inherited_properties);
  712. }
  713. else
  714. {
  715. PropertyIdList inherited_properties;
  716. for (auto& p : properties)
  717. {
  718. // If this property is an inherited property, then push it into the list to be passed onto our children.
  719. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(p);
  720. if (property != NULL &&
  721. property->IsInherited())
  722. inherited_properties.insert(p);
  723. }
  724. // Pass the list of those properties that are inherited onto our children.
  725. if (!inherited_properties.empty())
  726. {
  727. for (int i = 0; i < element->GetNumChildren(true); i++)
  728. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  729. }
  730. }
  731. // Clear all cached properties.
  732. cache->Clear();
  733. cache->ClearInherited();
  734. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  735. if (clear_em_properties && em_properties != NULL)
  736. {
  737. delete em_properties;
  738. em_properties = NULL;
  739. }
  740. // And send the event.
  741. element->DirtyProperties(properties);
  742. }
  743. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  744. void ElementStyle::DirtyInheritedProperties(const PropertyIdList& properties)
  745. {
  746. bool clear_em_properties = em_properties != NULL;
  747. PropertyIdList inherited_properties;
  748. for (auto& p : properties)
  749. {
  750. const Property *property = GetLocalProperty(p);
  751. if (property == NULL)
  752. {
  753. inherited_properties.insert(p);
  754. if (!clear_em_properties && em_properties != NULL && em_properties->find(p) != em_properties->end()) {
  755. clear_em_properties = true;
  756. }
  757. }
  758. }
  759. if (inherited_properties.empty())
  760. return;
  761. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  762. if (clear_em_properties && em_properties != NULL)
  763. {
  764. delete em_properties;
  765. em_properties = NULL;
  766. }
  767. // Clear cached inherited properties.
  768. cache->ClearInherited();
  769. // Pass the list of those properties that this element doesn't override onto our children.
  770. for (int i = 0; i < element->GetNumChildren(true); i++)
  771. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  772. element->DirtyProperties(properties);
  773. //element->OnPropertyChange(properties);
  774. }
  775. void ElementStyle::GetOffsetProperties(const Property **top, const Property **bottom, const Property **left, const Property **right )
  776. {
  777. cache->GetOffsetProperties(top, bottom, left, right);
  778. }
  779. void ElementStyle::GetBorderWidthProperties(const Property **border_top_width, const Property **border_bottom_width, const Property **border_left_width, const Property **bottom_right_width)
  780. {
  781. cache->GetBorderWidthProperties(border_top_width, border_bottom_width, border_left_width, bottom_right_width);
  782. }
  783. void ElementStyle::GetMarginProperties(const Property **margin_top, const Property **margin_bottom, const Property **margin_left, const Property **margin_right)
  784. {
  785. cache->GetMarginProperties(margin_top, margin_bottom, margin_left, margin_right);
  786. }
  787. void ElementStyle::GetPaddingProperties(const Property **padding_top, const Property **padding_bottom, const Property **padding_left, const Property **padding_right)
  788. {
  789. cache->GetPaddingProperties(padding_top, padding_bottom, padding_left, padding_right);
  790. }
  791. void ElementStyle::GetDimensionProperties(const Property **width, const Property **height)
  792. {
  793. cache->GetDimensionProperties(width, height);
  794. }
  795. void ElementStyle::GetLocalDimensionProperties(const Property **width, const Property **height)
  796. {
  797. cache->GetLocalDimensionProperties(width, height);
  798. }
  799. void ElementStyle::GetOverflow(int *overflow_x, int *overflow_y)
  800. {
  801. cache->GetOverflow(overflow_x, overflow_y);
  802. }
  803. int ElementStyle::GetPosition()
  804. {
  805. return cache->GetPosition();
  806. }
  807. int ElementStyle::GetFloat()
  808. {
  809. return cache->GetFloat();
  810. }
  811. int ElementStyle::GetDisplay()
  812. {
  813. return cache->GetDisplay();
  814. }
  815. int ElementStyle::GetWhitespace()
  816. {
  817. return cache->GetWhitespace();
  818. }
  819. int ElementStyle::GetPointerEvents()
  820. {
  821. return cache->GetPointerEvents();
  822. }
  823. const Property *ElementStyle::GetLineHeightProperty()
  824. {
  825. return cache->GetLineHeightProperty();
  826. }
  827. int ElementStyle::GetTextAlign()
  828. {
  829. return cache->GetTextAlign();
  830. }
  831. int ElementStyle::GetTextTransform()
  832. {
  833. return cache->GetTextTransform();
  834. }
  835. const Property *ElementStyle::GetVerticalAlignProperty()
  836. {
  837. return cache->GetVerticalAlignProperty();
  838. }
  839. // Returns 'perspective' property value from element's style or local cache.
  840. const Property *ElementStyle::GetPerspective()
  841. {
  842. return element->GetProperty(PropertyId::Perspective);
  843. }
  844. // Returns 'perspective-origin-x' property value from element's style or local cache.
  845. const Property *ElementStyle::GetPerspectiveOriginX()
  846. {
  847. return element->GetProperty(PropertyId::PerspectiveOriginX);
  848. }
  849. // Returns 'perspective-origin-y' property value from element's style or local cache.
  850. const Property *ElementStyle::GetPerspectiveOriginY()
  851. {
  852. return element->GetProperty(PropertyId::PerspectiveOriginY);
  853. }
  854. // Returns 'transform' property value from element's style or local cache.
  855. const Property *ElementStyle::GetTransform()
  856. {
  857. return element->GetProperty(PropertyId::Transform);
  858. }
  859. // Returns 'transform-origin-x' property value from element's style or local cache.
  860. const Property *ElementStyle::GetTransformOriginX()
  861. {
  862. return element->GetProperty(PropertyId::TransformOriginX);
  863. }
  864. // Returns 'transform-origin-y' property value from element's style or local cache.
  865. const Property *ElementStyle::GetTransformOriginY()
  866. {
  867. return element->GetProperty(PropertyId::TransformOriginY);
  868. }
  869. // Returns 'transform-origin-z' property value from element's style or local cache.
  870. const Property *ElementStyle::GetTransformOriginZ()
  871. {
  872. return element->GetProperty(PropertyId::TransformOriginZ);
  873. }
  874. }
  875. }