ElementStyle.cpp 30 KB

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