StyleProperties.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, 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/ContentStyleValue.h>
  13. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  25. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  26. #include <LibWeb/FontCache.h>
  27. #include <LibWeb/Layout/BlockContainer.h>
  28. #include <LibWeb/Layout/Node.h>
  29. #include <LibWeb/Platform/FontPlugin.h>
  30. namespace Web::CSS {
  31. StyleProperties::StyleProperties(StyleProperties const& other)
  32. : m_property_values(other.m_property_values)
  33. {
  34. if (other.m_font) {
  35. m_font = other.m_font->clone();
  36. } else {
  37. m_font = nullptr;
  38. }
  39. }
  40. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  41. {
  42. return adopt_ref(*new StyleProperties(*this));
  43. }
  44. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue const> value, CSS::CSSStyleDeclaration const* source_declaration)
  45. {
  46. m_property_values[to_underlying(id)] = StyleAndSourceDeclaration { move(value), source_declaration };
  47. }
  48. NonnullRefPtr<StyleValue const> StyleProperties::property(CSS::PropertyID property_id) const
  49. {
  50. auto value = m_property_values[to_underlying(property_id)];
  51. // By the time we call this method, all properties have values assigned.
  52. VERIFY(value.has_value());
  53. return value->style;
  54. }
  55. RefPtr<StyleValue const> StyleProperties::maybe_null_property(CSS::PropertyID property_id) const
  56. {
  57. auto value = m_property_values[to_underlying(property_id)];
  58. if (value.has_value())
  59. return value->style;
  60. return {};
  61. }
  62. CSS::CSSStyleDeclaration const* StyleProperties::property_source_declaration(CSS::PropertyID property_id) const
  63. {
  64. return m_property_values[to_underlying(property_id)].map([](auto& value) { return value.declaration; }).value_or(nullptr);
  65. }
  66. CSS::Size StyleProperties::size_value(CSS::PropertyID id) const
  67. {
  68. auto value = property(id);
  69. if (value->is_identifier()) {
  70. switch (value->to_identifier()) {
  71. case ValueID::Auto:
  72. return CSS::Size::make_auto();
  73. case ValueID::MinContent:
  74. return CSS::Size::make_min_content();
  75. case ValueID::MaxContent:
  76. return CSS::Size::make_max_content();
  77. case ValueID::FitContent:
  78. return CSS::Size::make_fit_content();
  79. case ValueID::None:
  80. return CSS::Size::make_none();
  81. default:
  82. VERIFY_NOT_REACHED();
  83. }
  84. }
  85. if (value->is_calculated())
  86. return CSS::Size::make_calculated(const_cast<CalculatedStyleValue&>(value->as_calculated()));
  87. if (value->is_percentage())
  88. return CSS::Size::make_percentage(value->as_percentage().percentage());
  89. if (value->is_length()) {
  90. auto length = value->as_length().length();
  91. if (length.is_auto())
  92. return CSS::Size::make_auto();
  93. return CSS::Size::make_length(length);
  94. }
  95. // FIXME: Support `fit-content(<length>)`
  96. dbgln("FIXME: Unsupported size value: `{}`, treating as `auto`", value->to_string());
  97. return CSS::Size::make_auto();
  98. }
  99. LengthPercentage StyleProperties::length_percentage_or_fallback(CSS::PropertyID id, LengthPercentage const& fallback) const
  100. {
  101. return length_percentage(id).value_or(fallback);
  102. }
  103. Optional<LengthPercentage> StyleProperties::length_percentage(CSS::PropertyID id) const
  104. {
  105. auto value = property(id);
  106. if (value->is_calculated())
  107. return LengthPercentage { const_cast<CalculatedStyleValue&>(value->as_calculated()) };
  108. if (value->is_percentage())
  109. return value->as_percentage().percentage();
  110. if (value->is_length())
  111. return value->as_length().length();
  112. if (value->has_auto())
  113. return LengthPercentage { Length::make_auto() };
  114. return {};
  115. }
  116. 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
  117. {
  118. LengthBox box;
  119. box.left() = length_percentage_or_fallback(left_id, default_value);
  120. box.top() = length_percentage_or_fallback(top_id, default_value);
  121. box.right() = length_percentage_or_fallback(right_id, default_value);
  122. box.bottom() = length_percentage_or_fallback(bottom_id, default_value);
  123. return box;
  124. }
  125. Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const
  126. {
  127. auto value = property(id);
  128. if (!value->has_color())
  129. return fallback;
  130. return value->to_color(node);
  131. }
  132. NonnullRefPtr<Gfx::Font const> StyleProperties::font_fallback(bool monospace, bool bold)
  133. {
  134. if (monospace && bold)
  135. return Platform::FontPlugin::the().default_fixed_width_font().bold_variant();
  136. if (monospace)
  137. return Platform::FontPlugin::the().default_fixed_width_font();
  138. if (bold)
  139. return Platform::FontPlugin::the().default_font().bold_variant();
  140. return Platform::FontPlugin::the().default_font();
  141. }
  142. // FIXME: This implementation is almost identical to line_height(Layout::Node) below. Maybe they can be combined somehow.
  143. CSSPixels StyleProperties::line_height(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const
  144. {
  145. auto line_height = property(CSS::PropertyID::LineHeight);
  146. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  147. return font_metrics.line_height;
  148. if (line_height->is_length()) {
  149. auto line_height_length = line_height->as_length().length();
  150. if (!line_height_length.is_auto())
  151. return line_height_length.to_px(viewport_rect, font_metrics, root_font_metrics);
  152. }
  153. if (line_height->is_number())
  154. return Length(line_height->as_number().number(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  155. if (line_height->is_percentage()) {
  156. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  157. auto& percentage = line_height->as_percentage().percentage();
  158. return Length(percentage.as_fraction(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  159. }
  160. if (line_height->is_calculated()) {
  161. // FIXME: Handle `line-height: calc(...)` despite not having a LayoutNode here.
  162. return font_metrics.line_height;
  163. }
  164. return font_metrics.line_height;
  165. }
  166. CSSPixels StyleProperties::line_height(Layout::Node const& layout_node) const
  167. {
  168. auto line_height = property(CSS::PropertyID::LineHeight);
  169. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  170. return layout_node.font().pixel_metrics().line_spacing();
  171. if (line_height->is_length()) {
  172. auto line_height_length = line_height->as_length().length();
  173. if (!line_height_length.is_auto())
  174. return line_height_length.to_px(layout_node);
  175. }
  176. if (line_height->is_number())
  177. return Length(line_height->as_number().number(), Length::Type::Em).to_px(layout_node);
  178. if (line_height->is_percentage()) {
  179. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  180. auto& percentage = line_height->as_percentage().percentage();
  181. return Length(percentage.as_fraction(), Length::Type::Em).to_px(layout_node);
  182. }
  183. if (line_height->is_calculated()) {
  184. if (line_height->as_calculated().resolves_to_number()) {
  185. auto resolved = line_height->as_calculated().resolve_number();
  186. if (!resolved.has_value()) {
  187. dbgln("FIXME: Failed to resolve calc() line-height (number): {}", line_height->as_calculated().to_string().release_value_but_fixme_should_propagate_errors());
  188. return layout_node.font().pixel_metrics().line_spacing();
  189. }
  190. return Length(resolved.value(), Length::Type::Em).to_px(layout_node);
  191. }
  192. auto resolved = line_height->as_calculated().resolve_length(layout_node);
  193. if (!resolved.has_value()) {
  194. dbgln("FIXME: Failed to resolve calc() line-height: {}", line_height->as_calculated().to_string().release_value_but_fixme_should_propagate_errors());
  195. return layout_node.font().pixel_metrics().line_spacing();
  196. }
  197. return resolved->to_px(layout_node);
  198. }
  199. return layout_node.font().pixel_metrics().line_spacing();
  200. }
  201. Optional<int> StyleProperties::z_index() const
  202. {
  203. auto value = property(CSS::PropertyID::ZIndex);
  204. if (value->has_auto())
  205. return {};
  206. if (value->is_integer()) {
  207. // Clamp z-index to the range of a signed 32-bit integer for consistency with other engines.
  208. auto integer = value->as_integer().integer();
  209. if (integer >= NumericLimits<int>::max())
  210. return NumericLimits<int>::max();
  211. if (integer <= NumericLimits<int>::min())
  212. return NumericLimits<int>::min();
  213. return static_cast<int>(integer);
  214. }
  215. return {};
  216. }
  217. static float resolve_opacity_value(CSS::StyleValue const& value)
  218. {
  219. float unclamped_opacity = 1.0f;
  220. if (value.is_number()) {
  221. unclamped_opacity = value.as_number().number();
  222. } else if (value.is_calculated()) {
  223. auto& calculated = value.as_calculated();
  224. if (calculated.resolves_to_percentage()) {
  225. auto maybe_percentage = value.as_calculated().resolve_percentage();
  226. if (maybe_percentage.has_value())
  227. unclamped_opacity = maybe_percentage->as_fraction();
  228. else
  229. dbgln("Unable to resolve calc() as opacity (percentage): {}", value.to_string());
  230. } else if (calculated.resolves_to_number()) {
  231. auto maybe_number = const_cast<CalculatedStyleValue&>(value.as_calculated()).resolve_number();
  232. if (maybe_number.has_value())
  233. unclamped_opacity = maybe_number.value();
  234. else
  235. dbgln("Unable to resolve calc() as opacity (number): {}", value.to_string());
  236. }
  237. } else if (value.is_percentage()) {
  238. unclamped_opacity = value.as_percentage().percentage().as_fraction();
  239. }
  240. return clamp(unclamped_opacity, 0.0f, 1.0f);
  241. }
  242. float StyleProperties::opacity() const
  243. {
  244. auto value = property(CSS::PropertyID::Opacity);
  245. return resolve_opacity_value(*value);
  246. }
  247. float StyleProperties::fill_opacity() const
  248. {
  249. auto value = property(CSS::PropertyID::FillOpacity);
  250. return resolve_opacity_value(*value);
  251. }
  252. float StyleProperties::stroke_opacity() const
  253. {
  254. auto value = property(CSS::PropertyID::StrokeOpacity);
  255. return resolve_opacity_value(*value);
  256. }
  257. float StyleProperties::stop_opacity() const
  258. {
  259. auto value = property(CSS::PropertyID::StopOpacity);
  260. return resolve_opacity_value(*value);
  261. }
  262. Optional<CSS::FillRule> StyleProperties::fill_rule() const
  263. {
  264. auto value = property(CSS::PropertyID::FillRule);
  265. return value_id_to_fill_rule(value->to_identifier());
  266. }
  267. Optional<CSS::FlexDirection> StyleProperties::flex_direction() const
  268. {
  269. auto value = property(CSS::PropertyID::FlexDirection);
  270. return value_id_to_flex_direction(value->to_identifier());
  271. }
  272. Optional<CSS::FlexWrap> StyleProperties::flex_wrap() const
  273. {
  274. auto value = property(CSS::PropertyID::FlexWrap);
  275. return value_id_to_flex_wrap(value->to_identifier());
  276. }
  277. Optional<CSS::FlexBasis> StyleProperties::flex_basis() const
  278. {
  279. auto value = property(CSS::PropertyID::FlexBasis);
  280. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::Content)
  281. return CSS::FlexBasisContent {};
  282. return size_value(CSS::PropertyID::FlexBasis);
  283. }
  284. float StyleProperties::flex_grow() const
  285. {
  286. auto value = property(CSS::PropertyID::FlexGrow);
  287. if (!value->is_number())
  288. return 0;
  289. return value->as_number().number();
  290. }
  291. float StyleProperties::flex_shrink() const
  292. {
  293. auto value = property(CSS::PropertyID::FlexShrink);
  294. if (!value->is_number())
  295. return 1;
  296. return value->as_number().number();
  297. }
  298. int StyleProperties::order() const
  299. {
  300. auto value = property(CSS::PropertyID::Order);
  301. if (!value->is_integer())
  302. return 0;
  303. return value->as_integer().integer();
  304. }
  305. Optional<CSS::ImageRendering> StyleProperties::image_rendering() const
  306. {
  307. auto value = property(CSS::PropertyID::ImageRendering);
  308. return value_id_to_image_rendering(value->to_identifier());
  309. }
  310. CSS::Length StyleProperties::border_spacing_horizontal() const
  311. {
  312. auto value = property(CSS::PropertyID::BorderSpacing);
  313. if (value->is_length())
  314. return value->as_length().length();
  315. auto const& list = value->as_value_list();
  316. return list.value_at(0, false)->as_length().length();
  317. }
  318. CSS::Length StyleProperties::border_spacing_vertical() const
  319. {
  320. auto value = property(CSS::PropertyID::BorderSpacing);
  321. if (value->is_length())
  322. return value->as_length().length();
  323. auto const& list = value->as_value_list();
  324. return list.value_at(1, false)->as_length().length();
  325. }
  326. Optional<CSS::CaptionSide> StyleProperties::caption_side() const
  327. {
  328. auto value = property(CSS::PropertyID::CaptionSide);
  329. return value_id_to_caption_side(value->to_identifier());
  330. }
  331. CSS::Clip StyleProperties::clip() const
  332. {
  333. auto value = property(CSS::PropertyID::Clip);
  334. if (!value->is_rect())
  335. return CSS::Clip::make_auto();
  336. return CSS::Clip(value->as_rect().rect());
  337. }
  338. Optional<CSS::JustifyContent> StyleProperties::justify_content() const
  339. {
  340. auto value = property(CSS::PropertyID::JustifyContent);
  341. return value_id_to_justify_content(value->to_identifier());
  342. }
  343. Vector<CSS::Transformation> StyleProperties::transformations() const
  344. {
  345. auto value = property(CSS::PropertyID::Transform);
  346. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::None)
  347. return {};
  348. if (!value->is_value_list())
  349. return {};
  350. auto& list = value->as_value_list();
  351. Vector<CSS::Transformation> transformations;
  352. for (auto& it : list.values()) {
  353. if (!it->is_transformation())
  354. return {};
  355. auto& transformation_style_value = it->as_transformation();
  356. CSS::Transformation transformation;
  357. transformation.function = transformation_style_value.transform_function();
  358. Vector<TransformValue> values;
  359. for (auto& transformation_value : transformation_style_value.values()) {
  360. if (transformation_value->is_calculated()) {
  361. auto& calculated = transformation_value->as_calculated();
  362. if (calculated.resolves_to_length()) {
  363. dbgln("FIXME: Unable to resolve length with no layout node! {}", calculated.to_string());
  364. } else if (calculated.resolves_to_percentage()) {
  365. values.append({ calculated.resolve_percentage().value() });
  366. } else if (calculated.resolves_to_number()) {
  367. values.append({ calculated.resolve_number().value() });
  368. } else if (calculated.resolves_to_angle()) {
  369. values.append({ calculated.resolve_angle().value() });
  370. } else {
  371. dbgln("FIXME: Unsupported calc value in transform! {}", calculated.to_string());
  372. }
  373. } else if (transformation_value->is_length()) {
  374. values.append({ transformation_value->as_length().length() });
  375. } else if (transformation_value->is_percentage()) {
  376. values.append({ transformation_value->as_percentage().percentage() });
  377. } else if (transformation_value->is_number()) {
  378. values.append({ transformation_value->as_number().number() });
  379. } else if (transformation_value->is_angle()) {
  380. values.append({ transformation_value->as_angle().angle() });
  381. } else {
  382. dbgln("FIXME: Unsupported value in transform! {}", transformation_value->to_string());
  383. }
  384. }
  385. transformation.values = move(values);
  386. transformations.append(move(transformation));
  387. }
  388. return transformations;
  389. }
  390. static Optional<LengthPercentage> length_percentage_for_style_value(StyleValue const& value)
  391. {
  392. if (value.is_length())
  393. return value.as_length().length();
  394. if (value.is_percentage())
  395. return value.as_percentage().percentage();
  396. return {};
  397. }
  398. CSS::TransformOrigin StyleProperties::transform_origin() const
  399. {
  400. auto value = property(CSS::PropertyID::TransformOrigin);
  401. if (!value->is_value_list() || value->as_value_list().size() != 2)
  402. return {};
  403. auto const& list = value->as_value_list();
  404. auto x_value = length_percentage_for_style_value(list.values()[0]);
  405. auto y_value = length_percentage_for_style_value(list.values()[1]);
  406. if (!x_value.has_value() || !y_value.has_value()) {
  407. return {};
  408. }
  409. return { x_value.value(), y_value.value() };
  410. }
  411. Optional<Color> StyleProperties::accent_color(Layout::NodeWithStyle const& node) const
  412. {
  413. auto value = property(CSS::PropertyID::AccentColor);
  414. if (value->has_color())
  415. return value->to_color(node);
  416. return {};
  417. }
  418. Optional<CSS::AlignContent> StyleProperties::align_content() const
  419. {
  420. auto value = property(CSS::PropertyID::AlignContent);
  421. return value_id_to_align_content(value->to_identifier());
  422. }
  423. Optional<CSS::AlignItems> StyleProperties::align_items() const
  424. {
  425. auto value = property(CSS::PropertyID::AlignItems);
  426. return value_id_to_align_items(value->to_identifier());
  427. }
  428. Optional<CSS::AlignSelf> StyleProperties::align_self() const
  429. {
  430. auto value = property(CSS::PropertyID::AlignSelf);
  431. return value_id_to_align_self(value->to_identifier());
  432. }
  433. Optional<CSS::Appearance> StyleProperties::appearance() const
  434. {
  435. auto value = property(CSS::PropertyID::Appearance);
  436. auto appearance = value_id_to_appearance(value->to_identifier());
  437. if (appearance.has_value()) {
  438. switch (*appearance) {
  439. // Note: All these compatibility values can be treated as 'auto'
  440. case CSS::Appearance::Textfield:
  441. case CSS::Appearance::MenulistButton:
  442. case CSS::Appearance::Searchfield:
  443. case CSS::Appearance::Textarea:
  444. case CSS::Appearance::PushButton:
  445. case CSS::Appearance::SliderHorizontal:
  446. case CSS::Appearance::Checkbox:
  447. case CSS::Appearance::Radio:
  448. case CSS::Appearance::SquareButton:
  449. case CSS::Appearance::Menulist:
  450. case CSS::Appearance::Listbox:
  451. case CSS::Appearance::Meter:
  452. case CSS::Appearance::ProgressBar:
  453. case CSS::Appearance::Button:
  454. appearance = CSS::Appearance::Auto;
  455. break;
  456. default:
  457. break;
  458. }
  459. }
  460. return appearance;
  461. }
  462. CSS::BackdropFilter StyleProperties::backdrop_filter() const
  463. {
  464. auto value = property(CSS::PropertyID::BackdropFilter);
  465. if (value->is_filter_value_list())
  466. return BackdropFilter(value->as_filter_value_list());
  467. return BackdropFilter::make_none();
  468. }
  469. Optional<CSS::Position> StyleProperties::position() const
  470. {
  471. auto value = property(CSS::PropertyID::Position);
  472. return value_id_to_position(value->to_identifier());
  473. }
  474. bool StyleProperties::operator==(StyleProperties const& other) const
  475. {
  476. if (m_property_values.size() != other.m_property_values.size())
  477. return false;
  478. for (size_t i = 0; i < m_property_values.size(); ++i) {
  479. auto const& my_style = m_property_values[i];
  480. auto const& other_style = other.m_property_values[i];
  481. if (!my_style.has_value()) {
  482. if (other_style.has_value())
  483. return false;
  484. continue;
  485. }
  486. if (!other_style.has_value())
  487. return false;
  488. auto const& my_value = *my_style->style;
  489. auto const& other_value = *other_style->style;
  490. if (my_value.type() != other_value.type())
  491. return false;
  492. if (my_value != other_value)
  493. return false;
  494. }
  495. return true;
  496. }
  497. Optional<CSS::TextAlign> StyleProperties::text_align() const
  498. {
  499. auto value = property(CSS::PropertyID::TextAlign);
  500. return value_id_to_text_align(value->to_identifier());
  501. }
  502. Optional<CSS::TextJustify> StyleProperties::text_justify() const
  503. {
  504. auto value = property(CSS::PropertyID::TextJustify);
  505. return value_id_to_text_justify(value->to_identifier());
  506. }
  507. Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
  508. {
  509. auto value = property(CSS::PropertyID::PointerEvents);
  510. return value_id_to_pointer_events(value->to_identifier());
  511. }
  512. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  513. {
  514. auto value = property(CSS::PropertyID::WhiteSpace);
  515. return value_id_to_white_space(value->to_identifier());
  516. }
  517. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  518. {
  519. auto value = property(property_id);
  520. return value_id_to_line_style(value->to_identifier());
  521. }
  522. Optional<CSS::Float> StyleProperties::float_() const
  523. {
  524. auto value = property(CSS::PropertyID::Float);
  525. return value_id_to_float(value->to_identifier());
  526. }
  527. Optional<CSS::Clear> StyleProperties::clear() const
  528. {
  529. auto value = property(CSS::PropertyID::Clear);
  530. return value_id_to_clear(value->to_identifier());
  531. }
  532. CSS::ContentData StyleProperties::content() const
  533. {
  534. auto value = property(CSS::PropertyID::Content);
  535. if (value->is_content()) {
  536. auto& content_style_value = value->as_content();
  537. CSS::ContentData content_data;
  538. // FIXME: The content is a list of things: strings, identifiers or functions that return strings, and images.
  539. // So it can't always be represented as a single String, but may have to be multiple boxes.
  540. // For now, we'll just assume strings since that is easiest.
  541. StringBuilder builder;
  542. for (auto const& item : content_style_value.content().values()) {
  543. if (item->is_string()) {
  544. builder.append(item->to_string().release_value_but_fixme_should_propagate_errors());
  545. } else {
  546. // TODO: Implement quotes, counters, images, and other things.
  547. }
  548. }
  549. content_data.type = ContentData::Type::String;
  550. content_data.data = builder.to_string().release_value_but_fixme_should_propagate_errors();
  551. if (content_style_value.has_alt_text()) {
  552. StringBuilder alt_text_builder;
  553. for (auto const& item : content_style_value.alt_text()->values()) {
  554. if (item->is_string()) {
  555. alt_text_builder.append(item->to_string().release_value_but_fixme_should_propagate_errors());
  556. } else {
  557. // TODO: Implement counters
  558. }
  559. }
  560. content_data.alt_text = alt_text_builder.to_string().release_value_but_fixme_should_propagate_errors();
  561. }
  562. return content_data;
  563. }
  564. switch (value->to_identifier()) {
  565. case ValueID::None:
  566. return { ContentData::Type::None };
  567. case ValueID::Normal:
  568. return { ContentData::Type::Normal };
  569. default:
  570. break;
  571. }
  572. return CSS::ContentData {};
  573. }
  574. Optional<CSS::Cursor> StyleProperties::cursor() const
  575. {
  576. auto value = property(CSS::PropertyID::Cursor);
  577. return value_id_to_cursor(value->to_identifier());
  578. }
  579. Optional<CSS::Visibility> StyleProperties::visibility() const
  580. {
  581. auto value = property(CSS::PropertyID::Visibility);
  582. if (!value->is_identifier())
  583. return {};
  584. return value_id_to_visibility(value->to_identifier());
  585. }
  586. Display StyleProperties::display() const
  587. {
  588. auto value = property(PropertyID::Display);
  589. if (value->is_display()) {
  590. return value->as_display().display();
  591. }
  592. return Display::from_short(Display::Short::Inline);
  593. }
  594. Vector<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  595. {
  596. auto value = property(CSS::PropertyID::TextDecorationLine);
  597. if (value->is_value_list()) {
  598. Vector<CSS::TextDecorationLine> lines;
  599. auto& values = value->as_value_list().values();
  600. for (auto const& item : values) {
  601. lines.append(value_id_to_text_decoration_line(item->to_identifier()).value());
  602. }
  603. return lines;
  604. }
  605. if (value->is_identifier() && value->to_identifier() == ValueID::None)
  606. return {};
  607. dbgln("FIXME: Unsupported value for text-decoration-line: {}", value->to_string());
  608. return {};
  609. }
  610. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  611. {
  612. auto value = property(CSS::PropertyID::TextDecorationStyle);
  613. return value_id_to_text_decoration_style(value->to_identifier());
  614. }
  615. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  616. {
  617. auto value = property(CSS::PropertyID::TextTransform);
  618. return value_id_to_text_transform(value->to_identifier());
  619. }
  620. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  621. {
  622. auto value = property(CSS::PropertyID::ListStyleType);
  623. return value_id_to_list_style_type(value->to_identifier());
  624. }
  625. Optional<CSS::ListStylePosition> StyleProperties::list_style_position() const
  626. {
  627. auto value = property(CSS::PropertyID::ListStylePosition);
  628. return value_id_to_list_style_position(value->to_identifier());
  629. }
  630. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  631. {
  632. return overflow(CSS::PropertyID::OverflowX);
  633. }
  634. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  635. {
  636. return overflow(CSS::PropertyID::OverflowY);
  637. }
  638. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  639. {
  640. auto value = property(property_id);
  641. return value_id_to_overflow(value->to_identifier());
  642. }
  643. Vector<ShadowData> StyleProperties::shadow(PropertyID property_id, Layout::Node const& layout_node) const
  644. {
  645. auto value = property(property_id);
  646. auto resolve_to_length = [&layout_node](NonnullRefPtr<StyleValue const> const& value) -> Optional<Length> {
  647. if (value->is_length())
  648. return value->as_length().length();
  649. if (value->is_calculated())
  650. return value->as_calculated().resolve_length(layout_node);
  651. return {};
  652. };
  653. auto make_shadow_data = [resolve_to_length](ShadowStyleValue const& value) -> Optional<ShadowData> {
  654. auto maybe_offset_x = resolve_to_length(value.offset_x());
  655. if (!maybe_offset_x.has_value())
  656. return {};
  657. auto maybe_offset_y = resolve_to_length(value.offset_y());
  658. if (!maybe_offset_y.has_value())
  659. return {};
  660. auto maybe_blur_radius = resolve_to_length(value.blur_radius());
  661. if (!maybe_blur_radius.has_value())
  662. return {};
  663. auto maybe_spread_distance = resolve_to_length(value.spread_distance());
  664. if (!maybe_spread_distance.has_value())
  665. return {};
  666. return ShadowData {
  667. value.color(),
  668. maybe_offset_x.release_value(),
  669. maybe_offset_y.release_value(),
  670. maybe_blur_radius.release_value(),
  671. maybe_spread_distance.release_value(),
  672. value.placement()
  673. };
  674. };
  675. if (value->is_value_list()) {
  676. auto const& value_list = value->as_value_list();
  677. Vector<ShadowData> shadow_data;
  678. shadow_data.ensure_capacity(value_list.size());
  679. for (auto const& layer_value : value_list.values()) {
  680. auto maybe_shadow_data = make_shadow_data(layer_value->as_shadow());
  681. if (!maybe_shadow_data.has_value())
  682. return {};
  683. shadow_data.append(maybe_shadow_data.release_value());
  684. }
  685. return shadow_data;
  686. }
  687. if (value->is_shadow()) {
  688. auto maybe_shadow_data = make_shadow_data(value->as_shadow());
  689. if (!maybe_shadow_data.has_value())
  690. return {};
  691. return { maybe_shadow_data.release_value() };
  692. }
  693. return {};
  694. }
  695. Vector<ShadowData> StyleProperties::box_shadow(Layout::Node const& layout_node) const
  696. {
  697. return shadow(PropertyID::BoxShadow, layout_node);
  698. }
  699. Vector<ShadowData> StyleProperties::text_shadow(Layout::Node const& layout_node) const
  700. {
  701. return shadow(PropertyID::TextShadow, layout_node);
  702. }
  703. Optional<CSS::BoxSizing> StyleProperties::box_sizing() const
  704. {
  705. auto value = property(CSS::PropertyID::BoxSizing);
  706. return value_id_to_box_sizing(value->to_identifier());
  707. }
  708. Variant<CSS::VerticalAlign, CSS::LengthPercentage> StyleProperties::vertical_align() const
  709. {
  710. auto value = property(CSS::PropertyID::VerticalAlign);
  711. if (value->is_identifier())
  712. return value_id_to_vertical_align(value->to_identifier()).release_value();
  713. if (value->is_length())
  714. return CSS::LengthPercentage(value->as_length().length());
  715. if (value->is_percentage())
  716. return CSS::LengthPercentage(value->as_percentage().percentage());
  717. if (value->is_calculated())
  718. return LengthPercentage { const_cast<CalculatedStyleValue&>(value->as_calculated()) };
  719. VERIFY_NOT_REACHED();
  720. }
  721. Optional<CSS::FontVariant> StyleProperties::font_variant() const
  722. {
  723. auto value = property(CSS::PropertyID::FontVariant);
  724. return value_id_to_font_variant(value->to_identifier());
  725. }
  726. CSS::GridTrackSizeList StyleProperties::grid_auto_columns() const
  727. {
  728. auto value = property(CSS::PropertyID::GridAutoColumns);
  729. return value->as_grid_track_size_list().grid_track_size_list();
  730. }
  731. CSS::GridTrackSizeList StyleProperties::grid_auto_rows() const
  732. {
  733. auto value = property(CSS::PropertyID::GridAutoRows);
  734. return value->as_grid_track_size_list().grid_track_size_list();
  735. }
  736. CSS::GridTrackSizeList StyleProperties::grid_template_columns() const
  737. {
  738. auto value = property(CSS::PropertyID::GridTemplateColumns);
  739. return value->as_grid_track_size_list().grid_track_size_list();
  740. }
  741. CSS::GridTrackSizeList StyleProperties::grid_template_rows() const
  742. {
  743. auto value = property(CSS::PropertyID::GridTemplateRows);
  744. return value->as_grid_track_size_list().grid_track_size_list();
  745. }
  746. CSS::GridTrackPlacement StyleProperties::grid_column_end() const
  747. {
  748. auto value = property(CSS::PropertyID::GridColumnEnd);
  749. return value->as_grid_track_placement().grid_track_placement();
  750. }
  751. CSS::GridTrackPlacement StyleProperties::grid_column_start() const
  752. {
  753. auto value = property(CSS::PropertyID::GridColumnStart);
  754. return value->as_grid_track_placement().grid_track_placement();
  755. }
  756. CSS::GridTrackPlacement StyleProperties::grid_row_end() const
  757. {
  758. auto value = property(CSS::PropertyID::GridRowEnd);
  759. return value->as_grid_track_placement().grid_track_placement();
  760. }
  761. CSS::GridTrackPlacement StyleProperties::grid_row_start() const
  762. {
  763. auto value = property(CSS::PropertyID::GridRowStart);
  764. return value->as_grid_track_placement().grid_track_placement();
  765. }
  766. Optional<CSS::BorderCollapse> StyleProperties::border_collapse() const
  767. {
  768. auto value = property(CSS::PropertyID::BorderCollapse);
  769. return value_id_to_border_collapse(value->to_identifier());
  770. }
  771. Vector<Vector<String>> StyleProperties::grid_template_areas() const
  772. {
  773. auto value = property(CSS::PropertyID::GridTemplateAreas);
  774. return value->as_grid_template_area().grid_template_area();
  775. }
  776. String StyleProperties::grid_area() const
  777. {
  778. auto value = property(CSS::PropertyID::GridArea);
  779. return value->as_string().to_string().release_value_but_fixme_should_propagate_errors();
  780. }
  781. Color StyleProperties::stop_color() const
  782. {
  783. auto value = property(CSS::PropertyID::StopColor);
  784. if (value->has_color()) {
  785. // FIXME: This is used by the SVGStopElement, which does not participate in layout,
  786. // so can't pass a layout node (so can't resolve some colors, e.g. palette ones or currentColor)
  787. return value->to_color({});
  788. }
  789. return Color::Black;
  790. }
  791. }