Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / editing / EditingStyle.h
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  * Copyright (C) 2013 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #ifndef EditingStyle_h
33 #define EditingStyle_h
34
35 #include "CSSPropertyNames.h"
36 #include "CSSValueKeywords.h"
37 #include "core/editing/WritingDirection.h"
38 #include "platform/heap/Handle.h"
39 #include "wtf/Forward.h"
40 #include "wtf/RefCounted.h"
41 #include "wtf/RefPtr.h"
42 #include "wtf/TriState.h"
43 #include "wtf/Vector.h"
44 #include "wtf/text/WTFString.h"
45
46 namespace WebCore {
47
48 class CSSStyleDeclaration;
49 class CSSComputedStyleDeclaration;
50 class CSSPrimitiveValue;
51 class CSSValue;
52 class Document;
53 class Element;
54 class HTMLElement;
55 class MutableStylePropertySet;
56 class Node;
57 class Position;
58 class QualifiedName;
59 class RenderStyle;
60 class StylePropertySet;
61 class VisibleSelection;
62
63 class EditingStyle : public RefCounted<EditingStyle> {
64 public:
65
66     enum PropertiesToInclude { AllProperties, OnlyEditingInheritableProperties, EditingPropertiesInEffect };
67     enum ShouldPreserveWritingDirection { PreserveWritingDirection, DoNotPreserveWritingDirection };
68     enum ShouldExtractMatchingStyle { ExtractMatchingStyle, DoNotExtractMatchingStyle };
69     static float NoFontDelta;
70
71     static PassRefPtr<EditingStyle> create()
72     {
73         return adoptRef(new EditingStyle());
74     }
75
76     static PassRefPtr<EditingStyle> create(Node* node, PropertiesToInclude propertiesToInclude = OnlyEditingInheritableProperties)
77     {
78         return adoptRef(new EditingStyle(node, propertiesToInclude));
79     }
80
81     static PassRefPtr<EditingStyle> create(const Position& position, PropertiesToInclude propertiesToInclude = OnlyEditingInheritableProperties)
82     {
83         return adoptRef(new EditingStyle(position, propertiesToInclude));
84     }
85
86     static PassRefPtr<EditingStyle> create(const StylePropertySet* style)
87     {
88         return adoptRef(new EditingStyle(style));
89     }
90
91     static PassRefPtr<EditingStyle> create(CSSPropertyID propertyID, const String& value)
92     {
93         return adoptRef(new EditingStyle(propertyID, value));
94     }
95
96     ~EditingStyle();
97
98     MutableStylePropertySet* style() { return m_mutableStyle.get(); }
99     bool textDirection(WritingDirection&) const;
100     bool isEmpty() const;
101     void overrideWithStyle(const StylePropertySet*);
102     void clear();
103     PassRefPtr<EditingStyle> copy() const;
104     PassRefPtr<EditingStyle> extractAndRemoveBlockProperties();
105     PassRefPtr<EditingStyle> extractAndRemoveTextDirection();
106     void removeBlockProperties();
107     void removeStyleAddedByNode(Node*);
108     void removeStyleConflictingWithStyleOfNode(Node*);
109     void collapseTextDecorationProperties();
110     enum ShouldIgnoreTextOnlyProperties { IgnoreTextOnlyProperties, DoNotIgnoreTextOnlyProperties };
111     TriState triStateOfStyle(EditingStyle*) const;
112     TriState triStateOfStyle(const VisibleSelection&) const;
113     bool conflictsWithInlineStyleOfElement(Element* element) const { return conflictsWithInlineStyleOfElement(element, 0, 0); }
114     bool conflictsWithInlineStyleOfElement(Element* element, EditingStyle* extractedStyle, Vector<CSSPropertyID>& conflictingProperties) const
115     {
116         return conflictsWithInlineStyleOfElement(element, extractedStyle, &conflictingProperties);
117     }
118     bool conflictsWithImplicitStyleOfElement(HTMLElement*, EditingStyle* extractedStyle = 0, ShouldExtractMatchingStyle = DoNotExtractMatchingStyle) const;
119     bool conflictsWithImplicitStyleOfAttributes(HTMLElement*) const;
120     bool extractConflictingImplicitStyleOfAttributes(HTMLElement*, ShouldPreserveWritingDirection, EditingStyle* extractedStyle,
121             Vector<QualifiedName>& conflictingAttributes, ShouldExtractMatchingStyle) const;
122     bool styleIsPresentInComputedStyleOfNode(Node*) const;
123
124     static bool elementIsStyledSpanOrHTMLEquivalent(const HTMLElement*);
125
126     void prepareToApplyAt(const Position&, ShouldPreserveWritingDirection = DoNotPreserveWritingDirection);
127     void mergeTypingStyle(Document*);
128     enum CSSPropertyOverrideMode { OverrideValues, DoNotOverrideValues };
129     void mergeInlineStyleOfElement(Element*, CSSPropertyOverrideMode, PropertiesToInclude = AllProperties);
130     static PassRefPtr<EditingStyle> wrappingStyleForSerialization(Node* context, bool shouldAnnotate);
131     void mergeStyleFromRules(Element*);
132     void mergeStyleFromRulesForSerialization(Element*);
133     void removeStyleFromRulesAndContext(Element*, Node* context);
134     void removePropertiesInElementDefaultStyle(Element*);
135     void forceInline();
136     int legacyFontSize(Document*) const;
137
138     float fontSizeDelta() const { return m_fontSizeDelta; }
139     bool hasFontSizeDelta() const { return m_fontSizeDelta != NoFontDelta; }
140
141     static PassRefPtr<EditingStyle> styleAtSelectionStart(const VisibleSelection&, bool shouldUseBackgroundColorInEffect = false);
142     static WritingDirection textDirectionForSelection(const VisibleSelection&, EditingStyle* typingStyle, bool& hasNestedOrMultipleEmbeddings);
143 private:
144     EditingStyle();
145     EditingStyle(Node*, PropertiesToInclude);
146     EditingStyle(const Position&, PropertiesToInclude);
147     explicit EditingStyle(const StylePropertySet*);
148     EditingStyle(CSSPropertyID, const String& value);
149     void init(Node*, PropertiesToInclude);
150     void removeTextFillAndStrokeColorsIfNeeded(RenderStyle*);
151     void setProperty(CSSPropertyID, const String& value, bool important = false);
152     void replaceFontSizeByKeywordIfPossible(RenderStyle*, CSSComputedStyleDeclaration*);
153     void extractFontSizeDelta();
154     TriState triStateOfStyle(CSSStyleDeclaration* styleToCompare, ShouldIgnoreTextOnlyProperties) const;
155     bool conflictsWithInlineStyleOfElement(Element*, EditingStyle* extractedStyle, Vector<CSSPropertyID>* conflictingProperties) const;
156     void mergeInlineAndImplicitStyleOfElement(Element*, CSSPropertyOverrideMode, PropertiesToInclude);
157     void mergeStyle(const StylePropertySet*, CSSPropertyOverrideMode);
158
159     RefPtrWillBePersistent<MutableStylePropertySet> m_mutableStyle;
160     bool m_shouldUseFixedDefaultFontSize;
161     float m_fontSizeDelta;
162
163     friend class HTMLElementEquivalent;
164     friend class HTMLAttributeEquivalent;
165 };
166
167 class StyleChange {
168 public:
169     StyleChange()
170         : m_applyBold(false)
171         , m_applyItalic(false)
172         , m_applyUnderline(false)
173         , m_applyLineThrough(false)
174         , m_applySubscript(false)
175         , m_applySuperscript(false)
176     { }
177
178     StyleChange(EditingStyle*, const Position&);
179
180     String cssStyle() const { return m_cssStyle; }
181     bool applyBold() const { return m_applyBold; }
182     bool applyItalic() const { return m_applyItalic; }
183     bool applyUnderline() const { return m_applyUnderline; }
184     bool applyLineThrough() const { return m_applyLineThrough; }
185     bool applySubscript() const { return m_applySubscript; }
186     bool applySuperscript() const { return m_applySuperscript; }
187     bool applyFontColor() const { return m_applyFontColor.length() > 0; }
188     bool applyFontFace() const { return m_applyFontFace.length() > 0; }
189     bool applyFontSize() const { return m_applyFontSize.length() > 0; }
190
191     String fontColor() { return m_applyFontColor; }
192     String fontFace() { return m_applyFontFace; }
193     String fontSize() { return m_applyFontSize; }
194
195     bool operator==(const StyleChange& other)
196     {
197         return m_cssStyle == other.m_cssStyle
198             && m_applyBold == other.m_applyBold
199             && m_applyItalic == other.m_applyItalic
200             && m_applyUnderline == other.m_applyUnderline
201             && m_applyLineThrough == other.m_applyLineThrough
202             && m_applySubscript == other.m_applySubscript
203             && m_applySuperscript == other.m_applySuperscript
204             && m_applyFontColor == other.m_applyFontColor
205             && m_applyFontFace == other.m_applyFontFace
206             && m_applyFontSize == other.m_applyFontSize;
207     }
208     bool operator!=(const StyleChange& other)
209     {
210         return !(*this == other);
211     }
212 private:
213     void extractTextStyles(Document*, MutableStylePropertySet*, bool shouldUseFixedFontDefaultSize);
214
215     String m_cssStyle;
216     bool m_applyBold;
217     bool m_applyItalic;
218     bool m_applyUnderline;
219     bool m_applyLineThrough;
220     bool m_applySubscript;
221     bool m_applySuperscript;
222     String m_applyFontColor;
223     String m_applyFontFace;
224     String m_applyFontSize;
225 };
226
227 // FIXME: Remove these functions or make them non-global to discourage using CSSStyleDeclaration directly.
228 CSSValueID getIdentifierValue(CSSStyleDeclaration*, CSSPropertyID);
229 CSSValueID getIdentifierValue(StylePropertySet*, CSSPropertyID);
230
231 } // namespace WebCore
232
233 #endif // EditingStyle_h