StyleProperties.cpp 42 KB

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