StyleProperties.cpp 27 KB

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