Skip to content

Commit 51161b6

Browse files
fix(UI): Script displayed in preview when using specific css (freeCodeCamp#56570)
Co-authored-by: Oliver Eyton-Williams <[email protected]>
1 parent edfb4bb commit 51161b6

File tree

8 files changed

+59
-9
lines changed

8 files changed

+59
-9
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,17 @@ const DOCUMENT_NOT_FOUND_ERROR = 'misc.document-notfound';
8686
// of the frame. React dom errors already appear in the console, so onerror
8787
// does not need to pass them on to the default error handler.
8888

89-
export const createHeader = (id = mainPreviewId) => `
89+
// The "fcc-hide-header" class on line 95 is added to ensure that the CSSHelper class ignores this style element
90+
// during tests, preventing CSS-related test failures.
91+
92+
export const createHeader = (id = mainPreviewId) =>
93+
`
9094
<base href='' />
95+
<style class="fcc-hide-header">
96+
head *, title, meta, link, script {
97+
display: none !important;
98+
}
99+
</style>
91100
<script>
92101
window.__frameId = '${id}';
93102
window.onerror = function(msg) {

curriculum/challenges/english/01-responsive-web-design/applied-visual-design/adjust-the-height-of-an-element-using-the-height-property.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Add a `height` property to the `h4` tag and set it to 25px.
2828
Your code should change the `h4` `height` property to a value of 25 pixels.
2929

3030
```js
31-
const spaceFreeText = document.querySelector("style")?.textContent?.replace(/\s/g, '');
31+
const spaceFreeText = document.querySelector("style:not(.fcc-hide-header)")?.textContent?.replace(/\s/g, '');
3232
const h4Element = document.querySelector('h4');
3333
assert.equal(Math.round(h4Element?.getBoundingClientRect()?.height),25);
3434
assert.match(spaceFreeText,/h4{\S*height:25px(;\S*}|})/);

curriculum/challenges/english/01-responsive-web-design/applied-visual-design/decrease-the-opacity-of-an-element.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Your code should set the `opacity` property to 0.7 on the anchor tags by selecti
2525

2626
```js
2727
assert.match(
28-
document.querySelector('style')?.textContent,
28+
document.querySelector("style:not(.fcc-hide-header)")?.textContent,
2929
/\.links\s*{([\s\S]*?;)*\s*opacity\s*:\s*0*\.70*\s*(;[\s\S]*?|\s*)}/
3030
);
3131
```

curriculum/challenges/english/14-responsive-web-design-22/learn-basic-css-by-building-a-cafe-menu/5f3477ae9675db8bb7655b30.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ assert(hasH1);
2121
You should not add a new `style` tag. Add the new CSS rules to the existing `style` tag.
2222

2323
```js
24-
const hasManyStyleTags = document.querySelectorAll('style').length > 1;
25-
assert(!hasManyStyleTags);
24+
assert.isAtMost(document.querySelectorAll('style').length, 2);
2625
```
2726

2827
You should add a new `h2` selector.

curriculum/challenges/english/14-responsive-web-design-22/learn-html-by-building-a-cat-photo-app/5dc174fcf86c76b9248c6eb2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ You appear to be using a browser extension that is modifying the page. Be sure t
6363

6464
```js
6565
assert.isAtMost(document.querySelectorAll('script').length, 2);
66-
assert.equal(document.querySelectorAll('style').length, 0);
66+
assert.equal(document.querySelectorAll('style').length, 1);
6767
assert.equal(document.querySelectorAll('link').length, 0);
6868
```
6969

curriculum/challenges/english/25-front-end-development/workshop-cafe-menu/5f3477ae9675db8bb7655b30.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ assert(hasH1);
2121
You should not add a new `style` tag. Add the new CSS rules to the existing `style` tag.
2222

2323
```js
24-
const hasManyStyleTags = document.querySelectorAll('style').length > 1;
25-
assert(!hasManyStyleTags);
24+
assert.isAtMost(document.querySelectorAll('style').length, 2);
2625
```
2726

2827
You should add a new `h2` selector.

curriculum/challenges/english/25-front-end-development/workshop-cat-photo-app/5dc174fcf86c76b9248c6eb2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ You appear to be using a browser extension that is modifying the page. Be sure t
5151

5252
```js
5353
assert.isAtMost(document.querySelectorAll('script').length, 2);
54-
assert.equal(document.querySelectorAll('style').length, 0);
54+
assert.equal(document.querySelectorAll('style').length, 1);
5555
assert.equal(document.querySelectorAll('link').length, 0);
5656
```
5757

e2e/iframe-script.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { expect, test } from '@playwright/test';
2+
import translations from '../client/i18n/locales/english/translations.json';
3+
import { focusEditor } from './utils/editor';
4+
5+
test.beforeEach(async ({ page }) => {
6+
await page.goto(
7+
'/learn/javascript-algorithms-and-data-structures-v8/build-a-cash-register-project/build-a-cash-register'
8+
);
9+
});
10+
11+
test.describe('Preventing Script From Displaying in Preview', () => {
12+
// this test is for inserting problematic <style> tag and checking if the preview is empty
13+
14+
test('should render an empty preview after inserting CSS', async ({
15+
page,
16+
isMobile
17+
}) => {
18+
await focusEditor({ page, isMobile });
19+
20+
// the pain in the butt <style> tag with display: block rule into the editor
21+
const styleTag = `
22+
<style>
23+
* {
24+
margin: 0;
25+
padding: 0;
26+
display: block;
27+
}
28+
</style>`;
29+
await page.keyboard.insertText(styleTag);
30+
31+
if (isMobile) {
32+
await page
33+
.getByRole('tab', { name: translations.learn['editor-tabs'].preview })
34+
.click();
35+
}
36+
37+
// Checks that the iframe preview is empty
38+
const frame = page.frameLocator('#fcc-main-frame').first();
39+
40+
// Make sure there are no visible elements inside the iframe's body
41+
await expect(frame.locator('body')).toBeEmpty();
42+
});
43+
});

0 commit comments

Comments
 (0)