Home | History | Annotate | Download | only in editing
      1 /*
      2  * Copyright (C) 2005 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/BreakBlockquoteCommand.h"
     28 
     29 #include "HTMLNames.h"
     30 #include "core/dom/NodeTraversal.h"
     31 #include "core/dom/Text.h"
     32 #include "core/editing/VisiblePosition.h"
     33 #include "core/editing/htmlediting.h"
     34 #include "core/html/HTMLElement.h"
     35 #include "core/rendering/RenderListItem.h"
     36 
     37 namespace WebCore {
     38 
     39 using namespace HTMLNames;
     40 
     41 BreakBlockquoteCommand::BreakBlockquoteCommand(Document *document)
     42     : CompositeEditCommand(document)
     43 {
     44 }
     45 
     46 void BreakBlockquoteCommand::doApply()
     47 {
     48     if (endingSelection().isNone())
     49         return;
     50 
     51     // Delete the current selection.
     52     if (endingSelection().isRange())
     53         deleteSelection(false, false);
     54 
     55     // This is a scenario that should never happen, but we want to
     56     // make sure we don't dereference a null pointer below.
     57 
     58     ASSERT(!endingSelection().isNone());
     59 
     60     if (endingSelection().isNone())
     61         return;
     62 
     63     VisiblePosition visiblePos = endingSelection().visibleStart();
     64 
     65     // pos is a position equivalent to the caret.  We use downstream() so that pos will
     66     // be in the first node that we need to move (there are a few exceptions to this, see below).
     67     Position pos = endingSelection().start().downstream();
     68 
     69     // Find the top-most blockquote from the start.
     70     Node* topBlockquote = highestEnclosingNodeOfType(pos, isMailBlockquote);
     71     if (!topBlockquote || !topBlockquote->parentNode() || !topBlockquote->isElementNode())
     72         return;
     73 
     74     RefPtr<Element> breakNode = createBreakElement(document());
     75 
     76     bool isLastVisPosInNode = isLastVisiblePositionInNode(visiblePos, topBlockquote);
     77 
     78     // If the position is at the beginning of the top quoted content, we don't need to break the quote.
     79     // Instead, insert the break before the blockquote, unless the position is as the end of the the quoted content.
     80     if (isFirstVisiblePositionInNode(visiblePos, topBlockquote) && !isLastVisPosInNode) {
     81         insertNodeBefore(breakNode.get(), topBlockquote);
     82         setEndingSelection(VisibleSelection(positionBeforeNode(breakNode.get()), DOWNSTREAM, endingSelection().isDirectional()));
     83         rebalanceWhitespace();
     84         return;
     85     }
     86 
     87     // Insert a break after the top blockquote.
     88     insertNodeAfter(breakNode.get(), topBlockquote);
     89 
     90     // If we're inserting the break at the end of the quoted content, we don't need to break the quote.
     91     if (isLastVisPosInNode) {
     92         setEndingSelection(VisibleSelection(positionBeforeNode(breakNode.get()), DOWNSTREAM, endingSelection().isDirectional()));
     93         rebalanceWhitespace();
     94         return;
     95     }
     96 
     97     // Don't move a line break just after the caret.  Doing so would create an extra, empty paragraph
     98     // in the new blockquote.
     99     if (lineBreakExistsAtVisiblePosition(visiblePos))
    100         pos = pos.next();
    101 
    102     // Adjust the position so we don't split at the beginning of a quote.
    103     while (isFirstVisiblePositionInNode(VisiblePosition(pos), enclosingNodeOfType(pos, isMailBlockquote)))
    104         pos = pos.previous();
    105 
    106     // startNode is the first node that we need to move to the new blockquote.
    107     Node* startNode = pos.deprecatedNode();
    108 
    109     // Split at pos if in the middle of a text node.
    110     if (startNode->isTextNode()) {
    111         Text* textNode = toText(startNode);
    112         if ((unsigned)pos.deprecatedEditingOffset() >= textNode->length()) {
    113             startNode = NodeTraversal::next(startNode);
    114             ASSERT(startNode);
    115         } else if (pos.deprecatedEditingOffset() > 0)
    116             splitTextNode(textNode, pos.deprecatedEditingOffset());
    117     } else if (pos.deprecatedEditingOffset() > 0) {
    118         Node* childAtOffset = startNode->childNode(pos.deprecatedEditingOffset());
    119         startNode = childAtOffset ? childAtOffset : NodeTraversal::next(startNode);
    120         ASSERT(startNode);
    121     }
    122 
    123     // If there's nothing inside topBlockquote to move, we're finished.
    124     if (!startNode->isDescendantOf(topBlockquote)) {
    125         setEndingSelection(VisibleSelection(VisiblePosition(firstPositionInOrBeforeNode(startNode)), endingSelection().isDirectional()));
    126         return;
    127     }
    128 
    129     // Build up list of ancestors in between the start node and the top blockquote.
    130     Vector<RefPtr<Element> > ancestors;
    131     for (Element* node = startNode->parentElement(); node && node != topBlockquote; node = node->parentElement())
    132         ancestors.append(node);
    133 
    134     // Insert a clone of the top blockquote after the break.
    135     RefPtr<Element> clonedBlockquote = toElement(topBlockquote)->cloneElementWithoutChildren();
    136     insertNodeAfter(clonedBlockquote.get(), breakNode.get());
    137 
    138     // Clone startNode's ancestors into the cloned blockquote.
    139     // On exiting this loop, clonedAncestor is the lowest ancestor
    140     // that was cloned (i.e. the clone of either ancestors.last()
    141     // or clonedBlockquote if ancestors is empty).
    142     RefPtr<Element> clonedAncestor = clonedBlockquote;
    143     for (size_t i = ancestors.size(); i != 0; --i) {
    144         RefPtr<Element> clonedChild = ancestors[i - 1]->cloneElementWithoutChildren();
    145         // Preserve list item numbering in cloned lists.
    146         if (clonedChild->isElementNode() && clonedChild->hasTagName(olTag)) {
    147             Node* listChildNode = i > 1 ? ancestors[i - 2].get() : startNode;
    148             // The first child of the cloned list might not be a list item element,
    149             // find the first one so that we know where to start numbering.
    150             while (listChildNode && !listChildNode->hasTagName(liTag))
    151                 listChildNode = listChildNode->nextSibling();
    152             if (listChildNode && listChildNode->renderer() && listChildNode->renderer()->isListItem())
    153                 setNodeAttribute(clonedChild, startAttr, String::number(toRenderListItem(listChildNode->renderer())->value()));
    154         }
    155 
    156         appendNode(clonedChild.get(), clonedAncestor.get());
    157         clonedAncestor = clonedChild;
    158     }
    159 
    160     moveRemainingSiblingsToNewParent(startNode, 0, clonedAncestor);
    161 
    162     if (!ancestors.isEmpty()) {
    163         // Split the tree up the ancestor chain until the topBlockquote
    164         // Throughout this loop, clonedParent is the clone of ancestor's parent.
    165         // This is so we can clone ancestor's siblings and place the clones
    166         // into the clone corresponding to the ancestor's parent.
    167         RefPtr<Element> ancestor;
    168         RefPtr<Element> clonedParent;
    169         for (ancestor = ancestors.first(), clonedParent = clonedAncestor->parentElement();
    170              ancestor && ancestor != topBlockquote;
    171              ancestor = ancestor->parentElement(), clonedParent = clonedParent->parentElement())
    172             moveRemainingSiblingsToNewParent(ancestor->nextSibling(), 0, clonedParent);
    173 
    174         // If the startNode's original parent is now empty, remove it
    175         Node* originalParent = ancestors.first().get();
    176         if (!originalParent->hasChildNodes())
    177             removeNode(originalParent);
    178     }
    179 
    180     // Make sure the cloned block quote renders.
    181     addBlockPlaceholderIfNeeded(clonedBlockquote.get());
    182 
    183     // Put the selection right before the break.
    184     setEndingSelection(VisibleSelection(positionBeforeNode(breakNode.get()), DOWNSTREAM, endingSelection().isDirectional()));
    185     rebalanceWhitespace();
    186 }
    187 
    188 } // namespace WebCore
    189