mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:17:36 +00:00
LibHTML: Collapse all-whitespace LayoutText into a single ' ' char.
This commit is contained in:
parent
8a0e21b22b
commit
0522a8f71c
3 changed files with 21 additions and 1 deletions
|
@ -38,7 +38,7 @@ void dump_tree(const LayoutNode& node)
|
||||||
printf(" ");
|
printf(" ");
|
||||||
printf("%s{%p}", node.class_name(), &node);
|
printf("%s{%p}", node.class_name(), &node);
|
||||||
if (node.is_text())
|
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");
|
printf("\n");
|
||||||
++indent;
|
++indent;
|
||||||
node.for_each_child([](auto& child) {
|
node.for_each_child([](auto& child) {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include <LibHTML/LayoutText.h>
|
#include <LibHTML/LayoutText.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
LayoutText::LayoutText(const Text& text)
|
LayoutText::LayoutText(const Text& text)
|
||||||
: LayoutNode(&text)
|
: LayoutNode(&text)
|
||||||
|
@ -8,3 +9,20 @@ LayoutText::LayoutText(const Text& text)
|
||||||
LayoutText::~LayoutText()
|
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();
|
||||||
|
}
|
||||||
|
|
|
@ -10,6 +10,8 @@ public:
|
||||||
|
|
||||||
const Text& node() const { return static_cast<const Text&>(*LayoutNode::node()); }
|
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 const char* class_name() const override { return "LayoutText"; }
|
||||||
virtual bool is_text() const final { return true; }
|
virtual bool is_text() const final { return true; }
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue