Exemple #1
0
void visibleTextQuads(const VisibleSelection& selection, Vector<FloatQuad>& quads)
{
    if (!selection.isRange())
        return;
    ASSERT(selection.firstRange());

    visibleTextQuads(*(selection.firstRange()), quads, true /* useSelectionHeight */);
}
Exemple #2
0
void visibleTextQuads(const VisibleSelection& selection, Vector<FloatQuad>& quads)
{
    if (!selection.isRange())
        return;

    // Make sure that both start and end have valid nodes associated otherwise
    // this can crash. See PR 220628.
    if (!selection.start().anchorNode() || !selection.end().anchorNode())
        return;

    visibleTextQuads(*(selection.firstRange()), quads, true /* useSelectionHeight */);
}
static gint webkitAccessibleTextGetNSelections(AtkText* text)
{
    AccessibilityObject* coreObject = core(text);
    VisibleSelection selection = coreObject->selection();

    // Only range selections are needed for the purpose of this method
    if (!selection.isRange())
        return 0;

    // We don't support multiple selections for now, so there's only
    // two possibilities
    // Also, we don't want to do anything if the selection does not
    // belong to the currently selected object. We have to check since
    // there's no way to get the selection for a given object, only
    // the global one (the API is a bit confusing)
    return selectionBelongsToObject(coreObject, selection) ? 1 : 0;
}
static void emitTextSelectionChange(AccessibilityObject* object, VisibleSelection selection, int offset)
{
    AtkObject* axObject = object->wrapper();
    if (!axObject || !ATK_IS_TEXT(axObject))
        return;

    // We need to adjust the offset for the list item marker in Left-To-Right text because
    // the list item marker is exposed through the text of the accessible list item rather
    // than through a separate accessible object.
    RenderObject* renderer = object->renderer();
    if (is<RenderListItem>(renderer) && renderer->style().direction() == LTR)
        offset += downcast<RenderListItem>(*renderer).markerTextWithSuffix().length();

    g_signal_emit_by_name(axObject, "text-caret-moved", offset);
    if (selection.isRange())
        g_signal_emit_by_name(axObject, "text-selection-changed");
}
bool InsertListCommand::modifyRange()
{
    VisibleSelection selection = selectionForParagraphIteration(endingSelection());
    ASSERT(selection.isRange());
    VisiblePosition startOfSelection = selection.visibleStart();
    VisiblePosition endOfSelection = selection.visibleEnd();
    VisiblePosition startOfLastParagraph = startOfParagraph(endOfSelection);
    
    if (startOfParagraph(startOfSelection) == startOfLastParagraph)
        return false;

    Node* startList = enclosingList(startOfSelection.deepEquivalent().node());
    Node* endList = enclosingList(endOfSelection.deepEquivalent().node());
    if (!startList || startList != endList)
        m_forceCreateList = true;

    setEndingSelection(startOfSelection);
    doApply();
    // Fetch the start of the selection after moving the first paragraph,
    // because moving the paragraph will invalidate the original start.  
    // We'll use the new start to restore the original selection after 
    // we modified all selected paragraphs.
    startOfSelection = endingSelection().visibleStart();
    VisiblePosition startOfCurrentParagraph = startOfNextParagraph(startOfSelection);
    while (startOfCurrentParagraph != startOfLastParagraph) {
        // doApply() may operate on and remove the last paragraph of the selection from the document 
        // if it's in the same list item as startOfCurrentParagraph.  Return early to avoid an 
        // infinite loop and because there is no more work to be done.
        // FIXME(<rdar://problem/5983974>): The endingSelection() may be incorrect here.  Compute 
        // the new location of endOfSelection and use it as the end of the new selection.
        if (!startOfLastParagraph.deepEquivalent().node()->inDocument())
            return true;
        setEndingSelection(startOfCurrentParagraph);
        doApply();
        startOfCurrentParagraph = startOfNextParagraph(endingSelection().visibleStart());
    }
    setEndingSelection(endOfSelection);
    doApply();
    // Fetch the end of the selection, for the reason mentioned above.
    endOfSelection = endingSelection().visibleEnd();
    setEndingSelection(VisibleSelection(startOfSelection, endOfSelection));
    m_forceCreateList = false;
    return true;
}
// This needs to be static so it can be called by canIncreaseSelectionListLevel and canDecreaseSelectionListLevel
static bool getStartEndListChildren(const VisibleSelection& selection, Node*& start, Node*& end)
{
    if (selection.isNone())
        return false;

    // start must be in a list child
    Node* startListChild = enclosingListChild(selection.start().anchorNode());
    if (!startListChild)
        return false;

    // end must be in a list child
    Node* endListChild = selection.isRange() ? enclosingListChild(selection.end().anchorNode()) : startListChild;
    if (!endListChild)
        return false;
    
    // For a range selection we want the following behavior:
    //      - the start and end must be within the same overall list
    //      - the start must be at or above the level of the rest of the range
    //      - if the end is anywhere in a sublist lower than start, the whole sublist gets moved
    // In terms of this function, this means:
    //      - endListChild must start out being be a sibling of startListChild, or be in a
    //         sublist of startListChild or a sibling
    //      - if endListChild is in a sublist of startListChild or a sibling, it must be adjusted
    //         to be the ancestor that is startListChild or its sibling
    while (startListChild->parentNode() != endListChild->parentNode()) {
        endListChild = endListChild->parentNode();
        if (!endListChild)
            return false;
    }
    
    // if the selection ends on a list item with a sublist, include the entire sublist
    if (endListChild->renderer()->isListItem()) {
        RenderObject* r = endListChild->renderer()->nextSibling();
        if (r && isListHTMLElement(r->node()))
            endListChild = r->node();
    }

    start = startListChild;
    end = endListChild;
    return true;
}
static void writeSelection(TextStream& ts, const RenderObject* o)
{
    Node* n = o->node();
    if (!n || !n->isDocumentNode())
        return;

    Document* doc = static_cast<Document*>(n);
    Frame* frame = doc->frame();
    if (!frame)
        return;

    VisibleSelection selection = frame->selection()->selection();
    if (selection.isCaret()) {
        ts << "caret: position " << selection.start().deprecatedEditingOffset() << " of " << nodePosition(selection.start().node());
        if (selection.affinity() == UPSTREAM)
            ts << " (upstream affinity)";
        ts << "\n";
    } else if (selection.isRange())
        ts << "selection start: position " << selection.start().deprecatedEditingOffset() << " of " << nodePosition(selection.start().node()) << "\n"
           << "selection end:   position " << selection.end().deprecatedEditingOffset() << " of " << nodePosition(selection.end().node()) << "\n";
}
void InsertListCommand::doApply()
{
    if (!endingSelection().isNonOrphanedCaretOrRange())
        return;

    if (!endingSelection().rootEditableElement())
        return;

    VisiblePosition visibleEnd = endingSelection().visibleEnd();
    VisiblePosition visibleStart = endingSelection().visibleStart();
    // When a selection ends at the start of a paragraph, we rarely paint
    // the selection gap before that paragraph, because there often is no gap.
    // In a case like this, it's not obvious to the user that the selection
    // ends "inside" that paragraph, so it would be confusing if InsertUn{Ordered}List
    // operated on that paragraph.
    // FIXME: We paint the gap before some paragraphs that are indented with left
    // margin/padding, but not others.  We should make the gap painting more consistent and
    // then use a left margin/padding rule here.
    if (visibleEnd != visibleStart && isStartOfParagraph(visibleEnd, CanSkipOverEditingBoundary)) {
        setEndingSelection(VisibleSelection(visibleStart, visibleEnd.previous(CannotCrossEditingBoundary), endingSelection().isDirectional()));
        if (!endingSelection().rootEditableElement())
            return;
    }

    const HTMLQualifiedName& listTag = (m_type == OrderedList) ? olTag : ulTag;
    if (endingSelection().isRange()) {
        bool forceListCreation = false;
        VisibleSelection selection = selectionForParagraphIteration(endingSelection());
        ASSERT(selection.isRange());
        VisiblePosition startOfSelection = selection.visibleStart();
        VisiblePosition endOfSelection = selection.visibleEnd();
        VisiblePosition startOfLastParagraph = startOfParagraph(endOfSelection, CanSkipOverEditingBoundary);

        RefPtrWillBeRawPtr<Range> currentSelection = endingSelection().firstRange();
        RefPtrWillBeRawPtr<ContainerNode> scopeForStartOfSelection = nullptr;
        RefPtrWillBeRawPtr<ContainerNode> scopeForEndOfSelection = nullptr;
        // FIXME: This is an inefficient way to keep selection alive because
        // indexForVisiblePosition walks from the beginning of the document to the
        // endOfSelection everytime this code is executed. But not using index is hard
        // because there are so many ways we can los eselection inside doApplyForSingleParagraph.
        int indexForStartOfSelection = indexForVisiblePosition(startOfSelection, scopeForStartOfSelection);
        int indexForEndOfSelection = indexForVisiblePosition(endOfSelection, scopeForEndOfSelection);

        if (startOfParagraph(startOfSelection, CanSkipOverEditingBoundary) != startOfLastParagraph) {
            forceListCreation = !selectionHasListOfType(selection, listTag);

            VisiblePosition startOfCurrentParagraph = startOfSelection;
            while (startOfCurrentParagraph.isNotNull() && !inSameParagraph(startOfCurrentParagraph, startOfLastParagraph, CanCrossEditingBoundary)) {
                // doApply() may operate on and remove the last paragraph of the selection from the document
                // if it's in the same list item as startOfCurrentParagraph.  Return early to avoid an
                // infinite loop and because there is no more work to be done.
                // FIXME(<rdar://problem/5983974>): The endingSelection() may be incorrect here.  Compute
                // the new location of endOfSelection and use it as the end of the new selection.
                if (!startOfLastParagraph.deepEquivalent().inDocument())
                    return;
                setEndingSelection(startOfCurrentParagraph);

                // Save and restore endOfSelection and startOfLastParagraph when necessary
                // since moveParagraph and movePragraphWithClones can remove nodes.
                doApplyForSingleParagraph(forceListCreation, listTag, *currentSelection);
                if (endOfSelection.isNull() || endOfSelection.isOrphan() || startOfLastParagraph.isNull() || startOfLastParagraph.isOrphan()) {
                    endOfSelection = visiblePositionForIndex(indexForEndOfSelection, scopeForEndOfSelection.get());
                    // If endOfSelection is null, then some contents have been deleted from the document.
                    // This should never happen and if it did, exit early immediately because we've lost the loop invariant.
                    ASSERT(endOfSelection.isNotNull());
                    if (endOfSelection.isNull() || !endOfSelection.rootEditableElement())
                        return;
                    startOfLastParagraph = startOfParagraph(endOfSelection, CanSkipOverEditingBoundary);
                }

                startOfCurrentParagraph = startOfNextParagraph(endingSelection().visibleStart());
            }
            setEndingSelection(endOfSelection);
        }
        doApplyForSingleParagraph(forceListCreation, listTag, *currentSelection);
        // Fetch the end of the selection, for the reason mentioned above.
        if (endOfSelection.isNull() || endOfSelection.isOrphan()) {
            endOfSelection = visiblePositionForIndex(indexForEndOfSelection, scopeForEndOfSelection.get());
            if (endOfSelection.isNull())
                return;
        }
        if (startOfSelection.isNull() || startOfSelection.isOrphan()) {
            startOfSelection = visiblePositionForIndex(indexForStartOfSelection, scopeForStartOfSelection.get());
            if (startOfSelection.isNull())
                return;
        }
        setEndingSelection(VisibleSelection(startOfSelection, endOfSelection, endingSelection().isDirectional()));
        return;
    }

    ASSERT(endingSelection().firstRange());
    doApplyForSingleParagraph(false, listTag, *endingSelection().firstRange());
}
void InsertListCommand::doApply(EditingState* editingState) {
    // Only entry points are Editor::Command::execute and
    // IndentOutdentCommand::outdentParagraph, both of which ensure clean layout.
    DCHECK(!document().needsLayoutTreeUpdate());

    if (!endingSelection().isNonOrphanedCaretOrRange())
        return;

    if (!endingSelection().rootEditableElement())
        return;

    VisiblePosition visibleEnd = endingSelection().visibleEnd();
    VisiblePosition visibleStart = endingSelection().visibleStart();
    // When a selection ends at the start of a paragraph, we rarely paint
    // the selection gap before that paragraph, because there often is no gap.
    // In a case like this, it's not obvious to the user that the selection
    // ends "inside" that paragraph, so it would be confusing if
    // InsertUn{Ordered}List operated on that paragraph.
    // FIXME: We paint the gap before some paragraphs that are indented with left
    // margin/padding, but not others.  We should make the gap painting more
    // consistent and then use a left margin/padding rule here.
    if (visibleEnd.deepEquivalent() != visibleStart.deepEquivalent() &&
            isStartOfParagraph(visibleEnd, CanSkipOverEditingBoundary)) {
        setEndingSelection(createVisibleSelection(
                               visibleStart,
                               previousPositionOf(visibleEnd, CannotCrossEditingBoundary),
                               endingSelection().isDirectional()));
        if (!endingSelection().rootEditableElement())
            return;
    }

    const HTMLQualifiedName& listTag = (m_type == OrderedList) ? olTag : ulTag;
    if (endingSelection().isRange()) {
        bool forceListCreation = false;
        VisibleSelection selection =
            selectionForParagraphIteration(endingSelection());
        DCHECK(selection.isRange());

        VisiblePosition visibleStartOfSelection = selection.visibleStart();
        VisiblePosition visibleEndOfSelection = selection.visibleEnd();
        PositionWithAffinity startOfSelection =
            visibleStartOfSelection.toPositionWithAffinity();
        PositionWithAffinity endOfSelection =
            visibleEndOfSelection.toPositionWithAffinity();
        Position startOfLastParagraph =
            startOfParagraph(visibleEndOfSelection, CanSkipOverEditingBoundary)
            .deepEquivalent();

        Range* currentSelection = firstRangeOf(endingSelection());
        ContainerNode* scopeForStartOfSelection = nullptr;
        ContainerNode* scopeForEndOfSelection = nullptr;
        // FIXME: This is an inefficient way to keep selection alive because
        // indexForVisiblePosition walks from the beginning of the document to the
        // visibleEndOfSelection everytime this code is executed. But not using
        // index is hard because there are so many ways we can lose selection inside
        // doApplyForSingleParagraph.
        int indexForStartOfSelection = indexForVisiblePosition(
                                           visibleStartOfSelection, scopeForStartOfSelection);
        int indexForEndOfSelection =
            indexForVisiblePosition(visibleEndOfSelection, scopeForEndOfSelection);

        if (startOfParagraph(visibleStartOfSelection, CanSkipOverEditingBoundary)
                .deepEquivalent() != startOfLastParagraph) {
            forceListCreation = !selectionHasListOfType(selection, listTag);

            VisiblePosition startOfCurrentParagraph = visibleStartOfSelection;
            while (inSameTreeAndOrdered(startOfCurrentParagraph.deepEquivalent(),
                                        startOfLastParagraph) &&
                    !inSameParagraph(startOfCurrentParagraph,
                                     createVisiblePosition(startOfLastParagraph),
                                     CanCrossEditingBoundary)) {
                // doApply() may operate on and remove the last paragraph of the
                // selection from the document if it's in the same list item as
                // startOfCurrentParagraph. Return early to avoid an infinite loop and
                // because there is no more work to be done.
                // FIXME(<rdar://problem/5983974>): The endingSelection() may be
                // incorrect here.  Compute the new location of visibleEndOfSelection
                // and use it as the end of the new selection.
                if (!startOfLastParagraph.isConnected())
                    return;
                setEndingSelection(startOfCurrentParagraph);

                // Save and restore visibleEndOfSelection and startOfLastParagraph when
                // necessary since moveParagraph and movePragraphWithClones can remove
                // nodes.
                bool singleParagraphResult = doApplyForSingleParagraph(
                                                 forceListCreation, listTag, *currentSelection, editingState);
                if (editingState->isAborted())
                    return;
                if (!singleParagraphResult)
                    break;

                document().updateStyleAndLayoutIgnorePendingStylesheets();

                // Make |visibleEndOfSelection| valid again.
                if (!endOfSelection.isConnected() ||
                        !startOfLastParagraph.isConnected()) {
                    visibleEndOfSelection = visiblePositionForIndex(
                                                indexForEndOfSelection, scopeForEndOfSelection);
                    endOfSelection = visibleEndOfSelection.toPositionWithAffinity();
                    // If visibleEndOfSelection is null, then some contents have been
                    // deleted from the document. This should never happen and if it did,
                    // exit early immediately because we've lost the loop invariant.
                    DCHECK(visibleEndOfSelection.isNotNull());
                    if (visibleEndOfSelection.isNull() ||
                            !rootEditableElementOf(visibleEndOfSelection))
                        return;
                    startOfLastParagraph = startOfParagraph(visibleEndOfSelection,
                                                            CanSkipOverEditingBoundary)
                                           .deepEquivalent();
                } else {
                    visibleEndOfSelection = createVisiblePosition(endOfSelection);
                }

                startOfCurrentParagraph =
                    startOfNextParagraph(endingSelection().visibleStart());
            }
            setEndingSelection(visibleEndOfSelection);
        }
        doApplyForSingleParagraph(forceListCreation, listTag, *currentSelection,
                                  editingState);
        if (editingState->isAborted())
            return;

        document().updateStyleAndLayoutIgnorePendingStylesheets();

        // Fetch the end of the selection, for the reason mentioned above.
        if (!endOfSelection.isConnected()) {
            visibleEndOfSelection = visiblePositionForIndex(indexForEndOfSelection,
                                    scopeForEndOfSelection);
            if (visibleEndOfSelection.isNull())
                return;
        } else {
            visibleEndOfSelection = createVisiblePosition(endOfSelection);
        }

        if (!startOfSelection.isConnected()) {
            visibleStartOfSelection = visiblePositionForIndex(
                                          indexForStartOfSelection, scopeForStartOfSelection);
            if (visibleStartOfSelection.isNull())
                return;
        } else {
            visibleStartOfSelection = createVisiblePosition(startOfSelection);
        }

        setEndingSelection(
            createVisibleSelection(visibleStartOfSelection, visibleEndOfSelection,
                                   endingSelection().isDirectional()));
        return;
    }

    DCHECK(firstRangeOf(endingSelection()));
    doApplyForSingleParagraph(false, listTag, *firstRangeOf(endingSelection()),
                              editingState);
}