Home | History | Annotate | Download | only in editing
      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 
     33 namespace WebCore {
     34 
     35 class CSSPrimitiveValue;
     36 class EditingStyle;
     37 class StyleChange;
     38 
     39 enum ShouldIncludeTypingStyle {
     40     IncludeTypingStyle,
     41     IgnoreTypingStyle
     42 };
     43 
     44 class ApplyStyleCommand : public CompositeEditCommand {
     45 public:
     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 
     68 private:
     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 shouldApplyInlineStyleToRun(EditingStyle*, Node* runStart, Node* pastEndNode);
     80     void removeConflictingInlineStyleFromRun(EditingStyle*, RefPtr<Node>& runStart, RefPtr<Node>& runEnd, PassRefPtr<Node> pastEndNode);
     81     bool removeInlineStyleFromElement(EditingStyle*, PassRefPtr<HTMLElement>, InlineStyleRemovalMode = RemoveIfNeeded, EditingStyle* extractedStyle = 0);
     82     inline bool shouldRemoveInlineStyleFromElement(EditingStyle* style, HTMLElement* element) {return removeInlineStyleFromElement(style, element, RemoveNone);}
     83     void replaceWithSpanOrRemoveIfWithoutAttributes(HTMLElement*&);
     84     bool removeImplicitlyStyledElement(EditingStyle*, HTMLElement*, InlineStyleRemovalMode, EditingStyle* extractedStyle);
     85     bool removeCSSStyle(EditingStyle*, HTMLElement*, InlineStyleRemovalMode = RemoveIfNeeded, EditingStyle* extractedStyle = 0);
     86     HTMLElement* highestAncestorWithConflictingInlineStyle(EditingStyle*, Node*);
     87     void applyInlineStyleToPushDown(Node*, EditingStyle*);
     88     void pushDownInlineStyleAroundNode(EditingStyle*, Node*);
     89     void removeInlineStyle(EditingStyle* , const Position& start, const Position& end);
     90     bool nodeFullySelected(Node*, const Position& start, const Position& end) const;
     91     bool nodeFullyUnselected(Node*, const Position& start, const Position& end) const;
     92 
     93     // style-application helpers
     94     void applyBlockStyle(EditingStyle*);
     95     void applyRelativeFontStyleChange(EditingStyle*);
     96     void applyInlineStyle(EditingStyle*);
     97     void fixRangeAndApplyInlineStyle(EditingStyle*, const Position& start, const Position& end);
     98     void applyInlineStyleToNodeRange(EditingStyle*, PassRefPtr<Node> startNode, PassRefPtr<Node> pastEndNode);
     99     void addBlockStyle(const StyleChange&, HTMLElement*);
    100     void addInlineStyleIfNeeded(EditingStyle*, PassRefPtr<Node> start, PassRefPtr<Node> end, EAddStyledElement = AddStyledElement);
    101     Position positionToComputeInlineStyleChange(PassRefPtr<Node>, RefPtr<Node>& dummyElement);
    102     void applyInlineStyleChange(PassRefPtr<Node> startNode, PassRefPtr<Node> endNode, StyleChange&, EAddStyledElement);
    103     void splitTextAtStart(const Position& start, const Position& end);
    104     void splitTextAtEnd(const Position& start, const Position& end);
    105     void splitTextElementAtStart(const Position& start, const Position& end);
    106     void splitTextElementAtEnd(const Position& start, const Position& end);
    107     bool shouldSplitTextElement(Element*, EditingStyle*);
    108     bool isValidCaretPositionInTextNode(const Position& position);
    109     bool mergeStartWithPreviousIfIdentical(const Position& start, const Position& end);
    110     bool mergeEndWithNextIfIdentical(const Position& start, const Position& end);
    111     void cleanupUnstyledAppleStyleSpans(Node* dummySpanAncestor);
    112 
    113     void surroundNodeRangeWithElement(PassRefPtr<Node> start, PassRefPtr<Node> end, PassRefPtr<Element>);
    114     float computedFontSize(Node*);
    115     void joinChildTextNodes(Node*, const Position& start, const Position& end);
    116 
    117     HTMLElement* splitAncestorsWithUnicodeBidi(Node*, bool before, WritingDirection allowedDirection);
    118     void removeEmbeddingUpToEnclosingBlock(Node* node, Node* unsplitAncestor);
    119 
    120     void updateStartEnd(const Position& newStart, const Position& newEnd);
    121     Position startPosition();
    122     Position endPosition();
    123 
    124     RefPtr<EditingStyle> m_style;
    125     EditAction m_editingAction;
    126     EPropertyLevel m_propertyLevel;
    127     Position m_start;
    128     Position m_end;
    129     bool m_useEndingSelection;
    130     RefPtr<Element> m_styledInlineElement;
    131     bool m_removeOnly;
    132     IsInlineElementToRemoveFunction m_isInlineElementToRemoveFunction;
    133 };
    134 
    135 enum ShouldStyleAttributeBeEmpty { AllowNonEmptyStyleAttribute, StyleAttributeShouldBeEmpty };
    136 bool isEmptyFontTag(const Element*, ShouldStyleAttributeBeEmpty = StyleAttributeShouldBeEmpty);
    137 bool isLegacyAppleStyleSpan(const Node*);
    138 bool isStyleSpanOrSpanWithOnlyStyleAttribute(const Element*);
    139 PassRefPtr<HTMLElement> createStyleSpanElement(Document*);
    140 
    141 } // namespace WebCore
    142 
    143 #endif
    144