ElementStyle.cpp 30 KB

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