Skip to content

A platform for community discussion. Free, open, simple.

A11Y: don't use the title attribute for category and tag descriptions (#42005)

Krisc1614b9a67c6
diff --git a/frontend/discourse/app/components/rich-hashtag-autocomplete-results.gjs b/frontend/discourse/app/components/rich-hashtag-autocomplete-results.gjs
index 81e0ca4c23d..8badccf3bb0 100644
--- a/frontend/discourse/app/components/rich-hashtag-autocomplete-results.gjs
+++ b/frontend/discourse/app/components/rich-hashtag-autocomplete-results.gjs
@@ -69,7 +69,6 @@ export default class RichHashtagAutocompleteResults extends Component {
<a
class="hashtag-autocomplete__link
{{if (eq index @selectedIndex) 'selected'}}"
- title={{result.description}}
href
{{on "click" (fn this.handleResultClick result index)}}
>
diff --git a/frontend/discourse/app/lib/render-tag.js b/frontend/discourse/app/lib/render-tag.js
index d01c147a291..13b00b932d0 100644
--- a/frontend/discourse/app/lib/render-tag.js
+++ b/frontend/discourse/app/lib/render-tag.js
@@ -49,17 +49,12 @@ export function defaultRenderTag(tag, params) {
classes.push(params.size);
}
- // remove all html tags from hover text
- const hoverDescription =
- params.description && params.description.replace(/<.+?>/g, "");
-
let val =
"<" +
htmlTag +
href +
" data-tag-name=" +
tagNameLower +
- (params.description ? ' title="' + escape(hoverDescription) + '" ' : "") +
" class='" +
classes.join(" ") +
"'>" +
diff --git a/frontend/discourse/app/lib/sidebar/user/categories-section/category-section-link.js b/frontend/discourse/app/lib/sidebar/user/categories-section/category-section-link.js
index 1fb98515c5d..07773023fac 100644
--- a/frontend/discourse/app/lib/sidebar/user/categories-section/category-section-link.js
+++ b/frontend/discourse/app/lib/sidebar/user/categories-section/category-section-link.js
@@ -177,8 +177,10 @@ export default class CategorySectionLink {
return "discovery.unreadCategory discovery.hotCategory discovery.topCategory discovery.newCategory discovery.latestCategory discovery.category discovery.categoryNone discovery.categoryAll";
}
+ // The link text already names the category. A title here would only repeat
+ // the description as a mouse-only tooltip that screen readers announce.
get title() {
- return this.category.descriptionText;
+ return null;
}
get text() {
diff --git a/frontend/discourse/app/lib/sidebar/user/tags-section/base-tag-section-link.js b/frontend/discourse/app/lib/sidebar/user/tags-section/base-tag-section-link.js
index 723dc2b36fc..2f95b2c0348 100644
--- a/frontend/discourse/app/lib/sidebar/user/tags-section/base-tag-section-link.js
+++ b/frontend/discourse/app/lib/sidebar/user/tags-section/base-tag-section-link.js
@@ -1,5 +1,3 @@
-import escape from "discourse/lib/escape";
-
let customTagSectionLinkPrefixIcons = {};
export function registerCustomTagSectionLinkPrefixIcon({
@@ -36,8 +34,10 @@ export default class BaseTagSectionLink {
return this.tagName;
}
+ // The link text already names the tag. A title here would only repeat the
+ // description as a mouse-only tooltip that screen readers announce.
get title() {
- return escape(this.tag.description);
+ return null;
}
get prefixType() {
diff --git a/frontend/discourse/app/ui-kit/helpers/d-category-link.js b/frontend/discourse/app/ui-kit/helpers/d-category-link.js
index 54ebada73c2..4a2fbb38c4e 100644
--- a/frontend/discourse/app/ui-kit/helpers/d-category-link.js
+++ b/frontend/discourse/app/ui-kit/helpers/d-category-link.js
@@ -134,13 +134,6 @@ function buildTopicCount(count) {
}
export function defaultCategoryLinkRenderer(category, opts) {
- // not ideal as we have to call it manually and we pass a fake category object
- // but there's not way around it for now
- let descriptionText = applyValueTransformer(
- "category-description-text",
- escapeExpression(get(category, "description_text")),
- { category }
- );
let restricted = get(category, "read_restricted");
let url = opts.url
? opts.url
@@ -187,7 +180,6 @@ export function defaultCategoryLinkRenderer(category, opts) {
? `style="--category-badge-color: #${category.color}"`
: ""
}
- ${descriptionText ? 'title="' + descriptionText + '" ' : ""}
>`;
if (opts.styleType === "icon" && opts.icon) {
diff --git a/frontend/discourse/tests/acceptance/sidebar-user-categories-section-test.js b/frontend/discourse/tests/acceptance/sidebar-user-categories-section-test.js
index 356b5543d3f..162f3824379 100644
--- a/frontend/discourse/tests/acceptance/sidebar-user-categories-section-test.js
+++ b/frontend/discourse/tests/acceptance/sidebar-user-categories-section-test.js
@@ -636,10 +636,8 @@ acceptance("Sidebar - Logged on user - Categories Section", function (needs) {
);
});
- test("category section link has the right title", async function (assert) {
+ test("category section link does not have a title", async function (assert) {
const categories = Site.current().categories;
-
- // Category with link HTML tag in description
const category = categories.find((c) => c.id === 28);
updateCurrentUser({
@@ -650,10 +648,9 @@ acceptance("Sidebar - Logged on user - Categories Section", function (needs) {
assert
.dom(`.sidebar-section-link-wrapper[data-category-id="${category.id}"] a`)
- .hasAttribute(
+ .doesNotHaveAttribute(
"title",
- category.descriptionText,
- "category description without HTML entity is used as the link's title"
+ "does not expose the category description as a tooltip"
);
});
diff --git a/frontend/discourse/tests/acceptance/transformers/category-description-text-test.js b/frontend/discourse/tests/acceptance/transformers/category-description-text-test.js
deleted file mode 100644
index 3c32d61d85f..00000000000
--- a/frontend/discourse/tests/acceptance/transformers/category-description-text-test.js
+++ /dev/null
@@ -1,26 +0,0 @@
-import { visit } from "@ember/test-helpers";
-import { test } from "qunit";
-import { withPluginApi } from "discourse/lib/plugin-api";
-import { acceptance } from "discourse/tests/helpers/qunit-helpers";
-
-acceptance("category-description-text transformer", function () {
- test("applying a value transformation", async function (assert) {
- withPluginApi((api) => {
- api.registerValueTransformer(
- "category-description-text",
- ({ value, context }) =>
- value[0] + "-" + context.category.id + "-transformed"
- );
- });
-
- await visit("/");
-
- assert
- .dom("[data-topic-id='11994'] .badge-category")
- .hasAttribute(
- "title",
- "A-1-transformed",
- "it transforms the category description text"
- );
- });
-});
diff --git a/frontend/discourse/tests/integration/components/select-kit/category-chooser-test.gjs b/frontend/discourse/tests/integration/components/select-kit/category-chooser-test.gjs
index 1f0c7886e68..7ae9e93a385 100644
--- a/frontend/discourse/tests/integration/components/select-kit/category-chooser-test.gjs
+++ b/frontend/discourse/tests/integration/components/select-kit/category-chooser-test.gjs
@@ -2,6 +2,7 @@ import { hash } from "@ember/helper";
import { getOwner } from "@ember/owner";
import { render } from "@ember/test-helpers";
import { module, test } from "qunit";
+import { withPluginApi } from "discourse/lib/plugin-api";
import CategoryChooser from "discourse/select-kit/components/category-chooser";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import selectKit from "discourse/tests/helpers/select-kit-helper";
@@ -306,5 +307,40 @@ module(
.dom(".category-desc", this.subject.rowByIndex(0).el())
.hasText('baz "bar ‘foo’');
});
+
+ test("applies the category-description-text value transformer", async function (assert) {
+ withPluginApi((api) => {
+ api.registerValueTransformer(
+ "category-description-text",
+ ({ value, context }) =>
+ value[0] + "-" + context.category.id + "-transformed"
+ );
+ });
+
+ const store = getOwner(this).lookup("service:store");
+ store.createRecord("category", {
+ id: 1,
+ name: "cat-with-description",
+ description_text: "A description",
+ });
+
+ await render(
+ <template>
+ <CategoryChooser
+ @value={{this.value}}
+ @options={{hash scopedCategoryId=1}}
+ />
+ </template>
+ );
+
+ await this.subject.expand();
+
+ assert
+ .dom(".category-desc", this.subject.rowByIndex(0).el())
+ .hasText(
+ "A-1-transformed",
+ "it transforms the category description text"
+ );
+ });
}
);
diff --git a/frontend/discourse/tests/integration/ui-kit/helpers/d-category-link-test.gjs b/frontend/discourse/tests/integration/ui-kit/helpers/d-category-link-test.gjs
index fd189c2f808..21a36778bba 100644
--- a/frontend/discourse/tests/integration/ui-kit/helpers/d-category-link-test.gjs
+++ b/frontend/discourse/tests/integration/ui-kit/helpers/d-category-link-test.gjs
@@ -13,14 +13,14 @@ module("Integration | ui-kit | Helper | dCategoryLink", function (hooks) {
assert.dom(".badge-category__name").hasText("foo");
});
- test("description_text", async function (assert) {
+ test("description_text is not rendered as a tooltip", async function (assert) {
await render(
<template>
{{dCategoryLink (hash name="foo" description_text="bar")}}
</template>
);
- assert.dom(".badge-category").hasAttribute("title", "bar");
+ assert.dom(".badge-category").doesNotHaveAttribute("title");
});
test("styleType option", async function (assert) {
diff --git a/frontend/discourse/tests/unit/lib/category-badge-test.js b/frontend/discourse/tests/unit/lib/category-badge-test.js
index b2a19b81cef..83f44129e55 100644
--- a/frontend/discourse/tests/unit/lib/category-badge-test.js
+++ b/frontend/discourse/tests/unit/lib/category-badge-test.js
@@ -33,7 +33,10 @@ module("Unit | Utility | category-badge", function (hooks) {
const label = tag.children[0];
assert
.dom(label)
- .hasAttribute("title", "cool description", "has the correct title");
+ .doesNotHaveAttribute(
+ "title",
+ "does not expose the description as a tooltip"
+ );
assert.dom(label.children[0]).hasText("hello", "has the category name");
});
diff --git a/frontend/discourse/tests/unit/lib/render-tag-test.js b/frontend/discourse/tests/unit/lib/render-tag-test.js
index 4bce6a4327c..aa1108b7944 100644
--- a/frontend/discourse/tests/unit/lib/render-tag-test.js
+++ b/frontend/discourse/tests/unit/lib/render-tag-test.js
@@ -8,16 +8,8 @@ module("Unit | Utility | render-tag", function (hooks) {
test("defaultRenderTag", function (assert) {
assert.strictEqual(
renderTag("foo", { description: "foo description" }),
- "<a href='/tag/foo' data-tag-name=foo title=\"foo description\" class='discourse-tag simple'>foo</a>",
- "formats tag as link with plain description in hover"
- );
-
- assert.strictEqual(
- renderTag("foo", {
- description: 'foo description <a href="localhost">link</a>',
- }),
- "<a href='/tag/foo' data-tag-name=foo title=\"foo description link\" class='discourse-tag simple'>foo</a>",
- "removes any html tags from description"
+ "<a href='/tag/foo' data-tag-name=foo class='discourse-tag simple'>foo</a>",
+ "does not expose the description as a tooltip"
);
});
diff --git a/lib/category_badge.rb b/lib/category_badge.rb
index a824f88f242..2056b860d18 100644
--- a/lib/category_badge.rb
+++ b/lib/category_badge.rb
@@ -51,7 +51,6 @@ module CategoryBadge
end
end || "--style-square"
class_names = "badge-category #{style_class} #{data[:parent_category] ? "--has-parent" : ""}"
- description = category.description_text ? "title='#{category.description_text}'" : ""
badge_styles = {
"--category-badge-color": "##{category.color}",
@@ -65,7 +64,7 @@ module CategoryBadge
result << "<span data-category-id='#{category.id}'"
result << " style='#{map_styles_to_string(badge_styles)}'"
result << " data-parent-category-id='#{data[:parent_category].id}'" if data[:parent_category]
- result << " data-drop-close='true' class='#{class_names}' #{description}>"
+ result << " data-drop-close='true' class='#{class_names}'>"
if category.style_type == "icon" && category.icon.present?
result << SvgSprite.raw_svg(category.icon)
diff --git a/spec/lib/category_badge_spec.rb b/spec/lib/category_badge_spec.rb
index 469f2bb4ea8..75cfbf26918 100644
--- a/spec/lib/category_badge_spec.rb
+++ b/spec/lib/category_badge_spec.rb
@@ -3,22 +3,22 @@
require "category_badge"
RSpec.describe CategoryBadge do
- it "escapes HTML in category names / descriptions" do
- c = Fabricate(:category, name: "<b>name</b>", description: "<b>title</b>")
+ it "escapes HTML in category names" do
+ c = Fabricate(:category, name: "<b>name</b>")
html = CategoryBadge.html_for(c)
- expect(html).not_to include("<b>title</b>")
expect(html).not_to include("<b>name</b>")
expect(html).to include("&lt;b&gt;name&lt;/b&gt;")
- expect(html).to include("title='title'")
end
- it "escapes code block contents" do
- c = Fabricate(:category, description: '<code>\' &lt;b id="x"&gt;</code>')
+ it "does not expose the category description as a tooltip" do
+ c = Fabricate(:category, description: "a description")
+
html = CategoryBadge.html_for(c)
- expect(html).to include("title='&#39; &lt;b id=&quot;x&quot;&gt;'")
+ expect(html).not_to include("title=")
+ expect(html).not_to include("a description")
end
it "includes color vars" do
diff --git a/spec/system/page_objects/components/navigation_menu/base.rb b/spec/system/page_objects/components/navigation_menu/base.rb
index 686d71513cb..75848eb3ad0 100644
--- a/spec/system/page_objects/components/navigation_menu/base.rb
+++ b/spec/system/page_objects/components/navigation_menu/base.rb
@@ -136,15 +136,6 @@ module PageObjects
expect(tag_section_links.map(&:text)).to eq(tag_names)
end
- def has_tag_section_link_with_title?(tag, title)
- section_link =
- find(
- ".sidebar-section[data-section-name='tags'] .sidebar-section-link-wrapper[data-tag-name='#{tag.name}'] .sidebar-section-link",
- )
-
- expect(section_link["title"]).to eq(title)
- end
-
def find_section_link(name)
find(".#{SIDEBAR_SECTION_LINK_SELECTOR}[data-link-name='#{name}']")
end
diff --git a/spec/system/viewing_sidebar_as_anonymous_user_spec.rb b/spec/system/viewing_sidebar_as_anonymous_user_spec.rb
index d300718f5f2..7b3c744625d 100644
--- a/spec/system/viewing_sidebar_as_anonymous_user_spec.rb
+++ b/spec/system/viewing_sidebar_as_anonymous_user_spec.rb
@@ -54,7 +54,6 @@ describe "Viewing sidebar as anonymous user" do
expect(sidebar).to have_tags_section
expect(sidebar).to have_all_tags_section_link
expect(sidebar).to have_tag_section_links([tag3, tag2, tag4, tag5, tag1])
- expect(sidebar).to have_tag_section_link_with_title(tag1, "tag 1 description ")
end
it "should display the site's top tags when `default_navigation_menu_tags` site setting has been set but the tags configured are hidden to the user" do
@@ -66,7 +65,6 @@ describe "Viewing sidebar as anonymous user" do
expect(sidebar).to have_tags_section
expect(sidebar).to have_all_tags_section_link
expect(sidebar).to have_tag_section_links([tag3, tag2, tag4, tag1, tag6])
- expect(sidebar).to have_tag_section_link_with_title(tag1, "tag 1 description ")
end
it "should display the tags configured in `default_navigation_menu_tags` site setting when it has been set" do
@@ -77,7 +75,6 @@ describe "Viewing sidebar as anonymous user" do
expect(sidebar).to have_tags_section
expect(sidebar).to have_all_tags_section_link
expect(sidebar).to have_tag_section_links([tag3, tag4])
- expect(sidebar).to have_tag_section_link_with_title(tag3, "tag 3 description")
end
end
end
diff --git a/spec/system/viewing_sidebar_spec.rb b/spec/system/viewing_sidebar_spec.rb
index c2786112ee0..ae291788f7f 100644
--- a/spec/system/viewing_sidebar_spec.rb
+++ b/spec/system/viewing_sidebar_spec.rb
@@ -146,8 +146,6 @@ describe "Viewing sidebar" do
expect(sidebar).to be_visible
expect(sidebar).to have_tags_section
expect(sidebar).to have_tag_section_links([tag3, tag2, tag4, tag5, tag1])
- expect(sidebar).to have_tag_section_link_with_title(tag3, "tag 3 description")
- expect(sidebar).to have_tag_section_link_with_title(tag1, "tag 1 description ")
expect(sidebar).to have_all_tags_section_link
end
@@ -161,8 +159,6 @@ describe "Viewing sidebar" do
expect(sidebar).to be_visible
expect(sidebar).to have_tags_section
expect(sidebar).to have_tag_section_links([tag1, tag2, tag3])
- expect(sidebar).to have_tag_section_link_with_title(tag3, "tag 3 description")
- expect(sidebar).to have_tag_section_link_with_title(tag1, "tag 1 description ")
expect(sidebar).to have_all_tags_section_link
end
end