StyleValue.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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/NonnullRefPtrVector.h>
  10. #include <AK/RefCounted.h>
  11. #include <AK/RefPtr.h>
  12. #include <AK/String.h>
  13. #include <AK/StringView.h>
  14. #include <AK/URL.h>
  15. #include <AK/WeakPtr.h>
  16. #include <LibGfx/Bitmap.h>
  17. #include <LibGfx/Color.h>
  18. #include <LibWeb/CSS/Length.h>
  19. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  20. #include <LibWeb/CSS/PropertyID.h>
  21. #include <LibWeb/CSS/ValueID.h>
  22. #include <LibWeb/Forward.h>
  23. #include <LibWeb/Loader/ImageResource.h>
  24. namespace Web::CSS {
  25. enum class Position {
  26. Static,
  27. Relative,
  28. Absolute,
  29. Fixed,
  30. Sticky,
  31. };
  32. enum class TextAlign {
  33. Left,
  34. Center,
  35. Right,
  36. Justify,
  37. LibwebCenter,
  38. };
  39. enum class TextDecorationLine {
  40. None,
  41. Underline,
  42. Overline,
  43. LineThrough,
  44. Blink,
  45. };
  46. enum class TextTransform {
  47. None,
  48. Capitalize,
  49. Uppercase,
  50. Lowercase,
  51. FullWidth,
  52. FullSizeKana,
  53. };
  54. enum class Display {
  55. None,
  56. Block,
  57. Inline,
  58. InlineBlock,
  59. ListItem,
  60. Table,
  61. TableRow,
  62. TableCell,
  63. TableHeaderGroup,
  64. TableRowGroup,
  65. TableFooterGroup,
  66. TableColumn,
  67. TableColumnGroup,
  68. TableCaption,
  69. Flex,
  70. };
  71. enum class FlexDirection {
  72. Row,
  73. RowReverse,
  74. Column,
  75. ColumnReverse,
  76. };
  77. enum class FlexWrap {
  78. Nowrap,
  79. Wrap,
  80. WrapReverse
  81. };
  82. enum class FlexBasis {
  83. Content,
  84. Length
  85. };
  86. enum class WhiteSpace {
  87. Normal,
  88. Pre,
  89. Nowrap,
  90. PreLine,
  91. PreWrap,
  92. };
  93. enum class Float {
  94. None,
  95. Left,
  96. Right,
  97. };
  98. enum class Clear {
  99. None,
  100. Left,
  101. Right,
  102. Both,
  103. };
  104. enum class Cursor {
  105. Auto,
  106. Default,
  107. None,
  108. ContextMenu,
  109. Help,
  110. Pointer,
  111. Progress,
  112. Wait,
  113. Cell,
  114. Crosshair,
  115. Text,
  116. VerticalText,
  117. Alias,
  118. Copy,
  119. Move,
  120. NoDrop,
  121. NotAllowed,
  122. Grab,
  123. Grabbing,
  124. EResize,
  125. NResize,
  126. NeResize,
  127. NwResize,
  128. SResize,
  129. SeResize,
  130. SwResize,
  131. WResize,
  132. EwResize,
  133. NsResize,
  134. NeswResize,
  135. NwseResize,
  136. ColResize,
  137. RowResize,
  138. AllScroll,
  139. ZoomIn,
  140. ZoomOut,
  141. };
  142. enum class LineStyle {
  143. None,
  144. Hidden,
  145. Dotted,
  146. Dashed,
  147. Solid,
  148. Double,
  149. Groove,
  150. Ridge,
  151. Inset,
  152. Outset,
  153. };
  154. enum class ListStyleType {
  155. None,
  156. Disc,
  157. Circle,
  158. Square,
  159. Decimal,
  160. DecimalLeadingZero,
  161. LowerAlpha,
  162. LowerLatin,
  163. LowerRoman,
  164. UpperAlpha,
  165. UpperLatin,
  166. UpperRoman,
  167. };
  168. enum class Overflow : u8 {
  169. Auto,
  170. Clip,
  171. Hidden,
  172. Scroll,
  173. Visible,
  174. };
  175. enum class Repeat : u8 {
  176. NoRepeat,
  177. Repeat,
  178. Round,
  179. Space,
  180. };
  181. enum class JustifyContent {
  182. FlexStart,
  183. FlexEnd,
  184. Center,
  185. SpaceBetween,
  186. SpaceAround,
  187. };
  188. class StyleValue : public RefCounted<StyleValue> {
  189. public:
  190. virtual ~StyleValue();
  191. enum class Type {
  192. Invalid,
  193. Inherit,
  194. Initial,
  195. String,
  196. Length,
  197. Color,
  198. Identifier,
  199. Image,
  200. Position,
  201. CustomProperty,
  202. Numeric,
  203. ValueList,
  204. };
  205. Type type() const { return m_type; }
  206. bool is_inherit() const { return type() == Type::Inherit; }
  207. bool is_initial() const { return type() == Type::Initial; }
  208. bool is_color() const { return type() == Type::Color; }
  209. bool is_identifier() const { return type() == Type::Identifier; }
  210. bool is_image() const { return type() == Type::Image; }
  211. bool is_string() const { return type() == Type::String; }
  212. bool is_length() const { return type() == Type::Length; }
  213. bool is_position() const { return type() == Type::Position; }
  214. bool is_custom_property() const { return type() == Type::CustomProperty; }
  215. bool is_numeric() const { return type() == Type::Numeric; }
  216. bool is_value_list() const { return type() == Type::ValueList; }
  217. bool is_builtin_or_dynamic() const
  218. {
  219. return is_inherit() || is_initial() || is_custom_property();
  220. }
  221. virtual String to_string() const = 0;
  222. virtual Length to_length() const { return Length::make_auto(); }
  223. virtual Color to_color(const DOM::Document&) const { return {}; }
  224. CSS::ValueID to_identifier() const;
  225. virtual bool is_auto() const { return false; }
  226. bool operator==(const StyleValue& other) const { return equals(other); }
  227. bool operator!=(const StyleValue& other) const { return !(*this == other); }
  228. virtual bool equals(const StyleValue& other) const
  229. {
  230. if (type() != other.type())
  231. return false;
  232. return to_string() == other.to_string();
  233. }
  234. protected:
  235. explicit StyleValue(Type);
  236. private:
  237. Type m_type { Type::Invalid };
  238. };
  239. // FIXME: Allow for fallback
  240. class CustomStyleValue : public StyleValue {
  241. public:
  242. static NonnullRefPtr<CustomStyleValue> create(const String& custom_property_name)
  243. {
  244. return adopt_ref(*new CustomStyleValue(custom_property_name));
  245. }
  246. String custom_property_name() const { return m_custom_property_name; }
  247. String to_string() const override { return m_custom_property_name; }
  248. private:
  249. explicit CustomStyleValue(const String& custom_property_name)
  250. : StyleValue(Type::CustomProperty)
  251. , m_custom_property_name(custom_property_name)
  252. {
  253. }
  254. String m_custom_property_name {};
  255. };
  256. class NumericStyleValue : public StyleValue {
  257. public:
  258. static NonnullRefPtr<NumericStyleValue> create(float value)
  259. {
  260. return adopt_ref(*new NumericStyleValue(value));
  261. }
  262. float value() const { return m_value; }
  263. String to_string() const override { return String::formatted("{}", m_value); }
  264. private:
  265. explicit NumericStyleValue(float value)
  266. : StyleValue(Type::Numeric)
  267. , m_value(value)
  268. {
  269. }
  270. float m_value { 0 };
  271. };
  272. class StringStyleValue : public StyleValue {
  273. public:
  274. static NonnullRefPtr<StringStyleValue> create(const String& string)
  275. {
  276. return adopt_ref(*new StringStyleValue(string));
  277. }
  278. virtual ~StringStyleValue() override { }
  279. String to_string() const override { return m_string; }
  280. private:
  281. explicit StringStyleValue(const String& string)
  282. : StyleValue(Type::String)
  283. , m_string(string)
  284. {
  285. }
  286. String m_string;
  287. };
  288. class LengthStyleValue : public StyleValue {
  289. public:
  290. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  291. {
  292. return adopt_ref(*new LengthStyleValue(length));
  293. }
  294. virtual ~LengthStyleValue() override { }
  295. virtual String to_string() const override { return m_length.to_string(); }
  296. virtual Length to_length() const override { return m_length; }
  297. const Length& length() const { return m_length; }
  298. virtual bool is_auto() const override { return m_length.is_auto(); }
  299. virtual bool equals(const StyleValue& other) const override
  300. {
  301. if (type() != other.type())
  302. return false;
  303. return m_length == static_cast<const LengthStyleValue&>(other).m_length;
  304. }
  305. private:
  306. explicit LengthStyleValue(const Length& length)
  307. : StyleValue(Type::Length)
  308. , m_length(length)
  309. {
  310. }
  311. Length m_length;
  312. };
  313. class InitialStyleValue final : public StyleValue {
  314. public:
  315. static NonnullRefPtr<InitialStyleValue> create() { return adopt_ref(*new InitialStyleValue); }
  316. virtual ~InitialStyleValue() override { }
  317. String to_string() const override { return "initial"; }
  318. private:
  319. InitialStyleValue()
  320. : StyleValue(Type::Initial)
  321. {
  322. }
  323. };
  324. class InheritStyleValue final : public StyleValue {
  325. public:
  326. static NonnullRefPtr<InheritStyleValue> create() { return adopt_ref(*new InheritStyleValue); }
  327. virtual ~InheritStyleValue() override { }
  328. String to_string() const override { return "inherit"; }
  329. private:
  330. InheritStyleValue()
  331. : StyleValue(Type::Inherit)
  332. {
  333. }
  334. };
  335. class ColorStyleValue : public StyleValue {
  336. public:
  337. static NonnullRefPtr<ColorStyleValue> create(Color color)
  338. {
  339. return adopt_ref(*new ColorStyleValue(color));
  340. }
  341. virtual ~ColorStyleValue() override { }
  342. Color color() const { return m_color; }
  343. String to_string() const override { return m_color.to_string(); }
  344. Color to_color(const DOM::Document&) const override { return m_color; }
  345. virtual bool equals(const StyleValue& other) const override
  346. {
  347. if (type() != other.type())
  348. return false;
  349. return m_color == static_cast<const ColorStyleValue&>(other).m_color;
  350. }
  351. private:
  352. explicit ColorStyleValue(Color color)
  353. : StyleValue(Type::Color)
  354. , m_color(color)
  355. {
  356. }
  357. Color m_color;
  358. };
  359. class IdentifierStyleValue final : public StyleValue {
  360. public:
  361. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  362. {
  363. return adopt_ref(*new IdentifierStyleValue(id));
  364. }
  365. virtual ~IdentifierStyleValue() override { }
  366. CSS::ValueID id() const { return m_id; }
  367. virtual String to_string() const override;
  368. virtual Color to_color(const DOM::Document&) const override;
  369. virtual bool equals(const StyleValue& other) const override
  370. {
  371. if (type() != other.type())
  372. return false;
  373. return m_id == static_cast<const IdentifierStyleValue&>(other).m_id;
  374. }
  375. private:
  376. explicit IdentifierStyleValue(CSS::ValueID id)
  377. : StyleValue(Type::Identifier)
  378. , m_id(id)
  379. {
  380. }
  381. CSS::ValueID m_id { CSS::ValueID::Invalid };
  382. };
  383. class ImageStyleValue final
  384. : public StyleValue
  385. , public ImageResourceClient {
  386. public:
  387. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt_ref(*new ImageStyleValue(url, document)); }
  388. virtual ~ImageStyleValue() override { }
  389. String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
  390. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  391. private:
  392. ImageStyleValue(const URL&, DOM::Document&);
  393. // ^ResourceClient
  394. virtual void resource_did_load() override;
  395. URL m_url;
  396. WeakPtr<DOM::Document> m_document;
  397. RefPtr<Gfx::Bitmap> m_bitmap;
  398. };
  399. class ValueListStyleValue final : public StyleValue {
  400. public:
  401. static NonnullRefPtr<ValueListStyleValue> create(Vector<StyleComponentValueRule>&& values) { return adopt_ref(*new ValueListStyleValue(move(values))); }
  402. virtual ~ValueListStyleValue() override { }
  403. virtual String to_string() const override;
  404. Vector<StyleComponentValueRule> const& values() const { return m_values; }
  405. private:
  406. ValueListStyleValue(Vector<StyleComponentValueRule>&&);
  407. Vector<StyleComponentValueRule> m_values;
  408. };
  409. inline CSS::ValueID StyleValue::to_identifier() const
  410. {
  411. if (is_identifier())
  412. return static_cast<const IdentifierStyleValue&>(*this).id();
  413. return CSS::ValueID::Invalid;
  414. }
  415. }