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#include "HTMLElement.h"
31#include "WritingDirection.h"
32
33namespace WebCore {
34
35class CSSPrimitiveValue;
36class EditingStyle;
37class StyleChange;
38
39enum ShouldIncludeTypingStyle {
40    IncludeTypingStyle,
41    IgnoreTypingStyle
42};
43
44class ApplyStyleCommand : public CompositeEditCommand {
45public:
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
68private:
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();
75    virtual EditAction editingAction() const;
76
77    // style-removal helpers
78    bool isStyledInlineElementToRemove(Element*) const;
79    bool removeStyleFromRunBeforeApplyingStyle(EditingStyle*, RefPtr<Node>& runStart, RefPtr<Node>& runEnd);
80    bool removeInlineStyleFromElement(EditingStyle*, PassRefPtr<HTMLElement>, InlineStyleRemovalMode = RemoveIfNeeded, EditingStyle* extractedStyle = 0);
81    inline bool shouldRemoveInlineStyleFromElement(EditingStyle* style, HTMLElement* element) {return removeInlineStyleFromElement(style, element, RemoveNone);}
82    void replaceWithSpanOrRemoveIfWithoutAttributes(HTMLElement*&);
83    bool removeImplicitlyStyledElement(EditingStyle*, HTMLElement*, InlineStyleRemovalMode, EditingStyle* extractedStyle);
84    bool removeCSSStyle(EditingStyle*, HTMLElement*, InlineStyleRemovalMode = RemoveIfNeeded, EditingStyle* extractedStyle = 0);
85    HTMLElement* highestAncestorWithConflictingInlineStyle(EditingStyle*, Node*);
86    void applyInlineStyleToPushDown(Node*, EditingStyle*);
87    void pushDownInlineStyleAroundNode(EditingStyle*, Node*);
88    void removeInlineStyle(EditingStyle* , const Position& start, const Position& end);
89    bool nodeFullySelected(Node*, const Position& start, const Position& end) const;
90    bool nodeFullyUnselected(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*, Node* startNode, Node* pastEndNode);
98    void addBlockStyle(const StyleChange&, HTMLElement*);
99    void addInlineStyleIfNeeded(EditingStyle*, PassRefPtr<Node> start, PassRefPtr<Node> end, EAddStyledElement = AddStyledElement);
100    void splitTextAtStart(const Position& start, const Position& end);
101    void splitTextAtEnd(const Position& start, const Position& end);
102    void splitTextElementAtStart(const Position& start, const Position& end);
103    void splitTextElementAtEnd(const Position& start, const Position& end);
104    bool shouldSplitTextElement(Element*, EditingStyle*);
105    bool isValidCaretPositionInTextNode(const Position& position);
106    bool mergeStartWithPreviousIfIdentical(const Position& start, const Position& end);
107    bool mergeEndWithNextIfIdentical(const Position& start, const Position& end);
108    void cleanupUnstyledAppleStyleSpans(Node* dummySpanAncestor);
109
110    void surroundNodeRangeWithElement(PassRefPtr<Node> start, PassRefPtr<Node> end, PassRefPtr<Element>);
111    float computedFontSize(Node*);
112    void joinChildTextNodes(Node*, const Position& start, const Position& end);
113
114    HTMLElement* splitAncestorsWithUnicodeBidi(Node*, bool before, WritingDirection allowedDirection);
115    void removeEmbeddingUpToEnclosingBlock(Node* node, Node* unsplitAncestor);
116
117    void updateStartEnd(const Position& newStart, const Position& newEnd);
118    Position startPosition();
119    Position endPosition();
120
121    RefPtr<EditingStyle> m_style;
122    EditAction m_editingAction;
123    EPropertyLevel m_propertyLevel;
124    Position m_start;
125    Position m_end;
126    bool m_useEndingSelection;
127    RefPtr<Element> m_styledInlineElement;
128    bool m_removeOnly;
129    IsInlineElementToRemoveFunction m_isInlineElementToRemoveFunction;
130};
131
132bool isStyleSpan(const Node*);
133PassRefPtr<HTMLElement> createStyleSpanElement(Document*);
134
135} // namespace WebCore
136
137#endif
138