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 "CompositeEditCommand.h" 30 31 namespace WebCore { 32 33 class CSSPrimitiveValue; 34 class HTMLElement; 35 class StyleChange; 36 37 class ApplyStyleCommand : public CompositeEditCommand { 38 public: 39 enum EPropertyLevel { PropertyDefault, ForceBlockProperties }; 40 41 static PassRefPtr<ApplyStyleCommand> create(Document* document, CSSStyleDeclaration* style, EditAction action = EditActionChangeAttributes, EPropertyLevel level = PropertyDefault) 42 { 43 return adoptRef(new ApplyStyleCommand(document, style, action, level)); 44 } 45 static PassRefPtr<ApplyStyleCommand> create(Document* document, CSSStyleDeclaration* style, const Position& start, const Position& end, EditAction action = EditActionChangeAttributes, EPropertyLevel level = PropertyDefault) 46 { 47 return adoptRef(new ApplyStyleCommand(document, style, start, end, action, level)); 48 } 49 static PassRefPtr<ApplyStyleCommand> create(PassRefPtr<Element> element, bool removeOnly = false, EditAction action = EditActionChangeAttributes) 50 { 51 return adoptRef(new ApplyStyleCommand(element, removeOnly, action)); 52 } 53 54 private: 55 ApplyStyleCommand(Document*, CSSStyleDeclaration*, EditAction, EPropertyLevel); 56 ApplyStyleCommand(Document*, CSSStyleDeclaration*, const Position& start, const Position& end, EditAction, EPropertyLevel); 57 ApplyStyleCommand(PassRefPtr<Element>, bool removeOnly, EditAction); 58 59 virtual void doApply(); 60 virtual EditAction editingAction() const; 61 style()62 CSSMutableStyleDeclaration* style() const { return m_style.get(); } 63 64 // style-removal helpers 65 bool shouldRemoveTextDecorationTag(CSSStyleDeclaration* styleToApply, int textDecorationAddedByTag) const; 66 bool implicitlyStyledElementShouldBeRemovedWhenApplyingStyle(HTMLElement*, CSSMutableStyleDeclaration*); 67 void replaceWithSpanOrRemoveIfWithoutAttributes(HTMLElement*&); 68 void removeHTMLFontStyle(CSSMutableStyleDeclaration*, HTMLElement*); 69 void removeHTMLBidiEmbeddingStyle(CSSMutableStyleDeclaration*, HTMLElement*); 70 void removeCSSStyle(CSSMutableStyleDeclaration*, HTMLElement*); 71 void removeInlineStyle(PassRefPtr<CSSMutableStyleDeclaration>, const Position& start, const Position& end); 72 bool nodeFullySelected(Node*, const Position& start, const Position& end) const; 73 bool nodeFullyUnselected(Node*, const Position& start, const Position& end) const; 74 PassRefPtr<CSSMutableStyleDeclaration> extractTextDecorationStyle(Node*); 75 PassRefPtr<CSSMutableStyleDeclaration> extractAndNegateTextDecorationStyle(Node*); 76 void applyTextDecorationStyle(Node*, CSSMutableStyleDeclaration *style); 77 void pushDownTextDecorationStyleAroundNode(Node*, bool forceNegate); 78 void pushDownTextDecorationStyleAtBoundaries(const Position& start, const Position& end); 79 80 // style-application helpers 81 void applyBlockStyle(CSSMutableStyleDeclaration*); 82 void applyRelativeFontStyleChange(CSSMutableStyleDeclaration*); 83 void applyInlineStyle(CSSMutableStyleDeclaration*); 84 void applyInlineStyleToRange(CSSMutableStyleDeclaration*, const Position& start, const Position& end); 85 void addBlockStyle(const StyleChange&, HTMLElement*); 86 void addInlineStyleIfNeeded(CSSMutableStyleDeclaration*, Node* start, Node* end); 87 bool splitTextAtStartIfNeeded(const Position& start, const Position& end); 88 bool splitTextAtEndIfNeeded(const Position& start, const Position& end); 89 bool splitTextElementAtStartIfNeeded(const Position& start, const Position& end); 90 bool splitTextElementAtEndIfNeeded(const Position& start, const Position& end); 91 bool mergeStartWithPreviousIfIdentical(const Position& start, const Position& end); 92 bool mergeEndWithNextIfIdentical(const Position& start, const Position& end); 93 void cleanupUnstyledAppleStyleSpans(Node* dummySpanAncestor); 94 95 void surroundNodeRangeWithElement(Node* start, Node* end, PassRefPtr<Element>); 96 float computedFontSize(const Node*); 97 void joinChildTextNodes(Node*, const Position& start, const Position& end); 98 99 HTMLElement* splitAncestorsWithUnicodeBidi(Node*, bool before, RefPtr<CSSPrimitiveValue> allowedDirection); 100 void removeEmbeddingUpToEnclosingBlock(Node* node, Node* unsplitAncestor); 101 102 void updateStartEnd(const Position& newStart, const Position& newEnd); 103 Position startPosition(); 104 Position endPosition(); 105 106 RefPtr<CSSMutableStyleDeclaration> m_style; 107 EditAction m_editingAction; 108 EPropertyLevel m_propertyLevel; 109 Position m_start; 110 Position m_end; 111 bool m_useEndingSelection; 112 RefPtr<Element> m_styledInlineElement; 113 bool m_removeOnly; 114 }; 115 116 bool isStyleSpan(const Node*); 117 PassRefPtr<HTMLElement> createStyleSpanElement(Document*); 118 RefPtr<CSSMutableStyleDeclaration> getPropertiesNotInComputedStyle(CSSStyleDeclaration* style, CSSComputedStyleDeclaration* computedStyle); 119 120 enum ShouldIncludeTypingStyle { 121 IncludeTypingStyle, 122 IgnoreTypingStyle 123 }; 124 125 PassRefPtr<CSSMutableStyleDeclaration> editingStyleAtPosition(Position, ShouldIncludeTypingStyle = IgnoreTypingStyle); 126 void prepareEditingStyleToApplyAt(CSSMutableStyleDeclaration*, Position); 127 void removeStylesAddedByNode(CSSMutableStyleDeclaration*, Node*); 128 129 } // namespace WebCore 130 131 #endif 132