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 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 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#pragma once
27
28#include "CompositeEditCommand.h"
29#include "HTMLElement.h"
30#include "WritingDirection.h"
31
32namespace WebCore {
33
34class CSSPrimitiveValue;
35class EditingStyle;
36class StyleChange;
37
38enum ShouldIncludeTypingStyle {
39 IncludeTypingStyle,
40 IgnoreTypingStyle
41};
42
43class ApplyStyleCommand : public CompositeEditCommand {
44public:
45 enum EPropertyLevel { PropertyDefault, ForceBlockProperties };
46 enum InlineStyleRemovalMode { RemoveIfNeeded, RemoveAlways, RemoveNone };
47 enum EAddStyledElement { AddStyledElement, DoNotAddStyledElement };
48 typedef bool (*IsInlineElementToRemoveFunction)(const Element*);
49
50 static Ref<ApplyStyleCommand> create(Document& document, const EditingStyle* style, EditAction action = EditAction::ChangeAttributes, EPropertyLevel level = PropertyDefault)
51 {
52 return adoptRef(*new ApplyStyleCommand(document, style, action, level));
53 }
54 static Ref<ApplyStyleCommand> create(Document& document, const EditingStyle* style, const Position& start, const Position& end, EditAction action = EditAction::ChangeAttributes, EPropertyLevel level = PropertyDefault)
55 {
56 return adoptRef(*new ApplyStyleCommand(document, style, start, end, action, level));
57 }
58 static Ref<ApplyStyleCommand> create(Ref<Element>&& element, bool removeOnly = false, EditAction action = EditAction::ChangeAttributes)
59 {
60 return adoptRef(*new ApplyStyleCommand(WTFMove(element), removeOnly, action));
61 }
62 static Ref<ApplyStyleCommand> create(Document& document, const EditingStyle* style, IsInlineElementToRemoveFunction isInlineElementToRemoveFunction, EditAction action = EditAction::ChangeAttributes)
63 {
64 return adoptRef(*new ApplyStyleCommand(document, style, isInlineElementToRemoveFunction, action));
65 }
66
67private:
68 ApplyStyleCommand(Document&, const EditingStyle*, EditAction, EPropertyLevel);
69 ApplyStyleCommand(Document&, const EditingStyle*, const Position& start, const Position& end, EditAction, EPropertyLevel);
70 ApplyStyleCommand(Ref<Element>&&, bool removeOnly, EditAction);
71 ApplyStyleCommand(Document&, const EditingStyle*, bool (*isInlineElementToRemove)(const Element*), EditAction);
72
73 void doApply() override;
74 bool shouldDispatchInputEvents() const final { return false; }
75
76 // style-removal helpers
77 bool isStyledInlineElementToRemove(Element*) const;
78 bool shouldApplyInlineStyleToRun(EditingStyle&, Node* runStart, Node* pastEndNode);
79 void removeConflictingInlineStyleFromRun(EditingStyle&, RefPtr<Node>& runStart, RefPtr<Node>& runEnd, Node* pastEndNode);
80 bool removeInlineStyleFromElement(EditingStyle&, HTMLElement&, InlineStyleRemovalMode = RemoveIfNeeded, EditingStyle* extractedStyle = nullptr);
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 = nullptr);
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(Element&, const Position& start, const Position& end) const;
90 bool nodeFullyUnselected(Element&, 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*, Node& start, Node& end, EAddStyledElement = AddStyledElement);
100 Position positionToComputeInlineStyleChange(Node&, RefPtr<Node>& dummyElement);
101 void applyInlineStyleChange(Node& startNode, Node& endNode, StyleChange&, EAddStyledElement);
102 void splitTextAtStart(const Position& start, const Position& end);
103 void splitTextAtEnd(const Position& start, const Position& end);
104 void splitTextElementAtStart(const Position& start, const Position& end);
105 void splitTextElementAtEnd(const Position& start, const Position& end);
106 bool shouldSplitTextElement(Element*, EditingStyle&);
107 bool isValidCaretPositionInTextNode(const Position& position);
108 bool mergeStartWithPreviousIfIdentical(const Position& start, const Position& end);
109 bool mergeEndWithNextIfIdentical(const Position& start, const Position& end);
110 void cleanupUnstyledAppleStyleSpans(ContainerNode* dummySpanAncestor);
111
112 void surroundNodeRangeWithElement(Node& start, Node& end, Ref<Element>&&);
113 float computedFontSize(Node*);
114 void joinChildTextNodes(Node*, const Position& start, const Position& end);
115
116 HTMLElement* splitAncestorsWithUnicodeBidi(Node*, bool before, WritingDirection allowedDirection);
117 void removeEmbeddingUpToEnclosingBlock(Node* node, Node* unsplitAncestor);
118
119 void updateStartEnd(const Position& newStart, const Position& newEnd);
120 Position startPosition();
121 Position endPosition();
122
123 RefPtr<EditingStyle> m_style;
124 EPropertyLevel m_propertyLevel;
125 Position m_start;
126 Position m_end;
127 bool m_useEndingSelection;
128 RefPtr<Element> m_styledInlineElement;
129 bool m_removeOnly;
130 IsInlineElementToRemoveFunction m_isInlineElementToRemoveFunction { nullptr };
131};
132
133enum ShouldStyleAttributeBeEmpty { AllowNonEmptyStyleAttribute, StyleAttributeShouldBeEmpty };
134bool isEmptyFontTag(const Element*, ShouldStyleAttributeBeEmpty = StyleAttributeShouldBeEmpty);
135bool isLegacyAppleStyleSpan(const Node*);
136bool isStyleSpanOrSpanWithOnlyStyleAttribute(const Element&);
137Ref<HTMLElement> createStyleSpanElement(Document&);
138
139} // namespace WebCore
140