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 "core/editing/CompositeEditCommand.h"
30#include "core/editing/WritingDirection.h"
31#include "core/html/HTMLElement.h"
32
33namespace blink {
34
35class EditingStyle;
36class HTMLSpanElement;
37class StyleChange;
38
39enum ShouldIncludeTypingStyle {
40    IncludeTypingStyle,
41    IgnoreTypingStyle
42};
43
44class ApplyStyleCommand FINAL : 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 PassRefPtrWillBeRawPtr<ApplyStyleCommand> create(Document& document, const EditingStyle* style, EditAction action = EditActionChangeAttributes, EPropertyLevel level = PropertyDefault)
52    {
53        return adoptRefWillBeNoop(new ApplyStyleCommand(document, style, action, level));
54    }
55    static PassRefPtrWillBeRawPtr<ApplyStyleCommand> create(Document& document, const EditingStyle* style, const Position& start, const Position& end, EditAction action = EditActionChangeAttributes, EPropertyLevel level = PropertyDefault)
56    {
57        return adoptRefWillBeNoop(new ApplyStyleCommand(document, style, start, end, action, level));
58    }
59    static PassRefPtrWillBeRawPtr<ApplyStyleCommand> create(PassRefPtrWillBeRawPtr<Element> element, bool removeOnly = false, EditAction action = EditActionChangeAttributes)
60    {
61        return adoptRefWillBeNoop(new ApplyStyleCommand(element, removeOnly, action));
62    }
63    static PassRefPtrWillBeRawPtr<ApplyStyleCommand> create(Document& document, const EditingStyle* style, IsInlineElementToRemoveFunction isInlineElementToRemoveFunction, EditAction action = EditActionChangeAttributes)
64    {
65        return adoptRefWillBeNoop(new ApplyStyleCommand(document, style, isInlineElementToRemoveFunction, action));
66    }
67
68    virtual void trace(Visitor*) OVERRIDE;
69
70private:
71    ApplyStyleCommand(Document&, const EditingStyle*, EditAction, EPropertyLevel);
72    ApplyStyleCommand(Document&, const EditingStyle*, const Position& start, const Position& end, EditAction, EPropertyLevel);
73    ApplyStyleCommand(PassRefPtrWillBeRawPtr<Element>, bool removeOnly, EditAction);
74    ApplyStyleCommand(Document&, const EditingStyle*, bool (*isInlineElementToRemove)(const Element*), EditAction);
75
76    virtual void doApply() OVERRIDE;
77    virtual EditAction editingAction() const OVERRIDE;
78
79    // style-removal helpers
80    bool isStyledInlineElementToRemove(Element*) const;
81    bool shouldApplyInlineStyleToRun(EditingStyle*, Node* runStart, Node* pastEndNode);
82    void removeConflictingInlineStyleFromRun(EditingStyle*, RefPtrWillBeMember<Node>& runStart, RefPtrWillBeMember<Node>& runEnd, PassRefPtrWillBeRawPtr<Node> pastEndNode);
83    bool removeInlineStyleFromElement(EditingStyle*, PassRefPtrWillBeRawPtr<HTMLElement>, InlineStyleRemovalMode = RemoveIfNeeded, EditingStyle* extractedStyle = 0);
84    inline bool shouldRemoveInlineStyleFromElement(EditingStyle* style, HTMLElement* element) {return removeInlineStyleFromElement(style, element, RemoveNone);}
85    void replaceWithSpanOrRemoveIfWithoutAttributes(HTMLElement*);
86    bool removeImplicitlyStyledElement(EditingStyle*, HTMLElement*, InlineStyleRemovalMode, EditingStyle* extractedStyle);
87    bool removeCSSStyle(EditingStyle*, HTMLElement*, InlineStyleRemovalMode = RemoveIfNeeded, EditingStyle* extractedStyle = 0);
88    HTMLElement* highestAncestorWithConflictingInlineStyle(EditingStyle*, Node*);
89    void applyInlineStyleToPushDown(Node*, EditingStyle*);
90    void pushDownInlineStyleAroundNode(EditingStyle*, Node*);
91    void removeInlineStyle(EditingStyle* , const Position& start, const Position& end);
92    bool elementFullySelected(HTMLElement&, const Position& start, const Position& end) const;
93
94    // style-application helpers
95    void applyBlockStyle(EditingStyle*);
96    void applyRelativeFontStyleChange(EditingStyle*);
97    void applyInlineStyle(EditingStyle*);
98    void fixRangeAndApplyInlineStyle(EditingStyle*, const Position& start, const Position& end);
99    void applyInlineStyleToNodeRange(EditingStyle*, PassRefPtrWillBeRawPtr<Node> startNode, PassRefPtrWillBeRawPtr<Node> pastEndNode);
100    void addBlockStyle(const StyleChange&, HTMLElement*);
101    void addInlineStyleIfNeeded(EditingStyle*, PassRefPtrWillBeRawPtr<Node> start, PassRefPtrWillBeRawPtr<Node> end, EAddStyledElement = AddStyledElement);
102    Position positionToComputeInlineStyleChange(PassRefPtrWillBeRawPtr<Node>, RefPtrWillBeMember<HTMLSpanElement>& dummyElement);
103    void applyInlineStyleChange(PassRefPtrWillBeRawPtr<Node> startNode, PassRefPtrWillBeRawPtr<Node> endNode, StyleChange&, EAddStyledElement);
104    void splitTextAtStart(const Position& start, const Position& end);
105    void splitTextAtEnd(const Position& start, const Position& end);
106    void splitTextElementAtStart(const Position& start, const Position& end);
107    void splitTextElementAtEnd(const Position& start, const Position& end);
108    bool shouldSplitTextElement(Element*, EditingStyle*);
109    bool isValidCaretPositionInTextNode(const Position& position);
110    bool mergeStartWithPreviousIfIdentical(const Position& start, const Position& end);
111    bool mergeEndWithNextIfIdentical(const Position& start, const Position& end);
112    void cleanupUnstyledAppleStyleSpans(ContainerNode* dummySpanAncestor);
113
114    void surroundNodeRangeWithElement(PassRefPtrWillBeRawPtr<Node> start, PassRefPtrWillBeRawPtr<Node> end, PassRefPtrWillBeRawPtr<Element>);
115    float computedFontSize(Node*);
116    void joinChildTextNodes(ContainerNode*, const Position& start, const Position& end);
117
118    HTMLElement* splitAncestorsWithUnicodeBidi(Node*, bool before, WritingDirection allowedDirection);
119    void removeEmbeddingUpToEnclosingBlock(Node*, HTMLElement* unsplitAncestor);
120
121    void updateStartEnd(const Position& newStart, const Position& newEnd);
122    Position startPosition();
123    Position endPosition();
124
125    RefPtrWillBeMember<EditingStyle> m_style;
126    EditAction m_editingAction;
127    EPropertyLevel m_propertyLevel;
128    Position m_start;
129    Position m_end;
130    bool m_useEndingSelection;
131    RefPtrWillBeMember<Element> m_styledInlineElement;
132    bool m_removeOnly;
133    IsInlineElementToRemoveFunction m_isInlineElementToRemoveFunction;
134};
135
136enum ShouldStyleAttributeBeEmpty { AllowNonEmptyStyleAttribute, StyleAttributeShouldBeEmpty };
137bool isEmptyFontTag(const Element*, ShouldStyleAttributeBeEmpty = StyleAttributeShouldBeEmpty);
138bool isLegacyAppleHTMLSpanElement(const Node*);
139bool isStyleSpanOrSpanWithOnlyStyleAttribute(const Element*);
140PassRefPtrWillBeRawPtr<HTMLSpanElement> createStyleSpanElement(Document&);
141
142} // namespace blink
143
144#endif
145