Skip to content

Commit 57695d7

Browse files
SembaukeNaomi CarriganShaunSHamilton
authored
feat: add lang code to iframe (freeCodeCamp#47365)
* feat: add lang code to iframe * feat: apply Naomi's code review Co-authored-by: Naomi Carrigan <nhcarrigan@gmail.com> * fix: Iframe title test * Apply suggestions from code review Co-authored-by: Naomi Carrigan <nhcarrigan@gmail.com> * fix: hopefully things make sense * fix: steal sem's idea for DOMParser * fix: make the solution work on the main frame Co-authored-by: Naomi Carrigan <nhcarrigan@gmail.com> Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
1 parent 6c6a0a8 commit 57695d7

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

client/i18n/locales/english/translations.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@
451451
"change-theme": "Sign in to change theme.",
452452
"translation-pending": "Help us translate",
453453
"certification-project": "Certification Project",
454+
"iframe-preview": "{{title}} preview",
454455
"iframe-alert": "Normally this link would bring you to another website! It works. This is a link to: {{externalLink}}",
455456
"document-notfound": "document not found"
456457
},

client/src/templates/Challenges/utils/build.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,29 @@ export function updatePreview(
279279
buildData.challengeType === challengeTypes.html ||
280280
buildData.challengeType === challengeTypes.multifileCertProject
281281
) {
282-
createMainPreviewFramer(document, proxyLogger)(buildData);
282+
createMainPreviewFramer(
283+
document,
284+
proxyLogger,
285+
getDocumentTitle(buildData)
286+
)(buildData);
283287
} else {
284288
throw new Error(
285289
`Cannot show preview for challenge type ${buildData.challengeType}`
286290
);
287291
}
288292
}
289293

294+
function getDocumentTitle(buildData: BuildChallengeData) {
295+
// Give iframe a title attribute for accessibility using the preview
296+
// document's <title>.
297+
298+
const parser = new DOMParser();
299+
const doc = parser.parseFromString(buildData?.sources?.index, 'text/html');
300+
const title = doc.querySelector('title');
301+
302+
return title?.innerText ?? 'challenge';
303+
}
304+
290305
export function updateProjectPreview(
291306
buildData: BuildChallengeData,
292307
document: Document
@@ -295,15 +310,10 @@ export function updateProjectPreview(
295310
buildData.challengeType === challengeTypes.html ||
296311
buildData.challengeType === challengeTypes.multifileCertProject
297312
) {
298-
// Give iframe a title attribute for accessibility using the preview
299-
// document's <title>.
300-
const titleMatch = buildData?.sources?.index?.match(
301-
/<title>(.*?)<\/title>/
302-
);
303-
const frameTitle = titleMatch
304-
? titleMatch[1].trim() + ' preview'
305-
: 'preview';
306-
createProjectPreviewFramer(document, frameTitle)(buildData);
313+
createProjectPreviewFramer(
314+
document,
315+
getDocumentTitle(buildData)
316+
)(buildData);
307317
} else {
308318
throw new Error(
309319
`Cannot show preview for challenge type ${buildData.challengeType}`

client/src/templates/Challenges/utils/frame.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ const createFrame =
124124
const frame = document.createElement('iframe');
125125
frame.id = id;
126126
if (typeof title === 'string') {
127-
frame.title = title;
127+
frame.title = i18next.t('misc.iframe-preview', { title });
128+
frame.lang = 'en';
128129
}
129130
return {
130131
...frameContext,
@@ -268,15 +269,16 @@ const writeContentToFrame = (frameContext: Context) => {
268269

269270
export const createMainPreviewFramer = (
270271
document: Document,
271-
proxyLogger: ProxyLogger
272+
proxyLogger: ProxyLogger,
273+
frameTitle: string
272274
) =>
273275
createFramer(
274276
document,
275277
mainPreviewId,
276278
initMainFrame,
277279
proxyLogger,
278280
undefined,
279-
'preview'
281+
frameTitle
280282
);
281283

282284
export const createProjectPreviewFramer = (

0 commit comments

Comments
 (0)