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

fix: Do not repeat processing of icon if it exists. #129

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 src/decorate.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function decorateButtons(element) {
* @param {string} [alt] alt text to be added to icon
*/
export function decorateIcon(span, prefix = '', alt = '') {
if (span.hasChildNodes()) return; // Any children we assume the icon is already decorated
const iconName = Array.from(span.classList).find((c) => c.startsWith('icon-')).substring(5);
const img = document.createElement('img');
img.dataset.iconName = iconName;
Expand Down
89 changes: 57 additions & 32 deletions test/decorate/decorateIcon.test.html
Original file line number Diff line number Diff line change
@@ -1,39 +1,64 @@
<!DOCTYPE html>
<html lang="en">
<body>
<script type="module">
/* eslint-env mocha */
import { runTests } from '@web/test-runner-mocha';
import { expect } from '@esm-bundle/chai';
import { decorateIcon } from '../../src/decorate.js';

window.hlx = {};

runTests(() => {
it('decorates icons with alt text', async () => {
window.hlx.codeBasePath = '/test/fixtures';

const iconA = document.createElement('span');
iconA.className = 'icon icon-a';
decorateIcon(iconA, '', 'icon a alt text');

const iconB = document.createElement('span');
iconB.className = 'icon icon-b';
decorateIcon(iconB);

document.body.prepend(iconA, iconB);

const icons = document.querySelectorAll('.icon');
icons.forEach((icon) => {
expect(icon.firstElementChild.alt).to.exist;
if (icon === iconA) {
expect(icon.firstElementChild.alt).to.be.equal('icon a alt text');
} else if (icon === iconB) {
expect(icon.firstElementChild.alt).to.be.equal('');
}
});
<script type="module">
/* eslint-env mocha */
import { runTests } from '@web/test-runner-mocha';
import { expect } from '@esm-bundle/chai';
import { decorateIcon } from '../../src/decorate.js';
import { decorateIcons } from '../../src/index.js';

window.hlx = {};

runTests(() => {
beforeEach(() => {
document.body.querySelectorAll('span').forEach((el) => el.remove());
});

it('decorates icons with alt text', async () => {
window.hlx.codeBasePath = '/test/fixtures';

const iconA = document.createElement('span');
iconA.className = 'icon icon-a';
decorateIcon(iconA, '', 'icon a alt text');

const iconB = document.createElement('span');
iconB.className = 'icon icon-b';
decorateIcon(iconB);

document.body.prepend(iconA, iconB);

const icons = document.querySelectorAll('.icon');
icons.forEach((icon) => {
expect(icon.firstElementChild.alt).to.exist;
if (icon === iconA) {
expect(icon.firstElementChild.alt).to.be.equal('icon a alt text');
} else if (icon === iconB) {
expect(icon.firstElementChild.alt).to.be.equal('');
}
});
});

it('does not process icon if img exists', async () => {
window.hlx.codeBasePath = '/test/fixtures';
const iconA = document.createElement('span');
iconA.className = 'icon icon-a';
decorateIcon(iconA, '', 'icon a alt text');

const iconB = document.createElement('span');
iconB.className = 'icon icon-b';
decorateIcon(iconB);

document.body.prepend(iconA, iconB);
decorateIcons(document.body);

const icons = document.querySelectorAll('.icon');
icons.forEach((icon) => {
const img = icon.querySelectorAll('img');
expect(img.length).to.be.equal(1);
});
});
</script>
});
</script>
</body>
</html>