StyleProperties.cpp 43 KB

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