2 * Copyright (C) 2010 Google 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef EditingStyle_h
32 #define EditingStyle_h
34 #include "CSSPropertyNames.h"
35 #include "PlatformString.h"
36 #include "WritingDirection.h"
37 #include <wtf/Forward.h>
38 #include <wtf/RefCounted.h>
39 #include <wtf/RefPtr.h>
40 #include <wtf/Vector.h>
44 class CSSStyleDeclaration;
45 class CSSComputedStyleDeclaration;
46 class CSSMutableStyleDeclaration;
47 class CSSPrimitiveValue;
57 class VisibleSelection;
59 enum TriState { FalseTriState, TrueTriState, MixedTriState };
61 class EditingStyle : public RefCounted<EditingStyle> {
64 enum PropertiesToInclude { AllProperties, OnlyEditingInheritableProperties, EditingPropertiesInEffect };
65 enum ShouldPreserveWritingDirection { PreserveWritingDirection, DoNotPreserveWritingDirection };
66 enum ShouldExtractMatchingStyle { ExtractMatchingStyle, DoNotExtractMatchingStyle };
67 static float NoFontDelta;
69 static PassRefPtr<EditingStyle> create()
71 return adoptRef(new EditingStyle());
74 static PassRefPtr<EditingStyle> create(Node* node, PropertiesToInclude propertiesToInclude = OnlyEditingInheritableProperties)
76 return adoptRef(new EditingStyle(node, propertiesToInclude));
79 static PassRefPtr<EditingStyle> create(const Position& position, PropertiesToInclude propertiesToInclude = OnlyEditingInheritableProperties)
81 return adoptRef(new EditingStyle(position, propertiesToInclude));
84 static PassRefPtr<EditingStyle> create(const CSSStyleDeclaration* style)
86 return adoptRef(new EditingStyle(style));
89 static PassRefPtr<EditingStyle> create(int propertyID, const String& value)
91 return adoptRef(new EditingStyle(propertyID, value));
96 CSSMutableStyleDeclaration* style() { return m_mutableStyle.get(); }
97 bool textDirection(WritingDirection&) const;
99 void setStyle(PassRefPtr<CSSMutableStyleDeclaration>);
100 void overrideWithStyle(const CSSMutableStyleDeclaration*);
102 PassRefPtr<EditingStyle> copy() const;
103 PassRefPtr<EditingStyle> extractAndRemoveBlockProperties();
104 PassRefPtr<EditingStyle> extractAndRemoveTextDirection();
105 void removeBlockProperties();
106 void removeStyleAddedByNode(Node*);
107 void removeStyleConflictingWithStyleOfNode(Node*);
108 void removeNonEditingProperties();
109 void collapseTextDecorationProperties();
110 enum ShouldIgnoreTextOnlyProperties { IgnoreTextOnlyProperties, DoNotIgnoreTextOnlyProperties };
111 TriState triStateOfStyle(EditingStyle*) const;
112 TriState triStateOfStyle(const VisibleSelection&) const;
113 bool conflictsWithInlineStyleOfElement(StyledElement* element) const { return conflictsWithInlineStyleOfElement(element, 0, 0); }
114 bool conflictsWithInlineStyleOfElement(StyledElement* element, EditingStyle* extractedStyle, Vector<CSSPropertyID>& conflictingProperties) const
116 return conflictsWithInlineStyleOfElement(element, extractedStyle, &conflictingProperties);
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;
124 static bool elementIsStyledSpanOrHTMLEquivalent(const HTMLElement*);
126 void prepareToApplyAt(const Position&, ShouldPreserveWritingDirection = DoNotPreserveWritingDirection);
127 void mergeTypingStyle(Document*);
128 enum CSSPropertyOverrideMode { OverrideValues, DoNotOverrideValues };
129 void mergeInlineStyleOfElement(StyledElement*, CSSPropertyOverrideMode, PropertiesToInclude = AllProperties);
130 static PassRefPtr<EditingStyle> wrappingStyleForSerialization(Node* context, bool shouldAnnotate);
131 void mergeStyleFromRules(StyledElement*);
132 void mergeStyleFromRulesForSerialization(StyledElement*);
133 void removeStyleFromRulesAndContext(StyledElement*, Node* context);
134 void removePropertiesInElementDefaultStyle(Element*);
136 int legacyFontSize(Document*) const;
138 float fontSizeDelta() const { return m_fontSizeDelta; }
139 bool hasFontSizeDelta() const { return m_fontSizeDelta != NoFontDelta; }
140 bool shouldUseFixedDefaultFontSize() const { return m_shouldUseFixedDefaultFontSize; }
142 static PassRefPtr<EditingStyle> styleAtSelectionStart(const VisibleSelection&, bool shouldUseBackgroundColorInEffect = false);
145 EditingStyle(Node*, PropertiesToInclude);
146 EditingStyle(const Position&, PropertiesToInclude);
147 EditingStyle(const CSSStyleDeclaration*);
148 EditingStyle(int propertyID, const String& value);
149 void init(Node*, PropertiesToInclude);
150 void removeTextFillAndStrokeColorsIfNeeded(RenderStyle*);
151 void setProperty(int propertyID, const String& value, bool important = false);
152 void replaceFontSizeByKeywordIfPossible(RenderStyle*, CSSComputedStyleDeclaration*);
153 void extractFontSizeDelta();
154 TriState triStateOfStyle(CSSStyleDeclaration* styleToCompare, ShouldIgnoreTextOnlyProperties) const;
155 bool conflictsWithInlineStyleOfElement(StyledElement*, EditingStyle* extractedStyle, Vector<CSSPropertyID>* conflictingProperties) const;
156 void mergeInlineAndImplicitStyleOfElement(StyledElement*, CSSPropertyOverrideMode, PropertiesToInclude);
157 void mergeStyle(CSSMutableStyleDeclaration*, CSSPropertyOverrideMode);
159 RefPtr<CSSMutableStyleDeclaration> m_mutableStyle;
160 bool m_shouldUseFixedDefaultFontSize;
161 float m_fontSizeDelta;
163 friend class HTMLElementEquivalent;
164 friend class HTMLAttributeEquivalent;
169 StyleChange(EditingStyle*, const Position&);
171 String cssStyle() const { return m_cssStyle; }
172 bool applyBold() const { return m_applyBold; }
173 bool applyItalic() const { return m_applyItalic; }
174 bool applyUnderline() const { return m_applyUnderline; }
175 bool applyLineThrough() const { return m_applyLineThrough; }
176 bool applySubscript() const { return m_applySubscript; }
177 bool applySuperscript() const { return m_applySuperscript; }
178 bool applyFontColor() const { return m_applyFontColor.length() > 0; }
179 bool applyFontFace() const { return m_applyFontFace.length() > 0; }
180 bool applyFontSize() const { return m_applyFontSize.length() > 0; }
182 String fontColor() { return m_applyFontColor; }
183 String fontFace() { return m_applyFontFace; }
184 String fontSize() { return m_applyFontSize; }
186 bool operator==(const StyleChange& other)
188 return m_cssStyle == other.m_cssStyle
189 && m_applyBold == other.m_applyBold
190 && m_applyItalic == other.m_applyItalic
191 && m_applyUnderline == other.m_applyUnderline
192 && m_applyLineThrough == other.m_applyLineThrough
193 && m_applySubscript == other.m_applySubscript
194 && m_applySuperscript == other.m_applySuperscript
195 && m_applyFontColor == other.m_applyFontColor
196 && m_applyFontFace == other.m_applyFontFace
197 && m_applyFontSize == other.m_applyFontSize;
199 bool operator!=(const StyleChange& other)
201 return !(*this == other);
204 void extractTextStyles(Document*, CSSMutableStyleDeclaration*, bool shouldUseFixedFontDefaultSize);
209 bool m_applyUnderline;
210 bool m_applyLineThrough;
211 bool m_applySubscript;
212 bool m_applySuperscript;
213 String m_applyFontColor;
214 String m_applyFontFace;
215 String m_applyFontSize;
218 // FIXME: Remove these functions or make them non-global to discourage using CSSStyleDeclaration directly.
219 int getIdentifierValue(CSSStyleDeclaration*, int propertyID);
221 } // namespace WebCore
223 #endif // EditingStyle_h