srctree

Robin Linden parent 26c541b0 92b07232
layout: Collapse leading whitespace in more places

Previously we would only collapse leading whitespace in the first textnode in a run. After this change, we will also collapse whitespace inthe cases where the preceeding node ended in whitespace.

inlinesplit
layout/layout.cpp added: 9, removed: 6, total 3
@@ -86,6 +86,12 @@ void collapse_whitespace(LayoutBox &box) {
last_text_box = current;
last_text_box->layout_text = util::trim_start(std::get<std::string_view>(last_text_box->layout_text));
} else if (!std::holds_alternative<std::monostate>(current->layout_text)) {
if (last_text_box != nullptr
&& last_text_box->text()
.transform([](auto sv) { return sv.empty() || util::is_whitespace(sv.back()); })
.value_or(false)) {
current->layout_text = util::trim_start(std::get<std::string_view>(current->layout_text));
}
last_text_box = current;
} else if (ends_text_run(*current)) {
last_text_box->layout_text = util::trim_end(std::get<std::string_view>(last_text_box->layout_text));
 
layout/layout_test.cpp added: 9, removed: 6, total 3
@@ -1259,14 +1259,11 @@ int main() {
});
 
etest::test("whitespace collapsing: text split across multiple inline elements", [] {
// This will break when we add more complete ws-collapsing to the layout
// system as the 2 spaces between "cr" and "lf" will be collapsed to
// only being 1 space.
constexpr auto kFirstText = " cr "sv;
constexpr auto kSecondText = " lf "sv;
constexpr auto kCollapsedFirst = util::trim_start(kFirstText);
constexpr auto kFirstWidth = kCollapsedFirst.length() * 5;
constexpr auto kCollapsedSecond = util::trim_end(kSecondText);
constexpr auto kCollapsedSecond = util::trim(kSecondText);
constexpr auto kSecondWidth = kCollapsedSecond.length() * 5;
 
dom::Element a{.name{"a"}, .children{dom::Text{std::string{kSecondText}}}};