1/*
2 * Copyright (C) 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/FormatBlockCommand.h"
28
29#include "bindings/core/v8/ExceptionStatePlaceholder.h"
30#include "core/HTMLNames.h"
31#include "core/dom/Element.h"
32#include "core/dom/Range.h"
33#include "core/editing/VisibleUnits.h"
34#include "core/editing/htmlediting.h"
35#include "core/html/HTMLBRElement.h"
36#include "core/html/HTMLElement.h"
37
38namespace blink {
39
40using namespace HTMLNames;
41
42static Node* enclosingBlockToSplitTreeTo(Node* startNode);
43static bool isElementForFormatBlock(const QualifiedName& tagName);
44static inline bool isElementForFormatBlock(Node* node)
45{
46    return node->isElementNode() && isElementForFormatBlock(toElement(node)->tagQName());
47}
48
49FormatBlockCommand::FormatBlockCommand(Document& document, const QualifiedName& tagName)
50    : ApplyBlockElementCommand(document, tagName)
51    , m_didApply(false)
52{
53}
54
55void FormatBlockCommand::formatSelection(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection)
56{
57    if (!isElementForFormatBlock(tagName()))
58        return;
59    ApplyBlockElementCommand::formatSelection(startOfSelection, endOfSelection);
60    m_didApply = true;
61}
62
63void FormatBlockCommand::formatRange(const Position& start, const Position& end, const Position& endOfSelection, RefPtrWillBeRawPtr<HTMLElement>& blockElement)
64{
65    Element* refElement = enclosingBlockFlowElement(VisiblePosition(end));
66    Element* root = editableRootForPosition(start);
67    // Root is null for elements with contenteditable=false.
68    if (!root || !refElement)
69        return;
70
71    Node* nodeToSplitTo = enclosingBlockToSplitTreeTo(start.deprecatedNode());
72    RefPtrWillBeRawPtr<Node> outerBlock = (start.deprecatedNode() == nodeToSplitTo) ? start.deprecatedNode() : splitTreeToNode(start.deprecatedNode(), nodeToSplitTo).get();
73    RefPtrWillBeRawPtr<Node> nodeAfterInsertionPosition = outerBlock;
74    RefPtrWillBeRawPtr<Range> range = Range::create(document(), start, endOfSelection);
75
76    if (isElementForFormatBlock(refElement->tagQName()) && VisiblePosition(start) == startOfBlock(VisiblePosition(start))
77        && (VisiblePosition(end) == endOfBlock(VisiblePosition(end)) || isNodeVisiblyContainedWithin(*refElement, *range))
78        && refElement != root && !root->isDescendantOf(refElement)) {
79        // Already in a block element that only contains the current paragraph
80        if (refElement->hasTagName(tagName()))
81            return;
82        nodeAfterInsertionPosition = refElement;
83    }
84
85    if (!blockElement) {
86        // Create a new blockquote and insert it as a child of the root editable element. We accomplish
87        // this by splitting all parents of the current paragraph up to that point.
88        blockElement = createBlockElement();
89        insertNodeBefore(blockElement, nodeAfterInsertionPosition);
90    }
91
92    Position lastParagraphInBlockNode = blockElement->lastChild() ? positionAfterNode(blockElement->lastChild()) : Position();
93    bool wasEndOfParagraph = isEndOfParagraph(VisiblePosition(lastParagraphInBlockNode));
94
95    moveParagraphWithClones(VisiblePosition(start), VisiblePosition(end), blockElement.get(), outerBlock.get());
96
97    // Copy the inline style of the original block element to the newly created block-style element.
98    if (outerBlock.get() != nodeAfterInsertionPosition.get() && toHTMLElement(nodeAfterInsertionPosition.get())->hasAttribute(styleAttr))
99        blockElement->setAttribute(styleAttr, toHTMLElement(nodeAfterInsertionPosition.get())->getAttribute(styleAttr));
100
101    if (wasEndOfParagraph && !isEndOfParagraph(VisiblePosition(lastParagraphInBlockNode)) && !isStartOfParagraph(VisiblePosition(lastParagraphInBlockNode)))
102        insertBlockPlaceholder(lastParagraphInBlockNode);
103}
104
105Element* FormatBlockCommand::elementForFormatBlockCommand(Range* range)
106{
107    if (!range)
108        return 0;
109
110    Node* commonAncestor = range->commonAncestorContainer();
111    while (commonAncestor && !isElementForFormatBlock(commonAncestor))
112        commonAncestor = commonAncestor->parentNode();
113
114    if (!commonAncestor)
115        return 0;
116
117    Element* rootEditableElement = range->startContainer()->rootEditableElement();
118    if (!rootEditableElement || commonAncestor->contains(rootEditableElement))
119        return 0;
120
121    return commonAncestor->isElementNode() ? toElement(commonAncestor) : 0;
122}
123
124bool isElementForFormatBlock(const QualifiedName& tagName)
125{
126    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, blockTags, ());
127    if (blockTags.isEmpty()) {
128        blockTags.add(addressTag);
129        blockTags.add(articleTag);
130        blockTags.add(asideTag);
131        blockTags.add(blockquoteTag);
132        blockTags.add(ddTag);
133        blockTags.add(divTag);
134        blockTags.add(dlTag);
135        blockTags.add(dtTag);
136        blockTags.add(footerTag);
137        blockTags.add(h1Tag);
138        blockTags.add(h2Tag);
139        blockTags.add(h3Tag);
140        blockTags.add(h4Tag);
141        blockTags.add(h5Tag);
142        blockTags.add(h6Tag);
143        blockTags.add(headerTag);
144        blockTags.add(hgroupTag);
145        blockTags.add(mainTag);
146        blockTags.add(navTag);
147        blockTags.add(pTag);
148        blockTags.add(preTag);
149        blockTags.add(sectionTag);
150    }
151    return blockTags.contains(tagName);
152}
153
154Node* enclosingBlockToSplitTreeTo(Node* startNode)
155{
156    Node* lastBlock = startNode;
157    for (Node* n = startNode; n; n = n->parentNode()) {
158        if (!n->hasEditableStyle())
159            return lastBlock;
160        if (isTableCell(n) || isHTMLBodyElement(*n) || !n->parentNode() || !n->parentNode()->hasEditableStyle() || isElementForFormatBlock(n))
161            return n;
162        if (isBlock(n))
163            lastBlock = n;
164        if (isHTMLListElement(n))
165            return n->parentNode()->hasEditableStyle() ? n->parentNode() : n;
166    }
167    return lastBlock;
168}
169
170}
171