- Revision
- 210779
- Author
- timothy_hor...@apple.com
- Date
- 2017-01-15 11:39:39 -0800 (Sun, 15 Jan 2017)
Log Message
De-duplicate more (nearly) identical code in Editor(Mac|IOS).mm
https://bugs.webkit.org/show_bug.cgi?id=167063
Reviewed by Dan Bernstein.
Source/WebCore:
No new tests, just refactoring.
* editing/Editor.h:
Adjust to fontAttributesForSelectionStart returning a RetainPtr.
* editing/cocoa/EditorCocoa.mm:
(WebCore::Editor::getTextDecorationAttributesRespectingTypingStyle):
Make use of more Obj-C literals (for NSNumber).
(WebCore::Editor::fontAttributesForSelectionStart):
Merge this from EditorMac and EditorIOS. There are a number of attributes
that are currently only extracted on Mac, and it's not clear why (and
probably should be shared).
(WebCore::Editor::stringSelectionForPasteboard):
(WebCore::Editor::stringSelectionForPasteboardWithImageAltText):
Merge these functions from EditorMac and EditorIOS. The iOS implementation
was missing a reasonable bug fix from r161925.
(WebCore::Editor::createFragmentAndAddResources):
Merge this from EditorMac and EditorIOS. The Mac implementation was missing
a reasonable bug fix from r203482.
* editing/ios/EditorIOS.mm:
(WebCore::Editor::fontAttributesForSelectionStart): Deleted.
(WebCore::Editor::stringSelectionForPasteboardWithImageAltText): Deleted.
(WebCore::Editor::createFragmentAndAddResources): Deleted.
* editing/mac/EditorMac.mm:
(WebCore::Editor::fontAttributesForSelectionStart): Deleted.
(WebCore::Editor::stringSelectionForPasteboard): Deleted.
(WebCore::Editor::stringSelectionForPasteboardWithImageAltText): Deleted.
(WebCore::Editor::createFragmentAndAddResources): Deleted.
Source/WebKit/mac:
* WebView/WebHTMLView.mm:
(-[WebHTMLView _selectionStartFontAttributesAsRTF]):
(-[WebHTMLView _updateFontPanel]):
* WebView/WebView.mm:
(-[WebView typingAttributes]):
Adjust to fontAttributesForSelectionStart returning a RetainPtr.
Modified Paths
Diff
Modified: trunk/Source/WebCore/ChangeLog (210778 => 210779)
--- trunk/Source/WebCore/ChangeLog 2017-01-15 17:47:00 UTC (rev 210778)
+++ trunk/Source/WebCore/ChangeLog 2017-01-15 19:39:39 UTC (rev 210779)
@@ -1,3 +1,43 @@
+2017-01-15 Tim Horton <timothy_hor...@apple.com>
+
+ De-duplicate more (nearly) identical code in Editor(Mac|IOS).mm
+ https://bugs.webkit.org/show_bug.cgi?id=167063
+
+ Reviewed by Dan Bernstein.
+
+ No new tests, just refactoring.
+
+ * editing/Editor.h:
+ Adjust to fontAttributesForSelectionStart returning a RetainPtr.
+
+ * editing/cocoa/EditorCocoa.mm:
+ (WebCore::Editor::getTextDecorationAttributesRespectingTypingStyle):
+ Make use of more Obj-C literals (for NSNumber).
+
+ (WebCore::Editor::fontAttributesForSelectionStart):
+ Merge this from EditorMac and EditorIOS. There are a number of attributes
+ that are currently only extracted on Mac, and it's not clear why (and
+ probably should be shared).
+
+ (WebCore::Editor::stringSelectionForPasteboard):
+ (WebCore::Editor::stringSelectionForPasteboardWithImageAltText):
+ Merge these functions from EditorMac and EditorIOS. The iOS implementation
+ was missing a reasonable bug fix from r161925.
+
+ (WebCore::Editor::createFragmentAndAddResources):
+ Merge this from EditorMac and EditorIOS. The Mac implementation was missing
+ a reasonable bug fix from r203482.
+
+ * editing/ios/EditorIOS.mm:
+ (WebCore::Editor::fontAttributesForSelectionStart): Deleted.
+ (WebCore::Editor::stringSelectionForPasteboardWithImageAltText): Deleted.
+ (WebCore::Editor::createFragmentAndAddResources): Deleted.
+ * editing/mac/EditorMac.mm:
+ (WebCore::Editor::fontAttributesForSelectionStart): Deleted.
+ (WebCore::Editor::stringSelectionForPasteboard): Deleted.
+ (WebCore::Editor::stringSelectionForPasteboardWithImageAltText): Deleted.
+ (WebCore::Editor::createFragmentAndAddResources): Deleted.
+
2017-01-15 Myles C. Maxfield <mmaxfi...@apple.com>
[Cocoa] Unify FontPlatformData's hashing and equality operators
Modified: trunk/Source/WebCore/editing/Editor.h (210778 => 210779)
--- trunk/Source/WebCore/editing/Editor.h 2017-01-15 17:47:00 UTC (rev 210778)
+++ trunk/Source/WebCore/editing/Editor.h 2017-01-15 19:39:39 UTC (rev 210779)
@@ -448,7 +448,7 @@
#if PLATFORM(COCOA)
void getTextDecorationAttributesRespectingTypingStyle(const RenderStyle&, NSMutableDictionary*) const;
- WEBCORE_EXPORT NSDictionary *fontAttributesForSelectionStart() const;
+ WEBCORE_EXPORT RetainPtr<NSDictionary> fontAttributesForSelectionStart() const;
WEBCORE_EXPORT String stringSelectionForPasteboard();
String stringSelectionForPasteboardWithImageAltText();
#if !PLATFORM(IOS)
Modified: trunk/Source/WebCore/editing/cocoa/EditorCocoa.mm (210778 => 210779)
--- trunk/Source/WebCore/editing/cocoa/EditorCocoa.mm 2017-01-15 17:47:00 UTC (rev 210778)
+++ trunk/Source/WebCore/editing/cocoa/EditorCocoa.mm 2017-01-15 19:39:39 UTC (rev 210779)
@@ -29,6 +29,8 @@
#import "ArchiveResource.h"
#import "CSSValueList.h"
#import "CSSValuePool.h"
+#import "CachedResourceLoader.h"
+#import "ColorMac.h"
#import "DocumentFragment.h"
#import "DocumentLoader.h"
#import "EditingStyle.h"
@@ -68,19 +70,78 @@
if (value && value->isValueList()) {
CSSValueList& valueList = downcast<CSSValueList>(*value);
if (valueList.hasValue(CSSValuePool::singleton().createIdentifierValue(CSSValueLineThrough).ptr()))
- [result setObject:[NSNumber numberWithInt:NSUnderlineStyleSingle] forKey:NSStrikethroughStyleAttributeName];
+ [result setObject:@(NSUnderlineStyleSingle) forKey:NSStrikethroughStyleAttributeName];
if (valueList.hasValue(CSSValuePool::singleton().createIdentifierValue(CSSValueUnderline).ptr()))
- [result setObject:[NSNumber numberWithInt:NSUnderlineStyleSingle] forKey:NSUnderlineStyleAttributeName];
+ [result setObject:@(NSUnderlineStyleSingle) forKey:NSUnderlineStyleAttributeName];
}
} else {
int decoration = style.textDecorationsInEffect();
if (decoration & TextDecorationLineThrough)
- [result setObject:[NSNumber numberWithInt:NSUnderlineStyleSingle] forKey:NSStrikethroughStyleAttributeName];
+ [result setObject:@(NSUnderlineStyleSingle) forKey:NSStrikethroughStyleAttributeName];
if (decoration & TextDecorationUnderline)
- [result setObject:[NSNumber numberWithInt:NSUnderlineStyleSingle] forKey:NSUnderlineStyleAttributeName];
+ [result setObject:@(NSUnderlineStyleSingle) forKey:NSUnderlineStyleAttributeName];
}
}
+RetainPtr<NSDictionary> Editor::fontAttributesForSelectionStart() const
+{
+ Node* nodeToRemove;
+ auto* style = styleForSelectionStart(&m_frame, nodeToRemove);
+ if (!style)
+ return nil;
+
+ RetainPtr<NSMutableDictionary> attributes = adoptNS([[NSMutableDictionary alloc] init]);
+
+ if (auto ctFont = style->fontCascade().primaryFont().getCTFont())
+ [attributes setObject:(id)ctFont forKey:NSFontAttributeName];
+
+ // FIXME: Why would we not want to retrieve these attributes on iOS?
+#if PLATFORM(MAC)
+ if (style->visitedDependentColor(CSSPropertyBackgroundColor).isVisible())
+ [attributes setObject:nsColor(style->visitedDependentColor(CSSPropertyBackgroundColor)) forKey:NSBackgroundColorAttributeName];
+
+ if (style->visitedDependentColor(CSSPropertyColor).isValid() && !Color::isBlackColor(style->visitedDependentColor(CSSPropertyColor)))
+ [attributes setObject:nsColor(style->visitedDependentColor(CSSPropertyColor)) forKey:NSForegroundColorAttributeName];
+
+ const ShadowData* shadowData = style->textShadow();
+ if (shadowData) {
+ RetainPtr<NSShadow> platformShadow = adoptNS([[NSShadow alloc] init]);
+ [platformShadow setShadowOffset:NSMakeSize(shadowData->x(), shadowData->y())];
+ [platformShadow setShadowBlurRadius:shadowData->radius()];
+ [platformShadow setShadowColor:nsColor(shadowData->color())];
+ [attributes setObject:platformShadow.get() forKey:NSShadowAttributeName];
+ }
+
+ int superscriptInt = 0;
+ switch (style->verticalAlign()) {
+ case BASELINE:
+ case BOTTOM:
+ case BASELINE_MIDDLE:
+ case LENGTH:
+ case MIDDLE:
+ case TEXT_BOTTOM:
+ case TEXT_TOP:
+ case TOP:
+ break;
+ case SUB:
+ superscriptInt = -1;
+ break;
+ case SUPER:
+ superscriptInt = 1;
+ break;
+ }
+ if (superscriptInt)
+ [attributes setObject:@(superscriptInt) forKey:NSSuperscriptAttributeName];
+#endif
+
+ getTextDecorationAttributesRespectingTypingStyle(*style, attributes.get());
+
+ if (nodeToRemove)
+ nodeToRemove->remove();
+
+ return attributes;
+}
+
FragmentAndResources Editor::createFragment(NSAttributedString *string)
{
// FIXME: The algorithm to convert an attributed string into HTML should be implemented here in WebCore.
@@ -117,6 +178,26 @@
return SharedBuffer::wrapCFData(archive->rawDataRepresentation().get());
}
+// FIXME: Makes no sense that selectedTextForDataTransfer always includes alt text, but stringSelectionForPasteboard does not.
+// This was left in a bad state when selectedTextForDataTransfer was added. Need to look over clients and fix this.
+String Editor::stringSelectionForPasteboard()
+{
+ if (!canCopy())
+ return emptyString();
+ String text = selectedText();
+ text.replace(noBreakSpace, ' ');
+ return text;
+}
+
+String Editor::stringSelectionForPasteboardWithImageAltText()
+{
+ if (!canCopy())
+ return emptyString();
+ String text = selectedTextForDataTransfer();
+ text.replace(noBreakSpace, ' ');
+ return text;
+}
+
void Editor::replaceSelectionWithAttributedString(NSAttributedString *attributedString, MailBlockquoteHandling mailBlockquoteHandling)
{
if (m_frame.selection().isNone())
@@ -184,5 +265,39 @@
return nullptr;
}
+RefPtr<DocumentFragment> Editor::createFragmentAndAddResources(NSAttributedString *string)
+{
+ if (!m_frame.page() || !m_frame.document())
+ return nullptr;
+ auto& document = *m_frame.document();
+ if (!document.isHTMLDocument() || !string)
+ return nullptr;
+
+ bool wasDeferringCallbacks = m_frame.page()->defersLoading();
+ if (!wasDeferringCallbacks)
+ m_frame.page()->setDefersLoading(true);
+
+ auto& cachedResourceLoader = document.cachedResourceLoader();
+ bool wasImagesEnabled = cachedResourceLoader.imagesEnabled();
+ if (wasImagesEnabled)
+ cachedResourceLoader.setImagesEnabled(false);
+
+ auto fragmentAndResources = createFragment(string);
+
+ if (DocumentLoader* loader = m_frame.loader().documentLoader()) {
+ for (auto& resource : fragmentAndResources.resources) {
+ if (resource)
+ loader->addArchiveResource(resource.releaseNonNull());
+ }
+ }
+
+ if (wasImagesEnabled)
+ cachedResourceLoader.setImagesEnabled(true);
+ if (!wasDeferringCallbacks)
+ m_frame.page()->setDefersLoading(false);
+
+ return WTFMove(fragmentAndResources.fragment);
}
+
+}
Modified: trunk/Source/WebCore/editing/ios/EditorIOS.mm (210778 => 210779)
--- trunk/Source/WebCore/editing/ios/EditorIOS.mm 2017-01-15 17:47:00 UTC (rev 210778)
+++ trunk/Source/WebCore/editing/ios/EditorIOS.mm 2017-01-15 19:39:39 UTC (rev 210779)
@@ -156,27 +156,6 @@
applyParagraphStyle(style.get());
}
-NSDictionary* Editor::fontAttributesForSelectionStart() const
-{
- Node* nodeToRemove;
- auto* style = styleForSelectionStart(&m_frame, nodeToRemove);
- if (!style)
- return nil;
-
- NSMutableDictionary* result = [NSMutableDictionary dictionary];
-
- CTFontRef font = style->fontCascade().primaryFont().getCTFont();
- if (font)
- [result setObject:(id)font forKey:NSFontAttributeName];
-
- getTextDecorationAttributesRespectingTypingStyle(*style, result);
-
- if (nodeToRemove)
- nodeToRemove->remove();
-
- return result;
-}
-
void Editor::removeUnchangeableStyles()
{
// This function removes styles that the user cannot modify by applying their default values.
@@ -201,13 +180,6 @@
applyStyleToSelection(defaultStyle.get(), EditActionChangeAttributes);
}
-String Editor::stringSelectionForPasteboardWithImageAltText()
-{
- String text = selectedTextForDataTransfer();
- text.replace(noBreakSpace, ' ');
- return text;
-}
-
static void getImage(Element& imageElement, RefPtr<Image>& image, CachedImage*& cachedImage)
{
auto* renderer = imageElement.renderer();
@@ -434,41 +406,6 @@
pasteAsFragment(fragment.releaseNonNull(), canSmartReplaceWithPasteboard(*pasteboard), false, mailBlockquoteHandling);
}
-RefPtr<DocumentFragment> Editor::createFragmentAndAddResources(NSAttributedString *string)
-{
- if (!m_frame.page() || !m_frame.document())
- return nullptr;
-
- auto& document = *m_frame.document();
- if (!document.isHTMLDocument() || !string)
- return nullptr;
-
- bool wasDeferringCallbacks = m_frame.page()->defersLoading();
- if (!wasDeferringCallbacks)
- m_frame.page()->setDefersLoading(true);
-
- auto& cachedResourceLoader = document.cachedResourceLoader();
- bool wasImagesEnabled = cachedResourceLoader.imagesEnabled();
- if (wasImagesEnabled)
- cachedResourceLoader.setImagesEnabled(false);
-
- auto fragmentAndResources = createFragment(string);
-
- if (DocumentLoader* loader = m_frame.loader().documentLoader()) {
- for (auto& resource : fragmentAndResources.resources) {
- if (resource)
- loader->addArchiveResource(resource.releaseNonNull());
- }
- }
-
- if (wasImagesEnabled)
- cachedResourceLoader.setImagesEnabled(true);
- if (!wasDeferringCallbacks)
- m_frame.page()->setDefersLoading(false);
-
- return WTFMove(fragmentAndResources.fragment);
-}
-
void Editor::insertDictationPhrases(Vector<Vector<String>>&& dictationPhrases, RetainPtr<id> metadata)
{
if (m_frame.selection().isNone())
Modified: trunk/Source/WebCore/editing/mac/EditorMac.mm (210778 => 210779)
--- trunk/Source/WebCore/editing/mac/EditorMac.mm 2017-01-15 17:47:00 UTC (rev 210778)
+++ trunk/Source/WebCore/editing/mac/EditorMac.mm 2017-01-15 19:39:39 UTC (rev 210779)
@@ -29,8 +29,6 @@
#import "Blob.h"
#import "CSSPrimitiveValueMappings.h"
#import "CSSValuePool.h"
-#import "CachedResourceLoader.h"
-#import "ColorMac.h"
#import "DOMURL.h"
#import "DataTransfer.h"
#import "DocumentFragment.h"
@@ -104,62 +102,6 @@
client()->setInsertionPasteboard(String());
}
-NSDictionary* Editor::fontAttributesForSelectionStart() const
-{
- Node* nodeToRemove;
- auto* style = styleForSelectionStart(&m_frame, nodeToRemove);
- if (!style)
- return nil;
-
- NSMutableDictionary* result = [NSMutableDictionary dictionary];
-
- if (style->visitedDependentColor(CSSPropertyBackgroundColor).isVisible())
- [result setObject:nsColor(style->visitedDependentColor(CSSPropertyBackgroundColor)) forKey:NSBackgroundColorAttributeName];
-
- if (auto ctFont = style->fontCascade().primaryFont().getCTFont())
- [result setObject:toNSFont(ctFont) forKey:NSFontAttributeName];
-
- if (style->visitedDependentColor(CSSPropertyColor).isValid() && !Color::isBlackColor(style->visitedDependentColor(CSSPropertyColor)))
- [result setObject:nsColor(style->visitedDependentColor(CSSPropertyColor)) forKey:NSForegroundColorAttributeName];
-
- const ShadowData* shadow = style->textShadow();
- if (shadow) {
- RetainPtr<NSShadow> s = adoptNS([[NSShadow alloc] init]);
- [s.get() setShadowOffset:NSMakeSize(shadow->x(), shadow->y())];
- [s.get() setShadowBlurRadius:shadow->radius()];
- [s.get() setShadowColor:nsColor(shadow->color())];
- [result setObject:s.get() forKey:NSShadowAttributeName];
- }
-
- int superscriptInt = 0;
- switch (style->verticalAlign()) {
- case BASELINE:
- case BOTTOM:
- case BASELINE_MIDDLE:
- case LENGTH:
- case MIDDLE:
- case TEXT_BOTTOM:
- case TEXT_TOP:
- case TOP:
- break;
- case SUB:
- superscriptInt = -1;
- break;
- case SUPER:
- superscriptInt = 1;
- break;
- }
- if (superscriptInt)
- [result setObject:[NSNumber numberWithInt:superscriptInt] forKey:NSSuperscriptAttributeName];
-
- getTextDecorationAttributesRespectingTypingStyle(*style, result);
-
- if (nodeToRemove)
- nodeToRemove->remove();
-
- return result;
-}
-
bool Editor::canCopyExcludingStandaloneImages()
{
const VisibleSelection& selection = m_frame.selection().selection();
@@ -243,26 +185,6 @@
client()->setInsertionPasteboard(String());
}
-// FIXME: Makes no sense that selectedTextForDataTransfer always includes alt text, but stringSelectionForPasteboard does not.
-// This was left in a bad state when selectedTextForDataTransfer was added. Need to look over clients and fix this.
-String Editor::stringSelectionForPasteboard()
-{
- if (!canCopy())
- return emptyString();
- String text = selectedText();
- text.replace(noBreakSpace, ' ');
- return text;
-}
-
-String Editor::stringSelectionForPasteboardWithImageAltText()
-{
- if (!canCopy())
- return emptyString();
- String text = selectedTextForDataTransfer();
- text.replace(noBreakSpace, ' ');
- return text;
-}
-
String Editor::selectionInHTMLFormat()
{
return createMarkup(*selectedRange(), nullptr, AnnotateForInterchange, false, ResolveNonLocalURLs);
@@ -566,33 +488,6 @@
return fragment;
}
-RefPtr<DocumentFragment> Editor::createFragmentAndAddResources(NSAttributedString *string)
-{
- if (!m_frame.page() || !document().isHTMLDocument())
- return nullptr;
-
- if (!string)
- return nullptr;
-
- bool wasDeferringCallbacks = m_frame.page()->defersLoading();
- if (!wasDeferringCallbacks)
- m_frame.page()->setDefersLoading(true);
-
- auto fragmentAndResources = createFragment(string);
-
- if (DocumentLoader* loader = m_frame.loader().documentLoader()) {
- for (auto& resource : fragmentAndResources.resources) {
- if (resource)
- loader->addArchiveResource(resource.releaseNonNull());
- }
- }
-
- if (!wasDeferringCallbacks)
- m_frame.page()->setDefersLoading(false);
-
- return WTFMove(fragmentAndResources.fragment);
-}
-
void Editor::applyFontStyles(const String& fontFamily, double fontSize, unsigned fontTraits)
{
auto& cssValuePool = CSSValuePool::singleton();
Modified: trunk/Source/WebKit/mac/ChangeLog (210778 => 210779)
--- trunk/Source/WebKit/mac/ChangeLog 2017-01-15 17:47:00 UTC (rev 210778)
+++ trunk/Source/WebKit/mac/ChangeLog 2017-01-15 19:39:39 UTC (rev 210779)
@@ -1,3 +1,17 @@
+2017-01-15 Tim Horton <timothy_hor...@apple.com>
+
+ De-duplicate more (nearly) identical code in Editor(Mac|IOS).mm
+ https://bugs.webkit.org/show_bug.cgi?id=167063
+
+ Reviewed by Dan Bernstein.
+
+ * WebView/WebHTMLView.mm:
+ (-[WebHTMLView _selectionStartFontAttributesAsRTF]):
+ (-[WebHTMLView _updateFontPanel]):
+ * WebView/WebView.mm:
+ (-[WebView typingAttributes]):
+ Adjust to fontAttributesForSelectionStart returning a RetainPtr.
+
2017-01-13 Joseph Pecoraro <pecor...@apple.com>
Remove ENABLE(DETAILS_ELEMENT) guards
Modified: trunk/Source/WebKit/mac/WebView/WebHTMLView.mm (210778 => 210779)
--- trunk/Source/WebKit/mac/WebView/WebHTMLView.mm 2017-01-15 17:47:00 UTC (rev 210778)
+++ trunk/Source/WebKit/mac/WebView/WebHTMLView.mm 2017-01-15 19:39:39 UTC (rev 210779)
@@ -5335,7 +5335,7 @@
{
Frame* coreFrame = core([self _frame]);
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"x"
- attributes:coreFrame ? coreFrame->editor().fontAttributesForSelectionStart() : nil];
+ attributes:coreFrame ? coreFrame->editor().fontAttributesForSelectionStart().get() : nil];
NSData *data = "" RTFFromRange:NSMakeRange(0, [string length]) documentAttributes:@{ }];
[string release];
return data;
@@ -6150,7 +6150,7 @@
bool multipleFonts = false;
NSFont *font = nil;
- NSDictionary *attributes = nil;
+ RetainPtr<NSDictionary> attributes;
if (Frame* coreFrame = core([self _frame])) {
if (const Font* fd = coreFrame->editor().fontForSelection(multipleFonts))
font = (NSFont *)fd->platformData().registeredFont();
@@ -6165,7 +6165,7 @@
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[fontManager setSelectedFont:font isMultiple:multipleFonts];
- [fontManager setSelectedAttributes:(attributes ? attributes : @{ }) isMultiple:multipleFonts];
+ [fontManager setSelectedAttributes:(attributes ? attributes.get() : @{ }) isMultiple:multipleFonts];
}
- (void)_setSoftSpaceRange:(NSRange)range
Modified: trunk/Source/WebKit/mac/WebView/WebView.mm (210778 => 210779)
--- trunk/Source/WebKit/mac/WebView/WebView.mm 2017-01-15 17:47:00 UTC (rev 210778)
+++ trunk/Source/WebKit/mac/WebView/WebView.mm 2017-01-15 19:39:39 UTC (rev 210779)
@@ -8282,12 +8282,11 @@
{
Frame* coreFrame = core([self _selectedOrMainFrame]);
if (coreFrame)
- return coreFrame->editor().fontAttributesForSelectionStart();
+ return coreFrame->editor().fontAttributesForSelectionStart().autorelease();
return nil;
}
-
@end
@implementation WebView (WebViewEditingInMail)