ResolvedCSSStyleDeclaration.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Debug.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <LibWeb/CSS/ResolvedCSSStyleDeclaration.h>
  10. #include <LibWeb/CSS/StyleComputer.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/DOM/Element.h>
  13. namespace Web::CSS {
  14. ResolvedCSSStyleDeclaration::ResolvedCSSStyleDeclaration(DOM::Element& element)
  15. : m_element(element)
  16. {
  17. }
  18. ResolvedCSSStyleDeclaration::~ResolvedCSSStyleDeclaration()
  19. {
  20. }
  21. size_t ResolvedCSSStyleDeclaration::length() const
  22. {
  23. return 0;
  24. }
  25. String ResolvedCSSStyleDeclaration::item(size_t index) const
  26. {
  27. (void)index;
  28. return {};
  29. }
  30. static CSS::ValueID to_css_value_id(CSS::BoxSizing value)
  31. {
  32. switch (value) {
  33. case CSS::BoxSizing::BorderBox:
  34. return CSS::ValueID::BorderBox;
  35. case CSS::BoxSizing::ContentBox:
  36. return CSS::ValueID::ContentBox;
  37. }
  38. VERIFY_NOT_REACHED();
  39. }
  40. static RefPtr<StyleValue> style_value_for_display(CSS::Display display)
  41. {
  42. if (display.is_none())
  43. return IdentifierStyleValue::create(CSS::ValueID::None);
  44. if (display.it_outside_and_inside()) {
  45. NonnullRefPtrVector<StyleValue> values;
  46. switch (display.outside()) {
  47. case CSS::Display::Outside::Inline:
  48. values.append(IdentifierStyleValue::create(CSS::ValueID::Inline));
  49. break;
  50. case CSS::Display::Outside::Block:
  51. values.append(IdentifierStyleValue::create(CSS::ValueID::Block));
  52. break;
  53. case CSS::Display::Outside::RunIn:
  54. values.append(IdentifierStyleValue::create(CSS::ValueID::RunIn));
  55. break;
  56. }
  57. switch (display.inside()) {
  58. case CSS::Display::Inside::Flow:
  59. values.append(IdentifierStyleValue::create(CSS::ValueID::Flow));
  60. break;
  61. case CSS::Display::Inside::FlowRoot:
  62. values.append(IdentifierStyleValue::create(CSS::ValueID::FlowRoot));
  63. break;
  64. case CSS::Display::Inside::Table:
  65. values.append(IdentifierStyleValue::create(CSS::ValueID::Table));
  66. break;
  67. case CSS::Display::Inside::Flex:
  68. values.append(IdentifierStyleValue::create(CSS::ValueID::Flex));
  69. break;
  70. case CSS::Display::Inside::Grid:
  71. values.append(IdentifierStyleValue::create(CSS::ValueID::Grid));
  72. break;
  73. case CSS::Display::Inside::Ruby:
  74. values.append(IdentifierStyleValue::create(CSS::ValueID::Ruby));
  75. break;
  76. }
  77. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  78. }
  79. if (display.is_internal()) {
  80. switch (display.internal()) {
  81. case CSS::Display::Internal::TableRowGroup:
  82. return IdentifierStyleValue::create(CSS::ValueID::TableRowGroup);
  83. case CSS::Display::Internal::TableHeaderGroup:
  84. return IdentifierStyleValue::create(CSS::ValueID::TableHeaderGroup);
  85. case CSS::Display::Internal::TableFooterGroup:
  86. return IdentifierStyleValue::create(CSS::ValueID::TableFooterGroup);
  87. case CSS::Display::Internal::TableRow:
  88. return IdentifierStyleValue::create(CSS::ValueID::TableRow);
  89. case CSS::Display::Internal::TableCell:
  90. return IdentifierStyleValue::create(CSS::ValueID::TableCell);
  91. case CSS::Display::Internal::TableColumnGroup:
  92. return IdentifierStyleValue::create(CSS::ValueID::TableColumnGroup);
  93. case CSS::Display::Internal::TableColumn:
  94. return IdentifierStyleValue::create(CSS::ValueID::TableColumn);
  95. case CSS::Display::Internal::TableCaption:
  96. return IdentifierStyleValue::create(CSS::ValueID::TableCaption);
  97. case CSS::Display::Internal::RubyBase:
  98. return IdentifierStyleValue::create(CSS::ValueID::RubyBase);
  99. case CSS::Display::Internal::RubyText:
  100. return IdentifierStyleValue::create(CSS::ValueID::RubyText);
  101. case CSS::Display::Internal::RubyBaseContainer:
  102. return IdentifierStyleValue::create(CSS::ValueID::RubyBaseContainer);
  103. case CSS::Display::Internal::RubyTextContainer:
  104. return IdentifierStyleValue::create(CSS::ValueID::RubyTextContainer);
  105. }
  106. }
  107. TODO();
  108. }
  109. static CSS::ValueID to_css_value_id(CSS::Float value)
  110. {
  111. switch (value) {
  112. case Float::None:
  113. return CSS::ValueID::None;
  114. case Float::Left:
  115. return CSS::ValueID::Left;
  116. case Float::Right:
  117. return CSS::ValueID::Right;
  118. }
  119. VERIFY_NOT_REACHED();
  120. }
  121. static CSS::ValueID to_css_value_id(CSS::Clear value)
  122. {
  123. switch (value) {
  124. case Clear::None:
  125. return CSS::ValueID::None;
  126. case Clear::Left:
  127. return CSS::ValueID::Left;
  128. case Clear::Right:
  129. return CSS::ValueID::Right;
  130. case Clear::Both:
  131. return CSS::ValueID::Both;
  132. }
  133. VERIFY_NOT_REACHED();
  134. }
  135. static CSS::ValueID to_css_value_id(CSS::TextDecorationLine value)
  136. {
  137. switch (value) {
  138. case TextDecorationLine::None:
  139. return CSS::ValueID::None;
  140. case TextDecorationLine::Underline:
  141. return CSS::ValueID::Underline;
  142. case TextDecorationLine::Overline:
  143. return CSS::ValueID::Overline;
  144. case TextDecorationLine::LineThrough:
  145. return CSS::ValueID::LineThrough;
  146. case TextDecorationLine::Blink:
  147. return CSS::ValueID::Blink;
  148. }
  149. VERIFY_NOT_REACHED();
  150. }
  151. static CSS::ValueID to_css_value_id(CSS::TextDecorationStyle value)
  152. {
  153. switch (value) {
  154. case TextDecorationStyle::Solid:
  155. return CSS::ValueID::Solid;
  156. case TextDecorationStyle::Double:
  157. return CSS::ValueID::Double;
  158. case TextDecorationStyle::Dotted:
  159. return CSS::ValueID::Dotted;
  160. case TextDecorationStyle::Dashed:
  161. return CSS::ValueID::Dashed;
  162. case TextDecorationStyle::Wavy:
  163. return CSS::ValueID::Wavy;
  164. }
  165. VERIFY_NOT_REACHED();
  166. }
  167. static CSS::ValueID to_css_value_id(CSS::Cursor value)
  168. {
  169. switch (value) {
  170. case Cursor::Auto:
  171. return CSS::ValueID::Auto;
  172. case Cursor::Default:
  173. return CSS::ValueID::Default;
  174. case Cursor::None:
  175. return CSS::ValueID::None;
  176. case Cursor::ContextMenu:
  177. return CSS::ValueID::ContextMenu;
  178. case Cursor::Help:
  179. return CSS::ValueID::Help;
  180. case Cursor::Pointer:
  181. return CSS::ValueID::Pointer;
  182. case Cursor::Progress:
  183. return CSS::ValueID::Progress;
  184. case Cursor::Wait:
  185. return CSS::ValueID::Wait;
  186. case Cursor::Cell:
  187. return CSS::ValueID::Cell;
  188. case Cursor::Crosshair:
  189. return CSS::ValueID::Crosshair;
  190. case Cursor::Text:
  191. return CSS::ValueID::Text;
  192. case Cursor::VerticalText:
  193. return CSS::ValueID::VerticalText;
  194. case Cursor::Alias:
  195. return CSS::ValueID::Alias;
  196. case Cursor::Copy:
  197. return CSS::ValueID::Copy;
  198. case Cursor::Move:
  199. return CSS::ValueID::Move;
  200. case Cursor::NoDrop:
  201. return CSS::ValueID::NoDrop;
  202. case Cursor::NotAllowed:
  203. return CSS::ValueID::NotAllowed;
  204. case Cursor::Grab:
  205. return CSS::ValueID::Grab;
  206. case Cursor::Grabbing:
  207. return CSS::ValueID::Grabbing;
  208. case Cursor::EResize:
  209. return CSS::ValueID::EResize;
  210. case Cursor::NResize:
  211. return CSS::ValueID::NResize;
  212. case Cursor::NeResize:
  213. return CSS::ValueID::NeResize;
  214. case Cursor::NwResize:
  215. return CSS::ValueID::NwResize;
  216. case Cursor::SResize:
  217. return CSS::ValueID::SResize;
  218. case Cursor::SeResize:
  219. return CSS::ValueID::SeResize;
  220. case Cursor::SwResize:
  221. return CSS::ValueID::SwResize;
  222. case Cursor::WResize:
  223. return CSS::ValueID::WResize;
  224. case Cursor::EwResize:
  225. return CSS::ValueID::EwResize;
  226. case Cursor::NsResize:
  227. return CSS::ValueID::NsResize;
  228. case Cursor::NeswResize:
  229. return CSS::ValueID::NeswResize;
  230. case Cursor::NwseResize:
  231. return CSS::ValueID::NwseResize;
  232. case Cursor::ColResize:
  233. return CSS::ValueID::ColResize;
  234. case Cursor::RowResize:
  235. return CSS::ValueID::RowResize;
  236. case Cursor::AllScroll:
  237. return CSS::ValueID::AllScroll;
  238. case Cursor::ZoomIn:
  239. return CSS::ValueID::ZoomIn;
  240. case Cursor::ZoomOut:
  241. return CSS::ValueID::ZoomOut;
  242. }
  243. VERIFY_NOT_REACHED();
  244. }
  245. static CSS::ValueID to_css_value_id(CSS::TextAlign value)
  246. {
  247. switch (value) {
  248. case TextAlign::Left:
  249. return CSS::ValueID::Left;
  250. case TextAlign::Center:
  251. return CSS::ValueID::Center;
  252. case TextAlign::Right:
  253. return CSS::ValueID::Right;
  254. case TextAlign::Justify:
  255. return CSS::ValueID::Justify;
  256. case TextAlign::LibwebCenter:
  257. return CSS::ValueID::LibwebCenter;
  258. }
  259. VERIFY_NOT_REACHED();
  260. }
  261. static CSS::ValueID to_css_value_id(CSS::TextTransform value)
  262. {
  263. switch (value) {
  264. case TextTransform::None:
  265. return CSS::ValueID::None;
  266. case TextTransform::Capitalize:
  267. return CSS::ValueID::Capitalize;
  268. case TextTransform::Uppercase:
  269. return CSS::ValueID::Uppercase;
  270. case TextTransform::Lowercase:
  271. return CSS::ValueID::Lowercase;
  272. case TextTransform::FullWidth:
  273. return CSS::ValueID::FullWidth;
  274. case TextTransform::FullSizeKana:
  275. return CSS::ValueID::FullSizeKana;
  276. }
  277. VERIFY_NOT_REACHED();
  278. }
  279. static CSS::ValueID to_css_value_id(CSS::Position value)
  280. {
  281. switch (value) {
  282. case Position::Static:
  283. return CSS::ValueID::Static;
  284. case Position::Relative:
  285. return CSS::ValueID::Relative;
  286. case Position::Absolute:
  287. return CSS::ValueID::Absolute;
  288. case Position::Fixed:
  289. return CSS::ValueID::Fixed;
  290. case Position::Sticky:
  291. return CSS::ValueID::Sticky;
  292. }
  293. VERIFY_NOT_REACHED();
  294. }
  295. static CSS::ValueID to_css_value_id(CSS::WhiteSpace value)
  296. {
  297. switch (value) {
  298. case WhiteSpace::Normal:
  299. return CSS::ValueID::Normal;
  300. case WhiteSpace::Pre:
  301. return CSS::ValueID::Pre;
  302. case WhiteSpace::Nowrap:
  303. return CSS::ValueID::Nowrap;
  304. case WhiteSpace::PreLine:
  305. return CSS::ValueID::PreLine;
  306. case WhiteSpace::PreWrap:
  307. return CSS::ValueID::PreWrap;
  308. }
  309. VERIFY_NOT_REACHED();
  310. }
  311. static CSS::ValueID to_css_value_id(CSS::FlexDirection value)
  312. {
  313. switch (value) {
  314. case FlexDirection::Row:
  315. return CSS::ValueID::Row;
  316. case FlexDirection::RowReverse:
  317. return CSS::ValueID::RowReverse;
  318. case FlexDirection::Column:
  319. return CSS::ValueID::Column;
  320. case FlexDirection::ColumnReverse:
  321. return CSS::ValueID::ColumnReverse;
  322. }
  323. VERIFY_NOT_REACHED();
  324. }
  325. static CSS::ValueID to_css_value_id(CSS::FlexWrap value)
  326. {
  327. switch (value) {
  328. case FlexWrap::Nowrap:
  329. return CSS::ValueID::Nowrap;
  330. case FlexWrap::Wrap:
  331. return CSS::ValueID::Wrap;
  332. case FlexWrap::WrapReverse:
  333. return CSS::ValueID::WrapReverse;
  334. }
  335. VERIFY_NOT_REACHED();
  336. }
  337. static CSS::ValueID to_css_value_id(CSS::ImageRendering value)
  338. {
  339. switch (value) {
  340. case ImageRendering::Auto:
  341. return CSS::ValueID::Auto;
  342. case ImageRendering::Pixelated:
  343. return CSS::ValueID::Pixelated;
  344. }
  345. VERIFY_NOT_REACHED();
  346. }
  347. static CSS::ValueID to_css_value_id(CSS::JustifyContent value)
  348. {
  349. switch (value) {
  350. case JustifyContent::FlexStart:
  351. return CSS::ValueID::FlexStart;
  352. case JustifyContent::FlexEnd:
  353. return CSS::ValueID::FlexEnd;
  354. case JustifyContent::Center:
  355. return CSS::ValueID::Center;
  356. case JustifyContent::SpaceBetween:
  357. return CSS::ValueID::SpaceBetween;
  358. case JustifyContent::SpaceAround:
  359. return CSS::ValueID::SpaceAround;
  360. }
  361. VERIFY_NOT_REACHED();
  362. }
  363. static CSS::ValueID to_css_value_id(CSS::Overflow value)
  364. {
  365. switch (value) {
  366. case Overflow::Auto:
  367. return CSS::ValueID::Auto;
  368. case Overflow::Clip:
  369. return CSS::ValueID::Clip;
  370. case Overflow::Hidden:
  371. return CSS::ValueID::Hidden;
  372. case Overflow::Scroll:
  373. return CSS::ValueID::Scroll;
  374. case Overflow::Visible:
  375. return CSS::ValueID::Visible;
  376. }
  377. VERIFY_NOT_REACHED();
  378. }
  379. static CSS::ValueID to_css_value_id(CSS::VerticalAlign value)
  380. {
  381. switch (value) {
  382. case CSS::VerticalAlign::Baseline:
  383. return CSS::ValueID::Baseline;
  384. case CSS::VerticalAlign::Bottom:
  385. return CSS::ValueID::Bottom;
  386. case CSS::VerticalAlign::Middle:
  387. return CSS::ValueID::Middle;
  388. case CSS::VerticalAlign::Sub:
  389. return CSS::ValueID::Sub;
  390. case CSS::VerticalAlign::Super:
  391. return CSS::ValueID::Super;
  392. case CSS::VerticalAlign::TextBottom:
  393. return CSS::ValueID::TextBottom;
  394. case CSS::VerticalAlign::TextTop:
  395. return CSS::ValueID::TextTop;
  396. case CSS::VerticalAlign::Top:
  397. return CSS::ValueID::Top;
  398. }
  399. VERIFY_NOT_REACHED();
  400. }
  401. static CSS::ValueID to_css_value_id(CSS::ListStyleType value)
  402. {
  403. switch (value) {
  404. case ListStyleType::None:
  405. return CSS::ValueID::None;
  406. case ListStyleType::Disc:
  407. return CSS::ValueID::Disc;
  408. case ListStyleType::Circle:
  409. return CSS::ValueID::Circle;
  410. case ListStyleType::Square:
  411. return CSS::ValueID::Square;
  412. case ListStyleType::Decimal:
  413. return CSS::ValueID::Decimal;
  414. case ListStyleType::DecimalLeadingZero:
  415. return CSS::ValueID::DecimalLeadingZero;
  416. case ListStyleType::LowerAlpha:
  417. return CSS::ValueID::LowerAlpha;
  418. case ListStyleType::LowerLatin:
  419. return CSS::ValueID::LowerLatin;
  420. case ListStyleType::LowerRoman:
  421. return CSS::ValueID::LowerRoman;
  422. case ListStyleType::UpperAlpha:
  423. return CSS::ValueID::UpperAlpha;
  424. case ListStyleType::UpperLatin:
  425. return CSS::ValueID::UpperLatin;
  426. case ListStyleType::UpperRoman:
  427. return CSS::ValueID::UpperRoman;
  428. }
  429. VERIFY_NOT_REACHED();
  430. }
  431. static CSS::ValueID to_css_value_id(CSS::LineStyle value)
  432. {
  433. switch (value) {
  434. case CSS::LineStyle::None:
  435. return CSS::ValueID::None;
  436. case CSS::LineStyle::Hidden:
  437. return CSS::ValueID::Hidden;
  438. case CSS::LineStyle::Dotted:
  439. return CSS::ValueID::Dotted;
  440. case CSS::LineStyle::Dashed:
  441. return CSS::ValueID::Dashed;
  442. case CSS::LineStyle::Solid:
  443. return CSS::ValueID::Solid;
  444. case CSS::LineStyle::Double:
  445. return CSS::ValueID::Double;
  446. case CSS::LineStyle::Groove:
  447. return CSS::ValueID::Groove;
  448. case CSS::LineStyle::Ridge:
  449. return CSS::ValueID::Ridge;
  450. case CSS::LineStyle::Inset:
  451. return CSS::ValueID::Inset;
  452. case CSS::LineStyle::Outset:
  453. return CSS::ValueID::Outset;
  454. }
  455. VERIFY_NOT_REACHED();
  456. }
  457. static NonnullRefPtr<StyleValue> value_or_default(Optional<StyleProperty> property, NonnullRefPtr<StyleValue> default_style)
  458. {
  459. if (property.has_value())
  460. return property.value().value;
  461. return default_style;
  462. }
  463. static NonnullRefPtr<StyleValue> style_value_for_length_percentage(LengthPercentage const& length_percentage)
  464. {
  465. if (length_percentage.is_percentage())
  466. return PercentageStyleValue::create(length_percentage.percentage());
  467. return LengthStyleValue::create(length_percentage.length());
  468. }
  469. RefPtr<StyleValue> ResolvedCSSStyleDeclaration::style_value_for_property(Layout::NodeWithStyle const& layout_node, PropertyID property_id) const
  470. {
  471. switch (property_id) {
  472. case CSS::PropertyID::Float:
  473. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().float_()));
  474. case CSS::PropertyID::Clear:
  475. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().clear()));
  476. case CSS::PropertyID::Cursor:
  477. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().cursor()));
  478. case CSS::PropertyID::Display:
  479. return style_value_for_display(layout_node.computed_values().display());
  480. case CSS::PropertyID::ZIndex: {
  481. auto maybe_z_index = layout_node.computed_values().z_index();
  482. if (!maybe_z_index.has_value())
  483. return {};
  484. return NumericStyleValue::create_integer(maybe_z_index.release_value());
  485. }
  486. case CSS::PropertyID::TextAlign:
  487. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().text_align()));
  488. case CSS::PropertyID::TextDecorationLine:
  489. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().text_decoration_line()));
  490. case CSS::PropertyID::TextDecorationStyle:
  491. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().text_decoration_style()));
  492. case CSS::PropertyID::TextTransform:
  493. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().text_transform()));
  494. case CSS::PropertyID::Position:
  495. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().position()));
  496. case CSS::PropertyID::WhiteSpace:
  497. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().white_space()));
  498. case CSS::PropertyID::FlexDirection:
  499. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().flex_direction()));
  500. case CSS::PropertyID::FlexWrap:
  501. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().flex_wrap()));
  502. case CSS::PropertyID::FlexBasis: {
  503. switch (layout_node.computed_values().flex_basis().type) {
  504. case FlexBasis::Content:
  505. return IdentifierStyleValue::create(CSS::ValueID::Content);
  506. case FlexBasis::LengthPercentage:
  507. return style_value_for_length_percentage(*layout_node.computed_values().flex_basis().length_percentage);
  508. case FlexBasis::Auto:
  509. return IdentifierStyleValue::create(CSS::ValueID::Auto);
  510. default:
  511. VERIFY_NOT_REACHED();
  512. }
  513. break;
  514. case CSS::PropertyID::FlexGrow:
  515. return NumericStyleValue::create_float(layout_node.computed_values().flex_grow());
  516. case CSS::PropertyID::FlexShrink:
  517. return NumericStyleValue::create_float(layout_node.computed_values().flex_shrink());
  518. case CSS::PropertyID::Opacity:
  519. return NumericStyleValue::create_float(layout_node.computed_values().opacity());
  520. case CSS::PropertyID::ImageRendering:
  521. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().image_rendering()));
  522. case CSS::PropertyID::JustifyContent:
  523. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().justify_content()));
  524. case CSS::PropertyID::BoxShadow: {
  525. auto box_shadow_layers = layout_node.computed_values().box_shadow();
  526. if (box_shadow_layers.is_empty())
  527. return {};
  528. auto make_box_shadow_style_value = [](BoxShadowData const& data) {
  529. return BoxShadowStyleValue::create(data.color, data.offset_x, data.offset_y, data.blur_radius, data.spread_distance, data.placement);
  530. };
  531. if (box_shadow_layers.size() == 1)
  532. return make_box_shadow_style_value(box_shadow_layers.first());
  533. NonnullRefPtrVector<StyleValue> box_shadow;
  534. box_shadow.ensure_capacity(box_shadow_layers.size());
  535. for (auto const& layer : box_shadow_layers)
  536. box_shadow.append(make_box_shadow_style_value(layer));
  537. return StyleValueList::create(move(box_shadow), StyleValueList::Separator::Comma);
  538. }
  539. case CSS::PropertyID::Width:
  540. return style_value_for_length_percentage(layout_node.computed_values().width().value_or(Length::make_auto()));
  541. case CSS::PropertyID::MinWidth:
  542. if (!layout_node.computed_values().min_width().has_value())
  543. return IdentifierStyleValue::create(CSS::ValueID::Auto);
  544. return style_value_for_length_percentage(layout_node.computed_values().min_width().value());
  545. case CSS::PropertyID::MaxWidth:
  546. if (!layout_node.computed_values().max_width().has_value())
  547. return IdentifierStyleValue::create(CSS::ValueID::None);
  548. return style_value_for_length_percentage(layout_node.computed_values().max_width().value());
  549. case CSS::PropertyID::Height:
  550. return style_value_for_length_percentage(layout_node.computed_values().height().value_or(Length::make_auto()));
  551. case CSS::PropertyID::MinHeight:
  552. if (!layout_node.computed_values().min_height().has_value())
  553. return IdentifierStyleValue::create(CSS::ValueID::Auto);
  554. return style_value_for_length_percentage(layout_node.computed_values().min_height().value());
  555. case CSS::PropertyID::MaxHeight:
  556. if (!layout_node.computed_values().max_height().has_value())
  557. return IdentifierStyleValue::create(CSS::ValueID::None);
  558. return style_value_for_length_percentage(layout_node.computed_values().max_height().value());
  559. case CSS::PropertyID::Margin: {
  560. auto margin = layout_node.computed_values().margin();
  561. auto values = NonnullRefPtrVector<StyleValue> {};
  562. values.append(style_value_for_length_percentage(margin.top));
  563. values.append(style_value_for_length_percentage(margin.right));
  564. values.append(style_value_for_length_percentage(margin.bottom));
  565. values.append(style_value_for_length_percentage(margin.left));
  566. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  567. }
  568. case CSS::PropertyID::MarginTop:
  569. return style_value_for_length_percentage(layout_node.computed_values().margin().top);
  570. case CSS::PropertyID::MarginRight:
  571. return style_value_for_length_percentage(layout_node.computed_values().margin().right);
  572. case CSS::PropertyID::MarginBottom:
  573. return style_value_for_length_percentage(layout_node.computed_values().margin().bottom);
  574. case CSS::PropertyID::MarginLeft:
  575. return style_value_for_length_percentage(layout_node.computed_values().margin().left);
  576. case CSS::PropertyID::Padding: {
  577. auto padding = layout_node.computed_values().padding();
  578. auto values = NonnullRefPtrVector<StyleValue> {};
  579. values.append(style_value_for_length_percentage(padding.top));
  580. values.append(style_value_for_length_percentage(padding.right));
  581. values.append(style_value_for_length_percentage(padding.bottom));
  582. values.append(style_value_for_length_percentage(padding.left));
  583. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  584. }
  585. case CSS::PropertyID::PaddingTop:
  586. return style_value_for_length_percentage(layout_node.computed_values().padding().top);
  587. case CSS::PropertyID::PaddingRight:
  588. return style_value_for_length_percentage(layout_node.computed_values().padding().right);
  589. case CSS::PropertyID::PaddingBottom:
  590. return style_value_for_length_percentage(layout_node.computed_values().padding().bottom);
  591. case CSS::PropertyID::PaddingLeft:
  592. return style_value_for_length_percentage(layout_node.computed_values().padding().left);
  593. case CSS::PropertyID::BorderRadius: {
  594. auto maybe_top_left_radius = property(CSS::PropertyID::BorderTopLeftRadius);
  595. auto maybe_top_right_radius = property(CSS::PropertyID::BorderTopRightRadius);
  596. auto maybe_bottom_left_radius = property(CSS::PropertyID::BorderBottomLeftRadius);
  597. auto maybe_bottom_right_radius = property(CSS::PropertyID::BorderBottomRightRadius);
  598. RefPtr<BorderRadiusStyleValue> top_left_radius, top_right_radius, bottom_left_radius, bottom_right_radius;
  599. if (maybe_top_left_radius.has_value()) {
  600. VERIFY(maybe_top_left_radius.value().value->is_border_radius());
  601. top_left_radius = maybe_top_left_radius.value().value->as_border_radius();
  602. }
  603. if (maybe_top_right_radius.has_value()) {
  604. VERIFY(maybe_top_right_radius.value().value->is_border_radius());
  605. top_right_radius = maybe_top_right_radius.value().value->as_border_radius();
  606. }
  607. if (maybe_bottom_left_radius.has_value()) {
  608. VERIFY(maybe_bottom_left_radius.value().value->is_border_radius());
  609. bottom_left_radius = maybe_bottom_left_radius.value().value->as_border_radius();
  610. }
  611. if (maybe_bottom_right_radius.has_value()) {
  612. VERIFY(maybe_bottom_right_radius.value().value->is_border_radius());
  613. bottom_right_radius = maybe_bottom_right_radius.value().value->as_border_radius();
  614. }
  615. return CombinedBorderRadiusStyleValue::create(top_left_radius.release_nonnull(), top_right_radius.release_nonnull(), bottom_right_radius.release_nonnull(), bottom_left_radius.release_nonnull());
  616. }
  617. // FIXME: The two radius components are not yet stored, as we currently don't actually render them.
  618. case CSS::PropertyID::BorderBottomLeftRadius:
  619. return BorderRadiusStyleValue::create(layout_node.computed_values().border_bottom_left_radius(), layout_node.computed_values().border_bottom_left_radius());
  620. case CSS::PropertyID::BorderBottomRightRadius:
  621. return BorderRadiusStyleValue::create(layout_node.computed_values().border_bottom_right_radius(), layout_node.computed_values().border_bottom_right_radius());
  622. case CSS::PropertyID::BorderTopLeftRadius:
  623. return BorderRadiusStyleValue::create(layout_node.computed_values().border_top_left_radius(), layout_node.computed_values().border_top_left_radius());
  624. case CSS::PropertyID::BorderTopRightRadius:
  625. return BorderRadiusStyleValue::create(layout_node.computed_values().border_top_right_radius(), layout_node.computed_values().border_top_right_radius());
  626. case CSS::PropertyID::BorderTopWidth:
  627. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_top().width));
  628. case CSS::PropertyID::BorderRightWidth:
  629. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_right().width));
  630. case CSS::PropertyID::BorderBottomWidth:
  631. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_bottom().width));
  632. case CSS::PropertyID::BorderLeftWidth:
  633. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_left().width));
  634. case CSS::PropertyID::BorderTopColor:
  635. return ColorStyleValue::create(layout_node.computed_values().border_top().color);
  636. case CSS::PropertyID::BorderRightColor:
  637. return ColorStyleValue::create(layout_node.computed_values().border_right().color);
  638. case CSS::PropertyID::BorderBottomColor:
  639. return ColorStyleValue::create(layout_node.computed_values().border_bottom().color);
  640. case CSS::PropertyID::BorderLeftColor:
  641. return ColorStyleValue::create(layout_node.computed_values().border_left().color);
  642. case CSS::PropertyID::BorderTopStyle:
  643. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().border_top().line_style));
  644. case CSS::PropertyID::BorderRightStyle:
  645. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().border_right().line_style));
  646. case CSS::PropertyID::BorderBottomStyle:
  647. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().border_bottom().line_style));
  648. case CSS::PropertyID::BorderLeftStyle:
  649. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().border_left().line_style));
  650. case CSS::PropertyID::BorderTop: {
  651. auto border = layout_node.computed_values().border_top();
  652. auto width = LengthStyleValue::create(Length::make_px(border.width));
  653. auto style = IdentifierStyleValue::create(to_css_value_id(border.line_style));
  654. auto color = ColorStyleValue::create(border.color);
  655. return BorderStyleValue::create(width, style, color);
  656. }
  657. case CSS::PropertyID::BorderRight: {
  658. auto border = layout_node.computed_values().border_right();
  659. auto width = LengthStyleValue::create(Length::make_px(border.width));
  660. auto style = IdentifierStyleValue::create(to_css_value_id(border.line_style));
  661. auto color = ColorStyleValue::create(border.color);
  662. return BorderStyleValue::create(width, style, color);
  663. }
  664. case CSS::PropertyID::BorderBottom: {
  665. auto border = layout_node.computed_values().border_bottom();
  666. auto width = LengthStyleValue::create(Length::make_px(border.width));
  667. auto style = IdentifierStyleValue::create(to_css_value_id(border.line_style));
  668. auto color = ColorStyleValue::create(border.color);
  669. return BorderStyleValue::create(width, style, color);
  670. }
  671. case CSS::PropertyID::BorderLeft: {
  672. auto border = layout_node.computed_values().border_left();
  673. auto width = LengthStyleValue::create(Length::make_px(border.width));
  674. auto style = IdentifierStyleValue::create(to_css_value_id(border.line_style));
  675. auto color = ColorStyleValue::create(border.color);
  676. return BorderStyleValue::create(width, style, color);
  677. }
  678. case CSS::PropertyID::OverflowX:
  679. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().overflow_x()));
  680. case CSS::PropertyID::OverflowY:
  681. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().overflow_y()));
  682. case CSS::PropertyID::Color:
  683. return ColorStyleValue::create(layout_node.computed_values().color());
  684. case PropertyID::BackgroundColor:
  685. return ColorStyleValue::create(layout_node.computed_values().background_color());
  686. case CSS::PropertyID::Background: {
  687. auto maybe_background_color = property(CSS::PropertyID::BackgroundColor);
  688. auto maybe_background_image = property(CSS::PropertyID::BackgroundImage);
  689. auto maybe_background_position = property(CSS::PropertyID::BackgroundPosition);
  690. auto maybe_background_size = property(CSS::PropertyID::BackgroundSize);
  691. auto maybe_background_repeat = property(CSS::PropertyID::BackgroundRepeat);
  692. auto maybe_background_attachment = property(CSS::PropertyID::BackgroundAttachment);
  693. auto maybe_background_origin = property(CSS::PropertyID::BackgroundOrigin);
  694. auto maybe_background_clip = property(CSS::PropertyID::BackgroundClip);
  695. return BackgroundStyleValue::create(
  696. value_or_default(maybe_background_color, InitialStyleValue::the()),
  697. value_or_default(maybe_background_image, IdentifierStyleValue::create(CSS::ValueID::None)),
  698. value_or_default(maybe_background_position, PositionStyleValue::create(PositionEdge::Left, Length::make_px(0), PositionEdge::Top, Length::make_px(0))),
  699. value_or_default(maybe_background_size, IdentifierStyleValue::create(CSS::ValueID::Auto)),
  700. value_or_default(maybe_background_repeat, BackgroundRepeatStyleValue::create(CSS::Repeat::Repeat, CSS::Repeat::Repeat)),
  701. value_or_default(maybe_background_attachment, IdentifierStyleValue::create(CSS::ValueID::Scroll)),
  702. value_or_default(maybe_background_origin, IdentifierStyleValue::create(CSS::ValueID::PaddingBox)),
  703. value_or_default(maybe_background_clip, IdentifierStyleValue::create(CSS::ValueID::BorderBox)));
  704. }
  705. case CSS::PropertyID::VerticalAlign:
  706. if (auto const* length_percentage = layout_node.computed_values().vertical_align().get_pointer<CSS::LengthPercentage>()) {
  707. if (length_percentage->is_length())
  708. return LengthStyleValue::create(length_percentage->length());
  709. if (length_percentage->is_percentage())
  710. return PercentageStyleValue::create(length_percentage->percentage());
  711. VERIFY_NOT_REACHED();
  712. }
  713. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().vertical_align().get<CSS::VerticalAlign>()));
  714. case CSS::PropertyID::ListStyleType:
  715. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().list_style_type()));
  716. case CSS::PropertyID::BoxSizing:
  717. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().box_sizing()));
  718. case CSS::PropertyID::Invalid:
  719. return IdentifierStyleValue::create(CSS::ValueID::Invalid);
  720. case CSS::PropertyID::Custom:
  721. dbgln_if(LIBWEB_CSS_DEBUG, "Computed style for custom properties was requested (?)");
  722. return {};
  723. default:
  724. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Computed style for the '{}' property was requested", string_from_property_id(property_id));
  725. return {};
  726. }
  727. }
  728. }
  729. Optional<StyleProperty> ResolvedCSSStyleDeclaration::property(PropertyID property_id) const
  730. {
  731. const_cast<DOM::Document&>(m_element->document()).ensure_layout();
  732. if (!m_element->layout_node()) {
  733. auto style = m_element->document().style_computer().compute_style(const_cast<DOM::Element&>(*m_element));
  734. if (auto maybe_property = style->property(property_id); maybe_property.has_value()) {
  735. return StyleProperty {
  736. .property_id = property_id,
  737. .value = maybe_property.release_value(),
  738. };
  739. }
  740. return {};
  741. }
  742. auto& layout_node = *m_element->layout_node();
  743. auto value = style_value_for_property(layout_node, property_id);
  744. if (!value)
  745. return {};
  746. return StyleProperty {
  747. .property_id = property_id,
  748. .value = value.release_nonnull(),
  749. };
  750. }
  751. bool ResolvedCSSStyleDeclaration::set_property(PropertyID, StringView)
  752. {
  753. return false;
  754. }
  755. String ResolvedCSSStyleDeclaration::serialized() const
  756. {
  757. // https://www.w3.org/TR/cssom/#dom-cssstyledeclaration-csstext
  758. // If the computed flag is set, then return the empty string.
  759. // NOTE: ResolvedCSSStyleDeclaration is something you would only get from window.getComputedStyle(),
  760. // which returns what the spec calls "resolved style". The "computed flag" is always set here.
  761. return String::empty();
  762. }
  763. }