ElementStyle.cpp 30 KB

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