StyleProperties.cpp 43 KB

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