StyleProperties.cpp 28 KB

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