浏览代码

LibWeb: Move white-space into LayoutStyle

Andreas Kling 5 年之前
父节点
当前提交
6b334e02e6

+ 19 - 0
Libraries/LibWeb/CSS/StyleProperties.cpp

@@ -244,6 +244,25 @@ CSS::TextAlign StyleProperties::text_align() const
     return CSS::TextAlign::Left;
     return CSS::TextAlign::Left;
 }
 }
 
 
+Optional<CSS::WhiteSpace> StyleProperties::white_space() const
+{
+    auto value = property(CSS::PropertyID::WhiteSpace);
+    if (!value.has_value() || !value.value()->is_string())
+        return {};
+    auto string = value.value()->to_string();
+    if (string == "normal")
+        return CSS::WhiteSpace::Normal;
+    if (string == "nowrap")
+        return CSS::WhiteSpace::Nowrap;
+    if (string == "pre")
+        return CSS::WhiteSpace::Pre;
+    if (string == "pre-line")
+        return CSS::WhiteSpace::PreLine;
+    if (string == "pre-wrap")
+        return CSS::WhiteSpace::PreWrap;
+    return {};
+}
+
 CSS::Display StyleProperties::display() const
 CSS::Display StyleProperties::display() const
 {
 {
     auto display = string_or_fallback(CSS::PropertyID::Display, "inline");
     auto display = string_or_fallback(CSS::PropertyID::Display, "inline");

+ 1 - 0
Libraries/LibWeb/CSS/StyleProperties.h

@@ -61,6 +61,7 @@ public:
     Color color_or_fallback(CSS::PropertyID, const Document&, Color fallback) const;
     Color color_or_fallback(CSS::PropertyID, const Document&, Color fallback) const;
     CSS::TextAlign text_align() const;
     CSS::TextAlign text_align() const;
     CSS::Display display() const;
     CSS::Display display() const;
+    Optional<CSS::WhiteSpace> white_space() const;
 
 
     const Gfx::Font& font() const
     const Gfx::Font& font() const
     {
     {

+ 8 - 0
Libraries/LibWeb/CSS/StyleValue.h

@@ -135,6 +135,14 @@ enum class Display {
     TableRowGroup,
     TableRowGroup,
 };
 };
 
 
+enum class WhiteSpace {
+    Normal,
+    Pre,
+    Nowrap,
+    PreLine,
+    PreWrap,
+};
+
 }
 }
 
 
 class StyleValue : public RefCounted<StyleValue> {
 class StyleValue : public RefCounted<StyleValue> {

+ 5 - 0
Libraries/LibWeb/Layout/LayoutNode.cpp

@@ -225,6 +225,11 @@ void LayoutNodeWithStyle::apply_style(const StyleProperties& specified_style)
 
 
     style.set_position(specified_style.position());
     style.set_position(specified_style.position());
     style.set_text_align(specified_style.text_align());
     style.set_text_align(specified_style.text_align());
+
+    auto white_space = specified_style.white_space();
+    if (white_space.has_value())
+        style.set_white_space(white_space.value());
+
     style.set_z_index(specified_style.z_index());
     style.set_z_index(specified_style.z_index());
     style.set_width(specified_style.length_or_fallback(CSS::PropertyID::Width, {}));
     style.set_width(specified_style.length_or_fallback(CSS::PropertyID::Width, {}));
     style.set_min_width(specified_style.length_or_fallback(CSS::PropertyID::MinWidth, {}));
     style.set_min_width(specified_style.length_or_fallback(CSS::PropertyID::MinWidth, {}));

+ 8 - 0
Libraries/LibWeb/Layout/LayoutStyle.h

@@ -31,11 +31,17 @@
 
 
 namespace Web {
 namespace Web {
 
 
+class InitialValues {
+public:
+    static CSS::WhiteSpace white_space() { return CSS::WhiteSpace::Normal; }
+};
+
 class LayoutStyle {
 class LayoutStyle {
 public:
 public:
     Optional<int> z_index() const { return m_z_index; }
     Optional<int> z_index() const { return m_z_index; }
     CSS::TextAlign text_align() const { return m_text_align; }
     CSS::TextAlign text_align() const { return m_text_align; }
     CSS::Position position() const { return m_position; }
     CSS::Position position() const { return m_position; }
+    CSS::WhiteSpace white_space() const { return m_white_space; }
     const Length& width() const { return m_width; }
     const Length& width() const { return m_width; }
     const Length& min_width() const { return m_min_width; }
     const Length& min_width() const { return m_min_width; }
     const Length& max_width() const { return m_max_width; }
     const Length& max_width() const { return m_max_width; }
@@ -47,6 +53,7 @@ protected:
     Optional<int> m_z_index;
     Optional<int> m_z_index;
     CSS::TextAlign m_text_align;
     CSS::TextAlign m_text_align;
     CSS::Position m_position;
     CSS::Position m_position;
+    CSS::WhiteSpace m_white_space { InitialValues::white_space() };
     Length m_width;
     Length m_width;
     Length m_min_width;
     Length m_min_width;
     Length m_max_width;
     Length m_max_width;
@@ -63,6 +70,7 @@ public:
     void set_z_index(Optional<int> value) { m_z_index = value; }
     void set_z_index(Optional<int> value) { m_z_index = value; }
     void set_text_align(CSS::TextAlign text_align) { m_text_align = text_align; }
     void set_text_align(CSS::TextAlign text_align) { m_text_align = text_align; }
     void set_position(CSS::Position position) { m_position = position; }
     void set_position(CSS::Position position) { m_position = position; }
+    void set_white_space(CSS::WhiteSpace value) { m_white_space = value; }
     void set_width(const Length& width) { m_width = width; }
     void set_width(const Length& width) { m_width = width; }
     void set_min_width(const Length& width) { m_min_width = width; }
     void set_min_width(const Length& width) { m_min_width = width; }
     void set_max_width(const Length& width) { m_max_width = width; }
     void set_max_width(const Length& width) { m_max_width = width; }

+ 4 - 5
Libraries/LibWeb/Layout/LayoutText.cpp

@@ -253,21 +253,20 @@ void LayoutText::split_into_lines(LayoutBlock& container, LayoutMode layout_mode
     bool do_collapse = true;
     bool do_collapse = true;
     bool do_wrap_lines = true;
     bool do_wrap_lines = true;
     bool do_wrap_breaks = false;
     bool do_wrap_breaks = false;
-    auto white_space_prop = specified_style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal");
 
 
-    if (white_space_prop == "nowrap") {
+    if (style().white_space() == CSS::WhiteSpace::Nowrap) {
         do_collapse = true;
         do_collapse = true;
         do_wrap_lines = false;
         do_wrap_lines = false;
         do_wrap_breaks = false;
         do_wrap_breaks = false;
-    } else if (white_space_prop == "pre") {
+    } else if (style().white_space() == CSS::WhiteSpace::Pre) {
         do_collapse = false;
         do_collapse = false;
         do_wrap_lines = false;
         do_wrap_lines = false;
         do_wrap_breaks = true;
         do_wrap_breaks = true;
-    } else if (white_space_prop == "pre-line") {
+    } else if (style().white_space() == CSS::WhiteSpace::PreLine) {
         do_collapse = true;
         do_collapse = true;
         do_wrap_lines = true;
         do_wrap_lines = true;
         do_wrap_breaks = true;
         do_wrap_breaks = true;
-    } else if (white_space_prop == "pre-wrap") {
+    } else if (style().white_space() == CSS::WhiteSpace::PreWrap) {
         do_collapse = false;
         do_collapse = false;
         do_wrap_lines = true;
         do_wrap_lines = true;
         do_wrap_breaks = true;
         do_wrap_breaks = true;