mirror of
				https://github.com/LadybirdBrowser/ladybird.git
				synced 2025-11-04 07:10:57 +00:00 
			
		
		
		
	This has always been a bit of a hack. Initially it made sense as a lot of properties that accept a length also accept `auto`, but while convenient, that leads to problems: It's easy to forget to check if a length is `auto`, and places that don't accept it end up with an invalid state lurking in the type system, which makes things unclear.
		
			
				
	
	
		
			124 lines
		
	
	
	
		
			2.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			124 lines
		
	
	
	
		
			2.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/*
 | 
						|
 * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
 | 
						|
 *
 | 
						|
 * SPDX-License-Identifier: BSD-2-Clause
 | 
						|
 */
 | 
						|
 | 
						|
#include <LibWeb/CSS/Size.h>
 | 
						|
 | 
						|
namespace Web::CSS {
 | 
						|
 | 
						|
Size::Size(Type type, Optional<LengthPercentage> length_percentage)
 | 
						|
    : m_type(type)
 | 
						|
    , m_length_percentage(move(length_percentage))
 | 
						|
{
 | 
						|
}
 | 
						|
 | 
						|
CSSPixels Size::to_px(Layout::Node const& node, CSSPixels reference_value) const
 | 
						|
{
 | 
						|
    if (!m_length_percentage.has_value())
 | 
						|
        return 0;
 | 
						|
    return m_length_percentage->resolved(node, reference_value).to_px(node);
 | 
						|
}
 | 
						|
 | 
						|
Size Size::make_auto()
 | 
						|
{
 | 
						|
    return Size { Type::Auto };
 | 
						|
}
 | 
						|
 | 
						|
Size Size::make_px(CSSPixels px)
 | 
						|
{
 | 
						|
    return make_length(Length::make_px(px));
 | 
						|
}
 | 
						|
 | 
						|
Size Size::make_length(Length length)
 | 
						|
{
 | 
						|
    return Size { Type::Length, move(length) };
 | 
						|
}
 | 
						|
 | 
						|
Size Size::make_percentage(Percentage percentage)
 | 
						|
{
 | 
						|
    return Size { Type::Percentage, move(percentage) };
 | 
						|
}
 | 
						|
 | 
						|
Size Size::make_calculated(NonnullRefPtr<CalculatedStyleValue const> calculated)
 | 
						|
{
 | 
						|
    return Size { Type::Calculated, move(calculated) };
 | 
						|
}
 | 
						|
 | 
						|
Size Size::make_length_percentage(LengthPercentage const& length_percentage)
 | 
						|
{
 | 
						|
    if (length_percentage.is_length())
 | 
						|
        return make_length(length_percentage.length());
 | 
						|
    if (length_percentage.is_percentage())
 | 
						|
        return make_percentage(length_percentage.percentage());
 | 
						|
    VERIFY(length_percentage.is_calculated());
 | 
						|
    return make_calculated(length_percentage.calculated());
 | 
						|
}
 | 
						|
 | 
						|
Size Size::make_min_content()
 | 
						|
{
 | 
						|
    return Size { Type::MinContent };
 | 
						|
}
 | 
						|
 | 
						|
Size Size::make_max_content()
 | 
						|
{
 | 
						|
    return Size { Type::MaxContent };
 | 
						|
}
 | 
						|
 | 
						|
Size Size::make_fit_content(LengthPercentage available_space)
 | 
						|
{
 | 
						|
    return Size { Type::FitContent, move(available_space) };
 | 
						|
}
 | 
						|
 | 
						|
Size Size::make_fit_content()
 | 
						|
{
 | 
						|
    return Size { Type::FitContent };
 | 
						|
}
 | 
						|
 | 
						|
Size Size::make_none()
 | 
						|
{
 | 
						|
    return Size { Type::None };
 | 
						|
}
 | 
						|
 | 
						|
bool Size::contains_percentage() const
 | 
						|
{
 | 
						|
    switch (m_type) {
 | 
						|
    case Type::Auto:
 | 
						|
    case Type::MinContent:
 | 
						|
    case Type::MaxContent:
 | 
						|
    case Type::None:
 | 
						|
        return false;
 | 
						|
    case Type::FitContent:
 | 
						|
        // FIXME: This should return m_length_percentage.contains_percentage()
 | 
						|
        //        but we have to update a lot of code to handle this.
 | 
						|
        return false;
 | 
						|
    default:
 | 
						|
        return m_length_percentage->contains_percentage();
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
String Size::to_string(SerializationMode mode) const
 | 
						|
{
 | 
						|
    switch (m_type) {
 | 
						|
    case Type::Auto:
 | 
						|
        return "auto"_string;
 | 
						|
    case Type::Calculated:
 | 
						|
    case Type::Length:
 | 
						|
    case Type::Percentage:
 | 
						|
        return m_length_percentage->to_string(mode);
 | 
						|
    case Type::MinContent:
 | 
						|
        return "min-content"_string;
 | 
						|
    case Type::MaxContent:
 | 
						|
        return "max-content"_string;
 | 
						|
    case Type::FitContent:
 | 
						|
        if (!m_length_percentage.has_value())
 | 
						|
            return "fit-content"_string;
 | 
						|
        return MUST(String::formatted("fit-content({})", m_length_percentage->to_string(mode)));
 | 
						|
    case Type::None:
 | 
						|
        return "none"_string;
 | 
						|
    }
 | 
						|
    VERIFY_NOT_REACHED();
 | 
						|
}
 | 
						|
 | 
						|
}
 |