ElementStyle.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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/Property.h"
  35. #include "../../Include/Rocket/Core/PropertyDefinition.h"
  36. #include "../../Include/Rocket/Core/PropertyDictionary.h"
  37. #include "../../Include/Rocket/Core/StyleSheetSpecification.h"
  38. #include "ElementBackground.h"
  39. #include "ElementBorder.h"
  40. #include "ElementDecoration.h"
  41. #include "ElementDefinition.h"
  42. #include "FontFaceHandle.h"
  43. namespace Rocket {
  44. namespace Core {
  45. ElementStyle::ElementStyle(Element* _element)
  46. {
  47. local_properties = NULL;
  48. em_properties = NULL;
  49. definition = NULL;
  50. element = _element;
  51. cache = new ElementStyleCache(this);
  52. definition_dirty = true;
  53. child_definition_dirty = true;
  54. }
  55. ElementStyle::~ElementStyle()
  56. {
  57. if (local_properties != NULL)
  58. delete local_properties;
  59. if (em_properties != NULL)
  60. delete em_properties;
  61. if (definition != NULL)
  62. definition->RemoveReference();
  63. delete cache;
  64. }
  65. static PropCounter prop_counter;
  66. PropCounter &ElementStyle::GetPropCounter()
  67. {
  68. return prop_counter;
  69. }
  70. // Returns the element's definition, updating if necessary.
  71. const ElementDefinition* ElementStyle::GetDefinition()
  72. {
  73. if (definition_dirty)
  74. {
  75. UpdateDefinition();
  76. }
  77. return definition;
  78. }
  79. void ElementStyle::UpdateDefinition()
  80. {
  81. if (definition_dirty)
  82. {
  83. definition_dirty = false;
  84. ElementDefinition* new_definition = NULL;
  85. const StyleSheet* style_sheet = GetStyleSheet();
  86. if (style_sheet != NULL)
  87. {
  88. new_definition = style_sheet->GetElementDefinition(element);
  89. }
  90. // Switch the property definitions if the definition has changed.
  91. if (new_definition != definition || new_definition == NULL)
  92. {
  93. PropertyNameList properties;
  94. if (definition != NULL)
  95. {
  96. definition->GetDefinedProperties(properties, pseudo_classes);
  97. definition->RemoveReference();
  98. }
  99. definition = new_definition;
  100. if (definition != NULL)
  101. definition->GetDefinedProperties(properties, pseudo_classes);
  102. DirtyProperties(properties);
  103. element->GetElementDecoration()->ReloadDecorators();
  104. }
  105. else if (new_definition != NULL)
  106. {
  107. new_definition->RemoveReference();
  108. }
  109. }
  110. if (child_definition_dirty)
  111. {
  112. for (int i = 0; i < element->GetNumChildren(true); i++)
  113. {
  114. element->GetChild(i)->GetStyle()->UpdateDefinition();
  115. }
  116. child_definition_dirty = false;
  117. }
  118. }
  119. // Sets or removes a pseudo-class on the element.
  120. void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
  121. {
  122. size_t num_pseudo_classes = pseudo_classes.size();
  123. if (activate)
  124. pseudo_classes.insert(pseudo_class);
  125. else
  126. pseudo_classes.erase(pseudo_class);
  127. if (pseudo_classes.size() != num_pseudo_classes)
  128. {
  129. element->GetElementDecoration()->DirtyDecorators();
  130. const ElementDefinition* definition = element->GetDefinition();
  131. if (definition != NULL)
  132. {
  133. PropertyNameList properties;
  134. definition->GetDefinedProperties(properties, pseudo_classes, pseudo_class);
  135. DirtyProperties(properties);
  136. switch (definition->GetPseudoClassVolatility(pseudo_class))
  137. {
  138. case ElementDefinition::FONT_VOLATILE:
  139. element->DirtyFont();
  140. break;
  141. case ElementDefinition::STRUCTURE_VOLATILE:
  142. DirtyChildDefinitions();
  143. break;
  144. default:
  145. break;
  146. }
  147. }
  148. }
  149. }
  150. // Checks if a specific pseudo-class has been set on the element.
  151. bool ElementStyle::IsPseudoClassSet(const String& pseudo_class) const
  152. {
  153. return (pseudo_classes.find(pseudo_class) != pseudo_classes.end());
  154. }
  155. const PseudoClassList& ElementStyle::GetActivePseudoClasses() const
  156. {
  157. return pseudo_classes;
  158. }
  159. // Sets or removes a class on the element.
  160. void ElementStyle::SetClass(const String& class_name, bool activate)
  161. {
  162. StringList::iterator class_location = std::find(classes.begin(), classes.end(), class_name);
  163. if (activate)
  164. {
  165. if (class_location == classes.end())
  166. {
  167. classes.push_back(class_name);
  168. DirtyDefinition();
  169. }
  170. }
  171. else
  172. {
  173. if (class_location != classes.end())
  174. {
  175. classes.erase(class_location);
  176. DirtyDefinition();
  177. }
  178. }
  179. }
  180. // Checks if a class is set on the element.
  181. bool ElementStyle::IsClassSet(const String& class_name) const
  182. {
  183. return std::find(classes.begin(), classes.end(), class_name) != classes.end();
  184. }
  185. // Specifies the entire list of classes for this element. This will replace any others specified.
  186. void ElementStyle::SetClassNames(const String& class_names)
  187. {
  188. classes.clear();
  189. StringUtilities::ExpandString(classes, class_names, ' ');
  190. DirtyDefinition();
  191. }
  192. // Returns the list of classes specified for this element.
  193. String ElementStyle::GetClassNames() const
  194. {
  195. String class_names;
  196. for (size_t i = 0; i < classes.size(); i++)
  197. {
  198. if (i != 0)
  199. {
  200. class_names.Append(" ");
  201. }
  202. class_names.Append(classes[i]);
  203. }
  204. return class_names;
  205. }
  206. // Sets a local property override on the element.
  207. bool ElementStyle::SetProperty(const String& name, const String& value)
  208. {
  209. if (local_properties == NULL)
  210. local_properties = new PropertyDictionary();
  211. if (StyleSheetSpecification::ParsePropertyDeclaration(*local_properties, name, value))
  212. {
  213. DirtyProperty(name);
  214. return true;
  215. }
  216. else
  217. {
  218. Log::Message(Log::LT_WARNING, "Syntax error parsing inline property declaration '%s: %s;'.", name.CString(), value.CString());
  219. return false;
  220. }
  221. }
  222. // Sets a local property override on the element to a pre-parsed value.
  223. bool ElementStyle::SetProperty(const String& name, const Property& property)
  224. {
  225. Property new_property = property;
  226. new_property.definition = StyleSheetSpecification::GetProperty(name);
  227. if (new_property.definition == NULL)
  228. return false;
  229. if (local_properties == NULL)
  230. local_properties = new PropertyDictionary();
  231. local_properties->SetProperty(name, new_property);
  232. DirtyProperty(name);
  233. return true;
  234. }
  235. // Removes a local property override on the element.
  236. void ElementStyle::RemoveProperty(const String& name)
  237. {
  238. if (local_properties == NULL)
  239. return;
  240. if (local_properties->GetProperty(name) != NULL)
  241. {
  242. local_properties->RemoveProperty(name);
  243. DirtyProperty(name);
  244. }
  245. }
  246. // Returns one of this element's properties.
  247. const Property* ElementStyle::GetProperty(const String& name)
  248. {
  249. if (prop_counter.find(name) == prop_counter.end())
  250. prop_counter[name] = 0;
  251. prop_counter[name] = prop_counter[name] + 1;
  252. const Property* local_property = GetLocalProperty(name);
  253. if (local_property != NULL)
  254. return local_property;
  255. // Fetch the property specification.
  256. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(name);
  257. if (property == NULL)
  258. return NULL;
  259. // If we can inherit this property, return our parent's property.
  260. if (property->IsInherited())
  261. {
  262. Element* parent = element->GetParentNode();
  263. while (parent != NULL)
  264. {
  265. const Property* parent_property = parent->style->GetLocalProperty(name);
  266. if (parent_property)
  267. return parent_property;
  268. parent = parent->GetParentNode();
  269. }
  270. }
  271. // No property available! Return the default value.
  272. return property->GetDefaultValue();
  273. }
  274. // Returns one of this element's properties.
  275. const Property* ElementStyle::GetLocalProperty(const String& name)
  276. {
  277. // Check for overriding local properties.
  278. if (local_properties != NULL)
  279. {
  280. const Property* property = local_properties->GetProperty(name);
  281. if (property != NULL)
  282. return property;
  283. }
  284. // Check for a property defined in an RCSS rule.
  285. if (definition != NULL)
  286. return definition->GetProperty(name, pseudo_classes);
  287. return NULL;
  288. }
  289. // Resolves one of this element's properties.
  290. float ElementStyle::ResolveProperty(const Property* property, float base_value)
  291. {
  292. if (!property)
  293. {
  294. ROCKET_ERROR;
  295. return 0.0f;
  296. }
  297. if (property->unit & Property::RELATIVE_UNIT)
  298. {
  299. if (property->unit & Property::PERCENT)
  300. return base_value * property->value.Get< float >() * 0.01f;
  301. else if (property->unit & Property::EM)
  302. return property->value.Get< float >() * ElementUtilities::GetFontSize(element);
  303. else if (property->unit & Property::REM)
  304. return property->value.Get< float >() * ElementUtilities::GetFontSize(element->GetOwnerDocument());
  305. else if (property->unit & Property::DP)
  306. return property->value.Get< float >() * ElementUtilities::GetDensityIndependentPixelRatio(element);
  307. }
  308. if (property->unit & Property::NUMBER || property->unit & Property::PX)
  309. {
  310. return property->value.Get< float >();
  311. }
  312. // Values based on pixels-per-inch.
  313. if (property->unit & Property::PPI_UNIT)
  314. {
  315. float inch = property->value.Get< float >() * element->GetRenderInterface()->GetPixelsPerInch();
  316. if (property->unit & Property::INCH) // inch
  317. return inch;
  318. if (property->unit & Property::CM) // centimeter
  319. return inch * (1.0f / 2.54f);
  320. if (property->unit & Property::MM) // millimeter
  321. return inch * (1.0f / 25.4f);
  322. if (property->unit & Property::PT) // point
  323. return inch * (1.0f / 72.0f);
  324. if (property->unit & Property::PC) // pica
  325. return inch * (1.0f / 6.0f);
  326. }
  327. // We're not a numeric property; return 0.
  328. return 0.0f;
  329. }
  330. // Resolves one of this element's properties.
  331. float ElementStyle::ResolveProperty(const String& name, float base_value)
  332. {
  333. const Property* property = GetProperty(name);
  334. if (!property)
  335. {
  336. ROCKET_ERROR;
  337. return 0.0f;
  338. }
  339. if (property->unit & Property::DP)
  340. {
  341. return property->value.Get< float >() * ElementUtilities::GetDensityIndependentPixelRatio(element);
  342. }
  343. if (property->unit & Property::RELATIVE_UNIT)
  344. {
  345. // The calculated value of the font-size property is inherited, so we need to check if this
  346. // is an inherited property. If so, then we return our parent's font size instead.
  347. if (name == FONT_SIZE)
  348. {
  349. // If the rem unit is used, the font-size is inherited directly from the document,
  350. // otherwise we use the parent's font size.
  351. if (property->unit & Property::REM)
  352. {
  353. Rocket::Core::ElementDocument* owner_document = element->GetOwnerDocument();
  354. if (owner_document == NULL)
  355. return 0;
  356. base_value = element->GetOwnerDocument()->ResolveProperty(FONT_SIZE, 0);
  357. }
  358. else
  359. {
  360. Rocket::Core::Element* parent = element->GetParentNode();
  361. if (parent == NULL)
  362. return 0;
  363. if (GetLocalProperty(FONT_SIZE) == NULL)
  364. return parent->ResolveProperty(FONT_SIZE, 0);
  365. // The base value for font size is always the height of *this* element's parent's font.
  366. base_value = parent->ResolveProperty(FONT_SIZE, 0);
  367. }
  368. }
  369. if (property->unit & Property::PERCENT)
  370. return base_value * property->value.Get< float >() * 0.01f;
  371. else if (property->unit & Property::EM)
  372. {
  373. // If an em-relative font size is specified, it is expressed relative to the parent's
  374. // font height.
  375. if (name == FONT_SIZE)
  376. return property->value.Get< float >() * base_value;
  377. else
  378. return property->value.Get< float >() * ElementUtilities::GetFontSize(element);
  379. }
  380. else if (property->unit & Property::REM)
  381. {
  382. // If an rem-relative font size is specified, it is expressed relative to the document's
  383. // font height.
  384. if (name == FONT_SIZE)
  385. return property->value.Get< float >() * base_value;
  386. else
  387. return property->value.Get< float >() * ElementUtilities::GetFontSize(element->GetOwnerDocument());
  388. }
  389. }
  390. if (property->unit & Property::NUMBER || property->unit & Property::PX)
  391. {
  392. return property->value.Get< float >();
  393. }
  394. // Values based on pixels-per-inch.
  395. if (property->unit & Property::PPI_UNIT)
  396. {
  397. float inch = property->value.Get< float >() * element->GetRenderInterface()->GetPixelsPerInch();
  398. if (property->unit & Property::INCH) // inch
  399. return inch;
  400. if (property->unit & Property::CM) // centimeter
  401. return inch / 2.54f;
  402. if (property->unit & Property::MM) // millimeter
  403. return inch / 25.4f;
  404. if (property->unit & Property::PT) // point
  405. return inch / 72.0f;
  406. if (property->unit & Property::PC) // pica
  407. return inch / 6.0f;
  408. }
  409. // We're not a numeric property; return 0.
  410. return 0.0f;
  411. }
  412. // Iterates over the properties defined on the element.
  413. bool ElementStyle::IterateProperties(int& index, PseudoClassList& property_pseudo_classes, String& name, const Property*& property)
  414. {
  415. // First check for locally defined properties.
  416. if (local_properties != NULL)
  417. {
  418. if (index < local_properties->GetNumProperties())
  419. {
  420. PropertyMap::const_iterator i = local_properties->GetProperties().begin();
  421. for (int count = 0; count < index; ++count)
  422. ++i;
  423. name = (*i).first;
  424. property = &((*i).second);
  425. property_pseudo_classes.clear();
  426. ++index;
  427. return true;
  428. }
  429. }
  430. const ElementDefinition* definition = GetDefinition();
  431. if (definition != NULL)
  432. {
  433. int index_offset = 0;
  434. if (local_properties != NULL)
  435. index_offset = local_properties->GetNumProperties();
  436. // Offset the index to be relative to the definition before we start indexing. When we do get a property back,
  437. // check that it hasn't been overridden by the element's local properties; if so, continue on to the next one.
  438. index -= index_offset;
  439. while (definition->IterateProperties(index, pseudo_classes, property_pseudo_classes, name, property))
  440. {
  441. if (local_properties == NULL ||
  442. local_properties->GetProperty(name) == NULL)
  443. {
  444. index += index_offset;
  445. return true;
  446. }
  447. }
  448. return false;
  449. }
  450. return false;
  451. }
  452. // Returns the active style sheet for this element. This may be NULL.
  453. StyleSheet* ElementStyle::GetStyleSheet() const
  454. {
  455. ElementDocument* document = element->GetOwnerDocument();
  456. if (document != NULL)
  457. return document->GetStyleSheet();
  458. return NULL;
  459. }
  460. void ElementStyle::DirtyDefinition()
  461. {
  462. definition_dirty = true;
  463. DirtyChildDefinitions();
  464. // Dirty the child definition update the element tree
  465. Element* parent = element->GetParentNode();
  466. while (parent)
  467. {
  468. parent->GetStyle()->child_definition_dirty = true;
  469. parent = parent->GetParentNode();
  470. }
  471. }
  472. void ElementStyle::DirtyChildDefinitions()
  473. {
  474. for (int i = 0; i < element->GetNumChildren(true); i++)
  475. element->GetChild(i)->GetStyle()->DirtyDefinition();
  476. }
  477. // Dirties every property.
  478. void ElementStyle::DirtyProperties()
  479. {
  480. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  481. DirtyProperties(properties);
  482. }
  483. // Dirties em-relative properties.
  484. void ElementStyle::DirtyEmProperties()
  485. {
  486. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  487. if (!em_properties)
  488. {
  489. // Check if any of these are currently em-relative. If so, dirty them.
  490. em_properties = new PropertyNameList;
  491. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  492. {
  493. // Skip font-size; this is relative to our parent's em, not ours.
  494. if (*list_iterator == FONT_SIZE)
  495. continue;
  496. // Get this property from this element. If this is em-relative, then add it to the list to
  497. // dirty.
  498. if (element->GetProperty(*list_iterator)->unit == Property::EM)
  499. em_properties->insert(*list_iterator);
  500. }
  501. }
  502. if (!em_properties->empty())
  503. DirtyProperties(*em_properties, false);
  504. // Now dirty all of our descendant's font-size properties that are relative to ems.
  505. int num_children = element->GetNumChildren(true);
  506. for (int i = 0; i < num_children; ++i)
  507. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  508. }
  509. // Dirties font-size on child elements if appropriate.
  510. void ElementStyle::DirtyInheritedEmProperties()
  511. {
  512. const Property* font_size = element->GetLocalProperty(FONT_SIZE);
  513. if (font_size == NULL)
  514. {
  515. int num_children = element->GetNumChildren(true);
  516. for (int i = 0; i < num_children; ++i)
  517. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  518. }
  519. else
  520. {
  521. if (font_size->unit & Property::RELATIVE_UNIT)
  522. DirtyProperty(FONT_SIZE);
  523. }
  524. }
  525. // Dirties rem properties.
  526. void ElementStyle::DirtyRemProperties()
  527. {
  528. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  529. PropertyNameList rem_properties;
  530. // Dirty all the properties of this element that use the rem unit.
  531. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  532. {
  533. if (element->GetProperty(*list_iterator)->unit == Property::REM)
  534. rem_properties.insert(*list_iterator);
  535. }
  536. if (!rem_properties.empty())
  537. DirtyProperties(rem_properties, false);
  538. // Now dirty all of our descendant's properties that use the rem unit.
  539. int num_children = element->GetNumChildren(true);
  540. for (int i = 0; i < num_children; ++i)
  541. element->GetChild(i)->GetStyle()->DirtyRemProperties();
  542. }
  543. void ElementStyle::DirtyDpProperties()
  544. {
  545. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  546. PropertyNameList dp_properties;
  547. // Dirty all the properties of this element that use the dp unit.
  548. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  549. {
  550. if (element->GetProperty(*list_iterator)->unit == Property::DP)
  551. dp_properties.insert(*list_iterator);
  552. }
  553. if (!dp_properties.empty())
  554. DirtyProperties(dp_properties, false);
  555. // Now dirty all of our descendant's properties that use the dp unit.
  556. int num_children = element->GetNumChildren(true);
  557. for (int i = 0; i < num_children; ++i)
  558. element->GetChild(i)->GetStyle()->DirtyDpProperties();
  559. }
  560. // Sets a single property as dirty.
  561. void ElementStyle::DirtyProperty(const String& property)
  562. {
  563. PropertyNameList properties;
  564. properties.insert(String(property));
  565. DirtyProperties(properties);
  566. }
  567. // Sets a list of properties as dirty.
  568. void ElementStyle::DirtyProperties(const PropertyNameList& properties, bool clear_em_properties)
  569. {
  570. if (properties.empty())
  571. return;
  572. bool all_inherited_dirty =
  573. StyleSheetSpecification::GetRegisteredProperties() == properties ||
  574. StyleSheetSpecification::GetRegisteredInheritedProperties() == properties;
  575. if (all_inherited_dirty)
  576. {
  577. const PropertyNameList &all_inherited_properties = StyleSheetSpecification::GetRegisteredInheritedProperties();
  578. for (int i = 0; i < element->GetNumChildren(true); i++)
  579. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(all_inherited_properties);
  580. // Clear all cached properties.
  581. cache->Clear();
  582. }
  583. else
  584. {
  585. PropertyNameList inherited_properties;
  586. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  587. {
  588. // If this property is an inherited property, then push it into the list to be passed onto our children.
  589. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(*i);
  590. if (property != NULL &&
  591. property->IsInherited())
  592. inherited_properties.insert(*i);
  593. }
  594. // Pass the list of those properties that are inherited onto our children.
  595. if (!inherited_properties.empty())
  596. {
  597. for (int i = 0; i < element->GetNumChildren(true); i++)
  598. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  599. }
  600. // Clear cached properties.
  601. cache->Clear();
  602. }
  603. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  604. if (clear_em_properties && em_properties != NULL)
  605. {
  606. delete em_properties;
  607. em_properties = NULL;
  608. }
  609. // And send the event.
  610. element->OnPropertyChange(properties);
  611. }
  612. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  613. void ElementStyle::DirtyInheritedProperties(const PropertyNameList& properties)
  614. {
  615. bool clear_em_properties = em_properties != NULL;
  616. PropertyNameList inherited_properties;
  617. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  618. {
  619. const Property *property = GetLocalProperty((*i));
  620. if (property == NULL)
  621. {
  622. inherited_properties.insert(*i);
  623. if (!clear_em_properties && em_properties != NULL && em_properties->find((*i)) != em_properties->end()) {
  624. clear_em_properties = true;
  625. }
  626. }
  627. }
  628. if (inherited_properties.empty())
  629. return;
  630. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  631. if (clear_em_properties && em_properties != NULL)
  632. {
  633. delete em_properties;
  634. em_properties = NULL;
  635. }
  636. // Clear cached inherited properties.
  637. cache->ClearInherited();
  638. // Pass the list of those properties that this element doesn't override onto our children.
  639. for (int i = 0; i < element->GetNumChildren(true); i++)
  640. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  641. element->OnPropertyChange(properties);
  642. }
  643. void ElementStyle::GetOffsetProperties(const Property **top, const Property **bottom, const Property **left, const Property **right )
  644. {
  645. cache->GetOffsetProperties(top, bottom, left, right);
  646. }
  647. void ElementStyle::GetBorderWidthProperties(const Property **border_top_width, const Property **border_bottom_width, const Property **border_left_width, const Property **bottom_right_width)
  648. {
  649. cache->GetBorderWidthProperties(border_top_width, border_bottom_width, border_left_width, bottom_right_width);
  650. }
  651. void ElementStyle::GetMarginProperties(const Property **margin_top, const Property **margin_bottom, const Property **margin_left, const Property **margin_right)
  652. {
  653. cache->GetMarginProperties(margin_top, margin_bottom, margin_left, margin_right);
  654. }
  655. void ElementStyle::GetPaddingProperties(const Property **padding_top, const Property **padding_bottom, const Property **padding_left, const Property **padding_right)
  656. {
  657. cache->GetPaddingProperties(padding_top, padding_bottom, padding_left, padding_right);
  658. }
  659. void ElementStyle::GetDimensionProperties(const Property **width, const Property **height)
  660. {
  661. cache->GetDimensionProperties(width, height);
  662. }
  663. void ElementStyle::GetLocalDimensionProperties(const Property **width, const Property **height)
  664. {
  665. cache->GetLocalDimensionProperties(width, height);
  666. }
  667. void ElementStyle::GetOverflow(int *overflow_x, int *overflow_y)
  668. {
  669. cache->GetOverflow(overflow_x, overflow_y);
  670. }
  671. int ElementStyle::GetPosition()
  672. {
  673. return cache->GetPosition();
  674. }
  675. int ElementStyle::GetFloat()
  676. {
  677. return cache->GetFloat();
  678. }
  679. int ElementStyle::GetDisplay()
  680. {
  681. return cache->GetDisplay();
  682. }
  683. int ElementStyle::GetWhitespace()
  684. {
  685. return cache->GetWhitespace();
  686. }
  687. int ElementStyle::GetPointerEvents()
  688. {
  689. return cache->GetPointerEvents();
  690. }
  691. const Property *ElementStyle::GetLineHeightProperty()
  692. {
  693. return cache->GetLineHeightProperty();
  694. }
  695. int ElementStyle::GetTextAlign()
  696. {
  697. return cache->GetTextAlign();
  698. }
  699. int ElementStyle::GetTextTransform()
  700. {
  701. return cache->GetTextTransform();
  702. }
  703. const Property *ElementStyle::GetVerticalAlignProperty()
  704. {
  705. return cache->GetVerticalAlignProperty();
  706. }
  707. }
  708. }