Pārlūkot izejas kodu

LibWeb: Port Layout::TextNode from DeprecatedString

Shannon Booth 1 gadu atpakaļ
vecāks
revīzija
56d10bf198

+ 1 - 1
Userland/Libraries/LibWeb/Dump.cpp

@@ -372,7 +372,7 @@ void dump_tree(StringBuilder& builder, Layout::Node const& layout_node, bool sho
                     for (size_t i = 0; i < indent; ++i)
                     for (size_t i = 0; i < indent; ++i)
                         builder.append("  "sv);
                         builder.append("  "sv);
                     auto& layout_text = static_cast<Layout::TextNode const&>(fragment.layout_node());
                     auto& layout_text = static_cast<Layout::TextNode const&>(fragment.layout_node());
-                    auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
+                    auto fragment_text = MUST(layout_text.text_for_rendering().substring_from_byte_offset(fragment.start(), fragment.length()));
                     builder.appendff("      \"{}\"\n", fragment_text);
                     builder.appendff("      \"{}\"\n", fragment_text);
                 }
                 }
             }
             }

+ 1 - 1
Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp

@@ -306,7 +306,7 @@ void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
                     next_width = iterator.next_non_whitespace_sequence_width();
                     next_width = iterator.next_non_whitespace_sequence_width();
                 } else {
                 } else {
                     // In whitespace-preserving contexts (white-space: pre*), we have to check manually.
                     // In whitespace-preserving contexts (white-space: pre*), we have to check manually.
-                    auto view = text_node.text_for_rendering().substring_view(item.offset_in_node, item.length_in_node);
+                    auto view = text_node.text_for_rendering().bytes_as_string_view().substring_view(item.offset_in_node, item.length_in_node);
                     is_whitespace = view.is_whitespace();
                     is_whitespace = view.is_whitespace();
                     if (is_whitespace)
                     if (is_whitespace)
                         next_width = iterator.next_non_whitespace_sequence_width();
                         next_width = iterator.next_non_whitespace_sequence_width();

+ 1 - 1
Userland/Libraries/LibWeb/Layout/InlineLevelIterator.cpp

@@ -148,7 +148,7 @@ CSSPixels InlineLevelIterator::next_non_whitespace_sequence_width()
             if (next_item.is_collapsible_whitespace)
             if (next_item.is_collapsible_whitespace)
                 break;
                 break;
             auto& next_text_node = verify_cast<Layout::TextNode>(*(next_item.node));
             auto& next_text_node = verify_cast<Layout::TextNode>(*(next_item.node));
-            auto next_view = next_text_node.text_for_rendering().substring_view(next_item.offset_in_node, next_item.length_in_node);
+            auto next_view = next_text_node.text_for_rendering().bytes_as_string_view().substring_view(next_item.offset_in_node, next_item.length_in_node);
             if (next_view.is_whitespace())
             if (next_view.is_whitespace())
                 break;
                 break;
         }
         }

+ 1 - 1
Userland/Libraries/LibWeb/Layout/LineBoxFragment.cpp

@@ -31,7 +31,7 @@ StringView LineBoxFragment::text() const
 {
 {
     if (!is<TextNode>(layout_node()))
     if (!is<TextNode>(layout_node()))
         return {};
         return {};
-    return verify_cast<TextNode>(layout_node()).text_for_rendering().substring_view(m_start, m_length);
+    return verify_cast<TextNode>(layout_node()).text_for_rendering().bytes_as_string_view().substring_view(m_start, m_length);
 }
 }
 
 
 CSSPixelRect const LineBoxFragment::absolute_rect() const
 CSSPixelRect const LineBoxFragment::absolute_rect() const

+ 24 - 22
Userland/Libraries/LibWeb/Layout/TextNode.cpp

@@ -33,7 +33,7 @@ static bool is_all_whitespace(StringView string)
 }
 }
 
 
 // https://w3c.github.io/mathml-core/#new-text-transform-values
 // https://w3c.github.io/mathml-core/#new-text-transform-values
