This program adds missing
alt attributes to
img tags and tweaks existing-ones:
altattribute is missing on any
imgtag, it will add an one.
altattribute is present on any
imgtag, it will run its contents through string-unfancy to:
- decode all HTML entities, recursively (in case multiple HTML encoding was applied)
- replace “fancy” characters with their simpler equivalents within ASCII range. For example, single curly quotes are changed into single apostrophes. This includes dashes and all sorts of double quotes.
- replace all non-breaking spaces with regular spaces
altattribute has single quotes, it will be replaced with empty double-quotes.
- It will also normalise the white space within
imgtags, leaving one space between attributes and leaving one space before the closing slash (XHTML) or closing bracket (HTML).
- You can turn it off, but by default all the contents of the image
ALTattributes will be trimmed and unfancie’d (curly quotes, m/n-dashes replaced with single quotes, minuses). That’s to keep it simple for old email consumption software and make it easier to QA them.
html-img-alt works fine with both HTML and XHTML; it doesn’t touch the closing slashes. Use a separate library for enforcing the closing slashes (or removing them) from singleton tags (
hr and so on).
The main USP of this library is that it does not parse the HTML. It will never
throw an error because of a dirty code. It might throw because of wrong input type, but not because of something in the code.
The main function
alts() is imported like this:
It’s a function which takes three input arguments, third-one optional:
The optional options object has the following shape:
|Are each image’s
alt attributes contents trimmed and processed by string-unfancy
Here are all defaults in one place for copying:
The function will return a string, a modified input.
You can import
It's a plain object:
The main function calculates the options to be used by merging the options you passed with these defaults.
You can import
This program was created many years ago, before we came up with an idea to lint email templates. The linter is still in progress.
Instead of checking one attribute (
img only, we can use linter to check any attribute on any tag (and more).