示例#1
0
static void writeSelection(TextStream& ts, const RenderObject* o)
{
    Node* n = o->element();
    if (!n || !n->isDocumentNode())
        return;

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

    Selection selection = frame->selection()->selection();
    if (selection.isCaret()) {
        ts << "caret: position " << selection.start().offset() << " of " << nodePosition(selection.start().node());
        if (selection.affinity() == UPSTREAM)
            ts << " (upstream affinity)";
        ts << "\n";
    } else if (selection.isRange())
        ts << "selection start: position " << selection.start().offset() << " of " << nodePosition(selection.start().node()) << "\n"
           << "selection end:   position " << selection.end().offset() << " of " << nodePosition(selection.end().node()) << "\n";
}
示例#2
0
void InsertLineBreakCommand::doApply()
{
    deleteSelection();
    Selection selection = endingSelection();
    if (selection.isNone())
        return;

    Position pos(selection.start().upstream());

    pos = positionAvoidingSpecialElementBoundary(pos);

    Node* styleNode = pos.node();
    bool isTabSpan = isTabSpanTextNode(styleNode);
    if (isTabSpan)
        styleNode = styleNode->parentNode()->parentNode();
    RenderObject* styleRenderer = styleNode->renderer();
    bool useBreakElement = !styleRenderer || !styleRenderer->style()->preserveNewline();

    RefPtr<Node> nodeToInsert;
    if (useBreakElement)
        nodeToInsert = createBreakElement(document());
    else
        nodeToInsert = document()->createTextNode("\n");
        // FIXME: Need to merge text nodes when inserting just after or before text.
    
    if (isTabSpan) {
        insertNodeAtTabSpanPosition(nodeToInsert.get(), pos);
        setEndingSelection(Position(nodeToInsert->traverseNextNode(), 0), DOWNSTREAM);
    } else if (isEndOfBlock(VisiblePosition(pos, selection.affinity()))) {
        Node* block = pos.node()->enclosingBlockFlowElement();
        
        // Insert an extra break element so that there will be a blank line after the last
        // inserted line break. In HTML, a line break at the end of a block ends the last
        // line in the block, while in editable text, a line break at the end of block
        // creates a last blank line. We need an extra break element to get HTML to act
        // the way editable text would.
        bool haveBreak = pos.downstream().node()->hasTagName(brTag) && pos.downstream().offset() == 0;
        insertNodeAt(nodeToInsert.get(), pos.node(), pos.offset());
        if (!haveBreak)
            insertNodeAfter(createBreakElement(document()).get(), nodeToInsert.get());
            
        setEndingSelection(Position(block, maxDeepOffset(block)), DOWNSTREAM);
    } else if (pos.offset() <= pos.node()->caretMinOffset()) {
        LOG(Editing, "input newline case 2");
        // Insert node before downstream position, and place caret there as well. 
        Position endingPosition = pos.downstream();
        insertNodeBeforePosition(nodeToInsert.get(), endingPosition);
        setEndingSelection(endingPosition, DOWNSTREAM);
    } else if (pos.offset() >= pos.node()->caretMaxOffset()) {
        LOG(Editing, "input newline case 3");
        // Insert BR after this node. Place caret in the position that is downstream
        // of the current position, reckoned before inserting the BR in between.
        Position endingPosition = pos.downstream();
        insertNodeAfterPosition(nodeToInsert.get(), pos);
        setEndingSelection(endingPosition, DOWNSTREAM);
    } else {
        // Split a text node
        LOG(Editing, "input newline case 4");
        ASSERT(pos.node()->isTextNode());
        
        // Do the split
        ExceptionCode ec = 0;
        Text *textNode = static_cast<Text *>(pos.node());
        RefPtr<Text> textBeforeNode = document()->createTextNode(textNode->substringData(0, selection.start().offset(), ec));
        deleteTextFromNode(textNode, 0, pos.offset());
        insertNodeBefore(textBeforeNode.get(), textNode);
        insertNodeBefore(nodeToInsert.get(), textNode);
        Position endingPosition = Position(textNode, 0);
        
        // Handle whitespace that occurs after the split
        updateLayout();
        if (!endingPosition.isRenderedCharacter()) {
            // Clear out all whitespace and insert one non-breaking space
            deleteInsignificantTextDownstream(endingPosition);
            ASSERT(!textNode->renderer() || textNode->renderer()->style()->collapseWhiteSpace());
            insertTextIntoNode(textNode, 0, nonBreakingSpaceString());
        }
        
        setEndingSelection(endingPosition, DOWNSTREAM);
    }

    // Handle the case where there is a typing style.
    // FIXME: Improve typing style.
    // See this bug: <rdar://problem/3769899> Implementation of typing style needs improvement
    
    CSSMutableStyleDeclaration* typingStyle = document()->frame()->typingStyle();
    
    if (typingStyle && typingStyle->length() > 0) {
        Selection selectionBeforeStyle = endingSelection();
        applyStyle(typingStyle, Position(nodeToInsert.get(), 0),
            Position(nodeToInsert.get(), maxDeepOffset(nodeToInsert.get())));
        setEndingSelection(selectionBeforeStyle);
    }

    rebalanceWhitespace();
}