-static DeprecatedString apply_math_auto_text_transform(DeprecatedString const& string)
+static String apply_math_auto_text_transform(String const& string)
 {
 {
 
 
     // https://w3c.github.io/mathml-core/#italic-mappings
     // https://w3c.github.io/mathml-core/#italic-mappings
@@ -268,27 +268,27 @@ static DeprecatedString apply_math_auto_text_transform(DeprecatedString const& s
         }
         }
     };
     };
 
 
-    StringBuilder builder(string.length());
+    StringBuilder builder(string.bytes().size());
 
 
-    for (auto code_point : Utf8View(string))
+    for (auto code_point : string.code_points())
         builder.append_code_point(map_code_point_to_italic(code_point));
         builder.append_code_point(map_code_point_to_italic(code_point));
 
 
-    return builder.to_deprecated_string();
+    return MUST(builder.to_string());
 }
 }
 
 
-static ErrorOr<DeprecatedString> apply_text_transform(DeprecatedString const& string, CSS::TextTransform text_transform)
+static ErrorOr<String> apply_text_transform(String const& string, CSS::TextTransform text_transform)
 {
 {
     switch (text_transform) {
     switch (text_transform) {
     case CSS::TextTransform::Uppercase:
     case CSS::TextTransform::Uppercase:
-        return Unicode::to_unicode_uppercase_full(string);
+        return string.to_uppercase();
     case CSS::TextTransform::Lowercase:
     case CSS::TextTransform::Lowercase:
-        return Unicode::to_unicode_lowercase_full(string);
+        return string.to_lowercase();
     case CSS::TextTransform::None:
     case CSS::TextTransform::None:
         return string;
         return string;
     case CSS::TextTransform::MathAuto:
     case CSS::TextTransform::MathAuto:
         return apply_math_auto_text_transform(string);
         return apply_math_auto_text_transform(string);
     case CSS::TextTransform::Capitalize: {
     case CSS::TextTransform::Capitalize: {
-        return TRY(Unicode::to_unicode_titlecase_full(string, {}, Unicode::TrailingCodePointTransformation::PreserveExisting)).to_deprecated_string();
+        return string.to_titlecase({}, TrailingCodePointTransformation::PreserveExisting);
     }
     }
     case CSS::TextTransform::FullSizeKana:
     case CSS::TextTransform::FullSizeKana:
     case CSS::TextTransform::FullWidth:
     case CSS::TextTransform::FullWidth:
@@ -304,7 +304,7 @@ void TextNode::invalidate_text_for_rendering()
     m_text_for_rendering = {};
     m_text_for_rendering = {};
 }
 }
 
 
-DeprecatedString const& TextNode::text_for_rendering() const
+String const& TextNode::text_for_rendering() const
 {
 {
     if (!m_text_for_rendering.has_value())
     if (!m_text_for_rendering.has_value())
         const_cast<TextNode*>(this)->compute_text_for_rendering();
         const_cast<TextNode*>(this)->compute_text_for_rendering();
@@ -330,10 +330,12 @@ void TextNode::compute_text_for_rendering()
     if (dom_node().is_editable() && !dom_node().is_uninteresting_whitespace_node())
     if (dom_node().is_editable() && !dom_node().is_uninteresting_whitespace_node())
         collapse = false;
         collapse = false;
 
 
-    auto data = apply_text_transform(dom_node().data().to_deprecated_string(), computed_values().text_transform()).release_value_but_fixme_should_propagate_errors();
+    auto data = apply_text_transform(dom_node().data(), computed_values().text_transform()).release_value_but_fixme_should_propagate_errors();
+
+    auto data_view = data.bytes_as_string_view();
 
 
     if (dom_node().is_password_input()) {
     if (dom_node().is_password_input()) {
-        m_text_for_rendering = DeprecatedString::repeated('*', data.length());
+        m_text_for_rendering = MUST(String::repeated('*', data_view.length()));
         return;
         return;
     }
     }
 
 
@@ -343,9 +345,9 @@ void TextNode::compute_text_for_rendering()
     }
     }
 
 
     // NOTE: A couple fast returns to avoid unnecessarily allocating a StringBuilder.
     // NOTE: A couple fast returns to avoid unnecessarily allocating a StringBuilder.
-    if (data.length() == 1) {
-        if (is_ascii_space(data[0])) {
-            static DeprecatedString s_single_space_string = " ";
+    if (data_view.length() == 1) {
+        if (is_ascii_space(data_view[0])) {
+            static String s_single_space_string = " "_string;
             m_text_for_rendering = s_single_space_string;
             m_text_for_rendering = s_single_space_string;
         } else {
         } else {
             m_text_for_rendering = data;
             m_text_for_rendering = data;
@@ -354,7 +356,7 @@ void TextNode::compute_text_for_rendering()
     }
     }
 
 
     bool contains_space = false;
     bool contains_space = false;
-    for (auto& c : data) {
+    for (auto c : data_view) {
         if (is_ascii_space(c)) {
         if (is_ascii_space(c)) {
             contains_space = true;
             contains_space = true;
             break;
             break;
@@ -365,26 +367,26 @@ void TextNode::compute_text_for_rendering()
         return;
         return;
     }
     }
 
 
-    StringBuilder builder(data.length());
+    StringBuilder builder(data_view.length());
     size_t index = 0;
     size_t index = 0;
 
 
-    auto skip_over_whitespace = [&index, &data] {
-        while (index < data.length() && is_ascii_space(data[index]))
+    auto skip_over_whitespace = [&index, &data_view] {
+        while (index < data_view.length() && is_ascii_space(data_view[index]))
             ++index;
             ++index;
     };
     };
 
 
-    while (index < data.length()) {
-        if (is_ascii_space(data[index])) {
+    while (index < data_view.length()) {
+        if (is_ascii_space(data_view[index])) {
             builder.append(' ');
             builder.append(' ');
             ++index;
             ++index;
             skip_over_whitespace();
             skip_over_whitespace();
         } else {
         } else {
-            builder.append(data[index]);
+            builder.append(data_view[index]);
             ++index;
             ++index;
         }
         }
     }
     }
 
 
-    m_text_for_rendering = builder.to_deprecated_string();
+    m_text_for_rendering = MUST(builder.to_string());
 }
 }
 
 
 TextNode::ChunkIterator::ChunkIterator(StringView text, bool wrap_lines, bool respect_linebreaks)
 TextNode::ChunkIterator::ChunkIterator(StringView text, bool wrap_lines, bool respect_linebreaks)

+ 2 - 2
Userland/Libraries/LibWeb/Layout/TextNode.h

@@ -23,7 +23,7 @@ public:
 
 
     const DOM::Text& dom_node() const { return static_cast<const DOM::Text&>(*Node::dom_node()); }
     const DOM::Text& dom_node() const { return static_cast<const DOM::Text&>(*Node::dom_node()); }
 
 
-    DeprecatedString const& text_for_rendering() const;
+    String const& text_for_rendering() const;
 
 
     struct Chunk {
     struct Chunk {
         Utf8View view;
         Utf8View view;
@@ -55,7 +55,7 @@ public:
 private:
 private:
     virtual bool is_text_node() const final { return true; }
     virtual bool is_text_node() const final { return true; }
 
 
-    Optional<DeprecatedString> m_text_for_rendering;
+    Optional<String> m_text_for_rendering;
 };
 };
 
 
 template<>
 template<>

+ 2 - 2
Userland/Libraries/LibWeb/Layout/TreeBuilder.cpp

@@ -234,13 +234,13 @@ ErrorOr<void> TreeBuilder::create_pseudo_element_if_needed(DOM::Element& element
 
 
 static bool is_ignorable_whitespace(Layout::Node const& node)
 static bool is_ignorable_whitespace(Layout::Node const& node)
 {
 {
-    if (node.is_text_node() && static_cast<TextNode const&>(node).text_for_rendering().is_whitespace())
+    if (node.is_text_node() && static_cast<TextNode const&>(node).text_for_rendering().bytes_as_string_view().is_whitespace())
         return true;
         return true;
 
 
     if (node.is_anonymous() && node.is_block_container() && static_cast<BlockContainer const&>(node).children_are_inline()) {
     if (node.is_anonymous() && node.is_block_container() && static_cast<BlockContainer const&>(node).children_are_inline()) {
         bool contains_only_white_space = true;
         bool contains_only_white_space = true;
         node.for_each_in_inclusive_subtree_of_type<TextNode>([&contains_only_white_space](auto& text_node) {
         node.for_each_in_inclusive_subtree_of_type<TextNode>([&contains_only_white_space](auto& text_node) {
-            if (!text_node.text_for_rendering().is_whitespace()) {
+            if (!text_node.text_for_rendering().bytes_as_string_view().is_whitespace()) {
                 contains_only_white_space = false;
                 contains_only_white_space = false;
                 return IterationDecision::Break;
                 return IterationDecision::Break;
             }
             }

+ 4 - 4
Userland/Libraries/LibWeb/Page/EventHandler.cpp

@@ -615,7 +615,7 @@ bool EventHandler::handle_doubleclick(CSSPixelPoint position, CSSPixelPoint scre
                 // Start from one before the index position to prevent selecting only spaces between words, caused by the addition below.
                 // Start from one before the index position to prevent selecting only spaces between words, caused by the addition below.
                 // This also helps us dealing with cases where index is equal to the string length.
                 // This also helps us dealing with cases where index is equal to the string length.
                 for (int i = result->index_in_node - 1; i >= 0; --i) {
                 for (int i = result->index_in_node - 1; i >= 0; --i) {
-                    if (is_ascii_space(text_for_rendering[i])) {
+                    if (is_ascii_space(text_for_rendering.bytes_as_string_view()[i])) {
                         // Don't include the space in the selection
                         // Don't include the space in the selection
                         return i + 1;
                         return i + 1;
                     }
                     }
@@ -624,11 +624,11 @@ bool EventHandler::handle_doubleclick(CSSPixelPoint position, CSSPixelPoint scre
             }();
             }();
 
 
             int first_word_break_after = [&] {
             int first_word_break_after = [&] {
-                for (size_t i = result->index_in_node; i < text_for_rendering.length(); ++i) {
-                    if (is_ascii_space(text_for_rendering[i]))
+                for (size_t i = result->index_in_node; i < text_for_rendering.bytes().size(); ++i) {
+                    if (is_ascii_space(text_for_rendering.bytes_as_string_view()[i]))
                         return i;
                         return i;
                 }
                 }
-                return text_for_rendering.length();
+                return text_for_rendering.bytes().size();
             }();
             }();
 
 
             auto& realm = node->document().realm();
             auto& realm = node->document().realm();

+ 1 - 1
Userland/Libraries/LibWeb/Painting/PaintableBox.cpp

@@ -645,7 +645,7 @@ static void paint_text_fragment(PaintContext& context, Layout::TextNode const& t
         auto text = text_node.text_for_rendering();
         auto text = text_node.text_for_rendering();
 
 
         DevicePixelPoint baseline_start { fragment_absolute_device_rect.x(), fragment_absolute_device_rect.y() + context.rounded_device_pixels(fragment.baseline()) };
         DevicePixelPoint baseline_start { fragment_absolute_device_rect.x(), fragment_absolute_device_rect.y() + context.rounded_device_pixels(fragment.baseline()) };
-        Utf8View view { text.substring_view(fragment.start(), fragment.length()) };
+        Utf8View view { text.code_points().substring_view(fragment.start(), fragment.length()) };
 
 
         auto& scaled_font = fragment.layout_node().scaled_font(context);
         auto& scaled_font = fragment.layout_node().scaled_font(context);