Skip to content

Commit

Permalink
Bug 1778965 - Clean-up a bit alt text code. r=TYLin
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio committed Jul 11, 2022
1 parent 53bdb1b commit f912244
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
20 changes: 11 additions & 9 deletions layout/base/nsCSSFrameConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1693,15 +1693,15 @@ already_AddRefed<nsIContent> nsCSSFrameConstructor::CreateGeneratedContent(
// localized text we have.
// XXX what if the 'alt' attribute is added later, how will we
// detect that and do the right thing here?
if (aOriginatingElement.HasAttr(kNameSpaceID_None, nsGkAtoms::alt)) {
if (aOriginatingElement.HasAttr(nsGkAtoms::alt)) {
nsCOMPtr<nsIContent> content;
NS_NewAttributeContent(mDocument->NodeInfoManager(), kNameSpaceID_None,
nsGkAtoms::alt, getter_AddRefs(content));
return content.forget();
}

if (aOriginatingElement.IsHTMLElement(nsGkAtoms::input)) {
if (aOriginatingElement.HasAttr(kNameSpaceID_None, nsGkAtoms::value)) {
if (aOriginatingElement.HasAttr(nsGkAtoms::value)) {
nsCOMPtr<nsIContent> content;
NS_NewAttributeContent(mDocument->NodeInfoManager(),
kNameSpaceID_None, nsGkAtoms::value,
Expand Down Expand Up @@ -7979,25 +7979,27 @@ void nsCSSFrameConstructor::WillDestroyFrameTree() {

// XXXbz I'd really like this method to go away. Once we have inline-block and
// I can just use that for sized broken images, that can happen, maybe.
void nsCSSFrameConstructor::GetAlternateTextFor(Element* aElement, nsAtom* aTag,
//
// NOTE(emilio): This needs to match MozAltContent handling.
void nsCSSFrameConstructor::GetAlternateTextFor(const Element& aElement,
nsAString& aAltText) {
// The "alt" attribute specifies alternate text that is rendered
// when the image can not be displayed.
if (aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::alt, aAltText)) {
if (aElement.GetAttr(nsGkAtoms::alt, aAltText)) {
return;
}

if (nsGkAtoms::input == aTag) {
// If there's no "alt" attribute, and aContent is an input element, then use
// the value of the "value" attribute
if (aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::value, aAltText)) {
if (aElement.IsHTMLElement(nsGkAtoms::input)) {
// If there's no "alt" attribute, and aElement is an input element, then use
// the value of the "value" attribute.
if (aElement.GetAttr(nsGkAtoms::value, aAltText)) {
return;
}

// If there's no "value" attribute either, then use the localized string for
// "Submit" as the alternate text.
nsContentUtils::GetMaybeLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
"Submit", aElement->OwnerDoc(),
"Submit", aElement.OwnerDoc(),
aAltText);
}
}
Expand Down
5 changes: 1 addition & 4 deletions layout/base/nsCSSFrameConstructor.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ class nsCSSFrameConstructor final : public nsFrameManager {
PresShell* aPresShell);
~nsCSSFrameConstructor() { MOZ_ASSERT(mFCItemsInUse == 0); }

// get the alternate text for a content node
static void GetAlternateTextFor(Element* aContent,
nsAtom* aTag, // content object's tag
nsAString& aAltText);
static void GetAlternateTextFor(const Element&, nsAString& aAltText);

private:
nsCSSFrameConstructor(const nsCSSFrameConstructor& aCopy) = delete;
Expand Down
6 changes: 2 additions & 4 deletions layout/generic/nsImageFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1748,8 +1748,7 @@ ImgDrawResult nsImageFrame::DisplayAltFeedback(gfxContext& aRenderingContext,
// If there's still room, display the alt-text
if (!inner.IsEmpty()) {
nsAutoString altText;
nsCSSFrameConstructor::GetAlternateTextFor(
mContent->AsElement(), mContent->NodeInfo()->NameAtom(), altText);
nsCSSFrameConstructor::GetAlternateTextFor(*mContent->AsElement(), altText);
DisplayAltText(PresContext(), aRenderingContext, altText, inner);
}

Expand Down Expand Up @@ -1958,8 +1957,7 @@ ImgDrawResult nsImageFrame::DisplayAltFeedbackWithoutLayer(
RefPtr<gfxContext> captureCtx = gfxContext::CreateOrNull(textDrawer);

nsAutoString altText;
nsCSSFrameConstructor::GetAlternateTextFor(
mContent->AsElement(), mContent->NodeInfo()->NameAtom(), altText);
nsCSSFrameConstructor::GetAlternateTextFor(*mContent->AsElement(), altText);
DisplayAltText(PresContext(), *captureCtx.get(), altText, inner);

textDrawer->TerminateShadows();
Expand Down

0 comments on commit f912244

Please sign in to comment.