StyleValue.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. CustomProperty,
  205. Numeric,
  206. ValueList,
  207. Calculated,
  208. BoxShadow,
  209. };
  210. Type type() const { return m_type; }
  211. bool is_inherit() const { return type() == Type::Inherit; }
  212. bool is_initial() const { return type() == Type::Initial; }
  213. bool is_color() const { return type() == Type::Color; }
  214. bool is_identifier() const { return type() == Type::Identifier; }
  215. bool is_image() const { return type() == Type::Image; }
  216. bool is_string() const { return type() == Type::String; }
  217. bool is_length() const { return type() == Type::Length; }
  218. bool is_custom_property() const { return type() == Type::CustomProperty; }
  219. bool is_numeric() const { return type() == Type::Numeric; }
  220. bool is_value_list() const { return type() == Type::ValueList; }
  221. bool is_box_shadow() const { return type() == Type::BoxShadow; }
  222. bool is_calculated() const { return type() == Type::Calculated; }
  223. bool is_builtin_or_dynamic() const
  224. {
  225. return is_inherit() || is_initial() || is_custom_property() || is_calculated();
  226. }
  227. virtual String to_string() const = 0;
  228. virtual Length to_length() const { return Length::make_auto(); }
  229. virtual Color to_color(const DOM::Document&) const { return {}; }
  230. CSS::ValueID to_identifier() const;
  231. virtual bool is_auto() const { return false; }
  232. bool operator==(const StyleValue& other) const { return equals(other); }
  233. bool operator!=(const StyleValue& other) const { return !(*this == other); }
  234. virtual bool equals(const StyleValue& other) const
  235. {
  236. if (type() != other.type())
  237. return false;
  238. return to_string() == other.to_string();
  239. }
  240. protected:
  241. explicit StyleValue(Type);
  242. private:
  243. Type m_type { Type::Invalid };
  244. };
  245. // FIXME: Allow for fallback
  246. class CustomStyleValue : public StyleValue {
  247. public:
  248. static NonnullRefPtr<CustomStyleValue> create(const String& custom_property_name)
  249. {
  250. return adopt_ref(*new CustomStyleValue(custom_property_name));
  251. }
  252. String custom_property_name() const { return m_custom_property_name; }
  253. String to_string() const override { return m_custom_property_name; }
  254. private:
  255. explicit CustomStyleValue(const String& custom_property_name)
  256. : StyleValue(Type::CustomProperty)
  257. , m_custom_property_name(custom_property_name)
  258. {
  259. }
  260. String m_custom_property_name {};
  261. };
  262. class NumericStyleValue : public StyleValue {
  263. public:
  264. static NonnullRefPtr<NumericStyleValue> create(float value)
  265. {
  266. return adopt_ref(*new NumericStyleValue(value));
  267. }
  268. float value() const { return m_value; }
  269. String to_string() const override { return String::formatted("{}", m_value); }
  270. private:
  271. explicit NumericStyleValue(float value)
  272. : StyleValue(Type::Numeric)
  273. , m_value(value)
  274. {
  275. }
  276. float m_value { 0 };
  277. };
  278. class StringStyleValue : public StyleValue {
  279. public:
  280. static NonnullRefPtr<StringStyleValue> create(const String& string)
  281. {
  282. return adopt_ref(*new StringStyleValue(string));
  283. }
  284. virtual ~StringStyleValue() override { }
  285. String to_string() const override { return m_string; }
  286. private:
  287. explicit StringStyleValue(const String& string)
  288. : StyleValue(Type::String)
  289. , m_string(string)
  290. {
  291. }
  292. String m_string;
  293. };
  294. class BoxShadowStyleValue : public StyleValue {
  295. public:
  296. static NonnullRefPtr<BoxShadowStyleValue> create(Length const& offset_x, Length const& offset_y, Length const& blur_radius, Color const& color)
  297. {
  298. return adopt_ref(*new BoxShadowStyleValue(offset_x, offset_y, blur_radius, color));
  299. }
  300. virtual ~BoxShadowStyleValue() override { }
  301. Length const& offset_x() const { return m_offset_x; }
  302. Length const& offset_y() const { return m_offset_y; }
  303. Length const& blur_radius() const { return m_blur_radius; }
  304. Color const& color() const { return m_color; }
  305. String to_string() const override { return String::formatted("BoxShadow offset_x: {}, offset_y: {}, blur_radius: {}, color: {}",
  306. m_offset_x.to_string(), m_offset_y.to_string(), m_blur_radius.to_string(), m_color.to_string()); }
  307. private:
  308. explicit BoxShadowStyleValue(Length const& offset_x, Length const& offset_y, Length const& blur_radius, Color const& color)
  309. : StyleValue(Type::BoxShadow)
  310. , m_offset_x(offset_x)
  311. , m_offset_y(offset_y)
  312. , m_blur_radius(blur_radius)
  313. , m_color(color)
  314. {
  315. }
  316. Length m_offset_x;
  317. Length m_offset_y;
  318. Length m_blur_radius;
  319. Color m_color;
  320. };
  321. class LengthStyleValue : public StyleValue {
  322. public:
  323. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  324. {
  325. return adopt_ref(*new LengthStyleValue(length));
  326. }
  327. virtual ~LengthStyleValue() override { }
  328. virtual String to_string() const override { return m_length.to_string(); }
  329. virtual Length to_length() const override { return m_length; }
  330. const Length& length() const { return m_length; }
  331. virtual bool is_auto() const override { return m_length.is_auto(); }
  332. virtual bool equals(const StyleValue& other) const override
  333. {
  334. if (type() != other.type())
  335. return false;
  336. return m_length == static_cast<const LengthStyleValue&>(other).m_length;
  337. }
  338. private:
  339. explicit LengthStyleValue(const Length& length)
  340. : StyleValue(Type::Length)
  341. , m_length(length)
  342. {
  343. }
  344. Length m_length;
  345. };
  346. class CalculatedStyleValue : public StyleValue {
  347. public:
  348. struct CalcSum;
  349. struct CalcSumPartWithOperator;
  350. struct CalcProduct;
  351. struct CalcProductPartWithOperator;
  352. struct CalcNumberSum;
  353. struct CalcNumberSumPartWithOperator;
  354. struct CalcNumberProduct;
  355. struct CalcNumberProductPartWithOperator;
  356. using CalcNumberValue = Variant<float, NonnullOwnPtr<CalcNumberSum>>;
  357. using CalcValue = Variant<float, CSS::Length, NonnullOwnPtr<CalcSum>>;
  358. // This represents that: https://drafts.csswg.org/css-values-3/#calc-syntax
  359. struct CalcSum {
  360. CalcSum(NonnullOwnPtr<CalcProduct> first_calc_product, NonnullOwnPtrVector<CalcSumPartWithOperator> additional)
  361. : first_calc_product(move(first_calc_product))
  362. , zero_or_more_additional_calc_products(move(additional)) {};
  363. NonnullOwnPtr<CalcProduct> first_calc_product;
  364. NonnullOwnPtrVector<CalcSumPartWithOperator> zero_or_more_additional_calc_products;
  365. };
  366. struct CalcNumberSum {
  367. CalcNumberSum(NonnullOwnPtr<CalcNumberProduct> first_calc_number_product, NonnullOwnPtrVector<CalcNumberSumPartWithOperator> additional)
  368. : first_calc_number_product(move(first_calc_number_product))
  369. , zero_or_more_additional_calc_number_products(move(additional)) {};
  370. NonnullOwnPtr<CalcNumberProduct> first_calc_number_product;
  371. NonnullOwnPtrVector<CalcNumberSumPartWithOperator> zero_or_more_additional_calc_number_products;
  372. };
  373. struct CalcProduct {
  374. CalcValue first_calc_value;
  375. NonnullOwnPtrVector<CalcProductPartWithOperator> zero_or_more_additional_calc_values;
  376. };
  377. struct CalcSumPartWithOperator {
  378. enum Operation {
  379. Add,
  380. Subtract,
  381. };
  382. CalcSumPartWithOperator(Operation op, NonnullOwnPtr<CalcProduct> calc_product)
  383. : op(op)
  384. , calc_product(move(calc_product)) {};
  385. Operation op;
  386. NonnullOwnPtr<CalcProduct> calc_product;
  387. };
  388. struct CalcProductPartWithOperator {
  389. enum {
  390. Multiply,
  391. Divide,
  392. } op;
  393. Variant<CalcValue, CalcNumberValue> value;
  394. };
  395. struct CalcNumberProduct {
  396. CalcNumberValue first_calc_number_value;
  397. NonnullOwnPtrVector<CalcNumberProductPartWithOperator> zero_or_more_additional_calc_number_values;
  398. };
  399. struct CalcNumberProductPartWithOperator {
  400. enum {
  401. Multiply,
  402. Divide,
  403. } op;
  404. CalcNumberValue value;
  405. };
  406. struct CalcNumberSumPartWithOperator {
  407. enum Operation {
  408. Add,
  409. Subtract,
  410. };
  411. CalcNumberSumPartWithOperator(Operation op, NonnullOwnPtr<CalcNumberProduct> calc_number_product)
  412. : op(op)
  413. , calc_number_product(move(calc_number_product)) {};
  414. Operation op;
  415. NonnullOwnPtr<CalcNumberProduct> calc_number_product;
  416. };
  417. static NonnullRefPtr<CalculatedStyleValue> create(String const& expression_string, NonnullOwnPtr<CalcSum> calc_sum)
  418. {
  419. return adopt_ref(*new CalculatedStyleValue(expression_string, move(calc_sum)));
  420. }
  421. String to_string() const override { return m_expression_string; }
  422. NonnullOwnPtr<CalcSum> const& expression() const { return m_expression; }
  423. private:
  424. explicit CalculatedStyleValue(String const& expression_string, NonnullOwnPtr<CalcSum> calc_sum)
  425. : StyleValue(Type::Calculated)
  426. , m_expression_string(expression_string)
  427. , m_expression(move(calc_sum))
  428. {
  429. }
  430. String m_expression_string;
  431. NonnullOwnPtr<CalcSum> m_expression;
  432. };
  433. class InitialStyleValue final : public StyleValue {
  434. public:
  435. static NonnullRefPtr<InitialStyleValue> create() { return adopt_ref(*new InitialStyleValue); }
  436. virtual ~InitialStyleValue() override { }
  437. String to_string() const override { return "initial"; }
  438. private:
  439. InitialStyleValue()
  440. : StyleValue(Type::Initial)
  441. {
  442. }
  443. };
  444. class InheritStyleValue final : public StyleValue {
  445. public:
  446. static NonnullRefPtr<InheritStyleValue> create() { return adopt_ref(*new InheritStyleValue); }
  447. virtual ~InheritStyleValue() override { }
  448. String to_string() const override { return "inherit"; }
  449. private:
  450. InheritStyleValue()
  451. : StyleValue(Type::Inherit)
  452. {
  453. }
  454. };
  455. class ColorStyleValue : public StyleValue {
  456. public:
  457. static NonnullRefPtr<ColorStyleValue> create(Color color)
  458. {
  459. return adopt_ref(*new ColorStyleValue(color));
  460. }
  461. virtual ~ColorStyleValue() override { }
  462. Color color() const { return m_color; }
  463. String to_string() const override { return m_color.to_string(); }
  464. Color to_color(const DOM::Document&) const override { return m_color; }
  465. virtual bool equals(const StyleValue& other) const override
  466. {
  467. if (type() != other.type())
  468. return false;
  469. return m_color == static_cast<const ColorStyleValue&>(other).m_color;
  470. }
  471. private:
  472. explicit ColorStyleValue(Color color)
  473. : StyleValue(Type::Color)
  474. , m_color(color)
  475. {
  476. }
  477. Color m_color;
  478. };
  479. class IdentifierStyleValue final : public StyleValue {
  480. public:
  481. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  482. {
  483. return adopt_ref(*new IdentifierStyleValue(id));
  484. }
  485. virtual ~IdentifierStyleValue() override { }
  486. CSS::ValueID id() const { return m_id; }
  487. virtual String to_string() const override;
  488. virtual Color to_color(const DOM::Document&) const override;
  489. virtual bool equals(const StyleValue& other) const override
  490. {
  491. if (type() != other.type())
  492. return false;
  493. return m_id == static_cast<const IdentifierStyleValue&>(other).m_id;
  494. }
  495. private:
  496. explicit IdentifierStyleValue(CSS::ValueID id)
  497. : StyleValue(Type::Identifier)
  498. , m_id(id)
  499. {
  500. }
  501. CSS::ValueID m_id { CSS::ValueID::Invalid };
  502. };
  503. class ImageStyleValue final
  504. : public StyleValue
  505. , public ImageResourceClient {
  506. public:
  507. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt_ref(*new ImageStyleValue(url, document)); }
  508. virtual ~ImageStyleValue() override { }
  509. String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
  510. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  511. private:
  512. ImageStyleValue(const URL&, DOM::Document&);
  513. // ^ResourceClient
  514. virtual void resource_did_load() override;
  515. URL m_url;
  516. WeakPtr<DOM::Document> m_document;
  517. RefPtr<Gfx::Bitmap> m_bitmap;
  518. };
  519. class ValueListStyleValue final : public StyleValue {
  520. public:
  521. static NonnullRefPtr<ValueListStyleValue> create(Vector<StyleComponentValueRule>&& values) { return adopt_ref(*new ValueListStyleValue(move(values))); }
  522. virtual ~ValueListStyleValue() override { }
  523. virtual String to_string() const override;
  524. Vector<StyleComponentValueRule> const& values() const { return m_values; }
  525. private:
  526. ValueListStyleValue(Vector<StyleComponentValueRule>&&);
  527. Vector<StyleComponentValueRule> m_values;
  528. };
  529. inline CSS::ValueID StyleValue::to_identifier() const
  530. {
  531. if (is_identifier())
  532. return static_cast<const IdentifierStyleValue&>(*this).id();
  533. return CSS::ValueID::Invalid;
  534. }
  535. }