SharePoint 2013 Fixing WCAG F38 Failure–Images without ALT tags–using a Control Adapter–Display Templates
The WCAG (Web Content Accessibility Guidelines) provide a baseline for accessibility standards so various tools, such as screen readers, can provide a reasonable experience for those with accessibility challenges.
With regards to images, the guideline provides that all Image tabs <img…> should probably (I say probably here for various reasons) have an ALT tag.
In the case of filler images, or “decorative” that isn’t representative of content, according to F38 here: They should have an empty ALT tag – thus ‘alt=””’.
So, we need to “add” an alt=”” tag to this “block” of HTML.
To do this, we can utilize a ControlAdapter – which is a Web Forms based concept, that allows interception at Render time for the control. In the past, ControlAdapters were used in SharePoint 2007 to provide all the rewriting of HTML Tables to more CSS friendly versions – ultimately at the time to help with the WCAG needs.
The main part of the control adapter to do this re-rendering is within the Render statement. Below are the primary methods that will do this rendering and fixup of the IMG tags:
namespace ImageLinkControlAdapter.Code
{
public class ImageLinkAdapter : ControlAdapter
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
/// First we get the control's planned HTML that is emmitted...
using (StringWriter baseStringWriter = new StringWriter())
using (HtmlTextWriter baseWriter = new HtmlTextWriter(baseStringWriter))
{
base.Render(baseWriter);
baseWriter.Flush();
baseWriter.Close();
/// Now we have an HTML element...
string baseHtml = baseStringWriter.ToString();
/// now fixit up...
writer.Write(RebuildImgTag(baseHtml));
}
}
internal string RebuildImgTag(string existingTagHtml)
{
var pattern = @"<img\s[^>]*>";
var rv = Regex.Replace(existingTagHtml, pattern, this.InsertAlt);
return rv;
}
internal string InsertAlt(Match match)
{
return this.InsertAlt(match.ToString());
}
internal string InsertAlt(string existingTag)
{
if (!existingTag.StartsWith("<img", StringComparison.InvariantCultureIgnoreCase))
return existingTag;
if (existingTag.Contains("alt=", StringComparison.InvariantCultureIgnoreCase))
return existingTag;
var insertPoint = existingTag.IndexOf("/>");
var rv = existingTag.Insert(insertPoint, "alt=\"\"");
return rv;
}
}
internal static class StringExtensions
{
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
return source.IndexOf(toCheck, comp) >= 0;
}
}</pre></div>
Finally, the full Visual Studio 2013 Solution and source is located here: