Expose document attributes and body background color through HTMLConverter.
authortimothy@apple.com <timothy@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 12 Mar 2019 22:42:49 +0000 (22:42 +0000)
committertimothy@apple.com <timothy@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 12 Mar 2019 22:42:49 +0000 (22:42 +0000)
https://bugs.webkit.org/show_bug.cgi?id=195636
rdar://problem/45055697

Reviewed by Tim Horton.

Source/WebCore:

* editing/cocoa/HTMLConverter.h:
* editing/cocoa/HTMLConverter.mm:
(HTMLConverter::convert):
(WebCore::attributedStringFromRange):
(WebCore::attributedStringFromSelection):
(WebCore::attributedStringBetweenStartAndEnd):

Source/WebCore/PAL:

* pal/spi/cocoa/NSAttributedStringSPI.h:
(NSBackgroundColorDocumentAttribute): Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242831 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/PAL/ChangeLog
Source/WebCore/PAL/pal/spi/cocoa/NSAttributedStringSPI.h
Source/WebCore/editing/cocoa/HTMLConverter.h
Source/WebCore/editing/cocoa/HTMLConverter.mm

index 65ddbd1..5c5f147 100644 (file)
@@ -1,3 +1,18 @@
+2019-03-12  Timothy Hatcher  <timothy@apple.com>
+
+        Expose document attributes and body background color through HTMLConverter.
+        https://bugs.webkit.org/show_bug.cgi?id=195636
+        rdar://problem/45055697
+
+        Reviewed by Tim Horton.
+
+        * editing/cocoa/HTMLConverter.h:
+        * editing/cocoa/HTMLConverter.mm:
+        (HTMLConverter::convert):
+        (WebCore::attributedStringFromRange):
+        (WebCore::attributedStringFromSelection):
+        (WebCore::attributedStringBetweenStartAndEnd):
+
 2019-03-12  Antti Koivisto  <antti@apple.com>
 
         Compositing layer that renders two positioned elements should not hit test
index 16aa71b..95c8322 100644 (file)
@@ -1,3 +1,14 @@
+2019-03-12  Timothy Hatcher  <timothy@apple.com>
+
+        Expose document attributes and body background color through HTMLConverter.
+        https://bugs.webkit.org/show_bug.cgi?id=195636
+        rdar://problem/45055697
+
+        Reviewed by Tim Horton.
+
+        * pal/spi/cocoa/NSAttributedStringSPI.h:
+        (NSBackgroundColorDocumentAttribute): Added.
+
 2019-03-12  Jennifer Moore  <jennifer.moore@apple.com>
 
         Check whether to launch a default action instead of action sheet
index 9265064..c876298 100644 (file)
@@ -91,6 +91,8 @@ SOFT_LINK_CONSTANT(UIFoundation, NSConvertedDocumentAttribute, NSString *)
 #define NSConvertedDocumentAttribute getNSConvertedDocumentAttribute()
 SOFT_LINK_CONSTANT(UIFoundation, NSCocoaVersionDocumentAttribute, NSString *)
 #define NSCocoaVersionDocumentAttribute getNSCocoaVersionDocumentAttribute()
+SOFT_LINK_CONSTANT(UIFoundation, NSBackgroundColorDocumentAttribute, NSString *)
+#define NSBackgroundColorDocumentAttribute getNSBackgroundColorDocumentAttribute()
 
 // We don't softlink NSSuperscriptAttributeName because UIFoundation stopped exporting it.
 // This attribute is being deprecated at the API level, but internally UIFoundation
index 2d55a43..aab49c4 100644 (file)
@@ -35,12 +35,12 @@ class VisibleSelection;
 
 enum class IncludeImagesInAttributedString { Yes, No };
 
-NSAttributedString *attributedStringFromSelection(const VisibleSelection&);
+NSAttributedString *attributedStringFromSelection(const VisibleSelection&, NSDictionary** documentAttributes = nullptr);
 
 // For testing purpose only
