LibHTML: Collapse all-whitespace LayoutText into a single ' ' char.

This commit is contained in:
Andreas Kling 2019-06-15 23:17:08 +02:00
parent 8a0e21b22b
commit 0522a8f71c
Notes: sideshowbarker 2024-07-19 13:35:28 +09:00
3 changed files with 21 additions and 1 deletions

View File

@ -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<const LayoutText&>(node).node().data().characters());
printf(" \"%s\"", static_cast<const LayoutText&>(node).text().characters());
printf("\n");
++indent;
node.for_each_child([](auto& child) {

View File

@ -1,4 +1,5 @@
#include <LibHTML/LayoutText.h>
#include <ctype.h>
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();
}

View File

@ -10,6 +10,8 @@ public:
const Text& node() const { return static_cast<const Text&>(*LayoutNode::node()); }
const String& text() const;
virtual const char* class_name() const override { return "LayoutText"; }
virtual bool is_text() const final { return true; }