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