Installation
Quick Take
Purpose
This program adds missing alt
attributes to img
tags and tweaks existing-ones:
- If
alt
attribute is missing on anyimg
tag, it will add an one. - If
alt
attribute is present on anyimg
tag, 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
- If
img
emptyalt
attribute has single quotes, it will be replaced with empty double-quotes. - It will also normalise the white space within
img
tags, 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
ALT
attributes 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 (br
, 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.
API — alts()
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:
Key | Type | Obligatory | Default | Description |
---|---|---|---|---|
unfancyTheAltContents Type: Boolean Obligatory: no Default: true | ||||
unfancyTheAltContents | Boolean | no | true | 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.
API — defaults
You can import defaults
:
It's a plain object:
The main function calculates the options to be used by merging the options you passed with these defaults.
API — version
You can import version
:
2020 summer update
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 (alt
) on img
only, we can use linter to check any attribute on any tag (and more).