[6.x]: An image whose asset has no alt text is output with the asset's URL as alt

Open
#678 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php

Research direction

Start by reading the asset reference construction in resources/assets/ckeditor/src/image/imageinsert/imageinsertui.js and the referenced resolution logic in Craft CMS's src/Element/Operations/ElementRefs.php. Trace how an empty alt value becomes a reference and check the relevant tests for reference resolution. Done means an image asset with no alternative text outputs alt="", while an asset with alternative text outputs that text.

Written by the indexing model from the issue text.

Description

repo:ckeditor
Description

In 6.0.0-beta.2, an image inserted from an asset that has no alternative text is output with the asset's URL as its alt attribute.

The editor marks the alt text with an asset reference, also when the asset has none:

// resources/assets/ckeditor/src/image/imageinsert/imageinsertui.js
const alt = `${asset.alt ?? ''}#asset:${asset.id}${siteId ? `@${siteId}` : ''}:alt`;

On save, craftcms/html-field 4.0.0 turns that into a reference tag (HtmlField.php, "Find any alt text element refs and swap them with ref tags"): alt="{asset:18@2:alt}", with ||<text> when there was text.

When the tag is resolved, Craft returns the element's URL for an attribute that is not set:

// craftcms/cms, src/Element/Operations/ElementRefs.php (6.x-dev at 3c73751)
if (empty($attribute) || ! isset($element->$attribute)) {
    return (string) $element->getUrl();
}

$asset->alt is null, so {asset:18@2:alt} resolves to the file's URL.

Steps to reproduce
  1. Craft 6.x-dev with craftcms/ckeditor 6.0.0-beta.2, a volume, and an image asset whose Alternative Text is empty.
  2. A CKEditor field with the "Insert image" button on an entry type; create an entry and insert the asset from the toolbar. Save.
  3. Output the field in a template, and look at the stored value.
Expected behavior

alt="", or the alt text once the asset has one.

Actual behavior

Stored:

<figure class="image"><img src="{asset:18:url||http://127.0.0.1:8097/storage/spike/spike-photo.png}" alt="{asset:18@2:alt||http://127.0.0.1:8097/storage/spike/spike-photo.png}" width="320" height="200" /></figure>

Output: the image carries alt="http://127.0.0.1:8097/storage/spike/spike-photo.png".

A screen reader reads the URL aloud for every such image.

Versions
  • craftcms/ckeditor 6.0.0-beta.2, craftcms/html-field 4.0.0
  • craftcms/cms 6.x-dev at 3c73751914389c1cde559d77a28ecf33265cb820
  • PHP 8.5.4, PostgreSQL 18, Ubuntu 26.04
Dominant language
PHP
Stars
47
Forks
35
Avg merge
1d 2h
Merged PRs (30d)
25

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from craftcms/ckeditor

All issues in craftcms/ckeditor

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.