Refactor EditingStyle::textDirection to return an Optional<WritingDirection> instead...
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 15 Feb 2019 16:52:33 +0000 (16:52 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 15 Feb 2019 16:52:33 +0000 (16:52 +0000)
https://bugs.webkit.org/show_bug.cgi?id=194686

Reviewed by Ryosuke Niwa.

Changes EditingStyle::textDirection to return an Optional<WritingDirection>, instead of taking a reference to
the resulting WritingDirection. No change in behavior.

* editing/ApplyStyleCommand.cpp:
(WebCore::ApplyStyleCommand::splitAncestorsWithUnicodeBidi):
(WebCore::ApplyStyleCommand::applyInlineStyle):
* editing/EditingStyle.cpp:
(WebCore::EditingStyle::textDirection const):
(WebCore::EditingStyle::textDirectionForSelection):
* editing/EditingStyle.h:

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

Source/WebCore/ChangeLog
Source/WebCore/editing/ApplyStyleCommand.cpp
Source/WebCore/editing/EditingStyle.cpp
Source/WebCore/editing/EditingStyle.h

index 991f60b..96ab5ca 100644 (file)
@@ -1,3 +1,21 @@
+2019-02-15  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Refactor EditingStyle::textDirection to return an Optional<WritingDirection> instead of a bool
+        https://bugs.webkit.org/show_bug.cgi?id=194686
+
+        Reviewed by Ryosuke Niwa.
+
+        Changes EditingStyle::textDirection to return an Optional<WritingDirection>, instead of taking a reference to
+        the resulting WritingDirection. No change in behavior.
+
+        * editing/ApplyStyleCommand.cpp:
+        (WebCore::ApplyStyleCommand::splitAncestorsWithUnicodeBidi):
+        (WebCore::ApplyStyleCommand::applyInlineStyle):
+        * editing/EditingStyle.cpp:
+        (WebCore::EditingStyle::textDirection const):
+        (WebCore::EditingStyle::textDirectionForSelection):
+        * editing/EditingStyle.h:
+
 2019-02-10  Darin Adler  <darin@apple.com>
 
         Replace more uses of String::format with StringConcatenate (mostly non-Apple platform-specific cases)
index 7a3a160..49163d7 100644 (file)
@@ -485,17 +485,15 @@ HTMLElement* ApplyStyleCommand::splitAncestorsWithUnicodeBidi(Node* node, bool b
 
     HTMLElement* unsplitAncestor = nullptr;
 
-    WritingDirection highestAncestorDirection;
-    if (allowedDirection != WritingDirection::Natural
-        && highestAncestorUnicodeBidi != CSSValueBidiOverride
-        && is<HTMLElement>(*highestAncestorWithUnicodeBidi)
-        && EditingStyle::create(highestAncestorWithUnicodeBidi, EditingStyle::AllProperties)->textDirection(highestAncestorDirection)
-        && highestAncestorDirection == allowedDirection) {
-        if (!nextHighestAncestorWithUnicodeBidi)
-            return downcast<HTMLElement>(highestAncestorWithUnicodeBidi);
-
-        unsplitAncestor = downcast<HTMLElement>(highestAncestorWithUnicodeBidi);
-        highestAncestorWithUnicodeBidi = nextHighestAncestorWithUnicodeBidi;
+    if (allowedDirection != WritingDirection::Natural && highestAncestorUnicodeBidi != CSSValueBidiOverride && is<HTMLElement>(*highestAncestorWithUnicodeBidi)) {
+        auto highestAncestorDirection = EditingStyle::create(highestAncestorWithUnicodeBidi, EditingStyle::AllProperties)->textDirection();
+        if (highestAncestorDirection && *highestAncestorDirection == allowedDirection) {
+            if (!nextHighestAncestorWithUnicodeBidi)
+                return downcast<HTMLElement>(highestAncestorWithUnicodeBidi);
+
+            unsplitAncestor = downcast<HTMLElement>(highestAncestorWithUnicodeBidi);
+            highestAncestorWithUnicodeBidi = nextHighestAncestorWithUnicodeBidi;
+        }
     }
 
     // Split every ancestor through highest ancestor with embedding.
@@ -616,14 +614,13 @@ void ApplyStyleCommand::applyInlineStyle(EditingStyle& style)
     // and prevent us from adding redundant ones, as described in:
     // <rdar://problem/3724344> Bolding and unbolding creates extraneous tags
     Position removeStart = start.upstream();
-    WritingDirection textDirection = WritingDirection::Natural;
-    bool hasTextDirection = style.textDirection(textDirection);
+    auto textDirection = style.textDirection();
     RefPtr<EditingStyle> styleWithoutEmbedding;
     RefPtr<EditingStyle> embeddingStyle;
-    if (hasTextDirection) {
+    if (textDirection.hasValue()) {
         // Leave alone an ancestor that provides the desired single level embedding, if there is one.
-        HTMLElement* startUnsplitAncestor = splitAncestorsWithUnicodeBidi(start.deprecatedNode(), true, textDirection);
-        HTMLElement* endUnsplitAncestor = splitAncestorsWithUnicodeBidi(end.deprecatedNode(), false, textDirection);
+        auto* startUnsplitAncestor = splitAncestorsWithUnicodeBidi(start.deprecatedNode(), true, *textDirection);
+        auto* endUnsplitAncestor = splitAncestorsWithUnicodeBidi(end.deprecatedNode(), false, *textDirection);
         removeEmbeddingUpToEnclosingBlock(start.deprecatedNode(), startUnsplitAncestor);
         removeEmbeddingUpToEnclosingBlock(end.deprecatedNode(), endUnsplitAncestor);
 
@@ -671,7 +668,7 @@ void ApplyStyleCommand::applyInlineStyle(EditingStyle& style)
     document().updateLayoutIgnorePendingStylesheets();
 
     RefPtr<EditingStyle> styleToApply = &style;
-    if (hasTextDirection) {
+    if (textDirection.hasValue()) {
         // Avoid applying the unicode-bidi and direction properties beneath ancestors that already have them.
         Node* embeddingStartNode = highestEmbeddingAncestor(start.deprecatedNode(), enclosingBlock(start.deprecatedNode()));
         Node* embeddingEndNode = highestEmbeddingAncestor(end.deprecatedNode(), enclosingBlock(end.deprecatedNode()));
index 6bbf014..8a45ba6 100644 (file)
@@ -553,32 +553,28 @@ Ref<MutableStyleProperties> EditingStyle::styleWithResolvedTextDecorations() con
     return style;
 }
 
-bool EditingStyle::textDirection(WritingDirection& writingDirection) const
+Optional<WritingDirection> EditingStyle::textDirection() const
 {
     if (!m_mutableStyle)
-        return false;
+        return WTF::nullopt;
 
     RefPtr<CSSValue> unicodeBidi = m_mutableStyle->getPropertyCSSValue(CSSPropertyUnicodeBidi);
     if (!is<CSSPrimitiveValue>(unicodeBidi))
-        return false;
+        return WTF::nullopt;
 
     CSSValueID unicodeBidiValue = downcast<CSSPrimitiveValue>(*unicodeBidi).valueID();
     if (unicodeBidiValue == CSSValueEmbed) {
         RefPtr<CSSValue> direction = m_mutableStyle->getPropertyCSSValue(CSSPropertyDirection);
         if (!is<CSSPrimitiveValue>(direction))
-            return false;
+            return WTF::nullopt;
 
-        writingDirection = downcast<CSSPrimitiveValue>(*direction).valueID() == CSSValueLtr ? WritingDirection::LeftToRight : WritingDirection::RightToLeft;
-
-        return true;
+        return downcast<CSSPrimitiveValue>(*direction).valueID() == CSSValueLtr ? WritingDirection::LeftToRight : WritingDirection::RightToLeft;
     }
 
-    if (unicodeBidiValue == CSSValueNormal) {
-        writingDirection = WritingDirection::Natural;
-        return true;
-    }
+    if (unicodeBidiValue == CSSValueNormal)
+        return WritingDirection::Natural;
 
-    return false;
+    return WTF::nullopt;
 }
 
 void EditingStyle::setStyle(RefPtr<MutableStyleProperties>&& style)
@@ -1497,10 +1493,11 @@ WritingDirection EditingStyle::textDirectionForSelection(const VisibleSelection&
     }
 
     if (selection.isCaret()) {
-        WritingDirection direction;
-        if (typingStyle && typingStyle->textDirection(direction)) {
-            hasNestedOrMultipleEmbeddings = false;
-            return direction;
+        if (typingStyle) {
+            if (auto direction = typingStyle->textDirection()) {
+                hasNestedOrMultipleEmbeddings = false;
+                return *direction;
+            }
         }
         node = selection.visibleStart().deepEquivalent().deprecatedNode();
     }
index b7991ef..fd08033 100644 (file)
@@ -36,6 +36,7 @@
 #include "StyleProperties.h"
 #include "WritingDirection.h"
 #include <wtf/Forward.h>
+#include <wtf/Optional.h>
 #include <wtf/RefCounted.h>
 #include <wtf/RefPtr.h>
 #include <wtf/TriState.h>
@@ -110,7 +111,7 @@ public:
 
     MutableStyleProperties* style() { return m_mutableStyle.get(); }
     Ref<MutableStyleProperties> styleWithResolvedTextDecorations() const;
-    bool textDirection(WritingDirection&) const;
+    Optional<WritingDirection> textDirection() const;
     bool isEmpty() const;
     void setStyle(RefPtr<MutableStyleProperties>&&);
     void overrideWithStyle(const StyleProperties&);