ElementStyle.cpp 30 KB

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