Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust .ck-content aria values (label, describedby) #166

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Improved accessibility for screen readers. ([#74](https://github.com/craftcms/ckeditor/issues/74), [#166](https://github.com/craftcms/ckeditor/pull/166))
- Fixed a bug where resized images weren’t getting updated `width` and `height` attributes. ([#165](https://github.com/craftcms/ckeditor/pull/165))

## 3.7.3 - 2024-02-08
Expand Down
36 changes: 36 additions & 0 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ protected function inputHtml(mixed $value, ElementInterface $element = null): st
$baseConfig = [
'defaultTransform' => $defaultTransform?->handle,
'elementSiteId' => $element?->siteId,
'accessibleFieldName' => $this->_accessibleFieldName($element),
'describedBy' => $this->_describedBy($view),
'findAndReplace' => [
'uiType' => 'dropdown',
],
Expand Down Expand Up @@ -910,4 +912,38 @@ private function _adjustPurifierConfig(HTMLPurifier_Config $purifierConfig): HTM

return $purifierConfig;
}

/**
* Returns an accessible name for the field (to be plugged into CKEditor's main editing area aria-label).
*
* @param ElementInterface|null $element
* @return string
*/
private function _accessibleFieldName(?ElementInterface $element = null): string
{
return Craft::t('site', $this->name) .
($element?->getFieldLayout()?->getField($this->handle)?->required ? ' ' . Craft::t('site', 'Required') : '') .
($this->getIsTranslatable($element) ? ' ' . $this->getTranslationDescription($element) : '');
}

/**
* Namespaces field's $describedBy value to be passed to the field.
*
* @param View $view
* @return string
*/
private function _describedBy(View $view): string
{
if (!empty($this->describedBy)) {
$describedByArray = explode(' ', $this->describedBy);
$namespace = trim(preg_replace('/\[|\]/', '-', $view->getNamespace()), '-');
foreach ($describedByArray as $key => $item) {
$describedByArray[$key] = "$namespace-$item";
}

return implode(' ', $describedByArray);
}

return '';
}
}
2 changes: 1 addition & 1 deletion src/web/assets/ckeditor/dist/ckeditor5-craftcms.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/ckeditor/dist/ckeditor5-craftcms.js.map

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions src/web/assets/ckeditor/src/ckeditor5-craftcms.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,33 @@ export const create = async function (element, config) {
Object.assign({plugins}, config),
);

// accessibility: https://github.com/craftcms/ckeditor/issues/74
editor.editing.view.change((writer) => {
const viewEditableRoot = editor.editing.view.document.getRoot();

// adjust aria-label
if (
typeof config.accessibleFieldName != 'undefined' &&
config.accessibleFieldName.length
) {
let ariaLabel = viewEditableRoot.getAttribute('aria-label');
writer.setAttribute(
'aria-label',
config.accessibleFieldName + ', ' + ariaLabel,
viewEditableRoot,
);
}

// adjust aria-describedby
if (typeof config.describedBy != 'undefined' && config.describedBy.length) {
writer.setAttribute(
'aria-describedby',
config.describedBy,
viewEditableRoot,
);
}
});

// Update the source element before the initial form value has been recorded,
// in case the value needs to be normalized
editor.updateSourceElement();
Expand Down