tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 604d0b84a755e5aa81c87bc072603db226343acf
parent 8189256b71e19b3621cd60364a3e36c544e005ef
Author: Eitan Isaacson <eitan@monotonous.org>
Date:   Fri, 21 Nov 2025 18:48:40 +0000

Bug 1998242 - P1: Factor out tooltip calculation. r=morgan

This is used in both name and description.

Differential Revision: https://phabricator.services.mozilla.com/D271309

Diffstat:
Maccessible/generic/LocalAccessible.cpp | 73+++++++++++++++++++++++++++++++++++--------------------------------------
Maccessible/generic/LocalAccessible.h | 6++++++
2 files changed, 41 insertions(+), 38 deletions(-)

diff --git a/accessible/generic/LocalAccessible.cpp b/accessible/generic/LocalAccessible.cpp @@ -137,26 +137,8 @@ ENameValueFlag LocalAccessible::Name(nsString& aName) const { if (!aName.IsEmpty()) return nameFlag; // In the end get the name from tooltip. - if (mContent->IsHTMLElement()) { - if (mContent->AsElement()->GetAttr(nsGkAtoms::title, aName)) { - aName.CompressWhitespace(); - return eNameFromTooltip; - } - } else if (mContent->IsXULElement()) { - if (mContent->AsElement()->GetAttr(nsGkAtoms::tooltiptext, aName)) { - aName.CompressWhitespace(); - return eNameFromTooltip; - } - } else if (mContent->IsSVGElement()) { - // If user agents need to choose among multiple 'desc' or 'title' - // elements for processing, the user agent shall choose the first one. - for (nsIContent* childElm = mContent->GetFirstChild(); childElm; - childElm = childElm->GetNextSibling()) { - if (childElm->IsSVGElement(nsGkAtoms::desc)) { - nsTextEquivUtils::AppendTextEquivFromContent(this, childElm, &aName); - return eNameFromTooltip; - } - } + if (Tooltip(aName)) { + return eNameFromTooltip; } if (auto cssAlt = CssAltContent(mContent)) { @@ -190,28 +172,14 @@ EDescriptionValueFlag LocalAccessible::Description( if (aDescription.IsEmpty()) { NativeDescription(aDescription); + aDescription.CompressWhitespace(); + } - if (aDescription.IsEmpty()) { - // Keep the Name() method logic. - if (mContent->IsHTMLElement()) { - mContent->AsElement()->GetAttr(nsGkAtoms::title, aDescription); - } else if (mContent->IsXULElement()) { - mContent->AsElement()->GetAttr(nsGkAtoms::tooltiptext, aDescription); - } else if (mContent->IsSVGElement()) { - for (nsIContent* childElm = mContent->GetFirstChild(); childElm; - childElm = childElm->GetNextSibling()) { - if (childElm->IsSVGElement(nsGkAtoms::desc)) { - nsTextEquivUtils::AppendTextEquivFromContent(this, childElm, - &aDescription); - break; - } - } - } - } + if (aDescription.IsEmpty()) { + Tooltip(aDescription); } if (!aDescription.IsEmpty()) { - aDescription.CompressWhitespace(); nsAutoString name; Name(name); // Don't expose a description if it is the same as the name. @@ -2665,6 +2633,35 @@ bool LocalAccessible::ARIADescription(nsString& aDescription) const { } // LocalAccessible protected +bool LocalAccessible::Tooltip(nsString& aTooltip) const { + if (!HasOwnContent()) { + return false; + } + + if (mContent->IsHTMLElement()) { + mContent->AsElement()->GetAttr(nsGkAtoms::title, aTooltip); + aTooltip.CompressWhitespace(); + return !aTooltip.IsEmpty(); + } else if (mContent->IsXULElement()) { + mContent->AsElement()->GetAttr(nsGkAtoms::tooltiptext, aTooltip); + aTooltip.CompressWhitespace(); + return !aTooltip.IsEmpty(); + } else if (mContent->IsSVGElement()) { + // If user agents need to choose among multiple 'desc' or 'title' + // elements for processing, the user agent shall choose the first one. + for (nsIContent* childElm = mContent->GetFirstChild(); childElm; + childElm = childElm->GetNextSibling()) { + if (childElm->IsSVGElement(nsGkAtoms::desc)) { + nsTextEquivUtils::AppendTextEquivFromContent(this, childElm, &aTooltip); + aTooltip.CompressWhitespace(); + return !aTooltip.IsEmpty(); + } + } + } + return false; +} + +// LocalAccessible protected ENameValueFlag LocalAccessible::NativeName(nsString& aName) const { if (mContent->IsHTMLElement()) { LocalAccessible* label = nullptr; diff --git a/accessible/generic/LocalAccessible.h b/accessible/generic/LocalAccessible.h @@ -873,6 +873,12 @@ class LocalAccessible : public nsISupports, public Accessible { bool ARIADescription(nsString& aDescription) const; /** + * Returns the accessible "tooltip", usually derived from title attribute in + * HTML or tooltiptext in XUL. + */ + bool Tooltip(nsString& aTooltip) const; + + /** * Returns the accessible name specified for this control using XUL * <label control="id" ...>. */