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

MWPW-123745 SVG alt text #401

Merged
merged 3 commits into from
Jan 25, 2023
Merged
Changes from 1 commit
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
17 changes: 14 additions & 3 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,24 @@ export async function loadBlock(block) {

export function decorateSVG(a) {
const { textContent, href } = a;
const ext = textContent?.substr(textContent.lastIndexOf('.') + 1);
const altTextFlagIndex = textContent.indexOf('|');
const sanitizeTextContent = altTextFlagIndex === -1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually verbs are for functions. Would sanitizedTextContent be more appropriate?

? textContent
: textContent?.slice(0 ,altTextFlagIndex).trim();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor spacing issue. Is your eslint working?

Suggested change
: textContent?.slice(0 ,altTextFlagIndex).trim();
: textContent?.slice(0, altTextFlagIndex).trim();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is your eslint working?

I thought it was. Looks like I need to double check.

const ext = sanitizeTextContent?.substring(sanitizeTextContent.lastIndexOf('.') + 1);
if (ext !== 'svg') return;

const altText = altTextFlagIndex === -1
? ''
: textContent.substring(textContent.indexOf('|') + 1).trim();
const textContentUrl = new URL(sanitizeTextContent);
const hrefUrl = new URL(href);
Copy link
Contributor

@meganthecoder meganthecoder Jan 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new URL can throw errors if the url is just a slug. Are you positive that the url will always have a domain? If not, it would be good to put these in a try-catch block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thanks for the feedback @meganthecoder!

const img = document.createElement('img');
img.src = localizeLink(textContent);
img.src = localizeLink(sanitizeTextContent);
img.alt = altText;
const pic = document.createElement('picture');
pic.append(img);
if (img.src === href) {
if (textContentUrl.pathname === hrefUrl.pathname) {
a.parentElement.replaceChild(pic, a);
} else {
a.textContent = '';
Expand Down