StyleProperties.cpp 46 KB

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