2 * Copyright (C) 2005, 2006, 2008, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef ApplyStyleCommand_h
27 #define ApplyStyleCommand_h
29 #include "CompositeEditCommand.h"
30 #include "HTMLElement.h"
34 class CSSPrimitiveValue;
38 enum ShouldIncludeTypingStyle {
43 class ApplyStyleCommand : public CompositeEditCommand {
45 enum EPropertyLevel { PropertyDefault, ForceBlockProperties };
46 enum InlineStyleRemovalMode { RemoveIfNeeded, RemoveAlways, RemoveNone };
47 enum EAddStyledElement { AddStyledElement, DoNotAddStyledElement };
48 typedef bool (*IsInlineElementToRemoveFunction)(const Element*);
50 static PassRefPtr<ApplyStyleCommand> create(Document* document, CSSStyleDeclaration* style, EditAction action = EditActionChangeAttributes, EPropertyLevel level = PropertyDefault)
52 return adoptRef(new ApplyStyleCommand(document, style, action, level));
54 static PassRefPtr<ApplyStyleCommand> create(Document* document, CSSStyleDeclaration* style, const Position& start, const Position& end, EditAction action = EditActionChangeAttributes, EPropertyLevel level = PropertyDefault)
56 return adoptRef(new ApplyStyleCommand(document, style, start, end, action, level));
58 static PassRefPtr<ApplyStyleCommand> create(PassRefPtr<Element> element, bool removeOnly = false, EditAction action = EditActionChangeAttributes)
60 return adoptRef(new ApplyStyleCommand(element, removeOnly, action));
62 static PassRefPtr<ApplyStyleCommand> create(Document* document, CSSStyleDeclaration* style, IsInlineElementToRemoveFunction isInlineElementToRemoveFunction, EditAction action = EditActionChangeAttributes)
64 return adoptRef(new ApplyStyleCommand(document, style, isInlineElementToRemoveFunction, action));
68 ApplyStyleCommand(Document*, CSSStyleDeclaration*, EditAction, EPropertyLevel);
69 ApplyStyleCommand(Document*, CSSStyleDeclaration*, const Position& start, const Position& end, EditAction, EPropertyLevel);
70 ApplyStyleCommand(PassRefPtr<Element>, bool removeOnly, EditAction);
71 ApplyStyleCommand(Document*, CSSStyleDeclaration*, bool (*isInlineElementToRemove)(const Element*), EditAction);
73 virtual void doApply();
74 virtual EditAction editingAction() const;
76 CSSMutableStyleDeclaration* style() const { return m_style.get(); }
78 // style-removal helpers
79 bool isStyledInlineElementToRemove(Element*) const;
80 bool removeStyleFromRunBeforeApplyingStyle(CSSMutableStyleDeclaration* style, RefPtr<Node>& runStart, RefPtr<Node>& runEnd);
81 bool removeInlineStyleFromElement(CSSMutableStyleDeclaration*, PassRefPtr<HTMLElement>, InlineStyleRemovalMode = RemoveIfNeeded, CSSMutableStyleDeclaration* extractedStyle = 0);
82 inline bool shouldRemoveInlineStyleFromElement(CSSMutableStyleDeclaration* style, HTMLElement* element) {return removeInlineStyleFromElement(style, element, RemoveNone);}
83 bool removeImplicitlyStyledElement(CSSMutableStyleDeclaration*, HTMLElement*, InlineStyleRemovalMode, CSSMutableStyleDeclaration* extractedStyle);
84 void replaceWithSpanOrRemoveIfWithoutAttributes(HTMLElement*&);
85 bool removeCSSStyle(CSSMutableStyleDeclaration*, HTMLElement*, InlineStyleRemovalMode = RemoveIfNeeded, CSSMutableStyleDeclaration* extractedStyle = 0);
86 HTMLElement* highestAncestorWithConflictingInlineStyle(CSSMutableStyleDeclaration*, Node*);
87 void applyInlineStyleToPushDown(Node*, CSSMutableStyleDeclaration *style);
88 void pushDownInlineStyleAroundNode(CSSMutableStyleDeclaration*, Node*);
89 void removeInlineStyle(PassRefPtr<CSSMutableStyleDeclaration>, const Position& start, const Position& end);
90 bool nodeFullySelected(Node*, const Position& start, const Position& end) const;
91 bool nodeFullyUnselected(Node*, const Position& start, const Position& end) const;
93 // style-application helpers
94 void applyBlockStyle(CSSMutableStyleDeclaration*);
95 void applyRelativeFontStyleChange(CSSMutableStyleDeclaration*);
96 void applyInlineStyle(CSSMutableStyleDeclaration*);
97 void fixRangeAndApplyInlineStyle(CSSMutableStyleDeclaration*, const Position& start, const Position& end);
98 void applyInlineStyleToNodeRange(CSSMutableStyleDeclaration*, Node* startNode, Node* pastEndNode);
99 void addBlockStyle(const StyleChange&, HTMLElement*);
100 void addInlineStyleIfNeeded(CSSMutableStyleDeclaration*, PassRefPtr<Node> start, PassRefPtr<Node> end, EAddStyledElement addStyledElement = AddStyledElement);
101 void splitTextAtStart(const Position& start, const Position& end);
102 void splitTextAtEnd(const Position& start, const Position& end);
103 void splitTextElementAtStart(const Position& start, const Position& end);
104 void splitTextElementAtEnd(const Position& start, const Position& end);
105 bool shouldSplitTextElement(Element* elem, CSSMutableStyleDeclaration*);
106 bool isValidCaretPositionInTextNode(const Position& position);
107 bool mergeStartWithPreviousIfIdentical(const Position& start, const Position& end);
108 bool mergeEndWithNextIfIdentical(const Position& start, const Position& end);
109 void cleanupUnstyledAppleStyleSpans(Node* dummySpanAncestor);
111 void surroundNodeRangeWithElement(PassRefPtr<Node> start, PassRefPtr<Node> end, PassRefPtr<Element>);
112 float computedFontSize(const Node*);
113 void joinChildTextNodes(Node*, const Position& start, const Position& end);
115 HTMLElement* splitAncestorsWithUnicodeBidi(Node*, bool before, int allowedDirection);
116 void removeEmbeddingUpToEnclosingBlock(Node* node, Node* unsplitAncestor);
118 void updateStartEnd(const Position& newStart, const Position& newEnd);
119 Position startPosition();
120 Position endPosition();
122 RefPtr<CSSMutableStyleDeclaration> m_style;
123 EditAction m_editingAction;
124 EPropertyLevel m_propertyLevel;
127 bool m_useEndingSelection;
128 RefPtr<Element> m_styledInlineElement;
130 IsInlineElementToRemoveFunction m_isInlineElementToRemoveFunction;
133 bool isStyleSpan(const Node*);
134 PassRefPtr<HTMLElement> createStyleSpanElement(Document*);
135 RefPtr<CSSMutableStyleDeclaration> getPropertiesNotIn(CSSStyleDeclaration* styleWithRedundantProperties, CSSStyleDeclaration* baseStyle);
137 } // namespace WebCore