StyleProperties.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. /*
  2. * Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibCore/DirIterator.h>
  9. #include <LibWeb/CSS/Clip.h>
  10. #include <LibWeb/CSS/StyleProperties.h>
  11. #include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
  12. #include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
  13. #include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/CounterDefinitionsStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/CounterStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/GridAutoFlowStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/MathDepthStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/OpenTypeTaggedStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  29. #include <LibWeb/CSS/StyleValues/ScrollbarGutterStyleValue.h>
  30. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  31. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  32. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  33. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  34. #include <LibWeb/Layout/BlockContainer.h>
  35. #include <LibWeb/Layout/Node.h>
  36. #include <LibWeb/Platform/FontPlugin.h>
  37. namespace Web::CSS {
  38. NonnullRefPtr<StyleProperties::Data> StyleProperties::Data::clone() const
  39. {
  40. auto clone = adopt_ref(*new StyleProperties::Data);
  41. clone->m_animation_name_source = m_animation_name_source;
  42. clone->m_transition_property_source = m_transition_property_source;
  43. clone->m_property_values = m_property_values;
  44. clone->m_property_important = m_property_important;
  45. clone->m_property_inherited = m_property_inherited;
  46. clone->m_animated_property_values = m_animated_property_values;
  47. clone->m_math_depth = m_math_depth;
  48. clone->m_font_list = m_font_list;
  49. clone->m_line_height = m_line_height;
  50. return clone;
  51. }
  52. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  53. {
  54. auto cloned = adopt_ref(*new StyleProperties);
  55. cloned->m_data = m_data;
  56. return cloned;
  57. }
  58. bool StyleProperties::is_property_important(CSS::PropertyID property_id) const
  59. {
  60. size_t n = to_underlying(property_id);
  61. return m_data->m_property_important[n / 8] & (1 << (n % 8));
  62. }
  63. void StyleProperties::set_property_important(CSS::PropertyID property_id, Important important)
  64. {
  65. size_t n = to_underlying(property_id);
  66. if (important == Important::Yes)
  67. m_data->m_property_important[n / 8] |= (1 << (n % 8));
  68. else
  69. m_data->m_property_important[n / 8] &= ~(1 << (n % 8));
  70. }
  71. bool StyleProperties::is_property_inherited(CSS::PropertyID property_id) const
  72. {
  73. size_t n = to_underlying(property_id);
  74. return m_data->m_property_inherited[n / 8] & (1 << (n % 8));
  75. }
  76. void StyleProperties::set_property_inherited(CSS::PropertyID property_id, Inherited inherited)
  77. {
  78. size_t n = to_underlying(property_id);
  79. if (inherited == Inherited::Yes)
  80. m_data->m_property_inherited[n / 8] |= (1 << (n % 8));
  81. else
  82. m_data->m_property_inherited[n / 8] &= ~(1 << (n % 8));
  83. }
  84. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<CSSStyleValue const> value, Inherited inherited, Important important)
  85. {
  86. m_data->m_property_values[to_underlying(id)] = move(value);
  87. set_property_important(id, important);
  88. set_property_inherited(id, inherited);
  89. }
  90. void StyleProperties::revert_property(CSS::PropertyID id, StyleProperties const& style_for_revert)
  91. {
  92. m_data->m_property_values[to_underlying(id)] = style_for_revert.m_data->m_property_values[to_underlying(id)];
  93. set_property_important(id, style_for_revert.is_property_important(id) ? Important::Yes : Important::No);
  94. set_property_inherited(id, style_for_revert.is_property_inherited(id) ? Inherited::Yes : Inherited::No);
  95. }
  96. void StyleProperties::set_animated_property(CSS::PropertyID id, NonnullRefPtr<CSSStyleValue const> value)
  97. {
  98. m_data->m_animated_property_values.set(id, move(value));
  99. }
  100. void StyleProperties::reset_animated_properties()
  101. {
  102. m_data->m_animated_property_values.clear();
  103. }
  104. NonnullRefPtr<CSSStyleValue const> StyleProperties::property(CSS::PropertyID property_id, WithAnimationsApplied return_animated_value) const
  105. {
  106. if (return_animated_value == WithAnimationsApplied::Yes) {
  107. if (auto animated_value = m_data->m_animated_property_values.get(property_id).value_or(nullptr))
  108. return *animated_value;
  109. }
  110. // By the time we call this method, all properties have values assigned.
  111. return *m_data->m_property_values[to_underlying(property_id)];
  112. }
  113. RefPtr<CSSStyleValue const> StyleProperties::maybe_null_property(CSS::PropertyID property_id) const
  114. {
  115. if (auto animated_value = m_data->m_animated_property_values.get(property_id).value_or(nullptr))
  116. return *animated_value;
  117. return m_data->m_property_values[to_underlying(property_id)];
  118. }
  119. CSS::Size StyleProperties::size_value(CSS::PropertyID id) const
  120. {
  121. auto value = property(id);
  122. if (value->is_keyword()) {
  123. switch (value->to_keyword()) {
  124. case Keyword::Auto:
  125. return CSS::Size::make_auto();
  126. case Keyword::MinContent:
  127. return CSS::Size::make_min_content();
  128. case Keyword::MaxContent:
  129. return CSS::Size::make_max_content();
  130. case Keyword::FitContent:
  131. return CSS::Size::make_fit_content();
  132. case Keyword::None:
  133. return CSS::Size::make_none();
  134. default:
  135. VERIFY_NOT_REACHED();
  136. }
  137. }
  138. if (value->is_math())
  139. return CSS::Size::make_calculated(const_cast<CSSMathValue&>(value->as_math()));
  140. if (value->is_percentage())
  141. return CSS::Size::make_percentage(value->as_percentage().percentage());
  142. if (value->is_length()) {
  143. auto length = value->as_length().length();
  144. if (length.is_auto())
  145. return CSS::Size::make_auto();
  146. return CSS::Size::make_length(length);
  147. }
  148. // FIXME: Support `fit-content(<length>)`
  149. dbgln("FIXME: Unsupported size value: `{}`, treating as `auto`", value->to_string());
  150. return CSS::Size::make_auto();
  151. }
  152. LengthPercentage StyleProperties::length_percentage_or_fallback(CSS::PropertyID id, LengthPercentage const& fallback) const
  153. {
  154. return length_percentage(id).value_or(fallback);
  155. }
  156. Optional<LengthPercentage> StyleProperties::length_percentage(CSS::PropertyID id) const
  157. {
  158. auto value = property(id);
  159. if (value->is_math())
  160. return LengthPercentage { const_cast<CSSMathValue&>(value->as_math()) };
  161. if (value->is_percentage())
  162. return value->as_percentage().percentage();
  163. if (value->is_length())
  164. return value->as_length().length();
  165. if (value->has_auto())
  166. return LengthPercentage { Length::make_auto() };
  167. return {};
  168. }
  169. LengthBox StyleProperties::length_box(CSS::PropertyID left_id, CSS::PropertyID top_id, CSS::PropertyID right_id, CSS::PropertyID bottom_id, const CSS::Length& default_value) const
  170. {
  171. LengthBox box;
  172. box.left() = length_percentage_or_fallback(left_id, default_value);
  173. box.top() = length_percentage_or_fallback(top_id, default_value);
  174. box.right() = length_percentage_or_fallback(right_id, default_value);
  175. box.bottom() = length_percentage_or_fallback(bottom_id, default_value);
  176. return box;
  177. }
  178. Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const
  179. {
  180. auto value = property(id);
  181. if (!value->has_color())
  182. return fallback;
  183. return value->to_color(node);
  184. }
  185. NonnullRefPtr<Gfx::Font const> StyleProperties::font_fallback(bool monospace, bool bold)
  186. {
  187. if (monospace && bold)
  188. return Platform::FontPlugin::the().default_fixed_width_font().bold_variant();
  189. if (monospace)
  190. return Platform::FontPlugin::the().default_fixed_width_font();
  191. if (bold)
  192. return Platform::FontPlugin::the().default_font().bold_variant();
  193. return Platform::FontPlugin::the().default_font();
  194. }
  195. CSSPixels StyleProperties::compute_line_height(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const
  196. {
  197. auto line_height = property(CSS::PropertyID::LineHeight);
  198. if (line_height->is_keyword() && line_height->to_keyword() == Keyword::Normal)
  199. return font_metrics.line_height;
  200. if (line_height->is_length()) {
  201. auto line_height_length = line_height->as_length().length();
  202. if (!line_height_length.is_auto())
  203. return line_height_length.to_px(viewport_rect, font_metrics, root_font_metrics);
  204. }
  205. if (line_height->is_number())
  206. return Length(line_height->as_number().number(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  207. if (line_height->is_percentage()) {
  208. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  209. auto& percentage = line_height->as_percentage().percentage();
  210. return Length(percentage.as_fraction(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  211. }
  212. if (line_height->is_math()) {
  213. if (line_height->as_math().resolves_to_number()) {
  214. auto resolved = line_height->as_math().resolve_number();
  215. if (!resolved.has_value()) {
  216. dbgln("FIXME: Failed to resolve calc() line-height (number): {}", line_height->as_math().to_string());
  217. return CSSPixels::nearest_value_for(m_data->m_font_list->first().pixel_metrics().line_spacing());
  218. }
  219. return Length(resolved.value(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  220. }
  221. auto resolved = line_height->as_math().resolve_length(Length::ResolutionContext { viewport_rect, font_metrics, root_font_metrics });
  222. if (!resolved.has_value()) {
  223. dbgln("FIXME: Failed to resolve calc() line-height: {}", line_height->as_math().to_string());
  224. return CSSPixels::nearest_value_for(m_data->m_font_list->first().pixel_metrics().line_spacing());
  225. }
  226. return resolved->to_px(viewport_rect, font_metrics, root_font_metrics);
  227. }
  228. return font_metrics.line_height;
  229. }
  230. Optional<int> StyleProperties::z_index() const
  231. {
  232. auto value = property(CSS::PropertyID::ZIndex);
  233. if (value->has_auto())
  234. return {};
  235. if (value->is_integer()) {
  236. // Clamp z-index to the range of a signed 32-bit integer for consistency with other engines.
  237. auto integer = value->as_integer().integer();
  238. if (integer >= NumericLimits<int>::max())
  239. return NumericLimits<int>::max();
  240. if (integer <= NumericLimits<int>::min())
  241. return NumericLimits<int>::min();
  242. return static_cast<int>(integer);
  243. }
  244. return {};
  245. }
  246. float StyleProperties::resolve_opacity_value(CSSStyleValue const& value)
  247. {
  248. float unclamped_opacity = 1.0f;
  249. if (value.is_number()) {
  250. unclamped_opacity = value.as_number().number();
  251. } else if (value.is_math()) {
  252. auto& calculated = value.as_math();
  253. if (calculated.resolves_to_percentage()) {
  254. auto maybe_percentage = value.as_math().resolve_percentage();
  255. if (maybe_percentage.has_value())
  256. unclamped_opacity = maybe_percentage->as_fraction();
  257. else
  258. dbgln("Unable to resolve calc() as opacity (percentage): {}", value.to_string());
  259. } else if (calculated.resolves_to_number()) {
  260. auto maybe_number = const_cast<CSSMathValue&>(value.as_math()).resolve_number();
  261. if (maybe_number.has_value())
  262. unclamped_opacity = maybe_number.value();
  263. else
  264. dbgln("Unable to resolve calc() as opacity (number): {}", value.to_string());
  265. }
  266. } else if (value.is_percentage()) {
  267. unclamped_opacity = value.as_percentage().percentage().as_fraction();
  268. }
  269. return clamp(unclamped_opacity, 0.0f, 1.0f);
  270. }
  271. float StyleProperties::opacity() const
  272. {
  273. auto value = property(CSS::PropertyID::Opacity);
  274. return resolve_opacity_value(*value);
  275. }
  276. float StyleProperties::fill_opacity() const
  277. {
  278. auto value = property(CSS::PropertyID::FillOpacity);
  279. return resolve_opacity_value(*value);
  280. }
  281. float StyleProperties::stroke_opacity() const
  282. {
  283. auto value = property(CSS::PropertyID::StrokeOpacity);
  284. return resolve_opacity_value(*value);
  285. }
  286. float StyleProperties::stop_opacity() const
  287. {
  288. auto value = property(CSS::PropertyID::StopOpacity);
  289. return resolve_opacity_value(*value);
  290. }
  291. Optional<CSS::FillRule> StyleProperties::fill_rule() const
  292. {
  293. auto value = property(CSS::PropertyID::FillRule);
  294. return keyword_to_fill_rule(value->to_keyword());
  295. }
  296. Optional<CSS::ClipRule> StyleProperties::clip_rule() const
  297. {
  298. auto value = property(CSS::PropertyID::ClipRule);
  299. return keyword_to_fill_rule(value->to_keyword());
  300. }
  301. Optional<CSS::FlexDirection> StyleProperties::flex_direction() const
  302. {
  303. auto value = property(CSS::PropertyID::FlexDirection);
  304. return keyword_to_flex_direction(value->to_keyword());
  305. }
  306. Optional<CSS::FlexWrap> StyleProperties::flex_wrap() const
  307. {
  308. auto value = property(CSS::PropertyID::FlexWrap);
  309. return keyword_to_flex_wrap(value->to_keyword());
  310. }
  311. Optional<CSS::FlexBasis> StyleProperties::flex_basis() const
  312. {
  313. auto value = property(CSS::PropertyID::FlexBasis);
  314. if (value->is_keyword() && value->to_keyword() == CSS::Keyword::Content)
  315. return CSS::FlexBasisContent {};
  316. return size_value(CSS::PropertyID::FlexBasis);
  317. }
  318. float StyleProperties::flex_grow() const
  319. {
  320. auto value = property(CSS::PropertyID::FlexGrow);
  321. if (!value->is_number())
  322. return 0;
  323. return value->as_number().number();
  324. }
  325. float StyleProperties::flex_shrink() const
  326. {
  327. auto value = property(CSS::PropertyID::FlexShrink);
  328. if (!value->is_number())
  329. return 1;
  330. return value->as_number().number();
  331. }
  332. int StyleProperties::order() const
  333. {
  334. auto value = property(CSS::PropertyID::Order);
  335. if (!value->is_integer())
  336. return 0;
  337. return value->as_integer().integer();
  338. }
  339. Optional<CSS::ImageRendering> StyleProperties::image_rendering() const
  340. {
  341. auto value = property(CSS::PropertyID::ImageRendering);
  342. return keyword_to_image_rendering(value->to_keyword());
  343. }
  344. CSS::Length StyleProperties::border_spacing_horizontal() const
  345. {
  346. auto value = property(CSS::PropertyID::BorderSpacing);
  347. if (value->is_length())
  348. return value->as_length().length();
  349. auto const& list = value->as_value_list();
  350. return list.value_at(0, false)->as_length().length();
  351. }
  352. CSS::Length StyleProperties::border_spacing_vertical() const
  353. {
  354. auto value = property(CSS::PropertyID::BorderSpacing);
  355. if (value->is_length())
  356. return value->as_length().length();
  357. auto const& list = value->as_value_list();
  358. return list.value_at(1, false)->as_length().length();
  359. }
  360. Optional<CSS::CaptionSide> StyleProperties::caption_side() const
  361. {
  362. auto value = property(CSS::PropertyID::CaptionSide);
  363. return keyword_to_caption_side(value->to_keyword());
  364. }
  365. CSS::Clip StyleProperties::clip() const
  366. {
  367. auto value = property(CSS::PropertyID::Clip);
  368. if (!value->is_rect())
  369. return CSS::Clip::make_auto();
  370. return CSS::Clip(value->as_rect().rect());
  371. }
  372. Optional<CSS::JustifyContent> StyleProperties::justify_content() const
  373. {
  374. auto value = property(CSS::PropertyID::JustifyContent);
  375. return keyword_to_justify_content(value->to_keyword());
  376. }
  377. Optional<CSS::JustifyItems> StyleProperties::justify_items() const
  378. {
  379. auto value = property(CSS::PropertyID::JustifyItems);
  380. return keyword_to_justify_items(value->to_keyword());
  381. }
  382. Optional<CSS::JustifySelf> StyleProperties::justify_self() const
  383. {
  384. auto value = property(CSS::PropertyID::JustifySelf);
  385. return keyword_to_justify_self(value->to_keyword());
  386. }
  387. Vector<CSS::Transformation> StyleProperties::transformations_for_style_value(CSSStyleValue const& value)
  388. {
  389. if (value.is_keyword() && value.to_keyword() == CSS::Keyword::None)
  390. return {};
  391. if (!value.is_value_list())
  392. return {};
  393. auto& list = value.as_value_list();
  394. Vector<CSS::Transformation> transformations;
  395. for (auto& it : list.values()) {
  396. if (!it->is_transformation())
  397. return {};
  398. auto& transformation_style_value = it->as_transformation();
  399. auto function = transformation_style_value.transform_function();
  400. auto function_metadata = transform_function_metadata(function);
  401. Vector<TransformValue> values;
  402. size_t argument_index = 0;
  403. for (auto& transformation_value : transformation_style_value.values()) {
  404. if (transformation_value->is_math()) {
  405. auto& calculated = transformation_value->as_math();
  406. if (calculated.resolves_to_length_percentage()) {
  407. values.append(CSS::LengthPercentage { calculated });
  408. } else if (calculated.resolves_to_percentage()) {
  409. // FIXME: Maybe transform this for loop to always check the metadata for the correct types
  410. if (function_metadata.parameters[argument_index].type == TransformFunctionParameterType::NumberPercentage) {
  411. values.append(NumberPercentage { calculated.resolve_percentage().value() });
  412. } else {
  413. values.append(LengthPercentage { calculated.resolve_percentage().value() });
  414. }
  415. } else if (calculated.resolves_to_number()) {
  416. values.append({ Number(Number::Type::Number, calculated.resolve_number().value()) });
  417. } else if (calculated.resolves_to_angle()) {
  418. values.append({ calculated.resolve_angle().value() });
  419. } else {
  420. dbgln("FIXME: Unsupported calc value in transform! {}", calculated.to_string());
  421. }
  422. } else if (transformation_value->is_length()) {
  423. values.append({ transformation_value->as_length().length() });
  424. } else if (transformation_value->is_percentage()) {
  425. if (function_metadata.parameters[argument_index].type == TransformFunctionParameterType::NumberPercentage) {
  426. values.append(NumberPercentage { transformation_value->as_percentage().percentage() });
  427. } else {
  428. values.append(LengthPercentage { transformation_value->as_percentage().percentage() });
  429. }
  430. } else if (transformation_value->is_number()) {
  431. values.append({ Number(Number::Type::Number, transformation_value->as_number().number()) });
  432. } else if (transformation_value->is_angle()) {
  433. values.append({ transformation_value->as_angle().angle() });
  434. } else {
  435. dbgln("FIXME: Unsupported value in transform! {}", transformation_value->to_string());
  436. }
  437. argument_index++;
  438. }
  439. transformations.empend(function, move(values));
  440. }
  441. return transformations;
  442. }
  443. Vector<CSS::Transformation> StyleProperties::transformations() const
  444. {
  445. return transformations_for_style_value(property(CSS::PropertyID::Transform));
  446. }
  447. static Optional<LengthPercentage> length_percentage_for_style_value(CSSStyleValue const& value)
  448. {
  449. if (value.is_length())
  450. return value.as_length().length();
  451. if (value.is_percentage())
  452. return value.as_percentage().percentage();
  453. return {};
  454. }
  455. Optional<CSS::TransformBox> StyleProperties::transform_box() const
  456. {
  457. auto value = property(CSS::PropertyID::TransformBox);
  458. return keyword_to_transform_box(value->to_keyword());
  459. }
  460. CSS::TransformOrigin StyleProperties::transform_origin() const
  461. {
  462. auto value = property(CSS::PropertyID::TransformOrigin);
  463. if (!value->is_value_list() || value->as_value_list().size() != 2)
  464. return {};
  465. auto const& list = value->as_value_list();
  466. auto x_value = length_percentage_for_style_value(list.values()[0]);
  467. auto y_value = length_percentage_for_style_value(list.values()[1]);
  468. if (!x_value.has_value() || !y_value.has_value()) {
  469. return {};
  470. }
  471. return { x_value.value(), y_value.value() };
  472. }
  473. Optional<Color> StyleProperties::accent_color(Layout::NodeWithStyle const& node) const
  474. {
  475. auto value = property(CSS::PropertyID::AccentColor);
  476. if (value->has_color())
  477. return value->to_color(node);
  478. return {};
  479. }
  480. Optional<CSS::AlignContent> StyleProperties::align_content() const
  481. {
  482. auto value = property(CSS::PropertyID::AlignContent);
  483. return keyword_to_align_content(value->to_keyword());
  484. }
  485. Optional<CSS::AlignItems> StyleProperties::align_items() const
  486. {
  487. auto value = property(CSS::PropertyID::AlignItems);
  488. return keyword_to_align_items(value->to_keyword());
  489. }
  490. Optional<CSS::AlignSelf> StyleProperties::align_self() const
  491. {
  492. auto value = property(CSS::PropertyID::AlignSelf);
  493. return keyword_to_align_self(value->to_keyword());
  494. }
  495. Optional<CSS::Appearance> StyleProperties::appearance() const
  496. {
  497. auto value = property(CSS::PropertyID::Appearance);
  498. auto appearance = keyword_to_appearance(value->to_keyword());
  499. if (appearance.has_value()) {
  500. switch (*appearance) {
  501. // Note: All these compatibility values can be treated as 'auto'
  502. case CSS::Appearance::Textfield:
  503. case CSS::Appearance::MenulistButton:
  504. case CSS::Appearance::Searchfield:
  505. case CSS::Appearance::Textarea:
  506. case CSS::Appearance::PushButton:
  507. case CSS::Appearance::SliderHorizontal:
  508. case CSS::Appearance::Checkbox:
  509. case CSS::Appearance::Radio:
  510. case CSS::Appearance::SquareButton:
  511. case CSS::Appearance::Menulist:
  512. case CSS::Appearance::Listbox:
  513. case CSS::Appearance::Meter:
  514. case CSS::Appearance::ProgressBar:
  515. case CSS::Appearance::Button:
  516. appearance = CSS::Appearance::Auto;
  517. break;
  518. default:
  519. break;
  520. }
  521. }
  522. return appearance;
  523. }
  524. CSS::BackdropFilter StyleProperties::backdrop_filter() const
  525. {
  526. auto value = property(CSS::PropertyID::BackdropFilter);
  527. if (value->is_filter_value_list())
  528. return BackdropFilter(value->as_filter_value_list());
  529. return BackdropFilter::make_none();
  530. }
  531. Optional<CSS::Positioning> StyleProperties::position() const
  532. {
  533. auto value = property(CSS::PropertyID::Position);
  534. return keyword_to_positioning(value->to_keyword());
  535. }
  536. bool StyleProperties::operator==(StyleProperties const& other) const
  537. {
  538. if (m_data->m_property_values.size() != other.m_data->m_property_values.size())
  539. return false;
  540. for (size_t i = 0; i < m_data->m_property_values.size(); ++i) {
  541. auto const& my_style = m_data->m_property_values[i];
  542. auto const& other_style = other.m_data->m_property_values[i];
  543. if (!my_style) {
  544. if (other_style)
  545. return false;
  546. continue;
  547. }
  548. if (!other_style)
  549. return false;
  550. auto const& my_value = *my_style;
  551. auto const& other_value = *other_style;
  552. if (my_value.type() != other_value.type())
  553. return false;
  554. if (my_value != other_value)
  555. return false;
  556. }
  557. return true;
  558. }
  559. Optional<CSS::TextAnchor> StyleProperties::text_anchor() const
  560. {
  561. auto value = property(CSS::PropertyID::TextAnchor);
  562. return keyword_to_text_anchor(value->to_keyword());
  563. }
  564. Optional<CSS::TextAlign> StyleProperties::text_align() const
  565. {
  566. auto value = property(CSS::PropertyID::TextAlign);
  567. return keyword_to_text_align(value->to_keyword());
  568. }
  569. Optional<CSS::TextJustify> StyleProperties::text_justify() const
  570. {
  571. auto value = property(CSS::PropertyID::TextJustify);
  572. return keyword_to_text_justify(value->to_keyword());
  573. }
  574. Optional<CSS::TextOverflow> StyleProperties::text_overflow() const
  575. {
  576. auto value = property(CSS::PropertyID::TextOverflow);
  577. return keyword_to_text_overflow(value->to_keyword());
  578. }
  579. Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
  580. {
  581. auto value = property(CSS::PropertyID::PointerEvents);
  582. return keyword_to_pointer_events(value->to_keyword());
  583. }
  584. Variant<LengthOrCalculated, NumberOrCalculated> StyleProperties::tab_size() const
  585. {
  586. auto value = property(CSS::PropertyID::TabSize);
  587. if (value->is_math()) {
  588. auto& math_value = value->as_math();
  589. if (math_value.resolves_to_length()) {
  590. return LengthOrCalculated { math_value };
  591. }
  592. if (math_value.resolves_to_number()) {
  593. return NumberOrCalculated { math_value };
  594. }
  595. }
  596. if (value->is_length())
  597. return LengthOrCalculated { value->as_length().length() };
  598. return NumberOrCalculated { value->as_number().number() };
  599. }
  600. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  601. {
  602. auto value = property(CSS::PropertyID::WhiteSpace);
  603. return keyword_to_white_space(value->to_keyword());
  604. }
  605. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  606. {
  607. auto value = property(property_id);
  608. return keyword_to_line_style(value->to_keyword());
  609. }
  610. Optional<CSS::OutlineStyle> StyleProperties::outline_style() const
  611. {
  612. auto value = property(CSS::PropertyID::OutlineStyle);
  613. return keyword_to_outline_style(value->to_keyword());
  614. }
  615. Optional<CSS::Float> StyleProperties::float_() const
  616. {
  617. auto value = property(CSS::PropertyID::Float);
  618. return keyword_to_float(value->to_keyword());
  619. }
  620. Optional<CSS::Clear> StyleProperties::clear() const
  621. {
  622. auto value = property(CSS::PropertyID::Clear);
  623. return keyword_to_clear(value->to_keyword());
  624. }
  625. Optional<CSS::ColumnSpan> StyleProperties::column_span() const
  626. {
  627. auto value = property(CSS::PropertyID::ColumnSpan);
  628. return keyword_to_column_span(value->to_keyword());
  629. }
  630. StyleProperties::ContentDataAndQuoteNestingLevel StyleProperties::content(DOM::Element& element, u32 initial_quote_nesting_level) const
  631. {
  632. auto value = property(CSS::PropertyID::Content);
  633. auto quotes_data = quotes();
  634. auto quote_nesting_level = initial_quote_nesting_level;
  635. auto get_quote_string = [&](bool open, auto depth) {
  636. switch (quotes_data.type) {
  637. case QuotesData::Type::None:
  638. return FlyString {};
  639. case QuotesData::Type::Auto:
  640. // FIXME: "A typographically appropriate used value for quotes is automatically chosen by the UA
  641. // based on the content language of the element and/or its parent."
  642. if (open)
  643. return depth == 0 ? "“"_fly_string : "‘"_fly_string;
  644. return depth == 0 ? "”"_fly_string : "’"_fly_string;
  645. case QuotesData::Type::Specified:
  646. // If the depth is greater than the number of pairs, the last pair is repeated.
  647. auto& level = quotes_data.strings[min(depth, quotes_data.strings.size() - 1)];
  648. return open ? level[0] : level[1];
  649. }
  650. VERIFY_NOT_REACHED();
  651. };
  652. if (value->is_content()) {
  653. auto& content_style_value = value->as_content();
  654. CSS::ContentData content_data;
  655. // FIXME: The content is a list of things: strings, identifiers or functions that return strings, and images.
  656. // So it can't always be represented as a single String, but may have to be multiple boxes.
  657. // For now, we'll just assume strings since that is easiest.
  658. StringBuilder builder;
  659. for (auto const& item : content_style_value.content().values()) {
  660. if (item->is_string()) {
  661. builder.append(item->as_string().string_value());
  662. } else if (item->is_keyword()) {
  663. switch (item->to_keyword()) {
  664. case Keyword::OpenQuote:
  665. builder.append(get_quote_string(true, quote_nesting_level++));
  666. break;
  667. case Keyword::CloseQuote:
  668. // A 'close-quote' or 'no-close-quote' that would make the depth negative is in error and is ignored
  669. // (at rendering time): the depth stays at 0 and no quote mark is rendered (although the rest of the
  670. // 'content' property's value is still inserted).
  671. // - https://www.w3.org/TR/CSS21/generate.html#quotes-insert
  672. // (This is missing from the CONTENT-3 spec.)
  673. if (quote_nesting_level > 0)
  674. builder.append(get_quote_string(false, --quote_nesting_level));
  675. break;
  676. case Keyword::NoOpenQuote:
  677. quote_nesting_level++;
  678. break;
  679. case Keyword::NoCloseQuote:
  680. // NOTE: See CloseQuote
  681. if (quote_nesting_level > 0)
  682. quote_nesting_level--;
  683. break;
  684. default:
  685. dbgln("`{}` is not supported in `content` (yet?)", item->to_string());
  686. break;
  687. }
  688. } else if (item->is_counter()) {
  689. builder.append(item->as_counter().resolve(element));
  690. } else {
  691. // TODO: Implement images, and other things.
  692. dbgln("`{}` is not supported in `content` (yet?)", item->to_string());
  693. }
  694. }
  695. content_data.type = ContentData::Type::String;
  696. content_data.data = MUST(builder.to_string());
  697. if (content_style_value.has_alt_text()) {
  698. StringBuilder alt_text_builder;
  699. for (auto const& item : content_style_value.alt_text()->values()) {
  700. if (item->is_string()) {
  701. alt_text_builder.append(item->as_string().string_value());
  702. } else if (item->is_counter()) {
  703. alt_text_builder.append(item->as_counter().resolve(element));
  704. } else {
  705. dbgln("`{}` is not supported in `content` alt-text (yet?)", item->to_string());
  706. }
  707. }
  708. content_data.alt_text = MUST(alt_text_builder.to_string());
  709. }
  710. return { content_data, quote_nesting_level };
  711. }
  712. switch (value->to_keyword()) {
  713. case Keyword::None:
  714. return { { ContentData::Type::None }, quote_nesting_level };
  715. case Keyword::Normal:
  716. return { { ContentData::Type::Normal }, quote_nesting_level };
  717. default:
  718. break;
  719. }
  720. return { {}, quote_nesting_level };
  721. }
  722. Optional<CSS::ContentVisibility> StyleProperties::content_visibility() const
  723. {
  724. auto value = property(CSS::PropertyID::ContentVisibility);
  725. return keyword_to_content_visibility(value->to_keyword());
  726. }
  727. Optional<CSS::Cursor> StyleProperties::cursor() const
  728. {
  729. auto value = property(CSS::PropertyID::Cursor);
  730. return keyword_to_cursor(value->to_keyword());
  731. }
  732. Optional<CSS::Visibility> StyleProperties::visibility() const
  733. {
  734. auto value = property(CSS::PropertyID::Visibility);
  735. if (!value->is_keyword())
  736. return {};
  737. return keyword_to_visibility(value->to_keyword());
  738. }
  739. Display StyleProperties::display() const
  740. {
  741. auto value = property(PropertyID::Display);
  742. if (value->is_display()) {
  743. return value->as_display().display();
  744. }
  745. return Display::from_short(Display::Short::Inline);
  746. }
  747. Vector<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  748. {
  749. auto value = property(CSS::PropertyID::TextDecorationLine);
  750. if (value->is_value_list()) {
  751. Vector<CSS::TextDecorationLine> lines;
  752. auto& values = value->as_value_list().values();
  753. for (auto const& item : values) {
  754. lines.append(keyword_to_text_decoration_line(item->to_keyword()).value());
  755. }
  756. return lines;
  757. }
  758. if (value->is_keyword() && value->to_keyword() == Keyword::None)
  759. return {};
  760. dbgln("FIXME: Unsupported value for text-decoration-line: {}", value->to_string());
  761. return {};
  762. }
  763. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  764. {
  765. auto value = property(CSS::PropertyID::TextDecorationStyle);
  766. return keyword_to_text_decoration_style(value->to_keyword());
  767. }
  768. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  769. {
  770. auto value = property(CSS::PropertyID::TextTransform);
  771. return keyword_to_text_transform(value->to_keyword());
  772. }
  773. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  774. {
  775. auto value = property(CSS::PropertyID::ListStyleType);
  776. return keyword_to_list_style_type(value->to_keyword());
  777. }
  778. Optional<CSS::ListStylePosition> StyleProperties::list_style_position() const
  779. {
  780. auto value = property(CSS::PropertyID::ListStylePosition);
  781. return keyword_to_list_style_position(value->to_keyword());
  782. }
  783. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  784. {
  785. return overflow(CSS::PropertyID::OverflowX);
  786. }
  787. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  788. {
  789. return overflow(CSS::PropertyID::OverflowY);
  790. }
  791. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  792. {
  793. auto value = property(property_id);
  794. return keyword_to_overflow(value->to_keyword());
  795. }
  796. Vector<ShadowData> StyleProperties::shadow(PropertyID property_id, Layout::Node const& layout_node) const
  797. {
  798. auto value = property(property_id);
  799. auto resolve_to_length = [&layout_node](NonnullRefPtr<CSSStyleValue const> const& value) -> Optional<Length> {
  800. if (value->is_length())
  801. return value->as_length().length();
  802. if (value->is_math())
  803. return value->as_math().resolve_length(layout_node);
  804. return {};
  805. };
  806. auto make_shadow_data = [resolve_to_length, &layout_node](ShadowStyleValue const& value) -> Optional<ShadowData> {
  807. auto maybe_offset_x = resolve_to_length(value.offset_x());
  808. if (!maybe_offset_x.has_value())
  809. return {};
  810. auto maybe_offset_y = resolve_to_length(value.offset_y());
  811. if (!maybe_offset_y.has_value())
  812. return {};
  813. auto maybe_blur_radius = resolve_to_length(value.blur_radius());
  814. if (!maybe_blur_radius.has_value())
  815. return {};
  816. auto maybe_spread_distance = resolve_to_length(value.spread_distance());
  817. if (!maybe_spread_distance.has_value())
  818. return {};
  819. return ShadowData {
  820. value.color()->to_color(verify_cast<Layout::NodeWithStyle>(layout_node)),
  821. maybe_offset_x.release_value(),
  822. maybe_offset_y.release_value(),
  823. maybe_blur_radius.release_value(),
  824. maybe_spread_distance.release_value(),
  825. value.placement()
  826. };
  827. };
  828. if (value->is_value_list()) {
  829. auto const& value_list = value->as_value_list();
  830. Vector<ShadowData> shadow_data;
  831. shadow_data.ensure_capacity(value_list.size());
  832. for (auto const& layer_value : value_list.values()) {
  833. auto maybe_shadow_data = make_shadow_data(layer_value->as_shadow());
  834. if (!maybe_shadow_data.has_value())
  835. return {};
  836. shadow_data.append(maybe_shadow_data.release_value());
  837. }
  838. return shadow_data;
  839. }
  840. if (value->is_shadow()) {
  841. auto maybe_shadow_data = make_shadow_data(value->as_shadow());
  842. if (!maybe_shadow_data.has_value())
  843. return {};
  844. return { maybe_shadow_data.release_value() };
  845. }
  846. return {};
  847. }
  848. Vector<ShadowData> StyleProperties::box_shadow(Layout::Node const& layout_node) const
  849. {
  850. return shadow(PropertyID::BoxShadow, layout_node);
  851. }
  852. Vector<ShadowData> StyleProperties::text_shadow(Layout::Node const& layout_node) const
  853. {
  854. return shadow(PropertyID::TextShadow, layout_node);
  855. }
  856. Optional<CSS::BoxSizing> StyleProperties::box_sizing() const
  857. {
  858. auto value = property(CSS::PropertyID::BoxSizing);
  859. return keyword_to_box_sizing(value->to_keyword());
  860. }
  861. Variant<CSS::VerticalAlign, CSS::LengthPercentage> StyleProperties::vertical_align() const
  862. {
  863. auto value = property(CSS::PropertyID::VerticalAlign);
  864. if (value->is_keyword())
  865. return keyword_to_vertical_align(value->to_keyword()).release_value();
  866. if (value->is_length())
  867. return CSS::LengthPercentage(value->as_length().length());
  868. if (value->is_percentage())
  869. return CSS::LengthPercentage(value->as_percentage().percentage());
  870. if (value->is_math())
  871. return LengthPercentage { const_cast<CSSMathValue&>(value->as_math()) };
  872. VERIFY_NOT_REACHED();
  873. }
  874. Optional<CSS::FontVariant> StyleProperties::font_variant() const
  875. {
  876. auto value = property(CSS::PropertyID::FontVariant);
  877. return keyword_to_font_variant(value->to_keyword());
  878. }
  879. Optional<FlyString> StyleProperties::font_language_override() const
  880. {
  881. auto value = property(CSS::PropertyID::FontLanguageOverride);
  882. if (value->is_string())
  883. return value->as_string().string_value();
  884. return {};
  885. }
  886. Optional<HashMap<FlyString, IntegerOrCalculated>> StyleProperties::font_feature_settings() const
  887. {
  888. auto value = property(PropertyID::FontFeatureSettings);
  889. if (value->is_keyword())
  890. return {}; // normal
  891. if (value->is_value_list()) {
  892. auto const& feature_tags = value->as_value_list().values();
  893. HashMap<FlyString, IntegerOrCalculated> result;
  894. result.ensure_capacity(feature_tags.size());
  895. for (auto const& tag_value : feature_tags) {
  896. auto const& feature_tag = tag_value->as_open_type_tagged();
  897. if (feature_tag.value()->is_integer()) {
  898. result.set(feature_tag.tag(), feature_tag.value()->as_integer().value());
  899. } else {
  900. VERIFY(feature_tag.value()->is_math());
  901. result.set(feature_tag.tag(), IntegerOrCalculated { feature_tag.value()->as_math() });
  902. }
  903. }
  904. return result;
  905. }
  906. return {};
  907. }
  908. Optional<HashMap<FlyString, NumberOrCalculated>> StyleProperties::font_variation_settings() const
  909. {
  910. auto value = property(CSS::PropertyID::FontVariationSettings);
  911. if (value->is_keyword())
  912. return {}; // normal
  913. if (value->is_value_list()) {
  914. auto const& axis_tags = value->as_value_list().values();
  915. HashMap<FlyString, NumberOrCalculated> result;
  916. result.ensure_capacity(axis_tags.size());
  917. for (auto const& tag_value : axis_tags) {
  918. auto const& axis_tag = tag_value->as_open_type_tagged();
  919. if (axis_tag.value()->is_number()) {
  920. result.set(axis_tag.tag(), axis_tag.value()->as_number().value());
  921. } else {
  922. VERIFY(axis_tag.value()->is_math());
  923. result.set(axis_tag.tag(), NumberOrCalculated { axis_tag.value()->as_math() });
  924. }
  925. }
  926. return result;
  927. }
  928. return {};
  929. }
  930. CSS::GridTrackSizeList StyleProperties::grid_auto_columns() const
  931. {
  932. auto value = property(CSS::PropertyID::GridAutoColumns);
  933. return value->as_grid_track_size_list().grid_track_size_list();
  934. }
  935. CSS::GridTrackSizeList StyleProperties::grid_auto_rows() const
  936. {
  937. auto value = property(CSS::PropertyID::GridAutoRows);
  938. return value->as_grid_track_size_list().grid_track_size_list();
  939. }
  940. CSS::GridTrackSizeList StyleProperties::grid_template_columns() const
  941. {
  942. auto value = property(CSS::PropertyID::GridTemplateColumns);
  943. return value->as_grid_track_size_list().grid_track_size_list();
  944. }
  945. CSS::GridTrackSizeList StyleProperties::grid_template_rows() const
  946. {
  947. auto value = property(CSS::PropertyID::GridTemplateRows);
  948. return value->as_grid_track_size_list().grid_track_size_list();
  949. }
  950. CSS::GridAutoFlow StyleProperties::grid_auto_flow() const
  951. {
  952. auto value = property(CSS::PropertyID::GridAutoFlow);
  953. if (!value->is_grid_auto_flow())
  954. return CSS::GridAutoFlow {};
  955. auto& grid_auto_flow_value = value->as_grid_auto_flow();
  956. return CSS::GridAutoFlow { .row = grid_auto_flow_value.is_row(), .dense = grid_auto_flow_value.is_dense() };
  957. }
  958. CSS::GridTrackPlacement StyleProperties::grid_column_end() const
  959. {
  960. auto value = property(CSS::PropertyID::GridColumnEnd);
  961. return value->as_grid_track_placement().grid_track_placement();
  962. }
  963. CSS::GridTrackPlacement StyleProperties::grid_column_start() const
  964. {
  965. auto value = property(CSS::PropertyID::GridColumnStart);
  966. return value->as_grid_track_placement().grid_track_placement();
  967. }
  968. CSS::GridTrackPlacement StyleProperties::grid_row_end() const
  969. {
  970. auto value = property(CSS::PropertyID::GridRowEnd);
  971. return value->as_grid_track_placement().grid_track_placement();
  972. }
  973. CSS::GridTrackPlacement StyleProperties::grid_row_start() const
  974. {
  975. auto value = property(CSS::PropertyID::GridRowStart);
  976. return value->as_grid_track_placement().grid_track_placement();
  977. }
  978. Optional<CSS::BorderCollapse> StyleProperties::border_collapse() const
  979. {
  980. auto value = property(CSS::PropertyID::BorderCollapse);
  981. return keyword_to_border_collapse(value->to_keyword());
  982. }
  983. Vector<Vector<String>> StyleProperties::grid_template_areas() const
  984. {
  985. auto value = property(CSS::PropertyID::GridTemplateAreas);
  986. return value->as_grid_template_area().grid_template_area();
  987. }
  988. Optional<CSS::ObjectFit> StyleProperties::object_fit() const
  989. {
  990. auto value = property(CSS::PropertyID::ObjectFit);
  991. return keyword_to_object_fit(value->to_keyword());
  992. }
  993. CSS::ObjectPosition StyleProperties::object_position() const
  994. {
  995. auto value = property(CSS::PropertyID::ObjectPosition);
  996. auto const& position = value->as_position();
  997. CSS::ObjectPosition object_position;
  998. auto const& edge_x = position.edge_x();
  999. auto const& edge_y = position.edge_y();
  1000. if (edge_x->is_edge()) {
  1001. auto const& edge = edge_x->as_edge();
  1002. object_position.edge_x = edge.edge();
  1003. object_position.offset_x = edge.offset();
  1004. }
  1005. if (edge_y->is_edge()) {
  1006. auto const& edge = edge_y->as_edge();
  1007. object_position.edge_y = edge.edge();
  1008. object_position.offset_y = edge.offset();
  1009. }
  1010. return object_position;
  1011. }
  1012. Optional<CSS::TableLayout> StyleProperties::table_layout() const
  1013. {
  1014. auto value = property(CSS::PropertyID::TableLayout);
  1015. return keyword_to_table_layout(value->to_keyword());
  1016. }
  1017. Optional<CSS::Direction> StyleProperties::direction() const
  1018. {
  1019. auto value = property(CSS::PropertyID::Direction);
  1020. return keyword_to_direction(value->to_keyword());
  1021. }
  1022. Optional<CSS::MaskType> StyleProperties::mask_type() const
  1023. {
  1024. auto value = property(CSS::PropertyID::MaskType);
  1025. return keyword_to_mask_type(value->to_keyword());
  1026. }
  1027. Color StyleProperties::stop_color() const
  1028. {
  1029. auto value = property(CSS::PropertyID::StopColor);
  1030. if (value->is_keyword()) {
  1031. // Workaround lack of layout node to resolve current color.
  1032. auto& keyword = value->as_keyword();
  1033. if (keyword.keyword() == CSS::Keyword::Currentcolor)
  1034. value = property(CSS::PropertyID::Color);
  1035. }
  1036. if (value->has_color()) {
  1037. // FIXME: This is used by the SVGStopElement, which does not participate in layout,
  1038. // so can't pass a layout node (so can't resolve some colors, e.g. palette ones)
  1039. return value->to_color({});
  1040. }
  1041. return Color::Black;
  1042. }
  1043. void StyleProperties::set_math_depth(int math_depth)
  1044. {
  1045. m_data->m_math_depth = math_depth;
  1046. // Make our children inherit our computed value, not our specified value.
  1047. set_property(PropertyID::MathDepth, MathDepthStyleValue::create_integer(IntegerStyleValue::create(math_depth)));
  1048. }
  1049. QuotesData StyleProperties::quotes() const
  1050. {
  1051. auto value = property(CSS::PropertyID::Quotes);
  1052. if (value->is_keyword()) {
  1053. switch (value->to_keyword()) {
  1054. case Keyword::Auto:
  1055. return QuotesData { .type = QuotesData::Type::Auto };
  1056. case Keyword::None:
  1057. return QuotesData { .type = QuotesData::Type::None };
  1058. default:
  1059. break;
  1060. }
  1061. }
  1062. if (value->is_value_list()) {
  1063. auto& value_list = value->as_value_list();
  1064. QuotesData quotes_data { .type = QuotesData::Type::Specified };
  1065. VERIFY(value_list.size() % 2 == 0);
  1066. for (auto i = 0u; i < value_list.size(); i += 2) {
  1067. quotes_data.strings.empend(
  1068. value_list.value_at(i, false)->as_string().string_value(),
  1069. value_list.value_at(i + 1, false)->as_string().string_value());
  1070. }
  1071. return quotes_data;
  1072. }
  1073. return InitialValues::quotes();
  1074. }
  1075. Vector<CounterData> StyleProperties::counter_data(PropertyID property_id) const
  1076. {
  1077. auto value = property(property_id);
  1078. if (value->is_counter_definitions()) {
  1079. auto& counter_definitions = value->as_counter_definitions().counter_definitions();
  1080. Vector<CounterData> result;
  1081. for (auto& counter : counter_definitions) {
  1082. CounterData data {
  1083. .name = counter.name,
  1084. .is_reversed = counter.is_reversed,
  1085. .value = {},
  1086. };
  1087. if (counter.value) {
  1088. if (counter.value->is_integer()) {
  1089. data.value = AK::clamp_to<i32>(counter.value->as_integer().integer());
  1090. } else if (counter.value->is_math()) {
  1091. auto maybe_int = counter.value->as_math().resolve_integer();
  1092. if (maybe_int.has_value())
  1093. data.value = AK::clamp_to<i32>(*maybe_int);
  1094. } else {
  1095. dbgln("Unimplemented type for {} integer value: '{}'", string_from_property_id(property_id), counter.value->to_string());
  1096. }
  1097. }
  1098. result.append(move(data));
  1099. }
  1100. return result;
  1101. }
  1102. if (value->to_keyword() == Keyword::None)
  1103. return {};
  1104. dbgln("Unhandled type for {} value: '{}'", string_from_property_id(property_id), value->to_string());
  1105. return {};
  1106. }
  1107. Optional<CSS::ScrollbarWidth> StyleProperties::scrollbar_width() const
  1108. {
  1109. auto value = property(CSS::PropertyID::ScrollbarWidth);
  1110. return keyword_to_scrollbar_width(value->to_keyword());
  1111. }
  1112. }