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