-WEBCORE_EXPORT NSAttributedString *attributedStringBetweenStartAndEnd(const Position&, const Position&);
+WEBCORE_EXPORT NSAttributedString *attributedStringBetweenStartAndEnd(const Position&, const Position&, NSDictionary** documentAttributes = nullptr);
 
-WEBCORE_EXPORT NSAttributedString *attributedStringFromRange(Range&);
+WEBCORE_EXPORT NSAttributedString *attributedStringFromRange(Range&, NSDictionary** documentAttributes = nullptr);
 #if !PLATFORM(IOS_FAMILY)
 WEBCORE_EXPORT NSAttributedString *editingAttributedStringFromRange(Range&, IncludeImagesInAttributedString = IncludeImagesInAttributedString::Yes);
 #endif
index ead5dca..e94d978 100644 (file)
@@ -276,9 +276,9 @@ class HTMLConverter {
 public:
     HTMLConverter(const Position&, const Position&);
     ~HTMLConverter();
-    
-    NSAttributedString* convert();
-    
+
+    NSAttributedString* convert(NSDictionary** documentAttributes = nullptr);
+
 private:
     Position m_start;
     Position m_end;
@@ -396,7 +396,7 @@ HTMLConverter::~HTMLConverter()
     [_writingDirectionArray release];
 }
 
-NSAttributedString *HTMLConverter::convert()
+NSAttributedString *HTMLConverter::convert(NSDictionary** documentAttributes)
 {
     if (comparePositions(m_start, m_end) > 0)
         return nil;
@@ -408,11 +408,20 @@ NSAttributedString *HTMLConverter::convert()
     if (!m_dataSource)
         return nil;
 
+    Document& document = commonAncestorContainer->document();
+    if (auto* body = document.bodyOrFrameset()) {
+        if (PlatformColor *backgroundColor = _colorForElement(*body, CSSPropertyBackgroundColor))
+            [_documentAttrs setObject:backgroundColor forKey:NSBackgroundColorDocumentAttribute];
+    }
+
     _domRangeStartIndex = 0;
     _traverseNode(*commonAncestorContainer, 0, false /* embedded */);
     if (_domRangeStartIndex > 0 && _domRangeStartIndex <= [_attrStr length])
         [_attrStr deleteCharactersInRange:NSMakeRange(0, _domRangeStartIndex)];
 
+    if (documentAttributes)
+        *documentAttributes = [[_documentAttrs retain] autorelease];
+
     return [[_attrStr retain] autorelease];
 }
 
@@ -2375,23 +2384,23 @@ static RetainPtr<NSFileWrapper> fileWrapperForElement(HTMLImageElement& element)
 #endif
 
 namespace WebCore {
-    
+
 // This function supports more HTML features than the editing variant below, such as tables.
-NSAttributedString *attributedStringFromRange(Range& range)
+NSAttributedString *attributedStringFromRange(Range& range, NSDictionary** documentAttributes)
 {
-    return HTMLConverter { range.startPosition(), range.endPosition() }.convert();
+    return HTMLConverter { range.startPosition(), range.endPosition() }.convert(documentAttributes);
 }
 
-NSAttributedString *attributedStringFromSelection(const VisibleSelection& selection)
+NSAttributedString *attributedStringFromSelection(const VisibleSelection& selection, NSDictionary** documentAttributes)
 {
-    return attributedStringBetweenStartAndEnd(selection.start(), selection.end());
+    return attributedStringBetweenStartAndEnd(selection.start(), selection.end(), documentAttributes);
 }
 
-NSAttributedString *attributedStringBetweenStartAndEnd(const Position& start, const Position& end)
+NSAttributedString *attributedStringBetweenStartAndEnd(const Position& start, const Position& end, NSDictionary** documentAttributes)
 {
-    return HTMLConverter { start, end }.convert();
+    return HTMLConverter { start, end }.convert(documentAttributes);
 }
-    
+
 #if !PLATFORM(IOS_FAMILY)
 
 // This function uses TextIterator, which makes offsets in its result compatible with HTML editing.