Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→html-img-altnext

html-img-alt4.2.1

Adds missing alt attributes to img tags. Non-parsing.

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — ALTS()
  • API — DEFAULTS
  • API — VERSION
  • 2020 SUMMER…
  • Changelog

Installation

Quick Take

Examples

  • Preserve original typography in alt text
  • Repair an alt attribute that has no value
  • Normalize typography and surrounding spaces in alt text
  • Add an alt attribute while preserving XHTML closing syntax

Purpose

This program adds missing alt attributes to img tags and tweaks existing-ones:

  1. If alt attribute is missing on any img tag, it will add an one.
  2. If alt attribute is present on any img 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
  3. If img empty alt attribute has single quotes, it will be replaced with empty double-quotes.
  4. 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).
  5. 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:

KeyTypeObligatoryDefaultDescription
unfancyTheAltContents
Type: Boolean
Obligatory: no
Default: true
unfancyTheAltContentsBooleannotrueAre 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, you can use the linter to check any attribute on any tag (and more).

Changelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab