Skip to content

Truly independent web browser

3.1 KB
Raw Blame
/*
 * Copyright (c) 2023, Ali Mohammad Pur <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibWeb/CSS/StyleValues/StyleValue.h>

namespace Web::CSS {

class ShorthandStyleValue final : public StyleValueWithDefaultOperators<ShorthandStyleValue> {
public:
    static ValueComparingNonnullRefPtr<ShorthandStyleValue const> create(PropertyID shorthand, Vector<PropertyID> sub_properties, Vector<ValueComparingNonnullRefPtr<StyleValue const>> values)
    {
        return adopt_ref(*new ShorthandStyleValue(shorthand, move(sub_properties), move(values)));
    }
    virtual ~ShorthandStyleValue() override;

    StyleValueVector values() const
    {
        auto const& values = m_value->shorthand._0->values;
        StyleValueVector result;
        result.ensure_capacity(values.length());
        for (size_t i = 0; i < values.length(); ++i) {
            auto const* child_data = static_cast<StyleValueFFI::StyleValueData const*>(values.pointer[i].pointer);
            result.unchecked_append(StyleValue::adopt_rust_style_value_data(StyleValueFFI::rust_style_value_retain(child_data)));
        }
        return result;
    }

    ValueComparingRefPtr<StyleValue const> longhand(PropertyID) const;

private:
    explicit ShorthandStyleValue(StyleValueFFI::StyleValueData const* data)
        : StyleValueWithDefaultOperators(Type::Shorthand, data)
    {
    }

    ShorthandStyleValue(PropertyID shorthand, Vector<PropertyID> sub_properties, Vector<ValueComparingNonnullRefPtr<StyleValue const>> values);

    // NB: StyleValue dispatches operations by type tag, so it may call private impls.
    friend class StyleValue;
    void set_style_sheet(StyleSheetState*);

    static StyleValueFFI::StyleValueData const* make_shorthand_data(PropertyID shorthand, Vector<PropertyID> const& sub_properties, Vector<ValueComparingNonnullRefPtr<StyleValue const>> const& values)
    {
        Vector<StyleValueFFI::StyleValueData const*> pointers;
        pointers.ensure_capacity(values.size());
        for (auto const& value : values)
            pointers.unchecked_append(StyleValueFFI::rust_style_value_retain(value->rust_style_value_data()));
        Vector<u16> property_ids;
        property_ids.ensure_capacity(sub_properties.size());
        for (auto property : sub_properties)
            property_ids.unchecked_append(to_underlying(property));
        return StyleValueFFI::rust_style_value_create_shorthand(to_underlying(shorthand), property_ids.data(), property_ids.size(), pointers.data(), pointers.size());
    }

    size_t size() const { return m_value->shorthand._0->values.length(); }

    PropertyID sub_property_at(size_t i) const
    {
        return static_cast<PropertyID>(m_value->shorthand._0->sub_properties.pointer[i]);
    }

    ValueComparingNonnullRefPtr<StyleValue const> value_at(size_t i) const
    {
        auto const* child_data = static_cast<StyleValueFFI::StyleValueData const*>(m_value->shorthand._0->values.pointer[i].pointer);
        return StyleValue::adopt_rust_style_value_data(StyleValueFFI::rust_style_value_retain(child_data));
    }
};

}