StyleValue.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/RefCounted.h>
  28. #include <AK/RefPtr.h>
  29. #include <AK/String.h>
  30. #include <AK/StringView.h>
  31. #include <AK/URL.h>
  32. #include <AK/WeakPtr.h>
  33. #include <LibGfx/Bitmap.h>
  34. #include <LibGfx/Color.h>
  35. #include <LibWeb/CSS/Length.h>
  36. #include <LibWeb/CSS/PropertyID.h>
  37. #include <LibWeb/CSS/ValueID.h>
  38. #include <LibWeb/Forward.h>
  39. #include <LibWeb/Loader/ImageResource.h>
  40. namespace Web::CSS {
  41. enum class Position {
  42. Static,
  43. Relative,
  44. Absolute,
  45. Fixed,
  46. Sticky,
  47. };
  48. enum class TextAlign {
  49. Left,
  50. Center,
  51. Right,
  52. Justify,
  53. LibwebCenter,
  54. };
  55. enum class TextDecorationLine {
  56. None,
  57. Underline,
  58. Overline,
  59. LineThrough,
  60. Blink,
  61. };
  62. enum class TextTransform {
  63. None,
  64. Capitalize,
  65. Uppercase,
  66. Lowercase,
  67. FullWidth,
  68. FullSizeKana,
  69. };
  70. enum class Display {
  71. None,
  72. Block,
  73. Inline,
  74. InlineBlock,
  75. ListItem,
  76. Table,
  77. TableRow,
  78. TableCell,
  79. TableHeaderGroup,
  80. TableRowGroup,
  81. TableFooterGroup,
  82. TableColumn,
  83. TableColumnGroup,
  84. TableCaption,
  85. Flex,
  86. };
  87. enum class FlexDirection {
  88. Row,
  89. RowReverse,
  90. Column,
  91. ColumnReverse,
  92. };
  93. enum class WhiteSpace {
  94. Normal,
  95. Pre,
  96. Nowrap,
  97. PreLine,
  98. PreWrap,
  99. };
  100. enum class Float {
  101. None,
  102. Left,
  103. Right,
  104. };
  105. enum class Clear {
  106. None,
  107. Left,
  108. Right,
  109. Both,
  110. };
  111. enum class Cursor {
  112. Auto,
  113. Default,
  114. None,
  115. ContextMenu,
  116. Help,
  117. Pointer,
  118. Progress,
  119. Wait,
  120. Cell,
  121. Crosshair,
  122. Text,
  123. VerticalText,
  124. Alias,
  125. Copy,
  126. Move,
  127. NoDrop,
  128. NotAllowed,
  129. Grab,
  130. Grabbing,
  131. EResize,
  132. NResize,
  133. NeResize,
  134. NwResize,
  135. SResize,
  136. SeResize,
  137. SwResize,
  138. WResize,
  139. EwResize,
  140. NsResize,
  141. NeswResize,
  142. NwseResize,
  143. ColResize,
  144. RowResize,
  145. AllScroll,
  146. ZoomIn,
  147. ZoomOut,
  148. };
  149. enum class LineStyle {
  150. None,
  151. Hidden,
  152. Dotted,
  153. Dashed,
  154. Solid,
  155. Double,
  156. Groove,
  157. Ridge,
  158. Inset,
  159. Outset,
  160. };
  161. enum class ListStyleType {
  162. None,
  163. Disc,
  164. Circle,
  165. Square,
  166. Decimal,
  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. class StyleValue : public RefCounted<StyleValue> {
  182. public:
  183. virtual ~StyleValue();
  184. enum class Type {
  185. Invalid,
  186. Inherit,
  187. Initial,
  188. String,
  189. Length,
  190. Color,
  191. Identifier,
  192. Image,
  193. Position,
  194. };
  195. Type type() const { return m_type; }
  196. bool is_inherit() const { return type() == Type::Inherit; }
  197. bool is_initial() const { return type() == Type::Initial; }
  198. bool is_color() const { return type() == Type::Color; }
  199. bool is_identifier() const { return type() == Type::Identifier; }
  200. bool is_image() const { return type() == Type::Image; }
  201. bool is_string() const { return type() == Type::String; }
  202. bool is_length() const { return type() == Type::Length; }
  203. bool is_position() const { return type() == Type::Position; }
  204. virtual String to_string() const = 0;
  205. virtual Length to_length() const { return Length::make_auto(); }
  206. virtual Color to_color(const DOM::Document&) const { return {}; }
  207. CSS::ValueID to_identifier() const;
  208. virtual bool is_auto() const { return false; }
  209. bool operator==(const StyleValue& other) const { return equals(other); }
  210. bool operator!=(const StyleValue& other) const { return !(*this == other); }
  211. virtual bool equals(const StyleValue& other) const
  212. {
  213. if (type() != other.type())
  214. return false;
  215. return to_string() == other.to_string();
  216. }
  217. protected:
  218. explicit StyleValue(Type);
  219. private:
  220. Type m_type { Type::Invalid };
  221. };
  222. class StringStyleValue : public StyleValue {
  223. public:
  224. static NonnullRefPtr<StringStyleValue> create(const String& string)
  225. {
  226. return adopt(*new StringStyleValue(string));
  227. }
  228. virtual ~StringStyleValue() override { }
  229. String to_string() const override { return m_string; }
  230. private:
  231. explicit StringStyleValue(const String& string)
  232. : StyleValue(Type::String)
  233. , m_string(string)
  234. {
  235. }
  236. String m_string;
  237. };
  238. class LengthStyleValue : public StyleValue {
  239. public:
  240. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  241. {
  242. return adopt(*new LengthStyleValue(length));
  243. }
  244. virtual ~LengthStyleValue() override { }
  245. virtual String to_string() const override { return m_length.to_string(); }
  246. virtual Length to_length() const override { return m_length; }
  247. const Length& length() const { return m_length; }
  248. virtual bool is_auto() const override { return m_length.is_auto(); }
  249. virtual bool equals(const StyleValue& other) const override
  250. {
  251. if (type() != other.type())
  252. return false;
  253. return m_length == static_cast<const LengthStyleValue&>(other).m_length;
  254. }
  255. private:
  256. explicit LengthStyleValue(const Length& length)
  257. : StyleValue(Type::Length)
  258. , m_length(length)
  259. {
  260. }
  261. Length m_length;
  262. };
  263. class InitialStyleValue final : public StyleValue {
  264. public:
  265. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  266. virtual ~InitialStyleValue() override { }
  267. String to_string() const override { return "initial"; }
  268. private:
  269. InitialStyleValue()
  270. : StyleValue(Type::Initial)
  271. {
  272. }
  273. };
  274. class InheritStyleValue final : public StyleValue {
  275. public:
  276. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  277. virtual ~InheritStyleValue() override { }
  278. String to_string() const override { return "inherit"; }
  279. private:
  280. InheritStyleValue()
  281. : StyleValue(Type::Inherit)
  282. {
  283. }
  284. };
  285. class ColorStyleValue : public StyleValue {
  286. public:
  287. static NonnullRefPtr<ColorStyleValue> create(Color color)
  288. {
  289. return adopt(*new ColorStyleValue(color));
  290. }
  291. virtual ~ColorStyleValue() override { }
  292. Color color() const { return m_color; }
  293. String to_string() const override { return m_color.to_string(); }
  294. Color to_color(const DOM::Document&) const override { return m_color; }
  295. virtual bool equals(const StyleValue& other) const override
  296. {
  297. if (type() != other.type())
  298. return false;
  299. return m_color == static_cast<const ColorStyleValue&>(other).m_color;
  300. }
  301. private:
  302. explicit ColorStyleValue(Color color)
  303. : StyleValue(Type::Color)
  304. , m_color(color)
  305. {
  306. }
  307. Color m_color;
  308. };
  309. class IdentifierStyleValue final : public StyleValue {
  310. public:
  311. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  312. {
  313. return adopt(*new IdentifierStyleValue(id));
  314. }
  315. virtual ~IdentifierStyleValue() override { }
  316. CSS::ValueID id() const { return m_id; }
  317. virtual String to_string() const override;
  318. virtual Color to_color(const DOM::Document&) const override;
  319. virtual bool equals(const StyleValue& other) const override
  320. {
  321. if (type() != other.type())
  322. return false;
  323. return m_id == static_cast<const IdentifierStyleValue&>(other).m_id;
  324. }
  325. private:
  326. explicit IdentifierStyleValue(CSS::ValueID id)
  327. : StyleValue(Type::Identifier)
  328. , m_id(id)
  329. {
  330. }
  331. CSS::ValueID m_id { CSS::ValueID::Invalid };
  332. };
  333. class ImageStyleValue final
  334. : public StyleValue
  335. , public ImageResourceClient {
  336. public:
  337. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt(*new ImageStyleValue(url, document)); }
  338. virtual ~ImageStyleValue() override { }
  339. String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
  340. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  341. private:
  342. ImageStyleValue(const URL&, DOM::Document&);
  343. // ^ResourceClient
  344. virtual void resource_did_load() override;
  345. URL m_url;
  346. WeakPtr<DOM::Document> m_document;
  347. RefPtr<Gfx::Bitmap> m_bitmap;
  348. };
  349. inline CSS::ValueID StyleValue::to_identifier() const
  350. {
  351. if (is_identifier())
  352. return static_cast<const IdentifierStyleValue&>(*this).id();
  353. return CSS::ValueID::Invalid;
  354. }
  355. }