From 33087f0bdf17aea7942cbb02672c9cd83f7545ee Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Wed, 12 Oct 2022 14:07:37 +0300 Subject: [PATCH] A11Y: Set role=presentation if alt attr is missing (#18546) This applies to all images posted that do not have a user description. --- .../tests/unit/lib/pretty-text-test.js | 11 +++++++--- .../pretty-text/addon/allow-lister.js | 1 + .../addon/engines/discourse-markdown-it.js | 8 +++++++- spec/fixtures/md/spec.txt | 2 +- spec/lib/pretty_text_spec.rb | 20 +++++++++---------- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js b/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js index 853573405e9..b0c2ead7d97 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js @@ -958,7 +958,7 @@ eviltrout

test("URLs in BBCode tags", function (assert) { assert.cooked( "[img]http://eviltrout.com/eviltrout.png[/img][img]http://samsaffron.com/samsaffron.png[/img]", - '

', + '

', "images are properly parsed" ); @@ -987,6 +987,11 @@ eviltrout

'

Red dot

', "It allows data images" ); + + assert.cooked( + "![](http://folksy.com/images/folksy-colour.png)", + '

' + ); }); test("attachment", function (assert) { @@ -1162,7 +1167,7 @@ eviltrout

); assert.cookedPara( "[img]http://eviltrout.com/eviltrout.png[/img]", - '', + '', "links images" ); assert.cookedPara( @@ -1210,7 +1215,7 @@ eviltrout

); assert.cookedPara( "[url=http://www.example.com][img]http://example.com/logo.png[/img][/url]", - '', + '', "supports [url] with an embedded [img]" ); }); diff --git a/app/assets/javascripts/pretty-text/addon/allow-lister.js b/app/assets/javascripts/pretty-text/addon/allow-lister.js index 91a33bef3f3..8b3eb446332 100644 --- a/app/assets/javascripts/pretty-text/addon/allow-lister.js +++ b/app/assets/javascripts/pretty-text/addon/allow-lister.js @@ -173,6 +173,7 @@ export const DEFAULT_LIST = [ "iframe[allowfullscreen]", "iframe[allow]", "img[alt]", + "img[role]", "img[height]", "img[title]", "img[width]", diff --git a/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown-it.js b/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown-it.js index 8f6d02d5fd7..8ce6442575b 100644 --- a/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown-it.js +++ b/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown-it.js @@ -253,7 +253,13 @@ function renderImageOrPlayableMedia(tokens, idx, options, env, slf) { } } - token.attrs[token.attrIndex("alt")][1] = altSplit.join("|"); + const altValue = altSplit.join("|").trim(); + if (altValue === "") { + token.attrSet("role", "presentation"); + } else { + token.attrSet("alt", altValue); + } + return slf.renderToken(tokens, idx, options); } diff --git a/spec/fixtures/md/spec.txt b/spec/fixtures/md/spec.txt index 50a660fa7ec..66cb1caec91 100644 --- a/spec/fixtures/md/spec.txt +++ b/spec/fixtures/md/spec.txt @@ -8564,7 +8564,7 @@ My ![foo bar](/path/to/train.jpg "title" ) ```````````````````````````````` example ![](/url) . -

+

```````````````````````````````` diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rb index b09d9f1036f..532f276d897 100644 --- a/spec/lib/pretty_text_spec.rb +++ b/spec/lib/pretty_text_spec.rb @@ -1771,13 +1771,13 @@ HTML it "supports img bbcode" do cooked = PrettyText.cook "[img]http://www.image/test.png[/img]" - html = "

\"\"

" + html = "

\"\"

" expect(cooked).to eq(html) end it "provides safety for img bbcode" do cooked = PrettyText.cook "[img]http://aaa.com[/img]" - html = '

' + html = '

' expect(cooked).to eq(html) end @@ -1862,10 +1862,10 @@ HTML html = <<~HTML

title with | title
-
-
+
+
stuff
-

+

HTML expect(cooked).to eq(html.strip) @@ -1881,11 +1881,11 @@ HTML MD html = <<~HTML -


-
-
-
-

+


+
+
+
+

HTML expect(cooked).to eq(html.strip)