ElementStyle.cpp 29 KB

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