Skip to content

Truly independent web browser

2.5 KB
Raw Blame
/*
 * Copyright (c) 2018-2021, Andreas Kling <[email protected]>
 * Copyright (c) 2022, Tobias Christiansen <[email protected]>
 * Copyright (c) 2025, Jelle Raaijmakers <[email protected]>
 * Copyright (c) 2026, Tim Ledbetter <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/HTML/FormAssociatedElement.h>
#include <LibWeb/Layout/NodeArena.h>
#include <LibWeb/Layout/TextNode.h>
#include <LibWeb/Painting/BoxViews.h>

namespace Web::Layout {

// The tree build stamped the row with whether an empty text produces a line box fragment and enrolled it for content
// sync. A layout node is made whenever something first asks for it, so it does nothing but bind.
TextNode::TextNode(DOM::Document& document, BindToPreparedArenaSlot bind, Compositing::RustFFI::NodeSlotId slot, RustFFI::NodeKind kind)
    : Node(document, bind, slot, kind)
{
}

TextNode::~TextNode() = default;

GeneratedTextNode::GeneratedTextNode(DOM::Document& document, BindToPreparedArenaSlot bind, Compositing::RustFFI::NodeSlotId slot, RustFFI::NodeKind kind)
    : TextNode(document, bind, slot, kind)
{
}

GeneratedTextNode::~GeneratedTextNode() = default;

Utf16String TextNode::rendered_text_for_dom() const
{
    Utf16String text;
    RustFFI::layout_script_rendered_text(document_host(), slot_id(this), &text,
        [](void* context, RustFFI::FfiRenderedTextView view) {
            *static_cast<Utf16String*>(context) = Utf16String::from_utf16({ reinterpret_cast<char16_t const*>(view.text), view.length_in_code_units });
        });
    return text;
}

RenderedTextForDOM TextNode::rendered_text_for_dom_with_offset_mapping() const
{
    RenderedTextForDOM rendered_text;
    RustFFI::layout_script_rendered_text_with_dom_offsets(document_host(), slot_id(this), &rendered_text,
        [](void* context, RustFFI::FfiRenderedTextWithOffsetsView view) {
            auto& output = *static_cast<RenderedTextForDOM*>(context);
            output.text = Utf16String::from_utf16({ reinterpret_cast<char16_t const*>(view.text), view.length_in_code_units });
            for (size_t i = 0; i < view.offset_mapping_length; ++i) {
                auto const& mapping = view.offset_mapping[i];
                output.offset_mapping.append({ mapping.dom_start_offset, mapping.dom_end_offset });
            }
        });
    return rendered_text;
}

RustFFI::FfiTextSourceRange TextNode::word_range_at(size_t dom_offset) const
{
    return RustFFI::layout_text_word_range(document_host(), slot_id(this), dom_offset);
}

}