ElementStyle.cpp 30 KB

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