70b6f352d48b063c9394079c2443103a05386617
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / editing / ApplyStyleCommand.h
1 /*
2  * Copyright (C) 2005, 2006, 2008, 2009 Apple 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 #ifndef ApplyStyleCommand_h
27 #define ApplyStyleCommand_h
28
29 #include "core/editing/CompositeEditCommand.h"
30 #include "core/editing/WritingDirection.h"
31 #include "core/html/HTMLElement.h"
32
33 namespace WebCore {
34
35 class CSSPrimitiveValue;
36 class EditingStyle;
37 class StyleChange;
38
39 enum ShouldIncludeTypingStyle {
40     IncludeTypingStyle,
41     IgnoreTypingStyle
42 };
43
44 class ApplyStyleCommand FINAL : public CompositeEditCommand {
45 public:
46     enum EPropertyLevel { PropertyDefault, ForceBlockProperties };
47     enum InlineStyleRemovalMode { RemoveIfNeeded, RemoveAlways, RemoveNone };
48     enum EAddStyledElement { AddStyledElement, DoNotAddStyledElement };
49     typedef bool (*IsInlineElementToRemoveFunction)(const Element*);
50
51     static PassRefPtr<ApplyStyleCommand> create(Document& document, const EditingStyle* style, EditAction action = EditActionChangeAttributes, EPropertyLevel level = PropertyDefault)
52     {
53         return adoptRef(new ApplyStyleCommand(document, style, action, level));
54     }
55     static PassRefPtr<ApplyStyleCommand> create(Document& document, const EditingStyle* style, const Position& start, const Position& end, EditAction action = EditActionChangeAttributes, EPropertyLevel level = PropertyDefault)
56     {
57         return adoptRef(new ApplyStyleCommand(document, style, start, end, action, level));
58     }
59     static PassRefPtr<ApplyStyleCommand> create(PassRefPtr<Element> element, bool removeOnly = false, EditAction action = EditActionChangeAttributes)
60     {
61         return adoptRef(new ApplyStyleCommand(element, removeOnly, action));
62     }
63     static PassRefPtr<ApplyStyleCommand> create(Document& document, const EditingStyle* style, IsInlineElementToRemoveFunction isInlineElementToRemoveFunction, EditAction action = EditActionChangeAttributes)
64     {
65         return adoptRef(new ApplyStyleCommand(document, style, isInlineElementToRemoveFunction, action));
66     }
67
68 private:
69     ApplyStyleCommand(Document&, const EditingStyle*, EditAction, EPropertyLevel);
70     ApplyStyleCommand(Document&, const EditingStyle*, const Position& start, const Position& end, EditAction, EPropertyLevel);
71     ApplyStyleCommand(PassRefPtr<Element>, bool removeOnly, EditAction);
72     ApplyStyleCommand(Document&, const EditingStyle*, bool (*isInlineElementToRemove)(const Element*), EditAction);
73
74     virtual void doApply() OVERRIDE;
75     virtual EditAction editingAction() const OVERRIDE;
76
77     // style-removal helpers
78     bool isStyledInlineElementToRemove(Element*) const;
79     bool shouldApplyInlineStyleToRun(EditingStyle*, Node* runStart, Node* pastEndNode);
80     void removeConflictingInlineStyleFromRun(EditingStyle*, RefPtr<Node>& runStart, RefPtr<Node>& runEnd, PassRefPtr<Node> pastEndNode);
81     bool removeInlineStyleFromElement(EditingStyle*, PassRefPtr<HTMLElement>, InlineStyleRemovalMode = RemoveIfNeeded, EditingStyle* extractedStyle = 0);
82     inline bool shouldRemoveInlineStyleFromElement(EditingStyle* style, HTMLElement* element) {return removeInlineStyleFromElement(style, element, RemoveNone);}
83     void replaceWithSpanOrRemoveIfWithoutAttributes(HTMLElement*&);
84     bool removeImplicitlyStyledElement(EditingStyle*, HTMLElement*, InlineStyleRemovalMode, EditingStyle* extractedStyle);
85     bool removeCSSStyle(EditingStyle*, HTMLElement*, InlineStyleRemovalMode = RemoveIfNeeded, EditingStyle* extractedStyle = 0);
86     HTMLElement* highestAncestorWithConflictingInlineStyle(EditingStyle*, Node*);
87     void applyInlineStyleToPushDown(Node*, EditingStyle*);
88     void pushDownInlineStyleAroundNode(EditingStyle*, Node*);
89     void removeInlineStyle(EditingStyle* , const Position& start, const Position& end);
90     bool nodeFullySelected(Node*, const Position& start, const Position& end) const;
91
92     // style-application helpers
93     void applyBlockStyle(EditingStyle*);
94     void applyRelativeFontStyleChange(EditingStyle*);
95     void applyInlineStyle(EditingStyle*);
96     void fixRangeAndApplyInlineStyle(EditingStyle*, const Position& start, const Position& end);
97     void applyInlineStyleToNodeRange(EditingStyle*, PassRefPtr<Node> startNode, PassRefPtr<Node> pastEndNode);
98     void addBlockStyle(const StyleChange&, HTMLElement*);
99     void addInlineStyleIfNeeded(EditingStyle*, PassRefPtr<Node> start, PassRefPtr<Node> end, EAddStyledElement = AddStyledElement);
100     Position positionToComputeInlineStyleChange(PassRefPtr<Node>, RefPtr<Node>& dummyElement);
101     void applyInlineStyleChange(PassRefPtr<Node> startNode, PassRefPtr<Node> endNode, StyleChange&, EAddStyledElement);
102     void splitTextAtStart(const Position& start, const Position& end);
103     void splitTextAtEnd(const Position& start, const Position& end);
104     void splitTextElementAtStart(const Position& start, const Position& end);
105     void splitTextElementAtEnd(const Position& start, const Position& end);
106     bool shouldSplitTextElement(Element*, EditingStyle*);
107     bool isValidCaretPositionInTextNode(const Position& position);
108     bool mergeStartWithPreviousIfIdentical(const Position& start, const Position& end);
109     bool mergeEndWithNextIfIdentical(const Position& start, const Position& end);
110     void cleanupUnstyledAppleStyleSpans(Node* dummySpanAncestor);
111
112     void surroundNodeRangeWithElement(PassRefPtr<Node> start, PassRefPtr<Node> end, PassRefPtr<Element>);
113     float computedFontSize(Node*);
114     void joinChildTextNodes(Node*, const Position& start, const Position& end);
115
116     HTMLElement* splitAncestorsWithUnicodeBidi(Node*, bool before, WritingDirection allowedDirection);
117     void removeEmbeddingUpToEnclosingBlock(Node* node, Node* unsplitAncestor);
118
119     void updateStartEnd(const Position& newStart, const Position& newEnd);
120     Position startPosition();
121     Position endPosition();
122
123     RefPtr<EditingStyle> m_style;
124     EditAction m_editingAction;
125     EPropertyLevel m_propertyLevel;
126     Position m_start;
127     Position m_end;
128     bool m_useEndingSelection;
129     RefPtr<Element> m_styledInlineElement;
130     bool m_removeOnly;
131     IsInlineElementToRemoveFunction m_isInlineElementToRemoveFunction;
132 };
133
134 enum ShouldStyleAttributeBeEmpty { AllowNonEmptyStyleAttribute, StyleAttributeShouldBeEmpty };
135 bool isEmptyFontTag(const Element*, ShouldStyleAttributeBeEmpty = StyleAttributeShouldBeEmpty);
136 bool isLegacyAppleStyleSpan(const Node*);
137 bool isStyleSpanOrSpanWithOnlyStyleAttribute(const Element*);
138 PassRefPtr<HTMLElement> createStyleSpanElement(Document&);
139
140 } // namespace WebCore
141
142 #endif