ResolvedCSSStyleDeclaration.cpp 34 KB

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