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 passing double dollars in the inlined DOM #2165

Merged
merged 4 commits into from
Jun 13, 2023
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
2 changes: 1 addition & 1 deletion packages/toolpad-app/src/server/toolpadAppBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function postProcessHtml(html: string, { config, dom }: PostProcessHtmlPa
)}] = ${serializedInitialState}</script>`,
];

return html.replaceAll(`<!-- __TOOLPAD_SCRIPTS__ -->`, toolpadScripts.join('\n'));
return html.replace(`<!-- __TOOLPAD_SCRIPTS__ -->`, () => toolpadScripts.join('\n'));
}

interface ToolpadVitePluginParams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function JsExpressionEditor({
.map(([propKey, propValue]) => {
return `${propKey}: ${propValue.replaceAll(
/\bThisComponent\b/g,
`RootObject[${JSON.stringify(key)}]`,
() => `RootObject[${JSON.stringify(key)}]`,
)}`;
})
.join('\n')}
Expand Down
60 changes: 60 additions & 0 deletions packages/toolpad-utils/src/fs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, test, beforeEach, afterEach, expect } from '@jest/globals';
import * as fs from 'fs/promises';
import * as path from 'path';
import { fileReplace, fileReplaceAll } from './fs';

describe('fileReplace', () => {
let testDir: string;

beforeEach(async () => {
testDir = await fs.mkdtemp('test-dir');
});

afterEach(async () => {
await fs.rm(testDir, { recursive: true, force: true });
});

test('can replace parts of a file', async () => {
const filePath = path.resolve(testDir, './test.txt');
await fs.writeFile(filePath, 'Hello World, Hello', { encoding: 'utf-8' });
await fileReplace(filePath, 'Hello', 'Goodbye');
const content = await fs.readFile(filePath, { encoding: 'utf-8' });
expect(content).toEqual('Goodbye World, Hello');
});

test('can replace with double dollar signs', async () => {
const filePath = path.resolve(testDir, './test.txt');
await fs.writeFile(filePath, 'Hello World', { encoding: 'utf-8' });
await fileReplace(filePath, 'Hello', '$$');
const content = await fs.readFile(filePath, { encoding: 'utf-8' });
expect(content).toEqual('$$ World');
});
});

describe('fileReplaceAll', () => {
let testDir: string;

beforeEach(async () => {
testDir = await fs.mkdtemp('test-dir');
});

afterEach(async () => {
await fs.rm(testDir, { recursive: true, force: true });
});

test('can replace parts of a file', async () => {
const filePath = path.resolve(testDir, './test.txt');
await fs.writeFile(filePath, 'Hello World, Hello', { encoding: 'utf-8' });
await fileReplaceAll(filePath, 'Hello', 'Goodbye');
const content = await fs.readFile(filePath, { encoding: 'utf-8' });
expect(content).toEqual('Goodbye World, Goodbye');
});

test('can replace with double dollar signs', async () => {
const filePath = path.resolve(testDir, './test.txt');
await fs.writeFile(filePath, 'Hello World', { encoding: 'utf-8' });
await fileReplaceAll(filePath, 'Hello', '$$');
const content = await fs.readFile(filePath, { encoding: 'utf-8' });
expect(content).toEqual('$$ World');
});
});
4 changes: 2 additions & 2 deletions packages/toolpad-utils/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export async function fileReplace(
replaceValue: string,
): Promise<void> {
const queriesFileContent = await fs.readFile(filePath, { encoding: 'utf-8' });
const updatedFileContent = queriesFileContent.replace(searchValue, replaceValue);
const updatedFileContent = queriesFileContent.replace(searchValue, () => replaceValue);
await fs.writeFile(filePath, updatedFileContent);
}

Expand All @@ -99,6 +99,6 @@ export async function fileReplaceAll(
replaceValue: string,
) {
const queriesFileContent = await fs.readFile(filePath, { encoding: 'utf-8' });
const updatedFileContent = queriesFileContent.replaceAll(searchValue, replaceValue);
const updatedFileContent = queriesFileContent.replaceAll(searchValue, () => replaceValue);
await fs.writeFile(filePath, updatedFileContent);
}
14 changes: 14 additions & 0 deletions test/integration/backend-basic/fixture/toolpad/pages/page/page.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: page
spec:
id: d9iAjrh
title: page
display: shell
content:
- component: PageRow
name: pageRow
children:
- component: Text
name: text
props:
value: $$
6 changes: 4 additions & 2 deletions test/integration/backend-basic/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { ToolpadRuntime } from '../../models/ToolpadRuntime';
import { ToolpadEditor } from '../../models/ToolpadEditor';
import { waitForMatch } from '../../utils/streams';

const BASIC_TESTS_PAGE_ID = '5q1xd0t';

test.use({
ignoreConsoleErrors: [
// Chrome:
Expand Down Expand Up @@ -49,7 +51,7 @@ test('functions basics', async ({ page }) => {

test('function editor reload', async ({ page, localApp }) => {
const editorModel = new ToolpadEditor(page);
await editorModel.goto();
await editorModel.goToPageById(BASIC_TESTS_PAGE_ID);

await expect(editorModel.appCanvas.getByText('edited hello')).toBeVisible();

Expand All @@ -61,7 +63,7 @@ test('function editor reload', async ({ page, localApp }) => {

test('function editor parameters update', async ({ page, localApp }) => {
const editorModel = new ToolpadEditor(page);
await editorModel.goto();
await editorModel.goToPageById(BASIC_TESTS_PAGE_ID);

await editorModel.componentEditor.getByRole('button', { name: 'withParams' }).click();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v1
kind: page
spec:
id: lWm2sZu
title: charset
title: Encoding
display: shell
content:
- component: PageRow
Expand All @@ -14,3 +14,7 @@ spec:
value:
$$jsExpression: |
`Can pass utf-8: "€"`
- component: Text
name: text
props:
value: 'Can pass double dollars: "$$"'
7 changes: 5 additions & 2 deletions test/integration/bindings/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ test('bindings', async ({ page }) => {
await expect(page.getByText('-test2-')).toBeVisible();
});

test('charset', async ({ page }) => {
test('encoding', async ({ page }) => {
const runtimeModel = new ToolpadRuntime(page);
await runtimeModel.gotoPage('charset');
await runtimeModel.gotoPage('encoding');

const test1 = page.getByText('Can pass utf-8: "€"');
await expect(test1).toBeVisible();

const test2 = page.getByText('Can pass double dollars: "$$"');
await expect(test2).toBeVisible();
});