-
-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LibWeb: Parse the rotate css property
- Loading branch information
Showing
10 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Userland/Libraries/LibWeb/CSS/StyleValues/RotationStyleValue.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2024, Steffen T. Larssen <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <AK/String.h> | ||
#include <LibWeb/CSS/StyleValues/CSSMathValue.h> | ||
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h> | ||
|
||
#include "RotationStyleValue.h" | ||
|
||
namespace Web::CSS { | ||
|
||
// https://www.w3.org/TR/2021/WD-css-transforms-2-20211109/#individual-transform-serialization | ||
String RotationStyleValue::to_string() const | ||
{ | ||
auto resolve_to_number = [](ValueComparingNonnullRefPtr<CSSStyleValue const> const& value) -> double { | ||
if (value->is_number()) | ||
return value->as_number().number(); | ||
if (value->is_math() && value->as_math().resolves_to_number()) | ||
return value->as_math().resolve_number().value(); | ||
|
||
VERIFY_NOT_REACHED(); | ||
}; | ||
|
||
auto x_value = resolve_to_number(m_properties.rotation_x); | ||
auto y_value = resolve_to_number(m_properties.rotation_y); | ||
auto z_value = resolve_to_number(m_properties.rotation_z); | ||
|
||
// If a rotation about the z axis (that is, in 2D) is specified, the property must serialize as just an <angle>. | ||
if (x_value == 0 && y_value == 0 && z_value == 1) | ||
return m_properties.angle->to_string(); | ||
|
||
// If the axis is parallel with the x or y axes, it must serialize as the appropriate keyword. | ||
if (x_value == 1 && y_value == 0 && z_value == 0) | ||
return MUST(String::formatted("x {}", m_properties.angle->to_string())); | ||
|
||
if (x_value == 0 && y_value == 1 && z_value == 0) | ||
return MUST(String::formatted("y {}", m_properties.angle->to_string())); | ||
|
||
// It must serialize as the keyword none if and only if none was originally specified. | ||
// NOTE: This is handled by returning a keyword from the parser. | ||
|
||
// If any other rotation is specified, the property must serialize with an axis specified. | ||
return MUST(String::formatted("{} {} {} {}", x_value, y_value, z_value, m_properties.angle->to_string())); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
Userland/Libraries/LibWeb/CSS/StyleValues/RotationStyleValue.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2024, Steffen T. Larssen <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <LibWeb/CSS/CSSStyleValue.h> | ||
|
||
namespace Web::CSS { | ||
|
||
class RotationStyleValue : public StyleValueWithDefaultOperators<RotationStyleValue> { | ||
public: | ||
static ValueComparingNonnullRefPtr<RotationStyleValue> create(ValueComparingNonnullRefPtr<CSSStyleValue const> angle, ValueComparingNonnullRefPtr<CSSStyleValue const> rotation_x, ValueComparingNonnullRefPtr<CSSStyleValue const> rotation_y, ValueComparingNonnullRefPtr<CSSStyleValue const> rotation_z) | ||
{ | ||
return adopt_ref(*new (nothrow) RotationStyleValue(move(angle), move(rotation_x), move(rotation_y), move(rotation_z))); | ||
} | ||
|
||
virtual ~RotationStyleValue() override = default; | ||
|
||
ValueComparingNonnullRefPtr<CSSStyleValue const> const& angle() const { return m_properties.angle; } | ||
ValueComparingNonnullRefPtr<CSSStyleValue const> const& rotation_x() const { return m_properties.rotation_x; } | ||
ValueComparingNonnullRefPtr<CSSStyleValue const> const& rotation_y() const { return m_properties.rotation_y; } | ||
ValueComparingNonnullRefPtr<CSSStyleValue const> const& rotation_z() const { return m_properties.rotation_z; } | ||
|
||
virtual String to_string() const override; | ||
|
||
bool properties_equal(RotationStyleValue const& other) const { return m_properties == other.m_properties; } | ||
|
||
private: | ||
explicit RotationStyleValue( | ||
ValueComparingNonnullRefPtr<CSSStyleValue const> angle, | ||
ValueComparingNonnullRefPtr<CSSStyleValue const> rotation_x, | ||
ValueComparingNonnullRefPtr<CSSStyleValue const> rotation_y, | ||
ValueComparingNonnullRefPtr<CSSStyleValue const> rotation_z) | ||
: StyleValueWithDefaultOperators(Type::Rotation) | ||
, m_properties { | ||
.angle = move(angle), | ||
.rotation_x = move(rotation_x), | ||
.rotation_y = move(rotation_y), | ||
.rotation_z = move(rotation_z) | ||
} | ||
{ | ||
} | ||
|
||
struct Properties { | ||
ValueComparingNonnullRefPtr<CSSStyleValue const> angle; | ||
ValueComparingNonnullRefPtr<CSSStyleValue const> rotation_x; | ||
ValueComparingNonnullRefPtr<CSSStyleValue const> rotation_y; | ||
ValueComparingNonnullRefPtr<CSSStyleValue const> rotation_z; | ||
bool operator==(Properties const&) const = default; | ||
} m_properties; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters