TextNode.h 1.6 KB

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