Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/Utf8View.h>
10#include <LibWeb/DOM/Text.h>
11#include <LibWeb/Layout/Node.h>
12
13namespace Web::Layout {
14
15class LineBoxFragment;
16
17class TextNode final : public Node {
18 JS_CELL(TextNode, Node);
19
20public:
21 TextNode(DOM::Document&, DOM::Text&);
22 virtual ~TextNode() override;
23
24 const DOM::Text& dom_node() const { return static_cast<const DOM::Text&>(*Node::dom_node()); }
25
26 DeprecatedString const& text_for_rendering() const { return m_text_for_rendering; }
27
28 struct Chunk {
29 Utf8View view;
30 size_t start { 0 };
31 size_t length { 0 };
32 bool has_breaking_newline { false };
33 bool is_all_whitespace { false };
34 };
35
36 class ChunkIterator {
37 public:
38 ChunkIterator(StringView text, bool wrap_lines, bool respect_linebreaks, bool is_generated_empty_string);
39 Optional<Chunk> next();
40
41 private:
42 Optional<Chunk> try_commit_chunk(Utf8View::Iterator const& start, Utf8View::Iterator const& end, bool has_breaking_newline) const;
43
44 bool const m_wrap_lines;
45 bool const m_respect_linebreaks;
46 bool m_should_emit_one_empty_chunk { false };
47 Utf8View m_utf8_view;
48 Utf8View::Iterator m_iterator;
49 };
50
51 void compute_text_for_rendering(bool collapse);
52
53 virtual JS::GCPtr<Painting::Paintable> create_paintable() const override;
54
55private:
56 virtual bool is_text_node() const final { return true; }
57
58 DeprecatedString m_text_for_rendering;
59};
60
61template<>
62inline bool Node::fast_is<TextNode>() const { return is_text_node(); }
63
64}