Serenity Operating System
at hosted 238 lines 6.6 kB view raw
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 27#pragma once 28 29#include <AK/RefCounted.h> 30#include <AK/RefPtr.h> 31#include <AK/String.h> 32#include <AK/StringView.h> 33#include <AK/URL.h> 34#include <AK/WeakPtr.h> 35#include <LibGfx/Color.h> 36#include <LibGfx/Bitmap.h> 37#include <LibWeb/CSS/Length.h> 38#include <LibWeb/CSS/PropertyID.h> 39 40namespace Web { 41 42class Document; 43 44namespace CSS { 45enum class ValueID { 46 Invalid, 47 VendorSpecificLink, 48 Center, 49 Left, 50 Right, 51 Justify, 52}; 53 54enum class Position { 55 Static, 56 Relative, 57 Absolute, 58 Fixed, 59 Sticky, 60}; 61} 62 63class StyleValue : public RefCounted<StyleValue> { 64public: 65 virtual ~StyleValue(); 66 67 enum class Type { 68 Invalid, 69 Inherit, 70 Initial, 71 String, 72 Length, 73 Color, 74 Identifier, 75 Image, 76 Position, 77 }; 78 79 Type type() const { return m_type; } 80 81 bool is_inherit() const { return type() == Type::Inherit; } 82 bool is_initial() const { return type() == Type::Initial; } 83 bool is_color() const { return type() == Type::Color; } 84 bool is_identifier() const { return type() == Type::Identifier; } 85 bool is_image() const { return type() == Type::Image; } 86 bool is_string() const { return type() == Type::String; } 87 bool is_length() const { return type() == Type::Length; } 88 bool is_position() const { return type() == Type::Position; } 89 90 virtual String to_string() const = 0; 91 virtual Length to_length() const { return {}; } 92 virtual Color to_color(const Document&) const { return {}; } 93 94 virtual bool is_auto() const { return false; } 95 96protected: 97 explicit StyleValue(Type); 98 99private: 100 Type m_type { Type::Invalid }; 101}; 102 103class StringStyleValue : public StyleValue { 104public: 105 static NonnullRefPtr<StringStyleValue> create(const String& string) 106 { 107 return adopt(*new StringStyleValue(string)); 108 } 109 virtual ~StringStyleValue() override {} 110 111 String to_string() const override { return m_string; } 112 113private: 114 explicit StringStyleValue(const String& string) 115 : StyleValue(Type::String) 116 , m_string(string) 117 { 118 } 119 120 String m_string; 121}; 122 123class LengthStyleValue : public StyleValue { 124public: 125 static NonnullRefPtr<LengthStyleValue> create(const Length& length) 126 { 127 return adopt(*new LengthStyleValue(length)); 128 } 129 virtual ~LengthStyleValue() override {} 130 131 virtual String to_string() const override { return m_length.to_string(); } 132 virtual Length to_length() const override { return m_length; } 133 134 const Length& length() const { return m_length; } 135 136 virtual bool is_auto() const override { return m_length.is_auto(); } 137 138private: 139 explicit LengthStyleValue(const Length& length) 140 : StyleValue(Type::Length) 141 , m_length(length) 142 { 143 } 144 145 Length m_length; 146}; 147 148class InitialStyleValue final : public StyleValue { 149public: 150 static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); } 151 virtual ~InitialStyleValue() override {} 152 153 String to_string() const override { return "initial"; } 154 155private: 156 InitialStyleValue() 157 : StyleValue(Type::Initial) 158 { 159 } 160}; 161 162class InheritStyleValue final : public StyleValue { 163public: 164 static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); } 165 virtual ~InheritStyleValue() override {} 166 167 String to_string() const override { return "inherit"; } 168 169private: 170 InheritStyleValue() 171 : StyleValue(Type::Inherit) 172 { 173 } 174}; 175 176class ColorStyleValue : public StyleValue { 177public: 178 static NonnullRefPtr<ColorStyleValue> create(Color color) 179 { 180 return adopt(*new ColorStyleValue(color)); 181 } 182 virtual ~ColorStyleValue() override {} 183 184 Color color() const { return m_color; } 185 String to_string() const override { return m_color.to_string(); } 186 Color to_color(const Document&) const override { return m_color; } 187 188private: 189 explicit ColorStyleValue(Color color) 190 : StyleValue(Type::Color) 191 , m_color(color) 192 { 193 } 194 195 Color m_color; 196}; 197 198class IdentifierStyleValue final : public StyleValue { 199public: 200 static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id) 201 { 202 return adopt(*new IdentifierStyleValue(id)); 203 } 204 virtual ~IdentifierStyleValue() override {} 205 206 CSS::ValueID id() const { return m_id; } 207 208 virtual String to_string() const override; 209 virtual Color to_color(const Document&) const override; 210 211private: 212 explicit IdentifierStyleValue(CSS::ValueID id) 213 : StyleValue(Type::Identifier) 214 , m_id(id) 215 { 216 } 217 218 CSS::ValueID m_id { CSS::ValueID::Invalid }; 219}; 220 221class ImageStyleValue final : public StyleValue { 222public: 223 static NonnullRefPtr<ImageStyleValue> create(const URL& url, Document& document) { return adopt(*new ImageStyleValue(url, document)); } 224 virtual ~ImageStyleValue() override {} 225 226 String to_string() const override { return String::format("Image{%s}", m_url.to_string().characters()); } 227 228 const Gfx::Bitmap* bitmap() const { return m_bitmap; } 229 230private: 231 ImageStyleValue(const URL&, Document&); 232 233 URL m_url; 234 WeakPtr<Document> m_document; 235 RefPtr<Gfx::Bitmap> m_bitmap; 236}; 237 238}