ElementStyle.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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(const String& name, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  73. {
  74. // Check for overriding local properties.
  75. if (local_properties != NULL)
  76. {
  77. const Property* property = local_properties->GetProperty(name);
  78. if (property != NULL)
  79. return property;
  80. }
  81. // Check for a property defined in an RCSS rule.
  82. if (definition != NULL)
  83. return definition->GetProperty(name, pseudo_classes);
  84. return NULL;
  85. }
  86. // Returns one of this element's properties.
  87. const Property* ElementStyle::GetProperty(const String& name, Element* element, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  88. {
  89. const Property* local_property = GetLocalProperty(name, local_properties, definition, pseudo_classes);
  90. if (local_property != NULL)
  91. return local_property;
  92. // Fetch the property specification.
  93. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(name);
  94. if (property == NULL)
  95. return NULL;
  96. // If we can inherit this property, return our parent's property.
  97. if (property->IsInherited())
  98. {
  99. Element* parent = element->GetParentNode();
  100. while (parent != NULL)
  101. {
  102. const Property* parent_property = parent->GetStyle()->GetLocalProperty(name);
  103. if (parent_property)
  104. return parent_property;
  105. parent = parent->GetParentNode();
  106. }
  107. }
  108. // No property available! Return the default value.
  109. return property->GetDefaultValue();
  110. }
  111. // Apply transition to relevant properties if a transition is defined on element.
  112. // Properties that are part of a transition are removed from the properties list.
  113. void ElementStyle::TransitionPropertyChanges(Element* element, PropertyNameList& properties, PropertyDictionary* local_properties, ElementDefinition* old_definition, ElementDefinition* new_definition,
  114. const PseudoClassList& pseudo_classes_before, const PseudoClassList& pseudo_classes_after)
  115. {
  116. ROCKET_ASSERT(element);
  117. if (!old_definition || !new_definition || properties.empty())
  118. return;
  119. if (const Property* transition_property = GetLocalProperty(TRANSITION, local_properties, new_definition, pseudo_classes_after))
  120. {
  121. auto transition_list = transition_property->Get<TransitionList>();
  122. if (!transition_list.none)
  123. {
  124. auto add_transition = [&](const Transition& transition) {
  125. bool transition_added = false;
  126. const Property* start_value = GetProperty(transition.name, element, local_properties, old_definition, pseudo_classes_before);
  127. const Property* target_value = GetProperty(transition.name, element, nullptr, new_definition, pseudo_classes_after);
  128. if (start_value && target_value && (*start_value != *target_value))
  129. transition_added = element->StartTransition(transition, *start_value, *target_value);
  130. return transition_added;
  131. };
  132. if (transition_list.all)
  133. {
  134. Transition transition = transition_list.transitions[0];
  135. for (auto it = properties.begin(); it != properties.end(); )
  136. {
  137. transition.name = *it;
  138. if (add_transition(transition))
  139. it = properties.erase(it);
  140. else
  141. ++it;
  142. }
  143. }
  144. else
  145. {
  146. for (auto& transition : transition_list.transitions)
  147. {
  148. if (auto it = properties.find(transition.name); it != properties.end())
  149. {
  150. if (add_transition(transition))
  151. properties.erase(it);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. void ElementStyle::UpdateDefinition()
  159. {
  160. if (definition_dirty)
  161. {
  162. definition_dirty = false;
  163. ElementDefinition* new_definition = NULL;
  164. const StyleSheet* style_sheet = GetStyleSheet();
  165. if (style_sheet != NULL)
  166. {
  167. new_definition = style_sheet->GetElementDefinition(element);
  168. }
  169. // Switch the property definitions if the definition has changed.
  170. if (new_definition != definition || new_definition == NULL)
  171. {
  172. PropertyNameList properties;
  173. if (definition != NULL)
  174. definition->GetDefinedProperties(properties, pseudo_classes);
  175. if (new_definition != NULL)
  176. new_definition->GetDefinedProperties(properties, pseudo_classes);
  177. TransitionPropertyChanges(element, properties, local_properties, definition, new_definition, pseudo_classes, pseudo_classes);
  178. if (definition != NULL)
  179. definition->RemoveReference();
  180. definition = new_definition;
  181. DirtyProperties(properties);
  182. element->GetElementDecoration()->DirtyDecorators(true);
  183. }
  184. else if (new_definition != NULL)
  185. {
  186. new_definition->RemoveReference();
  187. }
  188. }
  189. }
  190. // Sets or removes a pseudo-class on the element.
  191. void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
  192. {
  193. size_t num_pseudo_classes = pseudo_classes.size();
  194. auto pseudo_classes_before = pseudo_classes;
  195. if (activate)
  196. pseudo_classes.push_back(pseudo_class);
  197. else
  198. {
  199. // In case of duplicates, we do a loop here. We could do a sort and unique instead,
  200. // but that might even be slower for a small list with few duplicates, which
  201. // is probably the most common case.
  202. auto it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
  203. while(it != pseudo_classes.end())
  204. {
  205. pseudo_classes.erase(it);
  206. it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
  207. }
  208. }
  209. if (pseudo_classes.size() != num_pseudo_classes)
  210. {
  211. element->GetElementDecoration()->DirtyDecorators(false);
  212. if (definition != NULL)
  213. {
  214. PropertyNameList properties;
  215. definition->GetDefinedProperties(properties, pseudo_classes, pseudo_class);
  216. TransitionPropertyChanges(element, properties, local_properties, definition, definition, pseudo_classes_before, pseudo_classes);
  217. DirtyProperties(properties);
  218. switch (definition->GetPseudoClassVolatility(pseudo_class))
  219. {
  220. case ElementDefinition::FONT_VOLATILE:
  221. element->DirtyFont();
  222. break;
  223. case ElementDefinition::STRUCTURE_VOLATILE:
  224. DirtyChildDefinitions();
  225. break;
  226. default:
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. // Checks if a specific pseudo-class has been set on the element.
  233. bool ElementStyle::IsPseudoClassSet(const String& pseudo_class) const
  234. {
  235. return (std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class) != pseudo_classes.end());
  236. }
  237. const PseudoClassList& ElementStyle::GetActivePseudoClasses() const
  238. {
  239. return pseudo_classes;
  240. }
  241. // Sets or removes a class on the element.
  242. void ElementStyle::SetClass(const String& class_name, bool activate)
  243. {
  244. StringList::iterator class_location = std::find(classes.begin(), classes.end(), class_name);
  245. if (activate)
  246. {
  247. if (class_location == classes.end())
  248. {
  249. classes.push_back(class_name);
  250. DirtyDefinition();
  251. }
  252. }
  253. else
  254. {
  255. if (class_location != classes.end())
  256. {
  257. classes.erase(class_location);
  258. DirtyDefinition();
  259. }
  260. }
  261. }
  262. // Checks if a class is set on the element.
  263. bool ElementStyle::IsClassSet(const String& class_name) const
  264. {
  265. return std::find(classes.begin(), classes.end(), class_name) != classes.end();
  266. }
  267. // Specifies the entire list of classes for this element. This will replace any others specified.
  268. void ElementStyle::SetClassNames(const String& class_names)
  269. {
  270. classes.clear();
  271. StringUtilities::ExpandString(classes, class_names, ' ');
  272. DirtyDefinition();
  273. }
  274. // Returns the list of classes specified for this element.
  275. String ElementStyle::GetClassNames() const
  276. {
  277. String class_names;
  278. for (size_t i = 0; i < classes.size(); i++)
  279. {
  280. if (i != 0)
  281. {
  282. class_names += " ";
  283. }
  284. class_names += classes[i];
  285. }
  286. return class_names;
  287. }
  288. // Sets a local property override on the element.
  289. bool ElementStyle::SetProperty(const String& name, const String& value)
  290. {
  291. if (local_properties == NULL)
  292. local_properties = new PropertyDictionary();
  293. if (StyleSheetSpecification::ParsePropertyDeclaration(*local_properties, name, value))
  294. {
  295. DirtyProperty(name);
  296. return true;
  297. }
  298. else
  299. {
  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(const String& name, const Property& property)
  306. {
  307. Property new_property = property;
  308. new_property.definition = StyleSheetSpecification::GetProperty(name);
  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->SetProperty(name, new_property);
  315. DirtyProperty(name);
  316. return true;
  317. }
  318. // Removes a local property override on the element.
  319. void ElementStyle::RemoveProperty(const String& name)
  320. {
  321. if (local_properties == NULL)
  322. return;
  323. if (local_properties->GetProperty(name) != NULL)
  324. {
  325. local_properties->RemoveProperty(name);
  326. DirtyProperty(name);
  327. }
  328. }
  329. // Returns one of this element's properties.
  330. const Property* ElementStyle::GetProperty(const String& name)
  331. {
  332. return GetProperty(name, element, local_properties, definition, pseudo_classes);
  333. }
  334. // Returns one of this element's properties.
  335. const Property* ElementStyle::GetLocalProperty(const String& name)
  336. {
  337. return GetLocalProperty(name, local_properties, definition, pseudo_classes);
  338. }
  339. const PropertyMap * ElementStyle::GetLocalProperties() const
  340. {
  341. if (local_properties)
  342. return &local_properties->GetProperties();
  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(const String& property_name, const Property * property)
  406. {
  407. if ((property->unit & Property::LENGTH) && !(property->unit == Property::EM && property_name == FONT_SIZE))
  408. {
  409. return ResolveLength(property);
  410. }
  411. auto definition = property->definition;
  412. if (!definition) definition = StyleSheetSpecification::GetProperty(property_name);
  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(const String& name, float base_value)
  509. {
  510. const Property* property = GetProperty(name);
  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 (name == FONT_SIZE && 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(FONT_SIZE, 0);
  528. }
  529. else
  530. {
  531. Rocket::Core::Element* parent = element->GetParentNode();
  532. if (parent == NULL)
  533. return 0;
  534. if (GetLocalProperty(FONT_SIZE) == NULL)
  535. return parent->ResolveProperty(FONT_SIZE, 0);
  536. // The base value for font size is always the height of *this* element's parent's font.
  537. base_value = parent->ResolveProperty(FONT_SIZE, 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, String& name, const Property*& property)
  555. {
  556. // First check for locally defined properties.
  557. if (local_properties != NULL)
  558. {
  559. if (index < local_properties->GetNumProperties())
  560. {
  561. PropertyMap::const_iterator i = local_properties->GetProperties().begin();
  562. for (int count = 0; count < index; ++count)
  563. ++i;
  564. name = (*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 = local_properties->GetNumProperties();
  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, name, property))
  581. {
  582. if (local_properties == NULL ||
  583. local_properties->GetProperty(name) == NULL)
  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 PropertyNameList &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 PropertyNameList;
  624. for (auto& property : StyleSheetSpecification::GetRegisteredProperties())
  625. {
  626. // Skip font-size; this is relative to our parent's em, not ours.
  627. if (property == FONT_SIZE)
  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(FONT_SIZE);
  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(FONT_SIZE);
  656. }
  657. }
  658. // Dirties rem properties.
  659. void ElementStyle::DirtyRemProperties()
  660. {
  661. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  662. PropertyNameList rem_properties;
  663. // Dirty all the properties of this element that use the rem unit.
  664. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  665. {
  666. if (element->GetProperty(*list_iterator)->unit == Property::REM)
  667. rem_properties.insert(*list_iterator);
  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 PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  679. PropertyNameList dp_properties;
  680. // Dirty all the properties of this element that use the dp unit.
  681. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  682. {
  683. if (element->GetProperty(*list_iterator)->unit == Property::DP)
  684. dp_properties.insert(*list_iterator);
  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(const String& property)
  695. {
  696. PropertyNameList properties;
  697. properties.insert(String(property));
  698. DirtyProperties(properties);
  699. }
  700. // Sets a list of properties as dirty.
  701. void ElementStyle::DirtyProperties(const PropertyNameList& properties, bool clear_em_properties)
  702. {
  703. if (properties.empty())
  704. return;
  705. bool all_inherited_dirty =
  706. &properties == &StyleSheetSpecification::GetRegisteredProperties() ||
  707. StyleSheetSpecification::GetRegisteredProperties() == properties ||
  708. StyleSheetSpecification::GetRegisteredInheritedProperties() == properties;
  709. if (all_inherited_dirty)
  710. {
  711. const PropertyNameList &all_inherited_properties = StyleSheetSpecification::GetRegisteredInheritedProperties();
  712. for (int i = 0; i < element->GetNumChildren(true); i++)
  713. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(all_inherited_properties);
  714. }
  715. else
  716. {
  717. PropertyNameList inherited_properties;
  718. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  719. {
  720. // If this property is an inherited property, then push it into the list to be passed onto our children.
  721. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(*i);
  722. if (property != NULL &&
  723. property->IsInherited())
  724. inherited_properties.insert(*i);
  725. }
  726. // Pass the list of those properties that are inherited onto our children.
  727. if (!inherited_properties.empty())
  728. {
  729. for (int i = 0; i < element->GetNumChildren(true); i++)
  730. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  731. }
  732. }
  733. // Clear all cached properties.
  734. cache->Clear();
  735. cache->ClearInherited();
  736. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  737. if (clear_em_properties && em_properties != NULL)
  738. {
  739. delete em_properties;
  740. em_properties = NULL;
  741. }
  742. // And send the event.
  743. element->DirtyProperties(properties);
  744. }
  745. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  746. void ElementStyle::DirtyInheritedProperties(const PropertyNameList& properties)
  747. {
  748. bool clear_em_properties = em_properties != NULL;
  749. PropertyNameList inherited_properties;
  750. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  751. {
  752. const Property *property = GetLocalProperty((*i));
  753. if (property == NULL)
  754. {
  755. inherited_properties.insert(*i);
  756. if (!clear_em_properties && em_properties != NULL && em_properties->find((*i)) != em_properties->end()) {
  757. clear_em_properties = true;
  758. }
  759. }
  760. }
  761. if (inherited_properties.empty())
  762. return;
  763. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  764. if (clear_em_properties && em_properties != NULL)
  765. {
  766. delete em_properties;
  767. em_properties = NULL;
  768. }
  769. // Clear cached inherited properties.
  770. cache->ClearInherited();
  771. // Pass the list of those properties that this element doesn't override onto our children.
  772. for (int i = 0; i < element->GetNumChildren(true); i++)
  773. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  774. element->DirtyProperties(properties);
  775. }
  776. void ElementStyle::GetOffsetProperties(const Property **top, const Property **bottom, const Property **left, const Property **right )
  777. {
  778. cache->GetOffsetProperties(top, bottom, left, right);
  779. }
  780. void ElementStyle::GetBorderWidthProperties(const Property **border_top_width, const Property **border_bottom_width, const Property **border_left_width, const Property **bottom_right_width)
  781. {
  782. cache->GetBorderWidthProperties(border_top_width, border_bottom_width, border_left_width, bottom_right_width);
  783. }
  784. void ElementStyle::GetMarginProperties(const Property **margin_top, const Property **margin_bottom, const Property **margin_left, const Property **margin_right)
  785. {
  786. cache->GetMarginProperties(margin_top, margin_bottom, margin_left, margin_right);
  787. }
  788. void ElementStyle::GetPaddingProperties(const Property **padding_top, const Property **padding_bottom, const Property **padding_left, const Property **padding_right)
  789. {
  790. cache->GetPaddingProperties(padding_top, padding_bottom, padding_left, padding_right);
  791. }
  792. void ElementStyle::GetDimensionProperties(const Property **width, const Property **height)
  793. {
  794. cache->GetDimensionProperties(width, height);
  795. }
  796. void ElementStyle::GetLocalDimensionProperties(const Property **width, const Property **height)
  797. {
  798. cache->GetLocalDimensionProperties(width, height);
  799. }
  800. void ElementStyle::GetOverflow(int *overflow_x, int *overflow_y)
  801. {
  802. cache->GetOverflow(overflow_x, overflow_y);
  803. }
  804. int ElementStyle::GetPosition()
  805. {
  806. return cache->GetPosition();
  807. }
  808. int ElementStyle::GetFloat()
  809. {
  810. return cache->GetFloat();
  811. }
  812. int ElementStyle::GetDisplay()
  813. {
  814. return cache->GetDisplay();
  815. }
  816. int ElementStyle::GetWhitespace()
  817. {
  818. return cache->GetWhitespace();
  819. }
  820. int ElementStyle::GetPointerEvents()
  821. {
  822. return cache->GetPointerEvents();
  823. }
  824. const Property *ElementStyle::GetLineHeightProperty()
  825. {
  826. return cache->GetLineHeightProperty();
  827. }
  828. int ElementStyle::GetTextAlign()
  829. {
  830. return cache->GetTextAlign();
  831. }
  832. int ElementStyle::GetTextTransform()
  833. {
  834. return cache->GetTextTransform();
  835. }
  836. const Property *ElementStyle::GetVerticalAlignProperty()
  837. {
  838. return cache->GetVerticalAlignProperty();
  839. }
  840. // Returns 'perspective' property value from element's style or local cache.
  841. const Property *ElementStyle::GetPerspective()
  842. {
  843. return element->GetProperty(PERSPECTIVE);
  844. }
  845. // Returns 'perspective-origin-x' property value from element's style or local cache.
  846. const Property *ElementStyle::GetPerspectiveOriginX()
  847. {
  848. return element->GetProperty(PERSPECTIVE_ORIGIN_X);
  849. }
  850. // Returns 'perspective-origin-y' property value from element's style or local cache.
  851. const Property *ElementStyle::GetPerspectiveOriginY()
  852. {
  853. return element->GetProperty(PERSPECTIVE_ORIGIN_Y);
  854. }
  855. // Returns 'transform' property value from element's style or local cache.
  856. const Property *ElementStyle::GetTransform()
  857. {
  858. return element->GetProperty(TRANSFORM);
  859. }
  860. // Returns 'transform-origin-x' property value from element's style or local cache.
  861. const Property *ElementStyle::GetTransformOriginX()
  862. {
  863. return element->GetProperty(TRANSFORM_ORIGIN_X);
  864. }
  865. // Returns 'transform-origin-y' property value from element's style or local cache.
  866. const Property *ElementStyle::GetTransformOriginY()
  867. {
  868. return element->GetProperty(TRANSFORM_ORIGIN_Y);
  869. }
  870. // Returns 'transform-origin-z' property value from element's style or local cache.
  871. const Property *ElementStyle::GetTransformOriginZ()
  872. {
  873. return element->GetProperty(TRANSFORM_ORIGIN_Z);
  874. }
  875. }
  876. }