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;
58 enum TriState { FalseTriState, TrueTriState, MixedTriState };
60 class EditingStyle : public RefCounted<EditingStyle> {
63 enum PropertiesToInclude { AllProperties, OnlyEditingInheritableProperties, EditingInheritablePropertiesAndBackgroundColorInEffect };
64 enum ShouldPreserveWritingDirection { PreserveWritingDirection, DoNotPreserveWritingDirection };
65 enum ShouldExtractMatchingStyle { ExtractMatchingStyle, DoNotExtractMatchingStyle };
66 static float NoFontDelta;
68 static PassRefPtr<EditingStyle> create()
70 return adoptRef(new EditingStyle());
73 static PassRefPtr<EditingStyle> create(Node* node, PropertiesToInclude propertiesToInclude = OnlyEditingInheritableProperties)
75 return adoptRef(new EditingStyle(node, propertiesToInclude));
78 static PassRefPtr<EditingStyle> create(const Position& position, PropertiesToInclude propertiesToInclude = OnlyEditingInheritableProperties)
80 return adoptRef(new EditingStyle(position, propertiesToInclude));
83 static PassRefPtr<EditingStyle> create(const CSSStyleDeclaration* style)
85 return adoptRef(new EditingStyle(style));
88 static PassRefPtr<EditingStyle> create(int propertyID, const String& value)
90 return adoptRef(new EditingStyle(propertyID, value));
95 CSSMutableStyleDeclaration* style() { return m_mutableStyle.get(); }
96 bool textDirection(WritingDirection&) const;
98 void setStyle(PassRefPtr<CSSMutableStyleDeclaration>);
99 void overrideWithStyle(const CSSMutableStyleDeclaration*);
101 PassRefPtr<EditingStyle> copy() const;
102 PassRefPtr<EditingStyle> extractAndRemoveBlockProperties();
103 PassRefPtr<EditingStyle> extractAndRemoveTextDirection();
104 void removeBlockProperties();
105 void removeStyleAddedByNode(Node*);
106 void removeStyleConflictingWithStyleOfNode(Node*);
107 void removeNonEditingProperties();
108 void collapseTextDecorationProperties();
109 enum ShouldIgnoreTextOnlyProperties { IgnoreTextOnlyProperties, DoNotIgnoreTextOnlyProperties };
110 TriState triStateOfStyle(CSSStyleDeclaration*, ShouldIgnoreTextOnlyProperties = DoNotIgnoreTextOnlyProperties) const;
111 bool conflictsWithInlineStyleOfElement(StyledElement* element) const { return conflictsWithInlineStyleOfElement(element, 0, 0); }
112 bool conflictsWithInlineStyleOfElement(StyledElement* element, EditingStyle* extractedStyle, Vector<CSSPropertyID>& conflictingProperties) const
114 return conflictsWithInlineStyleOfElement(element, extractedStyle, &conflictingProperties);
116 bool conflictsWithImplicitStyleOfElement(HTMLElement*, EditingStyle* extractedStyle = 0, ShouldExtractMatchingStyle = DoNotExtractMatchingStyle) const;
117 bool conflictsWithImplicitStyleOfAttributes(HTMLElement*) const;
118 bool extractConflictingImplicitStyleOfAttributes(HTMLElement*, ShouldPreserveWritingDirection, EditingStyle* extractedStyle,
119 Vector<QualifiedName>& conflictingAttributes, ShouldExtractMatchingStyle) const;
120 bool styleIsPresentInComputedStyleOfNode(Node*) const;
122 static bool elementIsStyledSpanOrHTMLEquivalent(const HTMLElement*);
124 void prepareToApplyAt(const Position&, ShouldPreserveWritingDirection = DoNotPreserveWritingDirection);
125 void mergeTypingStyle(Document*);
126 void mergeInlineStyleOfElement(StyledElement*);
127 void mergeStyleFromRules(StyledElement*);
128 void mergeStyleFromRulesForSerialization(StyledElement*);
129 void removeStyleFromRulesAndContext(StyledElement*, Node* context);
130 void removePropertiesInElementDefaultStyle(Element*);
133 float fontSizeDelta() const { return m_fontSizeDelta; }
134 bool hasFontSizeDelta() const { return m_fontSizeDelta != NoFontDelta; }
135 bool shouldUseFixedDefaultFontSize() const { return m_shouldUseFixedDefaultFontSize; }
139 EditingStyle(Node*, PropertiesToInclude);
140 EditingStyle(const Position&, PropertiesToInclude);
141 EditingStyle(const CSSStyleDeclaration*);
142 EditingStyle(int propertyID, const String& value);
143 void init(Node*, PropertiesToInclude);
144 void removeTextFillAndStrokeColorsIfNeeded(RenderStyle*);
145 void setProperty(int propertyID, const String& value, bool important = false);
146 void replaceFontSizeByKeywordIfPossible(RenderStyle*, CSSComputedStyleDeclaration*);
147 void extractFontSizeDelta();
148 bool conflictsWithInlineStyleOfElement(StyledElement*, EditingStyle* extractedStyle, Vector<CSSPropertyID>* conflictingProperties) const;
149 void mergeStyle(CSSMutableStyleDeclaration*);
151 RefPtr<CSSMutableStyleDeclaration> m_mutableStyle;
152 bool m_shouldUseFixedDefaultFontSize;
153 float m_fontSizeDelta;
155 friend class HTMLElementEquivalent;
156 friend class HTMLAttributeEquivalent;
161 StyleChange(EditingStyle*, const Position&);
163 String cssStyle() const { return m_cssStyle; }
164 bool applyBold() const { return m_applyBold; }
165 bool applyItalic() const { return m_applyItalic; }
166 bool applyUnderline() const { return m_applyUnderline; }
167 bool applyLineThrough() const { return m_applyLineThrough; }
168 bool applySubscript() const { return m_applySubscript; }
169 bool applySuperscript() const { return m_applySuperscript; }
170 bool applyFontColor() const { return m_applyFontColor.length() > 0; }
171 bool applyFontFace() const { return m_applyFontFace.length() > 0; }
172 bool applyFontSize() const { return m_applyFontSize.length() > 0; }
174 String fontColor() { return m_applyFontColor; }
175 String fontFace() { return m_applyFontFace; }
176 String fontSize() { return m_applyFontSize; }
178 bool operator==(const StyleChange& other)
180 return m_cssStyle == other.m_cssStyle
181 && m_applyBold == other.m_applyBold
182 && m_applyItalic == other.m_applyItalic
183 && m_applyUnderline == other.m_applyUnderline
184 && m_applyLineThrough == other.m_applyLineThrough
185 && m_applySubscript == other.m_applySubscript
186 && m_applySuperscript == other.m_applySuperscript
187 && m_applyFontColor == other.m_applyFontColor
188 && m_applyFontFace == other.m_applyFontFace
189 && m_applyFontSize == other.m_applyFontSize;
191 bool operator!=(const StyleChange& other)
193 return !(*this == other);
196 void extractTextStyles(Document*, CSSMutableStyleDeclaration*, bool shouldUseFixedFontDefaultSize);
201 bool m_applyUnderline;
202 bool m_applyLineThrough;
203 bool m_applySubscript;
204 bool m_applySuperscript;
205 String m_applyFontColor;
206 String m_applyFontFace;
207 String m_applyFontSize;
210 // FIXME: Remove these functions or make them non-global to discourage using CSSStyleDeclaration directly.
211 int getIdentifierValue(CSSStyleDeclaration*, int propertyID);
212 enum LegacyFontSizeMode { AlwaysUseLegacyFontSize, UseLegacyFontSizeOnlyIfPixelValuesMatch };
213 int legacyFontSizeFromCSSValue(Document*, CSSPrimitiveValue*, bool shouldUseFixedFontDefaultSize, LegacyFontSizeMode);
214 bool hasTransparentBackgroundColor(CSSStyleDeclaration*);
215 PassRefPtr<CSSValue> backgroundColorInEffect(Node*);
217 } // namespace WebCore
219 #endif // EditingStyle_h