StyleValue.h 9.0 KB

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