Changeset 239127 in webkit


Ignore:
Timestamp:
Dec 12, 2018 1:20:10 PM (5 years ago)
Author:
rniwa@webkit.org
Message:

Make HTMLConverter take two Positions in preparation to make it work with shadow DOM
https://bugs.webkit.org/show_bug.cgi?id=192613

Reviewed by Darin Adler.

This patch makes HTMLConverter store two Position's instead of a Range so that HTMLConverter can work with
a selection which spans across shadow boundaries in the future.

No new tests since there should be no observable behavioral change.

  • editing/cocoa/EditorCocoa.mm:

(WebCore::Editor::writeSelectionToPasteboard): Uses the newly introduced writeSelectionToPasteboard.
(WebCore::Editor::writeSelection): Ditto.

  • editing/cocoa/HTMLConverter.h:
  • editing/cocoa/HTMLConverter.mm:

(HTMLConverter::HTMLConverter): Now takes two Position's.
(HTMLConverter::convert): Updated to work with Position's.
(HTMLConverter::_processText): Ditto.
(HTMLConverter::_traverseNode): Ditto.
(HTMLConverter::_traverseFooterNode): Ditto.
(HTMLConverterCaches::cacheAncestorsOfStartToBeConverted): Ditto.
(WebCore::attributedStringFromRange): Ditto.
(WebCore::attributedStringFromSelection): Added. For now, we first create a Range via toNormalizedRange
in order to preserve the exact behavior.

