StyleValue.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. Auto,
  90. };
  91. enum class WhiteSpace {
  92. Normal,
  93. Pre,
  94. Nowrap,
  95. PreLine,
  96. PreWrap,
  97. };
  98. enum class Float {
  99. None,
  100. Left,
  101. Right,
  102. };
  103. enum class Clear {
  104. None,
  105. Left,
  106. Right,
  107. Both,
  108. };
  109. enum class Cursor {
  110. Auto,
  111. Default,
  112. None,
  113. ContextMenu,
  114. Help,
  115. Pointer,
  116. Progress,
  117. Wait,
  118. Cell,
  119. Crosshair,
  120. Text,
  121. VerticalText,
  122. Alias,
  123. Copy,
  124. Move,
  125. NoDrop,
  126. NotAllowed,
  127. Grab,
  128. Grabbing,
  129. EResize,
  130. NResize,
  131. NeResize,
  132. NwResize,
  133. SResize,
  134. SeResize,
  135. SwResize,
  136. WResize,
  137. EwResize,
  138. NsResize,
  139. NeswResize,
  140. NwseResize,
  141. ColResize,
  142. RowResize,
  143. AllScroll,
  144. ZoomIn,
  145. ZoomOut,
  146. };
  147. enum class LineStyle {
  148. None,
  149. Hidden,
  150. Dotted,
  151. Dashed,
  152. Solid,
  153. Double,
  154. Groove,
  155. Ridge,
  156. Inset,
  157. Outset,
  158. };
  159. enum class ListStyleType {
  160. None,
  161. Disc,
  162. Circle,
  163. Square,
  164. Decimal,
  165. DecimalLeadingZero,
  166. LowerAlpha,
  167. LowerLatin,
  168. LowerRoman,
  169. UpperAlpha,
  170. UpperLatin,
  171. UpperRoman,
  172. };
  173. enum class Overflow : u8 {
  174. Auto,
  175. Clip,
  176. Hidden,
  177. Scroll,
  178. Visible,
  179. };
  180. enum class Repeat : u8 {
  181. NoRepeat,
  182. Repeat,
  183. Round,
  184. Space,
  185. };
  186. enum class JustifyContent {
  187. FlexStart,
  188. FlexEnd,
  189. Center,
  190. SpaceBetween,
  191. SpaceAround,
  192. };
  193. class StyleValue : public RefCounted<StyleValue> {
  194. public:
  195. virtual ~StyleValue();
  196. enum class Type {
  197. Invalid,
  198. Inherit,
  199. Initial,
  200. String,
  201. Length,
  202. Color,
  203. Identifier,
  204. Image,
  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_custom_property() const { return type() == Type::CustomProperty; }
  220. bool is_numeric() const { return type() == Type::Numeric; }
  221. bool is_value_list() const { return type() == Type::ValueList; }
  222. bool is_box_shadow() const { return type() == Type::BoxShadow; }
  223. bool is_calculated() const { return type() == Type::Calculated; }
  224. bool is_builtin_or_dynamic() const
  225. {
  226. return is_inherit() || is_initial() || is_custom_property() || is_calculated();
  227. }
  228. virtual String to_string() const = 0;
  229. virtual Length to_length() const { return Length::make_auto(); }
  230. virtual Color to_color(const DOM::Document&) const { return {}; }
  231. CSS::ValueID to_identifier() const;
  232. virtual bool is_auto() const { return false; }
  233. bool operator==(const StyleValue& other) const { return equals(other); }
  234. bool operator!=(const StyleValue& other) const { return !(*this == other); }
  235. virtual bool equals(const StyleValue& other) const
  236. {
  237. if (type() != other.type())
  238. return false;
  239. return to_string() == other.to_string();
  240. }
  241. protected:
  242. explicit StyleValue(Type);
  243. private:
  244. Type m_type { Type::Invalid };
  245. };
  246. // FIXME: Allow for fallback
  247. class CustomStyleValue : public StyleValue {
  248. public:
  249. static NonnullRefPtr<CustomStyleValue> create(const String& custom_property_name)
  250. {
  251. return adopt_ref(*new CustomStyleValue(custom_property_name));
  252. }
  253. String custom_property_name() const { return m_custom_property_name; }
  254. String to_string() const override { return m_custom_property_name; }
  255. private:
  256. explicit CustomStyleValue(const String& custom_property_name)
  257. : StyleValue(Type::CustomProperty)
  258. , m_custom_property_name(custom_property_name)
  259. {
  260. }
  261. String m_custom_property_name {};
  262. };
  263. class NumericStyleValue : public StyleValue {
  264. public:
  265. static NonnullRefPtr<NumericStyleValue> create(float value)
  266. {
  267. return adopt_ref(*new NumericStyleValue(value));
  268. }
  269. float value() const { return m_value; }
  270. String to_string() const override { return String::formatted("{}", m_value); }
  271. private:
  272. explicit NumericStyleValue(float value)
  273. : StyleValue(Type::Numeric)
  274. , m_value(value)
  275. {
  276. }
  277. float m_value { 0 };
  278. };
  279. class StringStyleValue : public StyleValue {
  280. public:
  281. static NonnullRefPtr<StringStyleValue> create(const String& string)
  282. {
  283. return adopt_ref(*new StringStyleValue(string));
  284. }
  285. virtual ~StringStyleValue() override { }
  286. String to_string() const override { return m_string; }
  287. private:
  288. explicit StringStyleValue(const String& string)
  289. : StyleValue(Type::String)
  290. , m_string(string)
  291. {
  292. }
  293. String m_string;
  294. };
  295. class BoxShadowStyleValue : public StyleValue {
  296. public:
  297. static NonnullRefPtr<BoxShadowStyleValue> create(Length const& offset_x, Length const& offset_y, Length const& blur_radius, Color const& color)
  298. {
  299. return adopt_ref(*new BoxShadowStyleValue(offset_x, offset_y, blur_radius, color));
  300. }
  301. virtual ~BoxShadowStyleValue() override { }
  302. Length const& offset_x() const { return m_offset_x; }
  303. Length const& offset_y() const { return m_offset_y; }
  304. Length const& blur_radius() const { return m_blur_radius; }
  305. Color const& color() const { return m_color; }
  306. String to_string() const override { return String::formatted("BoxShadow offset_x: {}, offset_y: {}, blur_radius: {}, color: {}",
  307. m_offset_x.to_string(), m_offset_y.to_string(), m_blur_radius.to_string(), m_color.to_string()); }
  308. private:
  309. explicit BoxShadowStyleValue(Length const& offset_x, Length const& offset_y, Length const& blur_radius, Color const& color)
  310. : StyleValue(Type::BoxShadow)
  311. , m_offset_x(offset_x)
  312. , m_offset_y(offset_y)
  313. , m_blur_radius(blur_radius)
  314. , m_color(color)
  315. {
  316. }
  317. Length m_offset_x;
  318. Length m_offset_y;
  319. Length m_blur_radius;
  320. Color m_color;
  321. };
  322. class LengthStyleValue : public StyleValue {
  323. public:
  324. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  325. {
  326. return adopt_ref(*new LengthStyleValue(length));
  327. }
  328. virtual ~LengthStyleValue() override { }
  329. virtual String to_string() const override { return m_length.to_string(); }
  330. virtual Length to_length() const override { return m_length; }
  331. const Length& length() const { return m_length; }
  332. virtual bool is_auto() const override { return m_length.is_auto(); }
  333. virtual bool equals(const StyleValue& other) const override
  334. {
  335. if (type() != other.type())
  336. return false;
  337. return m_length == static_cast<const LengthStyleValue&>(other).m_length;
  338. }
  339. private:
  340. explicit LengthStyleValue(const Length& length)
  341. : StyleValue(Type::Length)
  342. , m_length(length)
  343. {
  344. }
  345. Length m_length;
  346. };
  347. class CalculatedStyleValue : public StyleValue {
  348. public:
  349. struct CalcSum;
  350. struct CalcSumPartWithOperator;
  351. struct CalcProduct;
  352. struct CalcProductPartWithOperator;
  353. struct CalcNumberSum;
  354. struct CalcNumberSumPartWithOperator;
  355. struct CalcNumberProduct;
  356. struct CalcNumberProductPartWithOperator;
  357. using CalcNumberValue = Variant<float, NonnullOwnPtr<CalcNumberSum>>;
  358. using CalcValue = Variant<float, CSS::Length, NonnullOwnPtr<CalcSum>>;
  359. // This represents that: https://drafts.csswg.org/css-values-3/#calc-syntax
  360. struct CalcSum {
  361. CalcSum(NonnullOwnPtr<CalcProduct> first_calc_product, NonnullOwnPtrVector<CalcSumPartWithOperator> additional)
  362. : first_calc_product(move(first_calc_product))
  363. , zero_or_more_additional_calc_products(move(additional)) {};
  364. NonnullOwnPtr<CalcProduct> first_calc_product;
  365. NonnullOwnPtrVector<CalcSumPartWithOperator> zero_or_more_additional_calc_products;
  366. };
  367. struct CalcNumberSum {
  368. CalcNumberSum(NonnullOwnPtr<CalcNumberProduct> first_calc_number_product, NonnullOwnPtrVector<CalcNumberSumPartWithOperator> additional)
  369. : first_calc_number_product(move(first_calc_number_product))
  370. , zero_or_more_additional_calc_number_products(move(additional)) {};
  371. NonnullOwnPtr<CalcNumberProduct> first_calc_number_product;
  372. NonnullOwnPtrVector<CalcNumberSumPartWithOperator> zero_or_more_additional_calc_number_products;
  373. };
  374. struct CalcProduct {
  375. CalcValue first_calc_value;
  376. NonnullOwnPtrVector<CalcProductPartWithOperator> zero_or_more_additional_calc_values;
  377. };
  378. struct CalcSumPartWithOperator {
  379. enum Operation {
  380. Add,
  381. Subtract,
  382. };
  383. CalcSumPartWithOperator(Operation op, NonnullOwnPtr<CalcProduct> calc_product)
  384. : op(op)
  385. , calc_product(move(calc_product)) {};
  386. Operation op;
  387. NonnullOwnPtr<CalcProduct> calc_product;
  388. };
  389. struct CalcProductPartWithOperator {
  390. enum {
  391. Multiply,
  392. Divide,
  393. } op;
  394. Variant<CalcValue, CalcNumberValue> value;
  395. };
  396. struct CalcNumberProduct {
  397. CalcNumberValue first_calc_number_value;
  398. NonnullOwnPtrVector<CalcNumberProductPartWithOperator> zero_or_more_additional_calc_number_values;
  399. };
  400. struct CalcNumberProductPartWithOperator {
  401. enum {
  402. Multiply,
  403. Divide,
  404. } op;
  405. CalcNumberValue value;
  406. };
  407. struct CalcNumberSumPartWithOperator {
  408. enum Operation {
  409. Add,
  410. Subtract,
  411. };
  412. CalcNumberSumPartWithOperator(Operation op, NonnullOwnPtr<CalcNumberProduct> calc_number_product)
  413. : op(op)
  414. , calc_number_product(move(calc_number_product)) {};
  415. Operation op;
  416. NonnullOwnPtr<CalcNumberProduct> calc_number_product;
  417. };
  418. static NonnullRefPtr<CalculatedStyleValue> create(String const& expression_string, NonnullOwnPtr<CalcSum> calc_sum)
  419. {
  420. return adopt_ref(*new CalculatedStyleValue(expression_string, move(calc_sum)));
  421. }
  422. String to_string() const override { return m_expression_string; }
  423. NonnullOwnPtr<CalcSum> const& expression() const { return m_expression; }
  424. private:
  425. explicit CalculatedStyleValue(String const& expression_string, NonnullOwnPtr<CalcSum> calc_sum)
  426. : StyleValue(Type::Calculated)
  427. , m_expression_string(expression_string)
  428. , m_expression(move(calc_sum))
  429. {
  430. }
  431. String m_expression_string;
  432. NonnullOwnPtr<CalcSum> m_expression;
  433. };
  434. class InitialStyleValue final : public StyleValue {
  435. public:
  436. static NonnullRefPtr<InitialStyleValue> create() { return adopt_ref(*new InitialStyleValue); }
  437. virtual ~InitialStyleValue() override { }
  438. String to_string() const override { return "initial"; }
  439. private:
  440. InitialStyleValue()
  441. : StyleValue(Type::Initial)
  442. {
  443. }
  444. };
  445. class InheritStyleValue final : public StyleValue {
  446. public:
  447. static NonnullRefPtr<InheritStyleValue> create() { return adopt_ref(*new InheritStyleValue); }
  448. virtual ~InheritStyleValue() override { }
  449. String to_string() const override { return "inherit"; }
  450. private:
  451. InheritStyleValue()
  452. : StyleValue(Type::Inherit)
  453. {
  454. }
  455. };
  456. class ColorStyleValue : public StyleValue {
  457. public:
  458. static NonnullRefPtr<ColorStyleValue> create(Color color)
  459. {
  460. return adopt_ref(*new ColorStyleValue(color));
  461. }
  462. virtual ~ColorStyleValue() override { }
  463. Color color() const { return m_color; }
  464. String to_string() const override { return m_color.to_string(); }
  465. Color to_color(const DOM::Document&) const override { return m_color; }
  466. virtual bool equals(const StyleValue& other) const override
  467. {
  468. if (type() != other.type())
  469. return false;
  470. return m_color == static_cast<const ColorStyleValue&>(other).m_color;
  471. }
  472. private:
  473. explicit ColorStyleValue(Color color)
  474. : StyleValue(Type::Color)
  475. , m_color(color)
  476. {
  477. }
  478. Color m_color;
  479. };
  480. class IdentifierStyleValue final : public StyleValue {
  481. public:
  482. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  483. {
  484. return adopt_ref(*new IdentifierStyleValue(id));
  485. }
  486. virtual ~IdentifierStyleValue() override { }
  487. CSS::ValueID id() const { return m_id; }
  488. virtual String to_string() const override;
  489. virtual Color to_color(const DOM::Document&) const override;
  490. virtual bool equals(const StyleValue& other) const override
  491. {
  492. if (type() != other.type())
  493. return false;
  494. return m_id == static_cast<const IdentifierStyleValue&>(other).m_id;
  495. }
  496. private:
  497. explicit IdentifierStyleValue(CSS::ValueID id)
  498. : StyleValue(Type::Identifier)
  499. , m_id(id)
  500. {
  501. }
  502. CSS::ValueID m_id { CSS::ValueID::Invalid };
  503. };
  504. class ImageStyleValue final
  505. : public StyleValue
  506. , public ImageResourceClient {
  507. public:
  508. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt_ref(*new ImageStyleValue(url, document)); }
  509. virtual ~ImageStyleValue() override { }
  510. String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
  511. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  512. private:
  513. ImageStyleValue(const URL&, DOM::Document&);
  514. // ^ResourceClient
  515. virtual void resource_did_load() override;
  516. URL m_url;
  517. WeakPtr<DOM::Document> m_document;
  518. RefPtr<Gfx::Bitmap> m_bitmap;
  519. };
  520. class ValueListStyleValue final : public StyleValue {
  521. public:
  522. static NonnullRefPtr<ValueListStyleValue> create(Vector<StyleComponentValueRule>&& values) { return adopt_ref(*new ValueListStyleValue(move(values))); }
  523. virtual ~ValueListStyleValue() override { }
  524. virtual String to_string() const override;
  525. Vector<StyleComponentValueRule> const& values() const { return m_values; }
  526. private:
  527. ValueListStyleValue(Vector<StyleComponentValueRule>&&);
  528. Vector<StyleComponentValueRule> m_values;
  529. };
  530. inline CSS::ValueID StyleValue::to_identifier() const
  531. {
  532. if (is_identifier())
  533. return static_cast<const IdentifierStyleValue&>(*this).id();
  534. return CSS::ValueID::Invalid;
  535. }
  536. }