Skip to content

Truly independent web browser

4.0 KB
Raw Blame
/*
 * Copyright (c) 2021-2022, Andreas Kling <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/Utf16String.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Export.h>
#include <LibWeb/Forward.h>
#include <LibWeb/TextAffinity.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
#include <LibWebCommon/PixelUnits.h>

namespace Web::Selection {

class WEB_API Selection final : public Bindings::GCAllocatedWrappable {
    WEB_WRAPPABLE(Selection, Bindings::GCAllocatedWrappable);
    GC_DECLARE_ALLOCATOR(Selection);

public:
    [[nodiscard]] static GC::Ref<Selection> create(GC::Ref<DOM::Document>);

    virtual ~Selection() override;

    enum class Direction : u8 {
        Forwards,
        Backwards,
        Directionless,
    };

    GC::Ptr<DOM::Node> anchor_node();
    unsigned anchor_offset();
    GC::Ptr<DOM::Node> focus_node();
    unsigned focus_offset() const;
    bool is_collapsed() const;
    unsigned range_count() const;
    Utf16String type() const;
    Utf16String direction() const;
    WebIDL::ExceptionOr<GC::Ptr<DOM::Range>> get_range_at(unsigned index);
    void add_range(GC::Ref<DOM::Range>);
    WebIDL::ExceptionOr<void> remove_range(GC::Ref<DOM::Range>);
    void remove_all_ranges();
    void empty();
    WebIDL::ExceptionOr<void> collapse(GC::Ptr<DOM::Node>, unsigned offset);
    WebIDL::ExceptionOr<void> set_position(GC::Ptr<DOM::Node>, unsigned offset);
    WebIDL::ExceptionOr<void> collapse_to_start();
    WebIDL::ExceptionOr<void> collapse_to_end();
    WebIDL::ExceptionOr<void> extend(GC::Ref<DOM::Node>, unsigned offset);
    WebIDL::ExceptionOr<void> set_base_and_extent(GC::Ref<DOM::Node> anchor_node, unsigned anchor_offset, GC::Ref<DOM::Node> focus_node, unsigned focus_offset);
    WebIDL::ExceptionOr<void> select_all_children(GC::Ref<DOM::Node>);
    WebIDL::ExceptionOr<void> modify(Optional<Utf16String> alter, Optional<Utf16String> direction, Optional<Utf16String> granularity);
    WebIDL::ExceptionOr<void>
    delete_from_document();
    bool contains_node(GC::Ref<DOM::Node>, bool allow_partial_containment) const;

    Utf16String to_string() const;

    // Non-standard convenience accessor for the selection's range.
    GC::Ptr<DOM::Range> range() const;
    Optional<Utf16String> try_form_control_selected_text_for_stringifier() const;

    // Non-standard accessor for the selection's document.
    GC::Ref<DOM::Document> document() const;

    // Non-standard
    GC::Ptr<DOM::Position> cursor_position() const;

    // Non-standard: which visual line the focus renders on when it sits at a soft wrap boundary. Reset to
    // Downstream by every selection mutation; internal caret navigation assigns it after moving.
    TextAffinity focus_affinity() const { return m_focus_affinity; }
    void set_focus_affinity(TextAffinity affinity) { m_focus_affinity = affinity; }

    // Non-standard
    void scroll_focus_into_view();
    void move_offset_to_next_character(bool collapse_selection);
    void move_offset_to_previous_character(bool collapse_selection);
    void move_offset_to_next_word(bool collapse_selection);
    void move_offset_to_previous_word(bool collapse_selection);
    void move_offset_to_next_line(bool collapse_selection);
    void move_offset_to_previous_line(bool collapse_selection);

private:
    friend class SelectionModifier;
    explicit Selection(GC::Ref<DOM::Document>);

    [[nodiscard]] bool is_empty() const;

    virtual void visit_edges(GC::Cell::Visitor&) override;

    void set_range(GC::Ptr<DOM::Range>);

    // https://w3c.github.io/selection-api/#dfn-empty
    GC::Ptr<DOM::Range> m_range;

    GC::Ref<DOM::Document> m_document;
    Direction m_direction { Direction::Directionless };
    TextAffinity m_focus_affinity { TextAffinity::Downstream };
    // Repeated vertical moves preserve the inline-axis coordinate chosen by the first move. A non-vertical move
    // clears it so the next vertical sequence starts from the caret's new visual position.
    Optional<CSSPixels> m_preferred_inline_coordinate;
};

}