From 0522a8f71c027b1ae19a81cfc81ad0a01cc667d2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 15 Jun 2019 23:17:08 +0200 Subject: [PATCH] LibHTML: Collapse all-whitespace LayoutText into a single ' ' char. --- LibHTML/Dump.cpp | 2 +- LibHTML/LayoutText.cpp | 18 ++++++++++++++++++ LibHTML/LayoutText.h | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/LibHTML/Dump.cpp b/LibHTML/Dump.cpp index db50e4ecc2..41f964e299 100644 --- a/LibHTML/Dump.cpp +++ b/LibHTML/Dump.cpp @@ -38,7 +38,7 @@ void dump_tree(const LayoutNode& node) printf(" "); printf("%s{%p}", node.class_name(), &node); if (node.is_text()) - printf(" \"%s\"", static_cast(node).node().data().characters()); + printf(" \"%s\"", static_cast(node).text().characters()); printf("\n"); ++indent; node.for_each_child([](auto& child) { diff --git a/LibHTML/LayoutText.cpp b/LibHTML/LayoutText.cpp index e71c0cd8b4..8c1feb5f91 100644 --- a/LibHTML/LayoutText.cpp +++ b/LibHTML/LayoutText.cpp @@ -1,4 +1,5 @@ #include +#include LayoutText::LayoutText(const Text& text) : LayoutNode(&text) @@ -8,3 +9,20 @@ LayoutText::LayoutText(const Text& text) LayoutText::~LayoutText() { } + +static bool is_all_whitespace(const String& string) +{ + for (int i = 0; i < string.length(); ++i) { + if (!isspace(string[i])) + return false; + } + return true; +} + +const String& LayoutText::text() const +{ + static String one_space = " "; + if (is_all_whitespace(node().data())) + return one_space; + return node().data(); +} diff --git a/LibHTML/LayoutText.h b/LibHTML/LayoutText.h index 42127c0778..f9964b29c4 100644 --- a/LibHTML/LayoutText.h +++ b/LibHTML/LayoutText.h @@ -10,6 +10,8 @@ public: const Text& node() const { return static_cast(*LayoutNode::node()); } + const String& text() const; + virtual const char* class_name() const override { return "LayoutText"; } virtual bool is_text() const final { return true; }