StyleProperties.cpp 28 KB

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