Bug 1896047 part 2: Introduce CssAltContent class to query alternative text specified in the CSS content property. r=eeejay

This allows us to check for alt text and also to output all of it to a string.
For now, this only supports plain strings, but this will be extended in a subsequent patch.

Differential Revision: https://phabricator.services.mozilla.com/D210014
This commit is contained in:
James Teh 2024-05-17 00:24:14 +00:00
parent a157ee3a80
commit 1b49a39c03
3 changed files with 83 additions and 0 deletions

View file

@ -0,0 +1,45 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "CssAltContent.h"
#include "nsIContent.h"
#include "nsIFrame.h"
namespace mozilla::a11y {
CssAltContent::CssAltContent(nsIContent* aContent) {
nsIFrame* frame = aContent->GetPrimaryFrame();
if (!frame) {
return;
}
if (!frame->IsReplaced()) {
return;
}
// Check if this is for a pseudo-element.
if (aContent->IsInNativeAnonymousSubtree()) {
nsIContent* parent = aContent->GetParent();
if (parent && (parent->IsGeneratedContentContainerForBefore() ||
parent->IsGeneratedContentContainerForAfter() ||
parent->IsGeneratedContentContainerForMarker())) {
// We need the frame from the pseudo-element to get the content style.
frame = parent->GetPrimaryFrame();
if (!frame) {
return;
}
}
}
mItems = frame->StyleContent()->AltContentItems();
}
void CssAltContent::AppendToString(nsAString& aOut) {
// There can be multiple alt text items.
for (const auto& item : mItems) {
if (item.IsString()) {
aOut.Append(NS_ConvertUTF8toUTF16(item.AsString().AsString()));
}
}
}
} // namespace mozilla::a11y

View file

@ -0,0 +1,37 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef CssAltContent_h_
#define CssAltContent_h_
#include "nsStyleStruct.h"
namespace mozilla::a11y {
/**
* Queries alternative text specified in the CSS content property.
*/
class MOZ_STACK_CLASS CssAltContent {
public:
explicit CssAltContent(nsIContent* aContent);
/**
* Checks whether any CSS alt text has been specified. For example:
* if (CssAltContent(someContentNode)) ...
*/
explicit operator bool() const { return !mItems.IsEmpty(); }
/**
* Append all CSS alt text to a string.
*/
void AppendToString(nsAString& aOut);
private:
mozilla::Span<const mozilla::StyleContentItem> mItems;
};
} // namespace mozilla::a11y
#endif

View file

@ -48,6 +48,7 @@ UNIFIED_SOURCES += [
"ARIAStateMap.cpp",
"Asserts.cpp",
"CachedTableAccessible.cpp",
"CssAltContent.cpp",
"DocManager.cpp",
"EmbeddedObjCollector.cpp",
"EventQueue.cpp",