ElementStyle.cpp 30 KB

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