StyleValue.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobi@tobyase.de>
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/NonnullOwnPtr.h>
  10. #include <AK/NonnullOwnPtrVector.h>
  11. #include <AK/NonnullRefPtrVector.h>
  12. #include <AK/RefCounted.h>
  13. #include <AK/RefPtr.h>
  14. #include <AK/String.h>
  15. #include <AK/StringView.h>
  16. #include <AK/URL.h>
  17. #include <AK/Variant.h>
  18. #include <AK/Vector.h>
  19. #include <AK/WeakPtr.h>
  20. #include <LibGfx/Bitmap.h>
  21. #include <LibGfx/Color.h>
  22. #include <LibWeb/CSS/Length.h>
  23. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  24. #include <LibWeb/CSS/PropertyID.h>
  25. #include <LibWeb/CSS/ValueID.h>
  26. #include <LibWeb/Forward.h>
  27. #include <LibWeb/Loader/ImageResource.h>
  28. namespace Web::CSS {
  29. enum class Position {
  30. Static,
  31. Relative,
  32. Absolute,
  33. Fixed,
  34. Sticky,
  35. };
  36. enum class TextAlign {
  37. Left,
  38. Center,
  39. Right,
  40. Justify,
  41. LibwebCenter,
  42. };
  43. enum class TextDecorationLine {
  44. None,
  45. Underline,
  46. Overline,
  47. LineThrough,
  48. Blink,
  49. };
  50. enum class TextTransform {
  51. None,
  52. Capitalize,
  53. Uppercase,
  54. Lowercase,
  55. FullWidth,
  56. FullSizeKana,
  57. };
  58. enum class Display {
  59. None,
  60. Block,
  61. Inline,
  62. InlineBlock,
  63. ListItem,
  64. Table,
  65. TableRow,
  66. TableCell,
  67. TableHeaderGroup,
  68. TableRowGroup,
  69. TableFooterGroup,
  70. TableColumn,
  71. TableColumnGroup,
  72. TableCaption,
  73. Flex,
  74. };
  75. enum class FlexDirection {
  76. Row,
  77. RowReverse,
  78. Column,
  79. ColumnReverse,
  80. };
  81. enum class FlexWrap {
  82. Nowrap,
  83. Wrap,
  84. WrapReverse
  85. };
  86. enum class FlexBasis {
  87. Content,
  88. Length
  89. };
  90. enum class WhiteSpace {
  91. Normal,
  92. Pre,
  93. Nowrap,
  94. PreLine,
  95. PreWrap,
  96. };
  97. enum class Float {
  98. None,
  99. Left,
  100. Right,
  101. };
  102. enum class Clear {
  103. None,
  104. Left,
  105. Right,
  106. Both,
  107. };
  108. enum class Cursor {
  109. Auto,
  110. Default,
  111. None,
  112. ContextMenu,
  113. Help,
  114. Pointer,
  115. Progress,
  116. Wait,
  117. Cell,
  118. Crosshair,
  119. Text,
  120. VerticalText,
  121. Alias,
  122. Copy,
  123. Move,
  124. NoDrop,
  125. NotAllowed,
  126. Grab,
  127. Grabbing,
  128. EResize,
  129. NResize,
  130. NeResize,
  131. NwResize,
  132. SResize,
  133. SeResize,
  134. SwResize,
  135. WResize,
  136. EwResize,
  137. NsResize,
  138. NeswResize,
  139. NwseResize,
  140. ColResize,
  141. RowResize,
  142. AllScroll,
  143. ZoomIn,
  144. ZoomOut,
  145. };
  146. enum class LineStyle {
  147. None,
  148. Hidden,
  149. Dotted,
  150. Dashed,
  151. Solid,
  152. Double,
  153. Groove,
  154. Ridge,
  155. Inset,
  156. Outset,
  157. };
  158. enum class ListStyleType {
  159. None,
  160. Disc,
  161. Circle,
  162. Square,
  163. Decimal,
  164. DecimalLeadingZero,
  165. LowerAlpha,
  166. LowerLatin,
  167. LowerRoman,
  168. UpperAlpha,
  169. UpperLatin,
  170. UpperRoman,
  171. };
  172. enum class Overflow : u8 {
  173. Auto,
  174. Clip,
  175. Hidden,
  176. Scroll,
  177. Visible,
  178. };
  179. enum class Repeat : u8 {
  180. NoRepeat,
  181. Repeat,
  182. Round,
  183. Space,
  184. };
  185. enum class JustifyContent {
  186. FlexStart,
  187. FlexEnd,
  188. Center,
  189. SpaceBetween,
  190. SpaceAround,
  191. };
  192. class StyleValue : public RefCounted<StyleValue> {
  193. public:
  194. virtual ~StyleValue();
  195. enum class Type {
  196. Invalid,
  197. Inherit,
  198. Initial,
  199. String,
  200. Length,
  201. Color,
  202. Identifier,
  203. Image,
  204. Position,
  205. CustomProperty,
  206. Numeric,
  207. ValueList,
  208. Calculated,
  209. BoxShadow,
  210. };
  211. Type type() const { return m_type; }
  212. bool is_inherit() const { return type() == Type::Inherit; }
  213. bool is_initial() const { return type() == Type::Initial; }
  214. bool is_color() const { return type() == Type::Color; }
  215. bool is_identifier() const { return type() == Type::Identifier; }
  216. bool is_image() const { return type() == Type::Image; }
  217. bool is_string() const { return type() == Type::String; }
  218. bool is_length() const { return type() == Type::Length; }
  219. bool is_position() const { return type() == Type::Position; }
  220. bool is_custom_property() const { return type() == Type::CustomProperty; }
  221. bool is_numeric() const { return type() == Type::Numeric; }
  222. bool is_value_list() const
  223. {
  224. return type() == Type::ValueList;
  225. }
  226. bool is_builtin_or_dynamic() const
  227. {
  228. return is_inherit() || is_initial() || is_custom_property();
  229. }
  230. bool is_box_shadow() const { return type() == Type::BoxShadow; }
  231. bool is_calculated() const
  232. {
  233. return type() == Type::Calculated;
  234. }
  235. virtual String to_string() const = 0;
  236. virtual Length to_length() const { return Length::make_auto(); }
  237. virtual Color to_color(const DOM::Document&) const { return {}; }
  238. CSS::ValueID to_identifier() const;
  239. virtual bool is_auto() const { return false; }
  240. bool operator==(const StyleValue& other) const { return equals(other); }
  241. bool operator!=(const StyleValue& other) const { return !(*this == other); }
  242. virtual bool equals(const StyleValue& other) const
  243. {
  244. if (type() != other.type())
  245. return false;
  246. return to_string() == other.to_string();
  247. }
  248. protected:
  249. explicit StyleValue(Type);
  250. private:
  251. Type m_type { Type::Invalid };
  252. };
  253. // FIXME: Allow for fallback
  254. class CustomStyleValue : public StyleValue {
  255. public:
  256. static NonnullRefPtr<CustomStyleValue> create(const String& custom_property_name)
  257. {
  258. return adopt_ref(*new CustomStyleValue(custom_property_name));
  259. }
  260. String custom_property_name() const { return m_custom_property_name; }
  261. String to_string() const override { return m_custom_property_name; }
  262. private:
  263. explicit CustomStyleValue(const String& custom_property_name)
  264. : StyleValue(Type::CustomProperty)
  265. , m_custom_property_name(custom_property_name)
  266. {
  267. }
  268. String m_custom_property_name {};
  269. };
  270. class NumericStyleValue : public StyleValue {
  271. public:
  272. static NonnullRefPtr<NumericStyleValue> create(float value)
  273. {
  274. return adopt_ref(*new NumericStyleValue(value));
  275. }
  276. float value() const { return m_value; }
  277. String to_string() const override { return String::formatted("{}", m_value); }
  278. private:
  279. explicit NumericStyleValue(float value)
  280. : StyleValue(Type::Numeric)
  281. , m_value(value)
  282. {
  283. }
  284. float m_value { 0 };
  285. };
  286. class StringStyleValue : public StyleValue {
  287. public:
  288. static NonnullRefPtr<StringStyleValue> create(const String& string)
  289. {
  290. return adopt_ref(*new StringStyleValue(string));
  291. }
  292. virtual ~StringStyleValue() override { }
  293. String to_string() const override { return m_string; }
  294. private:
  295. explicit StringStyleValue(const String& string)
  296. : StyleValue(Type::String)
  297. , m_string(string)
  298. {
  299. }
  300. String m_string;
  301. };
  302. class BoxShadowStyleValue : public StyleValue {
  303. public:
  304. static NonnullRefPtr<BoxShadowStyleValue> create(Length const& offset_x, Length const& offset_y, Length const& blur_radius, Color const& color)
  305. {
  306. return adopt_ref(*new BoxShadowStyleValue(offset_x, offset_y, blur_radius, color));
  307. }
  308. virtual ~BoxShadowStyleValue() override { }
  309. Length const& offset_x() const { return m_offset_x; }
  310. Length const& offset_y() const { return m_offset_y; }
  311. Length const& blur_radius() const { return m_blur_radius; }
  312. Color const& color() const { return m_color; }
  313. String to_string() const override { return String::formatted("BoxShadow offset_x: {}, offset_y: {}, blur_radius: {}, color: {}",
  314. m_offset_x.to_string(), m_offset_y.to_string(), m_blur_radius.to_string(), m_color.to_string()); }
  315. private:
  316. explicit BoxShadowStyleValue(Length const& offset_x, Length const& offset_y, Length const& blur_radius, Color const& color)
  317. : StyleValue(Type::BoxShadow)
  318. , m_offset_x(offset_x)
  319. , m_offset_y(offset_y)
  320. , m_blur_radius(blur_radius)
  321. , m_color(color)
  322. {
  323. }
  324. Length m_offset_x;
  325. Length m_offset_y;
  326. Length m_blur_radius;
  327. Color m_color;
  328. };
  329. class LengthStyleValue : public StyleValue {
  330. public:
  331. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  332. {
  333. return adopt_ref(*new LengthStyleValue(length));
  334. }
  335. virtual ~LengthStyleValue() override { }
  336. virtual String to_string() const override { return m_length.to_string(); }
  337. virtual Length to_length() const override { return m_length; }
  338. const Length& length() const { return m_length; }
  339. virtual bool is_auto() const override { return m_length.is_auto(); }
  340. virtual bool equals(const StyleValue& other) const override
  341. {
  342. if (type() != other.type())
  343. return false;
  344. return m_length == static_cast<const LengthStyleValue&>(other).m_length;
  345. }
  346. private:
  347. explicit LengthStyleValue(const Length& length)
  348. : StyleValue(Type::Length)
  349. , m_length(length)
  350. {
  351. }
  352. Length m_length;
  353. };
  354. class CalculatedStyleValue : public StyleValue {
  355. public:
  356. struct CalcSum;
  357. struct CalcSumPartWithOperator;
  358. struct CalcProduct;
  359. struct CalcProductPartWithOperator;
  360. struct CalcNumberSum;
  361. struct CalcNumberSumPartWithOperator;
  362. struct CalcNumberProduct;
  363. struct CalcNumberProductPartWithOperator;
  364. using CalcNumberValue = Variant<float, NonnullOwnPtr<CalcNumberSum>>;
  365. using CalcValue = Variant<float, CSS::Length, NonnullOwnPtr<CalcSum>>;
  366. // This represents that: https://drafts.csswg.org/css-values-3/#calc-syntax
  367. struct CalcSum {
  368. CalcSum(NonnullOwnPtr<CalcProduct> first_calc_product, NonnullOwnPtrVector<CalcSumPartWithOperator> additional)
  369. : first_calc_product(move(first_calc_product))
  370. , zero_or_more_additional_calc_products(move(additional)) {};
  371. NonnullOwnPtr<CalcProduct> first_calc_product;
  372. NonnullOwnPtrVector<CalcSumPartWithOperator> zero_or_more_additional_calc_products;
  373. };
  374. struct CalcNumberSum {
  375. CalcNumberSum(NonnullOwnPtr<CalcNumberProduct> first_calc_number_product, NonnullOwnPtrVector<CalcNumberSumPartWithOperator> additional)
  376. : first_calc_number_product(move(first_calc_number_product))
  377. , zero_or_more_additional_calc_number_products(move(additional)) {};
  378. NonnullOwnPtr<CalcNumberProduct> first_calc_number_product;
  379. NonnullOwnPtrVector<CalcNumberSumPartWithOperator> zero_or_more_additional_calc_number_products;
  380. };
  381. struct CalcProduct {
  382. CalcValue first_calc_value;
  383. NonnullOwnPtrVector<CalcProductPartWithOperator> zero_or_more_additional_calc_values;
  384. };
  385. struct CalcSumPartWithOperator {
  386. enum Operation {
  387. Add,
  388. Subtract,
  389. };
  390. CalcSumPartWithOperator(Operation op, NonnullOwnPtr<CalcProduct> calc_product)
  391. : op(op)
  392. , calc_product(move(calc_product)) {};
  393. Operation op;
  394. NonnullOwnPtr<CalcProduct> calc_product;
  395. };
  396. struct CalcProductPartWithOperator {
  397. enum {
  398. Multiply,
  399. Divide,
  400. } op;
  401. Variant<CalcValue, CalcNumberValue> value;
  402. };
  403. struct CalcNumberProduct {
  404. CalcNumberValue first_calc_number_value;
  405. NonnullOwnPtrVector<CalcNumberProductPartWithOperator> zero_or_more_additional_calc_number_values;
  406. };
  407. struct CalcNumberProductPartWithOperator {
  408. enum {
  409. Multiply,
  410. Divide,
  411. } op;
  412. CalcNumberValue value;
  413. };
  414. struct CalcNumberSumPartWithOperator {
  415. enum Operation {
  416. Add,
  417. Subtract,
  418. };
  419. CalcNumberSumPartWithOperator(Operation op, NonnullOwnPtr<CalcNumberProduct> calc_number_product)
  420. : op(op)
  421. , calc_number_product(move(calc_number_product)) {};
  422. Operation op;
  423. NonnullOwnPtr<CalcNumberProduct> calc_number_product;
  424. };
  425. static NonnullRefPtr<CalculatedStyleValue> create(String const& expression_string, NonnullOwnPtr<CalcSum> calc_sum)
  426. {
  427. return adopt_ref(*new CalculatedStyleValue(expression_string, move(calc_sum)));
  428. }
  429. String to_string() const override { return m_expression_string; }
  430. NonnullOwnPtr<CalcSum> const& expression() const { return m_expression; }
  431. private:
  432. explicit CalculatedStyleValue(String const& expression_string, NonnullOwnPtr<CalcSum> calc_sum)
  433. : StyleValue(Type::Calculated)
  434. , m_expression_string(expression_string)
  435. , m_expression(move(calc_sum))
  436. {
  437. }
  438. String m_expression_string;
  439. NonnullOwnPtr<CalcSum> m_expression;
  440. };
  441. class InitialStyleValue final : public StyleValue {
  442. public:
  443. static NonnullRefPtr<InitialStyleValue> create() { return adopt_ref(*new InitialStyleValue); }
  444. virtual ~InitialStyleValue() override { }
  445. String to_string() const override { return "initial"; }
  446. private:
  447. InitialStyleValue()
  448. : StyleValue(Type::Initial)
  449. {
  450. }
  451. };
  452. class InheritStyleValue final : public StyleValue {
  453. public:
  454. static NonnullRefPtr<InheritStyleValue> create() { return adopt_ref(*new InheritStyleValue); }
  455. virtual ~InheritStyleValue() override { }
  456. String to_string() const override { return "inherit"; }
  457. private:
  458. InheritStyleValue()
  459. : StyleValue(Type::Inherit)
  460. {
  461. }
  462. };
  463. class ColorStyleValue : public StyleValue {
  464. public:
  465. static NonnullRefPtr<ColorStyleValue> create(Color color)
  466. {
  467. return adopt_ref(*new ColorStyleValue(color));
  468. }
  469. virtual ~ColorStyleValue() override { }
  470. Color color() const { return m_color; }
  471. String to_string() const override { return m_color.to_string(); }
  472. Color to_color(const DOM::Document&) const override { return m_color; }
  473. virtual bool equals(const StyleValue& other) const override
  474. {
  475. if (type() != other.type())
  476. return false;
  477. return m_color == static_cast<const ColorStyleValue&>(other).m_color;
  478. }
  479. private:
  480. explicit ColorStyleValue(Color color)
  481. : StyleValue(Type::Color)
  482. , m_color(color)
  483. {
  484. }
  485. Color m_color;
  486. };
  487. class IdentifierStyleValue final : public StyleValue {
  488. public:
  489. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  490. {
  491. return adopt_ref(*new IdentifierStyleValue(id));
  492. }
  493. virtual ~IdentifierStyleValue() override { }
  494. CSS::ValueID id() const { return m_id; }
  495. virtual String to_string() const override;
  496. virtual Color to_color(const DOM::Document&) const override;
  497. virtual bool equals(const StyleValue& other) const override
  498. {
  499. if (type() != other.type())
  500. return false;
  501. return m_id == static_cast<const IdentifierStyleValue&>(other).m_id;
  502. }
  503. private:
  504. explicit IdentifierStyleValue(CSS::ValueID id)
  505. : StyleValue(Type::Identifier)
  506. , m_id(id)
  507. {
  508. }
  509. CSS::ValueID m_id { CSS::ValueID::Invalid };
  510. };
  511. class ImageStyleValue final
  512. : public StyleValue
  513. , public ImageResourceClient {
  514. public:
  515. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt_ref(*new ImageStyleValue(url, document)); }
  516. virtual ~ImageStyleValue() override { }
  517. String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
  518. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  519. private:
  520. ImageStyleValue(const URL&, DOM::Document&);
  521. // ^ResourceClient
  522. virtual void resource_did_load() override;
  523. URL m_url;
  524. WeakPtr<DOM::Document> m_document;
  525. RefPtr<Gfx::Bitmap> m_bitmap;
  526. };
  527. class ValueListStyleValue final : public StyleValue {
  528. public:
  529. static NonnullRefPtr<ValueListStyleValue> create(Vector<StyleComponentValueRule>&& values) { return adopt_ref(*new ValueListStyleValue(move(values))); }
  530. virtual ~ValueListStyleValue() override { }
  531. virtual String to_string() const override;
  532. Vector<StyleComponentValueRule> const& values() const { return m_values; }
  533. private:
  534. ValueListStyleValue(Vector<StyleComponentValueRule>&&);
  535. Vector<StyleComponentValueRule> m_values;
  536. };
  537. inline CSS::ValueID StyleValue::to_identifier() const
  538. {
  539. if (is_identifier())
  540. return static_cast<const IdentifierStyleValue&>(*this).id();
  541. return CSS::ValueID::Invalid;
  542. }
  543. }