ElementStyle.cpp 29 KB

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