Skip to content

Commit

Permalink
Reland "Fix case of incorrect removal of dir=auto state in HTMLElemen…
Browse files Browse the repository at this point in the history
…t::OnDirAttrChanged."

This is a reland of commit 2de8c4d

Original change's description:
> Fix case of incorrect removal of dir=auto state in HTMLElement::OnDirAttrChanged.
>
> When a dir=auto attribute is removed, we might still need to be in the
> ancestor-or-self-has-dir-auto state because of an ancestor.  This state
> is needed to know when we need to handle auto direction changes for
> dynamic changes to text.
>
> Bug: 576815
> Change-Id: I5d8cd262a165962fbd0bc14aa0de8bd16c970400
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4812429
> Commit-Queue: Di Zhang <dizhangg@chromium.org>
> Commit-Queue: David Baron <dbaron@chromium.org>
> Reviewed-by: Di Zhang <dizhangg@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#1209530}

Bug: 576815
Change-Id: Ic490f7cf61efcea32f981e9e55c00cc5f6ef2a46
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4939634
Reviewed-by: Di Zhang <dizhangg@chromium.org>
Auto-Submit: David Baron <dbaron@chromium.org>
Commit-Queue: Di Zhang <dizhangg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1209769}
  • Loading branch information
dbaron authored and Chromium LUCI CQ committed Oct 14, 2023
1 parent 5be39fe commit 70ae366
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion third_party/blink/renderer/core/dom/element.h
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,7 @@ class CORE_EXPORT Element : public ContainerNode, public Animatable {
TextDirection ParentDirectionality() const;
void AdjustDirectionalityIfNeededAfterChildrenChanged(
const ChildrenChange& change);
bool RecalcSelfOrAncestorHasDirAuto();
template <typename Traversal>
absl::optional<TextDirection> ResolveAutoDirectionality(
bool& is_deferred,
Expand Down Expand Up @@ -1496,7 +1497,6 @@ class CORE_EXPORT Element : public ContainerNode, public Animatable {
}

void RecomputeDirectionFromParent();
bool RecalcSelfOrAncestorHasDirAuto();

ShadowRoot& CreateAndAttachShadowRoot(ShadowRootType);

Expand Down
7 changes: 5 additions & 2 deletions third_party/blink/renderer/core/html/html_element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3024,8 +3024,11 @@ void HTMLElement::OnDirAttrChanged(const AttributeModificationParams& params) {
}

if (is_old_auto && !is_new_auto) {
ClearSelfOrAncestorHasDirAutoAttribute();
UpdateDescendantHasDirAutoAttribute(false /* has_dir_auto */);
if (!RuntimeEnabledFeatures::CSSPseudoDirEnabled() ||
!RecalcSelfOrAncestorHasDirAuto()) {
ClearSelfOrAncestorHasDirAutoAttribute();
UpdateDescendantHasDirAutoAttribute(false /* has_dir_auto */);
}
} else if (!is_old_auto && is_new_auto) {
SetSelfOrAncestorHasDirAutoAttribute();
UpdateDescendantHasDirAutoAttribute(true /* has_dir_auto */);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,20 @@ test(() => {
assert_true(ele.matches(":dir(ltr)"), "is LTR after change");
assert_true(ele2.matches(":dir(ltr)"), "child is LTR after change");
}, "Non-HTML element text contents influence dir=auto");

test(() => {
const e1 = document.createElement("div");
e1.dir = "auto";
const e2 = document.createElement("div");
e2.dir = "auto";
e2.innerText = "A";
e1.append(e2);
assert_true(e1.matches(":dir(ltr)"), "parent is LTR before changes");
assert_true(e2.matches(":dir(ltr)"), "child is LTR before changes");
e2.removeAttribute("dir");
assert_true(e1.matches(":dir(ltr)"), "parent is LTR after removing dir attribute on child");
assert_true(e2.matches(":dir(ltr)"), "child is LTR after removing dir attribute on child");
e2.firstChild.data = "\u05D0";
assert_false(e1.matches(":dir(ltr)"), "parent is RTL after changing text in child");
assert_false(e2.matches(":dir(ltr)"), "child is RTL after changing text in child");
}, "text changes apply to dir=auto on further ancestor after removing dir=auto from closer ancestor");

0 comments on commit 70ae366

Please sign in to comment.