StyleValue.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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/Forward.h>
  38. #include <LibWeb/Loader/ImageResource.h>
  39. namespace Web::CSS {
  40. enum class ValueID {
  41. Invalid,
  42. VendorSpecificLink,
  43. VendorSpecificPaletteDesktopBackground,
  44. VendorSpecificPaletteActiveWindowBorder1,
  45. VendorSpecificPaletteActiveWindowBorder2,
  46. VendorSpecificPaletteActiveWindowTitle,
  47. VendorSpecificPaletteInactiveWindowBorder1,
  48. VendorSpecificPaletteInactiveWindowBorder2,
  49. VendorSpecificPaletteInactiveWindowTitle,
  50. VendorSpecificPaletteMovingWindowBorder1,
  51. VendorSpecificPaletteMovingWindowBorder2,
  52. VendorSpecificPaletteMovingWindowTitle,
  53. VendorSpecificPaletteHighlightWindowBorder1,
  54. VendorSpecificPaletteHighlightWindowBorder2,
  55. VendorSpecificPaletteHighlightWindowTitle,
  56. VendorSpecificPaletteMenuStripe,
  57. VendorSpecificPaletteMenuBase,
  58. VendorSpecificPaletteMenuBaseText,
  59. VendorSpecificPaletteMenuSelection,
  60. VendorSpecificPaletteMenuSelectionText,
  61. VendorSpecificPaletteWindow,
  62. VendorSpecificPaletteWindowText,
  63. VendorSpecificPaletteButton,
  64. VendorSpecificPaletteButtonText,
  65. VendorSpecificPaletteBase,
  66. VendorSpecificPaletteBaseText,
  67. VendorSpecificPaletteThreedHighlight,
  68. VendorSpecificPaletteThreedShadow1,
  69. VendorSpecificPaletteThreedShadow2,
  70. VendorSpecificPaletteHoverHighlight,
  71. VendorSpecificPaletteSelection,
  72. VendorSpecificPaletteSelectionText,
  73. VendorSpecificPaletteInactiveSelection,
  74. VendorSpecificPaletteInactiveSelectionText,
  75. VendorSpecificPaletteRubberBandFill,
  76. VendorSpecificPaletteRubberBandBorder,
  77. VendorSpecificPaletteLink,
  78. VendorSpecificPaletteActiveLink,
  79. VendorSpecificPaletteVisitedLink,
  80. VendorSpecificPaletteRuler,
  81. VendorSpecificPaletteRulerBorder,
  82. VendorSpecificPaletteRulerActiveText,
  83. VendorSpecificPaletteRulerInactiveText,
  84. VendorSpecificPaletteTextCursor,
  85. VendorSpecificPaletteFocusOutline,
  86. VendorSpecificPaletteSyntaxComment,
  87. VendorSpecificPaletteSyntaxNumber,
  88. VendorSpecificPaletteSyntaxString,
  89. VendorSpecificPaletteSyntaxType,
  90. VendorSpecificPaletteSyntaxPunctuation,
  91. VendorSpecificPaletteSyntaxOperator,
  92. VendorSpecificPaletteSyntaxKeyword,
  93. VendorSpecificPaletteSyntaxControlKeyword,
  94. VendorSpecificPaletteSyntaxIdentifier,
  95. VendorSpecificPaletteSyntaxPreprocessorStatement,
  96. VendorSpecificPaletteSyntaxPreprocessorValue,
  97. Center,
  98. Left,
  99. Right,
  100. Justify,
  101. VendorSpecificCenter,
  102. Bold,
  103. Bolder,
  104. Large,
  105. Larger,
  106. Lighter,
  107. Medium,
  108. Normal,
  109. Small,
  110. Smaller,
  111. XLarge,
  112. XSmall,
  113. XxLarge,
  114. XxSmall,
  115. XxxLarge,
  116. Static,
  117. Relative,
  118. Absolute,
  119. Fixed,
  120. Sticky,
  121. };
  122. enum class Position {
  123. Static,
  124. Relative,
  125. Absolute,
  126. Fixed,
  127. Sticky,
  128. };
  129. enum class TextAlign {
  130. Left,
  131. Center,
  132. Right,
  133. Justify,
  134. VendorSpecificCenter,
  135. };
  136. enum class Display {
  137. None,
  138. Block,
  139. Inline,
  140. InlineBlock,
  141. ListItem,
  142. Table,
  143. TableRow,
  144. TableCell,
  145. TableHeaderGroup,
  146. TableRowGroup,
  147. TableFooterGroup,
  148. };
  149. enum class WhiteSpace {
  150. Normal,
  151. Pre,
  152. Nowrap,
  153. PreLine,
  154. PreWrap,
  155. };
  156. enum class Float {
  157. None,
  158. Left,
  159. Right,
  160. };
  161. enum class Clear {
  162. None,
  163. Left,
  164. Right,
  165. Both,
  166. };
  167. enum class LineStyle {
  168. None,
  169. Hidden,
  170. Dotted,
  171. Dashed,
  172. Solid,
  173. Double,
  174. Groove,
  175. Ridge,
  176. Inset,
  177. Outset,
  178. };
  179. class StyleValue : public RefCounted<StyleValue> {
  180. public:
  181. virtual ~StyleValue();
  182. enum class Type {
  183. Invalid,
  184. Inherit,
  185. Initial,
  186. String,
  187. Length,
  188. Color,
  189. Identifier,
  190. Image,
  191. Position,
  192. };
  193. Type type() const { return m_type; }
  194. bool is_inherit() const { return type() == Type::Inherit; }
  195. bool is_initial() const { return type() == Type::Initial; }
  196. bool is_color() const { return type() == Type::Color; }
  197. bool is_identifier() const { return type() == Type::Identifier; }
  198. bool is_image() const { return type() == Type::Image; }
  199. bool is_string() const { return type() == Type::String; }
  200. bool is_length() const { return type() == Type::Length; }
  201. bool is_position() const { return type() == Type::Position; }
  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. virtual bool is_auto() const { return false; }
  206. bool operator==(const StyleValue& other) const { return equals(other); }
  207. bool operator!=(const StyleValue& other) const { return !(*this == other); }
  208. virtual bool equals(const StyleValue& other) const
  209. {
  210. if (type() != other.type())
  211. return false;
  212. return to_string() == other.to_string();
  213. }
  214. protected:
  215. explicit StyleValue(Type);
  216. private:
  217. Type m_type { Type::Invalid };
  218. };
  219. class StringStyleValue : public StyleValue {
  220. public:
  221. static NonnullRefPtr<StringStyleValue> create(const String& string)
  222. {
  223. return adopt(*new StringStyleValue(string));
  224. }
  225. virtual ~StringStyleValue() override { }
  226. String to_string() const override { return m_string; }
  227. private:
  228. explicit StringStyleValue(const String& string)
  229. : StyleValue(Type::String)
  230. , m_string(string)
  231. {
  232. }
  233. String m_string;
  234. };
  235. class LengthStyleValue : public StyleValue {
  236. public:
  237. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  238. {
  239. return adopt(*new LengthStyleValue(length));
  240. }
  241. virtual ~LengthStyleValue() override { }
  242. virtual String to_string() const override { return m_length.to_string(); }
  243. virtual Length to_length() const override { return m_length; }
  244. const Length& length() const { return m_length; }
  245. virtual bool is_auto() const override { return m_length.is_auto(); }
  246. private:
  247. explicit LengthStyleValue(const Length& length)
  248. : StyleValue(Type::Length)
  249. , m_length(length)
  250. {
  251. }
  252. Length m_length;
  253. };
  254. class InitialStyleValue final : public StyleValue {
  255. public:
  256. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  257. virtual ~InitialStyleValue() override { }
  258. String to_string() const override { return "initial"; }
  259. private:
  260. InitialStyleValue()
  261. : StyleValue(Type::Initial)
  262. {
  263. }
  264. };
  265. class InheritStyleValue final : public StyleValue {
  266. public:
  267. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  268. virtual ~InheritStyleValue() override { }
  269. String to_string() const override { return "inherit"; }
  270. private:
  271. InheritStyleValue()
  272. : StyleValue(Type::Inherit)
  273. {
  274. }
  275. };
  276. class ColorStyleValue : public StyleValue {
  277. public:
  278. static NonnullRefPtr<ColorStyleValue> create(Color color)
  279. {
  280. return adopt(*new ColorStyleValue(color));
  281. }
  282. virtual ~ColorStyleValue() override { }
  283. Color color() const { return m_color; }
  284. String to_string() const override { return m_color.to_string(); }
  285. Color to_color(const DOM::Document&) const override { return m_color; }
  286. private:
  287. explicit ColorStyleValue(Color color)
  288. : StyleValue(Type::Color)
  289. , m_color(color)
  290. {
  291. }
  292. Color m_color;
  293. };
  294. class IdentifierStyleValue final : public StyleValue {
  295. public:
  296. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  297. {
  298. return adopt(*new IdentifierStyleValue(id));
  299. }
  300. virtual ~IdentifierStyleValue() override { }
  301. CSS::ValueID id() const { return m_id; }
  302. virtual String to_string() const override;
  303. virtual Color to_color(const DOM::Document&) const override;
  304. virtual bool equals(const StyleValue& other) const override
  305. {
  306. if (type() != other.type())
  307. return false;
  308. return m_id == static_cast<const IdentifierStyleValue&>(other).m_id;
  309. }
  310. private:
  311. explicit IdentifierStyleValue(CSS::ValueID id)
  312. : StyleValue(Type::Identifier)
  313. , m_id(id)
  314. {
  315. }
  316. CSS::ValueID m_id { CSS::ValueID::Invalid };
  317. };
  318. class ImageStyleValue final
  319. : public StyleValue
  320. , public ImageResourceClient {
  321. public:
  322. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt(*new ImageStyleValue(url, document)); }
  323. virtual ~ImageStyleValue() override { }
  324. String to_string() const override { return String::format("Image{%s}", m_url.to_string().characters()); }
  325. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  326. private:
  327. ImageStyleValue(const URL&, DOM::Document&);
  328. // ^ResourceClient
  329. virtual void resource_did_load() override;
  330. URL m_url;
  331. WeakPtr<DOM::Document> m_document;
  332. RefPtr<Gfx::Bitmap> m_bitmap;
  333. };
  334. }