Location:
trunk/Source/WebCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r239123 r239127  
     12018-12-11  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        Make HTMLConverter take two Positions in preparation to make it work with shadow DOM
     4        https://bugs.webkit.org/show_bug.cgi?id=192613
     5
     6        Reviewed by Darin Adler.
     7
     8        This patch makes HTMLConverter store two Position's instead of a Range so that HTMLConverter can work with
     9        a selection which spans across shadow boundaries in the future.
     10
     11        No new tests since there should be no observable behavioral change.
     12
     13        * editing/cocoa/EditorCocoa.mm:
     14        (WebCore::Editor::writeSelectionToPasteboard): Uses the newly introduced writeSelectionToPasteboard.
     15        (WebCore::Editor::writeSelection): Ditto.
     16        * editing/cocoa/HTMLConverter.h:
     17        * editing/cocoa/HTMLConverter.mm:
     18        (HTMLConverter::HTMLConverter): Now takes two Position's.
     19        (HTMLConverter::convert): Updated to work with Position's.
     20        (HTMLConverter::_processText): Ditto.
     21        (HTMLConverter::_traverseNode): Ditto.
     22        (HTMLConverter::_traverseFooterNode): Ditto.
     23        (HTMLConverterCaches::cacheAncestorsOfStartToBeConverted): Ditto.
     24        (WebCore::attributedStringFromRange): Ditto.
     25        (WebCore::attributedStringFromSelection): Added. For now, we first create a Range via toNormalizedRange
     26        in order to preserve the exact behavior.
     27
    1282018-12-12  Michael Catanzaro  <mcatanzaro@igalia.com>
    229
  • trunk/Source/WebCore/editing/cocoa/EditorCocoa.mm

    r238771 r239127  
    104104void Editor::writeSelectionToPasteboard(Pasteboard& pasteboard)
    105105{
    106     NSAttributedString *attributedString = attributedStringFromRange(*selectedRange());
     106    NSAttributedString *attributedString = attributedStringFromSelection(m_frame.selection().selection());
    107107
    108108    PasteboardWebContent content;
     
    122122void Editor::writeSelection(PasteboardWriterData& pasteboardWriterData)
    123123{
    124     NSAttributedString *attributedString = attributedStringFromRange(*selectedRange());
     124    NSAttributedString *attributedString = attributedStringFromSelection(m_frame.selection().selection());
    125125
    126126    PasteboardWriterData::WebContent webContent;
  • trunk/Source/WebCore/editing/cocoa/HTMLConverter.h

    r237266 r239127  
    3131   
    3232class Range;
     33class VisibleSelection;
    3334
    3435enum class IncludeImagesInAttributedString { Yes, No };
    35    
     36
     37NSAttributedString *attributedStringFromSelection(const VisibleSelection&);
    3638WEBCORE_EXPORT NSAttributedString *attributedStringFromRange(Range&);
    3739#if !PLATFORM(IOS_FAMILY)
  • trunk/Source/WebCore/editing/cocoa/HTMLConverter.mm

    r237266 r239127  
    3737#import "Document.h"
    3838#import "DocumentLoader.h"
     39#import "Editing.h"
    3940#import "Element.h"
    4041#import "ElementTraversal.h"
     
    6263#import "StyledElement.h"
    6364#import "TextIterator.h"
     65#import "VisibleSelection.h"
    6466#import <objc/runtime.h>
    6567#import <pal/spi/cocoa/NSAttributedStringSPI.h>
     
    337339    RefPtr<CSSValue> inlineStylePropertyForElement(Element&, CSSPropertyID);
    338340
    339     Node* cacheAncestorsOfStartToBeConverted(const Range&);
     341    Node* cacheAncestorsOfStartToBeConverted(const Position&, const Position&);
    340342    bool isAncestorsOfStartToBeConverted(Node& node) const { return m_ancestorsUnderCommonAncestor.contains(&node); }
    341343
     
    360362class HTMLConverter {
    361363public:
    362     HTMLConverter(Range&);
     364    HTMLConverter(const Position&, const Position&);
    363365    ~HTMLConverter();
    364366   
     
    366368   
    367369private:
    368     Ref<Range> m_range;
     370    Position m_start;
     371    Position m_end;
    369372    DocumentLoader* m_dataSource;
    370373   
     
    435438};
    436439
    437 HTMLConverter::HTMLConverter(Range& range)
    438     : m_range(range)
     440HTMLConverter::HTMLConverter(const Position& start, const Position& end)
     441    : m_start(start)
     442    , m_end(end)
    439443    , m_dataSource(nullptr)
    440444{
     
    482486NSAttributedString *HTMLConverter::convert()
    483487{
    484     Node* commonAncestorContainer = _caches->cacheAncestorsOfStartToBeConverted(m_range);
     488    if (comparePositions(m_start, m_end) > 0)
     489        return nil;
     490
     491    Node* commonAncestorContainer = _caches->cacheAncestorsOfStartToBeConverted(m_start, m_end);
    485492    ASSERT(commonAncestorContainer);
    486493
     
    21982205    unsigned startOffset = 0;
    21992206    unsigned endOffset = originalString.length();
    2200     if (&characterData == &m_range->startContainer()) {
    2201         startOffset = m_range->startOffset();
     2207    if (&characterData == m_start.containerNode()) {
     2208        startOffset = m_start.offsetInContainerNode();
    22022209        _domRangeStartIndex = [_attrStr length];
    22032210        _flags.reachedStart = YES;
    22042211    }
    2205     if (&characterData == &m_range->endContainer()) {
    2206         endOffset = m_range->endOffset();
     2212    if (&characterData == m_end.containerNode()) {
     2213        endOffset = m_end.offsetInContainerNode();
    22072214        _flags.reachedEnd = YES;
    22082215    }
     
    22782285    bool isStart = false;
    22792286    bool isEnd = false;
    2280     if (&node == &m_range->startContainer()) {
    2281         startOffset = m_range->startOffset();
     2287    if (&node == m_start.containerNode()) {
     2288        startOffset = m_start.offsetInContainerNode();
    22822289        isStart = true;
    22832290        _flags.reachedStart = YES;
    22842291    }
    2285     if (&node == &m_range->endContainer()) {
    2286         endOffset = m_range->endOffset();
     2292    if (&node == m_end.containerNode()) {
     2293        endOffset = m_end.offsetInContainerNode();
    22872294        isEnd = true;
    22882295    }
     
    23392346    bool isStart = false;
    23402347    bool isEnd = false;
    2341     if (&element == &m_range->startContainer()) {
    2342         startOffset = m_range->startOffset();
     2348    if (&element == m_start.containerNode()) {
     2349        startOffset = m_start.offsetInContainerNode();
    23432350        isStart = true;
    23442351        _flags.reachedStart = YES;
    23452352    }
    2346     if (&element == &m_range->endContainer()) {
    2347         endOffset = m_range->endOffset();
     2353    if (&element == m_end.containerNode()) {
     2354        endOffset = m_end.offsetInContainerNode();
    23482355        isEnd = true;
    23492356    }
     
    23802387}
    23812388
    2382 Node* HTMLConverterCaches::cacheAncestorsOfStartToBeConverted(const Range& range)
    2383 {
    2384     Node* commonAncestor = range.commonAncestorContainer();
    2385     Node* ancestor = &range.startContainer();
     2389Node* HTMLConverterCaches::cacheAncestorsOfStartToBeConverted(const Position& start, const Position& end)
     2390{
     2391    Node* commonAncestor = Range::commonAncestorContainer(start.containerNode(), end.containerNode());
     2392    Node* ancestor = start.containerNode();
    23862393
    23872394    while (ancestor) {
     
    24542461NSAttributedString *attributedStringFromRange(Range& range)
    24552462{
    2456     HTMLConverter converter(range);
    2457     return converter.convert();
     2463    return HTMLConverter { range.startPosition(), range.endPosition() }.convert();
     2464}
     2465
     2466NSAttributedString *attributedStringFromSelection(const VisibleSelection& selection)
     2467{
     2468    auto range = selection.toNormalizedRange();
     2469    ASSERT(range);
     2470    return HTMLConverter { range->startPosition(), range->endPosition() }.convert();
    24582471}
    24592472   
Note: See TracChangeset for help on using the changeset viewer.