summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h
blob: 92f97d2a6400c127013ffe006c288906408da12f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/String.h>
#include <AK/Vector.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/CSS/StyleValue.h>

namespace Web::CSS {

struct StyleProperty {
    CSS::PropertyID property_id;
    NonnullRefPtr<StyleValue> value;
    bool important { false };
};

class CSSStyleDeclaration
    : public RefCounted<CSSStyleDeclaration>
    , public Bindings::Wrappable {
public:
    using WrapperType = Bindings::CSSStyleDeclarationWrapper;

    static NonnullRefPtr<CSSStyleDeclaration> create(Vector<StyleProperty>&& properties)
    {
        return adopt(*new CSSStyleDeclaration(move(properties)));
    }

    virtual ~CSSStyleDeclaration();

    const Vector<StyleProperty>& properties() const { return m_properties; }

    size_t length() const { return m_properties.size(); }
    String item(size_t index) const;

protected:
    explicit CSSStyleDeclaration(Vector<StyleProperty>&&);

private:
    friend class Bindings::CSSStyleDeclarationWrapper;

    Vector<StyleProperty> m_properties;
};

class ElementInlineCSSStyleDeclaration final : public CSSStyleDeclaration {
public:
    static NonnullRefPtr<ElementInlineCSSStyleDeclaration> create(DOM::Element& element) { return adopt(*new ElementInlineCSSStyleDeclaration(element)); }
    virtual ~ElementInlineCSSStyleDeclaration() override;

    DOM::Element* element() { return m_element.ptr(); }
    const DOM::Element* element() const { return m_element.ptr(); }

private:
    explicit ElementInlineCSSStyleDeclaration(DOM::Element&);

    WeakPtr<DOM::Element> m_element;
};

}

namespace Web::Bindings {

CSSStyleDeclarationWrapper* wrap(JS::GlobalObject&, CSS::CSSStyleDeclaration&);

}