StyleValue.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 FlexWrap {
  74. Nowrap,
  75. Wrap,
  76. WrapReverse
  77. };
  78. enum class FlexBasis {
  79. Content,
  80. Length
  81. };
  82. enum class WhiteSpace {
  83. Normal,
  84. Pre,
  85. Nowrap,
  86. PreLine,
  87. PreWrap,
  88. };
  89. enum class Float {
  90. None,
  91. Left,
  92. Right,
  93. };
  94. enum class Clear {
  95. None,
  96. Left,
  97. Right,
  98. Both,
  99. };
  100. enum class Cursor {
  101. Auto,
  102. Default,
  103. None,
  104. ContextMenu,
  105. Help,
  106. Pointer,
  107. Progress,
  108. Wait,
  109. Cell,
  110. Crosshair,
  111. Text,
  112. VerticalText,
  113. Alias,
  114. Copy,
  115. Move,
  116. NoDrop,
  117. NotAllowed,
  118. Grab,
  119. Grabbing,
  120. EResize,
  121. NResize,
  122. NeResize,
  123. NwResize,
  124. SResize,
  125. SeResize,
  126. SwResize,
  127. WResize,
  128. EwResize,
  129. NsResize,
  130. NeswResize,
  131. NwseResize,
  132. ColResize,
  133. RowResize,
  134. AllScroll,
  135. ZoomIn,
  136. ZoomOut,
  137. };
  138. enum class LineStyle {
  139. None,
  140. Hidden,
  141. Dotted,
  142. Dashed,
  143. Solid,
  144. Double,
  145. Groove,
  146. Ridge,
  147. Inset,
  148. Outset,
  149. };
  150. enum class ListStyleType {
  151. None,
  152. Disc,
  153. Circle,
  154. Square,
  155. Decimal,
  156. DecimalLeadingZero,
  157. LowerAlpha,
  158. LowerLatin,
  159. UpperAlpha,
  160. UpperLatin,
  161. };
  162. enum class Overflow : u8 {
  163. Auto,
  164. Clip,
  165. Hidden,
  166. Scroll,
  167. Visible,
  168. };
  169. enum class Repeat : u8 {
  170. NoRepeat,
  171. Repeat,
  172. Round,
  173. Space,
  174. };
  175. class StyleValue : public RefCounted<StyleValue> {
  176. public:
  177. virtual ~StyleValue();
  178. enum class Type {
  179. Invalid,
  180. Inherit,
  181. Initial,
  182. String,
  183. Length,
  184. Color,
  185. Identifier,
  186. Image,
  187. Position,
  188. CustomProperty,
  189. Numeric,
  190. };
  191. Type type() const { return m_type; }
  192. bool is_inherit() const { return type() == Type::Inherit; }
  193. bool is_initial() const { return type() == Type::Initial; }
  194. bool is_color() const { return type() == Type::Color; }
  195. bool is_identifier() const { return type() == Type::Identifier; }
  196. bool is_image() const { return type() == Type::Image; }
  197. bool is_string() const { return type() == Type::String; }
  198. bool is_length() const { return type() == Type::Length; }
  199. bool is_position() const { return type() == Type::Position; }
  200. bool is_custom_property() const { return type() == Type::CustomProperty; }
  201. bool is_numeric() const { return type() == Type::Numeric; }
  202. virtual String to_string() const = 0;
  203. virtual Length to_length() const { return Length::make_auto(); }
  204. virtual Color to_color(const DOM::Document&) const { return {}; }
  205. CSS::ValueID to_identifier() const;
  206. virtual bool is_auto() const { return false; }
  207. bool operator==(const StyleValue& other) const { return equals(other); }
  208. bool operator!=(const StyleValue& other) const { return !(*this == other); }
  209. virtual bool equals(const StyleValue& other) const
  210. {
  211. if (type() != other.type())
  212. return false;
  213. return to_string() == other.to_string();
  214. }
  215. protected:
  216. explicit StyleValue(Type);
  217. private:
  218. Type m_type { Type::Invalid };
  219. };
  220. // FIXME: Allow for fallback
  221. class CustomStyleValue : public StyleValue {
  222. public:
  223. static NonnullRefPtr<CustomStyleValue> create(const String& custom_property_name)
  224. {
  225. return adopt_ref(*new CustomStyleValue(custom_property_name));
  226. }
  227. String custom_property_name() const { return m_custom_property_name; }
  228. String to_string() const override { return m_custom_property_name; }
  229. private:
  230. explicit CustomStyleValue(const String& custom_property_name)
  231. : StyleValue(Type::CustomProperty)
  232. , m_custom_property_name(custom_property_name)
  233. {
  234. }
  235. String m_custom_property_name {};
  236. };
  237. class NumericStyleValue : public StyleValue {
  238. public:
  239. static NonnullRefPtr<NumericStyleValue> create(float value)
  240. {
  241. return adopt_ref(*new NumericStyleValue(value));
  242. }
  243. float value() const { return m_value; }
  244. String to_string() const override { return String::formatted("{}", m_value); }
  245. private:
  246. explicit NumericStyleValue(float value)
  247. : StyleValue(Type::Numeric)
  248. , m_value(value)
  249. {
  250. }
  251. float m_value { 0 };
  252. };
  253. class StringStyleValue : public StyleValue {
  254. public:
  255. static NonnullRefPtr<StringStyleValue> create(const String& string)
  256. {
  257. return adopt_ref(*new StringStyleValue(string));
  258. }
  259. virtual ~StringStyleValue() override { }
  260. String to_string() const override { return m_string; }
  261. private:
  262. explicit StringStyleValue(const String& string)
  263. : StyleValue(Type::String)
  264. , m_string(string)
  265. {
  266. }
  267. String m_string;
  268. };
  269. class LengthStyleValue : public StyleValue {
  270. public:
  271. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  272. {
  273. return adopt_ref(*new LengthStyleValue(length));
  274. }
  275. virtual ~LengthStyleValue() override { }
  276. virtual String to_string() const override { return m_length.to_string(); }
  277. virtual Length to_length() const override { return m_length; }
  278. const Length& length() const { return m_length; }
  279. virtual bool is_auto() const override { return m_length.is_auto(); }
  280. virtual bool equals(const StyleValue& other) const override
  281. {
  282. if (type() != other.type())
  283. return false;
  284. return m_length == static_cast<const LengthStyleValue&>(other).m_length;
  285. }
  286. private:
  287. explicit LengthStyleValue(const Length& length)
  288. : StyleValue(Type::Length)
  289. , m_length(length)
  290. {
  291. }
  292. Length m_length;
  293. };
  294. class InitialStyleValue final : public StyleValue {
  295. public:
  296. static NonnullRefPtr<InitialStyleValue> create() { return adopt_ref(*new InitialStyleValue); }
  297. virtual ~InitialStyleValue() override { }
  298. String to_string() const override { return "initial"; }
  299. private:
  300. InitialStyleValue()
  301. : StyleValue(Type::Initial)
  302. {
  303. }
  304. };
  305. class InheritStyleValue final : public StyleValue {
  306. public:
  307. static NonnullRefPtr<InheritStyleValue> create() { return adopt_ref(*new InheritStyleValue); }
  308. virtual ~InheritStyleValue() override { }
  309. String to_string() const override { return "inherit"; }
  310. private:
  311. InheritStyleValue()
  312. : StyleValue(Type::Inherit)
  313. {
  314. }
  315. };
  316. class ColorStyleValue : public StyleValue {
  317. public:
  318. static NonnullRefPtr<ColorStyleValue> create(Color color)
  319. {
  320. return adopt_ref(*new ColorStyleValue(color));
  321. }
  322. virtual ~ColorStyleValue() override { }
  323. Color color() const { return m_color; }
  324. String to_string() const override { return m_color.to_string(); }
  325. Color to_color(const DOM::Document&) const override { return m_color; }
  326. virtual bool equals(const StyleValue& other) const override
  327. {
  328. if (type() != other.type())
  329. return false;
  330. return m_color == static_cast<const ColorStyleValue&>(other).m_color;
  331. }
  332. private:
  333. explicit ColorStyleValue(Color color)
  334. : StyleValue(Type::Color)
  335. , m_color(color)
  336. {
  337. }
  338. Color m_color;
  339. };
  340. class IdentifierStyleValue final : public StyleValue {
  341. public:
  342. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  343. {
  344. return adopt_ref(*new IdentifierStyleValue(id));
  345. }
  346. virtual ~IdentifierStyleValue() override { }
  347. CSS::ValueID id() const { return m_id; }
  348. virtual String to_string() const override;
  349. virtual Color to_color(const DOM::Document&) const override;
  350. virtual bool equals(const StyleValue& other) const override
  351. {
  352. if (type() != other.type())
  353. return false;
  354. return m_id == static_cast<const IdentifierStyleValue&>(other).m_id;
  355. }
  356. private:
  357. explicit IdentifierStyleValue(CSS::ValueID id)
  358. : StyleValue(Type::Identifier)
  359. , m_id(id)
  360. {
  361. }
  362. CSS::ValueID m_id { CSS::ValueID::Invalid };
  363. };
  364. class ImageStyleValue final
  365. : public StyleValue
  366. , public ImageResourceClient {
  367. public:
  368. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt_ref(*new ImageStyleValue(url, document)); }
  369. virtual ~ImageStyleValue() override { }
  370. String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
  371. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  372. private:
  373. ImageStyleValue(const URL&, DOM::Document&);
  374. // ^ResourceClient
  375. virtual void resource_did_load() override;
  376. URL m_url;
  377. WeakPtr<DOM::Document> m_document;
  378. RefPtr<Gfx::Bitmap> m_bitmap;
  379. };
  380. inline CSS::ValueID StyleValue::to_identifier() const
  381. {
  382. if (is_identifier())
  383. return static_cast<const IdentifierStyleValue&>(*this).id();
  384. return CSS::ValueID::Invalid;
  385. }
  386. }