Commit 2dd2875d authored by yosin's avatar yosin Committed by Commit bot

Move textDirectionForSelection in EditingStyleUtilities class to EditorCommand.cpp

This patch moves |textDirectionForSelection()| to |EditingStyleUtilities| class
to "EditorCommand.cpp" since it is used only in "EditorCommand.cpp" for
improving code health.

BUG=657237
TEST=n/a; no behavior changes

Review-Url: https://codereview.chromium.org/2734043004
Cr-Commit-Position: refs/heads/master@{#455712}
parent b4fed54d
...@@ -157,117 +157,6 @@ EditingStyle* EditingStyleUtilities::createStyleAtSelectionStart( ...@@ -157,117 +157,6 @@ EditingStyle* EditingStyleUtilities::createStyleAtSelectionStart(
return style; return style;
} }
static bool isUnicodeBidiNestedOrMultipleEmbeddings(CSSValueID valueID) {
return valueID == CSSValueEmbed || valueID == CSSValueBidiOverride ||
valueID == CSSValueWebkitIsolate ||
valueID == CSSValueWebkitIsolateOverride ||
valueID == CSSValueWebkitPlaintext || valueID == CSSValueIsolate ||
valueID == CSSValueIsolateOverride || valueID == CSSValuePlaintext;
}
WritingDirection EditingStyleUtilities::textDirectionForSelection(
const VisibleSelection& selection,
EditingStyle* typingStyle,
bool& hasNestedOrMultipleEmbeddings) {
hasNestedOrMultipleEmbeddings = true;
if (selection.isNone())
return NaturalWritingDirection;
Position position = mostForwardCaretPosition(selection.start());
Node* node = position.anchorNode();
if (!node)
return NaturalWritingDirection;
Position end;
if (selection.isRange()) {
end = mostBackwardCaretPosition(selection.end());
DCHECK(end.document());
const EphemeralRange caretRange(position.parentAnchoredEquivalent(),
end.parentAnchoredEquivalent());
for (Node& n : caretRange.nodes()) {
if (!n.isStyledElement())
continue;
CSSComputedStyleDeclaration* style =
CSSComputedStyleDeclaration::create(&n);
const CSSValue* unicodeBidi =
style->getPropertyCSSValue(CSSPropertyUnicodeBidi);
if (!unicodeBidi || !unicodeBidi->isIdentifierValue())
continue;
CSSValueID unicodeBidiValue =
toCSSIdentifierValue(unicodeBidi)->getValueID();
if (isUnicodeBidiNestedOrMultipleEmbeddings(unicodeBidiValue))
return NaturalWritingDirection;
}
}
if (selection.isCaret()) {
WritingDirection direction;
if (typingStyle && typingStyle->textDirection(direction)) {
hasNestedOrMultipleEmbeddings = false;
return direction;
}
node = selection.visibleStart().deepEquivalent().anchorNode();
}
DCHECK(node);
// The selection is either a caret with no typing attributes or a range in
// which no embedding is added, so just use the start position to decide.
Node* block = enclosingBlock(node);
WritingDirection foundDirection = NaturalWritingDirection;
for (Node& runner : NodeTraversal::inclusiveAncestorsOf(*node)) {
if (runner == block)
break;
if (!runner.isStyledElement())
continue;
Element* element = &toElement(runner);
CSSComputedStyleDeclaration* style =
CSSComputedStyleDeclaration::create(element);
const CSSValue* unicodeBidi =
style->getPropertyCSSValue(CSSPropertyUnicodeBidi);
if (!unicodeBidi || !unicodeBidi->isIdentifierValue())
continue;
CSSValueID unicodeBidiValue =
toCSSIdentifierValue(unicodeBidi)->getValueID();
if (unicodeBidiValue == CSSValueNormal)
continue;
if (unicodeBidiValue == CSSValueBidiOverride)
return NaturalWritingDirection;
DCHECK(isEmbedOrIsolate(unicodeBidiValue)) << unicodeBidiValue;
const CSSValue* direction =
style->getPropertyCSSValue(CSSPropertyDirection);
if (!direction || !direction->isIdentifierValue())
continue;
int directionValue = toCSSIdentifierValue(direction)->getValueID();
if (directionValue != CSSValueLtr && directionValue != CSSValueRtl)
continue;
if (foundDirection != NaturalWritingDirection)
return NaturalWritingDirection;
// In the range case, make sure that the embedding element persists until
// the end of the range.
if (selection.isRange() && !end.anchorNode()->isDescendantOf(element))
return NaturalWritingDirection;
foundDirection = directionValue == CSSValueLtr
? LeftToRightWritingDirection
: RightToLeftWritingDirection;
}
hasNestedOrMultipleEmbeddings = false;
return foundDirection;
}
bool EditingStyleUtilities::isTransparentColorValue(const CSSValue* cssValue) { bool EditingStyleUtilities::isTransparentColorValue(const CSSValue* cssValue) {
if (!cssValue) if (!cssValue)
return true; return true;
......
...@@ -57,10 +57,6 @@ class EditingStyleUtilities { ...@@ -57,10 +57,6 @@ class EditingStyleUtilities {
const VisibleSelection&, const VisibleSelection&,
bool shouldUseBackgroundColorInEffect = false, bool shouldUseBackgroundColorInEffect = false,
MutableStylePropertySet* styleToCheck = nullptr); MutableStylePropertySet* styleToCheck = nullptr);
static WritingDirection textDirectionForSelection(
const VisibleSelection&,
EditingStyle* typingStyle,
bool& hasNestedOrMultipleEmbeddings);
static bool isEmbedOrIsolate(CSSValueID unicodeBidi) { static bool isEmbedOrIsolate(CSSValueID unicodeBidi) {
return unicodeBidi == CSSValueIsolate || return unicodeBidi == CSSValueIsolate ||
unicodeBidi == CSSValueWebkitIsolate || unicodeBidi == CSSValueEmbed; unicodeBidi == CSSValueWebkitIsolate || unicodeBidi == CSSValueEmbed;
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "core/CSSValueKeywords.h" #include "core/CSSValueKeywords.h"
#include "core/HTMLNames.h" #include "core/HTMLNames.h"
#include "core/clipboard/Pasteboard.h" #include "core/clipboard/Pasteboard.h"
#include "core/css/CSSComputedStyleDeclaration.h"
#include "core/css/CSSIdentifierValue.h" #include "core/css/CSSIdentifierValue.h"
#include "core/css/CSSValueList.h" #include "core/css/CSSValueList.h"
#include "core/css/StylePropertySet.h" #include "core/css/StylePropertySet.h"
...@@ -461,15 +462,128 @@ static String valueStyle(LocalFrame& frame, CSSPropertyID propertyID) { ...@@ -461,15 +462,128 @@ static String valueStyle(LocalFrame& frame, CSSPropertyID propertyID) {
return frame.editor().selectionStartCSSPropertyValue(propertyID); return frame.editor().selectionStartCSSPropertyValue(propertyID);
} }
static bool isUnicodeBidiNestedOrMultipleEmbeddings(CSSValueID valueID) {
return valueID == CSSValueEmbed || valueID == CSSValueBidiOverride ||
valueID == CSSValueWebkitIsolate ||
valueID == CSSValueWebkitIsolateOverride ||
valueID == CSSValueWebkitPlaintext || valueID == CSSValueIsolate ||
valueID == CSSValueIsolateOverride || valueID == CSSValuePlaintext;
}
// TODO(editing-dev): We should make |textDirectionForSelection()| to take
// |selectionInDOMTree|.
static WritingDirection textDirectionForSelection(
const VisibleSelection& selection,
EditingStyle* typingStyle,
bool& hasNestedOrMultipleEmbeddings) {
hasNestedOrMultipleEmbeddings = true;
if (selection.isNone())
return NaturalWritingDirection;
Position position = mostForwardCaretPosition(selection.start());
Node* node = position.anchorNode();
if (!node)
return NaturalWritingDirection;
Position end;
if (selection.isRange()) {
end = mostBackwardCaretPosition(selection.end());
DCHECK(end.document());
const EphemeralRange caretRange(position.parentAnchoredEquivalent(),
end.parentAnchoredEquivalent());
for (Node& n : caretRange.nodes()) {
if (!n.isStyledElement())
continue;
CSSComputedStyleDeclaration* style =
CSSComputedStyleDeclaration::create(&n);
const CSSValue* unicodeBidi =
style->getPropertyCSSValue(CSSPropertyUnicodeBidi);
if (!unicodeBidi || !unicodeBidi->isIdentifierValue())
continue;
CSSValueID unicodeBidiValue =
toCSSIdentifierValue(unicodeBidi)->getValueID();
if (isUnicodeBidiNestedOrMultipleEmbeddings(unicodeBidiValue))
return NaturalWritingDirection;
}
}
if (selection.isCaret()) {
WritingDirection direction;
if (typingStyle && typingStyle->textDirection(direction)) {
hasNestedOrMultipleEmbeddings = false;
return direction;
}
node = selection.visibleStart().deepEquivalent().anchorNode();
}
DCHECK(node);
// The selection is either a caret with no typing attributes or a range in
// which no embedding is added, so just use the start position to decide.
Node* block = enclosingBlock(node);
WritingDirection foundDirection = NaturalWritingDirection;
for (Node& runner : NodeTraversal::inclusiveAncestorsOf(*node)) {
if (runner == block)
break;
if (!runner.isStyledElement())
continue;
Element* element = &toElement(runner);
CSSComputedStyleDeclaration* style =
CSSComputedStyleDeclaration::create(element);
const CSSValue* unicodeBidi =
style->getPropertyCSSValue(CSSPropertyUnicodeBidi);
if (!unicodeBidi || !unicodeBidi->isIdentifierValue())
continue;
CSSValueID unicodeBidiValue =
toCSSIdentifierValue(unicodeBidi)->getValueID();
if (unicodeBidiValue == CSSValueNormal)
continue;
if (unicodeBidiValue == CSSValueBidiOverride)
return NaturalWritingDirection;
DCHECK(EditingStyleUtilities::isEmbedOrIsolate(unicodeBidiValue))
<< unicodeBidiValue;
const CSSValue* direction =
style->getPropertyCSSValue(CSSPropertyDirection);
if (!direction || !direction->isIdentifierValue())
continue;
int directionValue = toCSSIdentifierValue(direction)->getValueID();
if (directionValue != CSSValueLtr && directionValue != CSSValueRtl)
continue;
if (foundDirection != NaturalWritingDirection)
return NaturalWritingDirection;
// In the range case, make sure that the embedding element persists until
// the end of the range.
if (selection.isRange() && !end.anchorNode()->isDescendantOf(element))
return NaturalWritingDirection;
foundDirection = directionValue == CSSValueLtr
? LeftToRightWritingDirection
: RightToLeftWritingDirection;
}
hasNestedOrMultipleEmbeddings = false;
return foundDirection;
}
static TriState stateTextWritingDirection(LocalFrame& frame, static TriState stateTextWritingDirection(LocalFrame& frame,
WritingDirection direction) { WritingDirection direction) {
frame.document()->updateStyleAndLayoutIgnorePendingStylesheets(); frame.document()->updateStyleAndLayoutIgnorePendingStylesheets();
bool hasNestedOrMultipleEmbeddings; bool hasNestedOrMultipleEmbeddings;
WritingDirection selectionDirection = WritingDirection selectionDirection = textDirectionForSelection(
EditingStyleUtilities::textDirectionForSelection( frame.selection().computeVisibleSelectionInDOMTreeDeprecated(),
frame.selection().computeVisibleSelectionInDOMTreeDeprecated(), frame.editor().typingStyle(), hasNestedOrMultipleEmbeddings);
frame.editor().typingStyle(), hasNestedOrMultipleEmbeddings);
// FXIME: We should be returning MixedTriState when selectionDirection == // FXIME: We should be returning MixedTriState when selectionDirection ==
// direction && hasNestedOrMultipleEmbeddings // direction && hasNestedOrMultipleEmbeddings
return (selectionDirection == direction && !hasNestedOrMultipleEmbeddings) return (selectionDirection == direction && !hasNestedOrMultipleEmbeddings)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment