Home | History | Annotate | Download | only in editing
      1 /*
      2  * Copyright (C) 2005, 2006 Apple Computer, Inc.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "core/editing/InsertLineBreakCommand.h"
     28 
     29 #include "core/HTMLNames.h"
     30 #include "core/dom/Document.h"
     31 #include "core/dom/Text.h"
     32 #include "core/editing/EditingStyle.h"
     33 #include "core/editing/FrameSelection.h"
     34 #include "core/editing/VisiblePosition.h"
     35 #include "core/editing/VisibleUnits.h"
     36 #include "core/editing/htmlediting.h"
     37 #include "core/frame/LocalFrame.h"
     38 #include "core/html/HTMLElement.h"
     39 #include "core/rendering/RenderObject.h"
     40 
     41 namespace WebCore {
     42 
     43 using namespace HTMLNames;
     44 
     45 InsertLineBreakCommand::InsertLineBreakCommand(Document& document)
     46     : CompositeEditCommand(document)
     47 {
     48 }
     49 
     50 bool InsertLineBreakCommand::preservesTypingStyle() const
     51 {
     52     return true;
     53 }
     54 
     55 // Whether we should insert a break element or a '\n'.
     56 bool InsertLineBreakCommand::shouldUseBreakElement(const Position& insertionPos)
     57 {
     58     // An editing position like [input, 0] actually refers to the position before
     59     // the input element, and in that case we need to check the input element's
     60     // parent's renderer.
     61     Position p(insertionPos.parentAnchoredEquivalent());
     62     return p.deprecatedNode()->renderer() && !p.deprecatedNode()->renderer()->style()->preserveNewline();
     63 }
     64 
     65 void InsertLineBreakCommand::doApply()
     66 {
     67     deleteSelection();
     68     VisibleSelection selection = endingSelection();
     69     if (!selection.isNonOrphanedCaretOrRange())
     70         return;
     71 
     72     VisiblePosition caret(selection.visibleStart());
     73     // FIXME: If the node is hidden, we should still be able to insert text.
     74     // For now, we return to avoid a crash.  https://bugs.webkit.org/show_bug.cgi?id=40342
     75     if (caret.isNull())
     76         return;
     77 
     78     Position pos(caret.deepEquivalent());
     79 
     80     pos = positionAvoidingSpecialElementBoundary(pos);
     81 
     82     pos = positionOutsideTabSpan(pos);
     83 
     84     RefPtrWillBeRawPtr<Node> nodeToInsert = nullptr;
     85     if (shouldUseBreakElement(pos))
     86         nodeToInsert = createBreakElement(document());
     87     else
     88         nodeToInsert = document().createTextNode("\n");
     89 
     90     // FIXME: Need to merge text nodes when inserting just after or before text.
     91 
     92     if (isEndOfParagraph(caret) && !lineBreakExistsAtVisiblePosition(caret)) {
     93         bool needExtraLineBreak = !isHTMLHRElement(*pos.deprecatedNode()) && !isHTMLTableElement(*pos.deprecatedNode());
     94 
     95         insertNodeAt(nodeToInsert.get(), pos);
     96 
     97         if (needExtraLineBreak)
     98             insertNodeBefore(nodeToInsert->cloneNode(false), nodeToInsert);
     99 
    100         VisiblePosition endingPosition(positionBeforeNode(nodeToInsert.get()));
    101         setEndingSelection(VisibleSelection(endingPosition, endingSelection().isDirectional()));
    102     } else if (pos.deprecatedEditingOffset() <= caretMinOffset(pos.deprecatedNode())) {
    103         insertNodeAt(nodeToInsert.get(), pos);
    104 
    105         // Insert an extra br or '\n' if the just inserted one collapsed.
    106         if (!isStartOfParagraph(VisiblePosition(positionBeforeNode(nodeToInsert.get()))))
    107             insertNodeBefore(nodeToInsert->cloneNode(false).get(), nodeToInsert.get());
    108 
    109         setEndingSelection(VisibleSelection(positionInParentAfterNode(*nodeToInsert), DOWNSTREAM, endingSelection().isDirectional()));
    110     // If we're inserting after all of the rendered text in a text node, or into a non-text node,
    111     // a simple insertion is sufficient.
    112     } else if (pos.deprecatedEditingOffset() >= caretMaxOffset(pos.deprecatedNode()) || !pos.deprecatedNode()->isTextNode()) {
    113         insertNodeAt(nodeToInsert.get(), pos);
    114         setEndingSelection(VisibleSelection(positionInParentAfterNode(*nodeToInsert), DOWNSTREAM, endingSelection().isDirectional()));
    115     } else if (pos.deprecatedNode()->isTextNode()) {
    116         // Split a text node
    117         Text* textNode = toText(pos.deprecatedNode());
    118         splitTextNode(textNode, pos.deprecatedEditingOffset());
    119         insertNodeBefore(nodeToInsert, textNode);
    120         Position endingPosition = firstPositionInNode(textNode);
    121 
    122         // Handle whitespace that occurs after the split
    123         document().updateLayoutIgnorePendingStylesheets();
    124         if (!endingPosition.isRenderedCharacter()) {
    125             Position positionBeforeTextNode(positionInParentBeforeNode(*textNode));
    126             // Clear out all whitespace and insert one non-breaking space
    127             deleteInsignificantTextDownstream(endingPosition);
    128             ASSERT(!textNode->renderer() || textNode->renderer()->style()->collapseWhiteSpace());
    129             // Deleting insignificant whitespace will remove textNode if it contains nothing but insignificant whitespace.
    130             if (textNode->inDocument())
    131                 insertTextIntoNode(textNode, 0, nonBreakingSpaceString());
    132             else {
    133                 RefPtrWillBeRawPtr<Text> nbspNode = document().createTextNode(nonBreakingSpaceString());
    134                 insertNodeAt(nbspNode.get(), positionBeforeTextNode);
    135                 endingPosition = firstPositionInNode(nbspNode.get());
    136             }
    137         }
    138 
    139         setEndingSelection(VisibleSelection(endingPosition, DOWNSTREAM, endingSelection().isDirectional()));
    140     }
    141 
    142     // Handle the case where there is a typing style.
    143 
    144     RefPtrWillBeRawPtr<EditingStyle> typingStyle = document().frame()->selection().typingStyle();
    145 
    146     if (typingStyle && !typingStyle->isEmpty()) {
    147         // Apply the typing style to the inserted line break, so that if the selection
    148         // leaves and then comes back, new input will have the right style.
    149         // FIXME: We shouldn't always apply the typing style to the line break here,
    150         // see <rdar://problem/5794462>.
    151         applyStyle(typingStyle.get(), firstPositionInOrBeforeNode(nodeToInsert.get()), lastPositionInOrAfterNode(nodeToInsert.get()));
    152         // Even though this applyStyle operates on a Range, it still sets an endingSelection().
    153         // It tries to set a VisibleSelection around the content it operated on. So, that VisibleSelection
    154         // will either (a) select the line break we inserted, or it will (b) be a caret just
    155         // before the line break (if the line break is at the end of a block it isn't selectable).
    156         // So, this next call sets the endingSelection() to a caret just after the line break
    157         // that we inserted, or just before it if it's at the end of a block.
    158         setEndingSelection(endingSelection().visibleEnd());
    159     }
    160 
    161     rebalanceWhitespace();
    162 }
    163 
    164 }
    165