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 modify input params #44

Merged
merged 1 commit into from
Apr 9, 2024
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
44 changes: 43 additions & 1 deletion src/render.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {createUikitPlugin} from './plugins/index.js';
import {createRenderFunction} from './render.js';
import type {Plugin} from './types.js';
import type {Link, Meta, Plugin, Script, Stylesheet} from './types.js';

function dirPlugin(): Plugin<void> {
return {
name: 'dirPlugin',
apply({renderContent: {htmlAttributes}}) {
htmlAttributes.dir = 'ltr';

Check warning on line 9 in src/render.test.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Assignment to property of function parameter 'htmlAttributes'
},
};
}
Expand Down Expand Up @@ -53,3 +53,45 @@
/<body class="test g-root g-root_theme_light">\s*<div id="root">\s*content\s*<\/div>\s*<\/body>/,
);
});

function modifyPlugin(): Plugin<void> {
return {
name: 'modifyPlugin',
apply({
renderContent: {meta, links, scripts, inlineScripts, styleSheets, inlineStyleSheets},
}) {
meta.push({name: 'test', content: 'test'});
links.push({rel: 'test', href: 'test'});
scripts.push({src: 'test.js'});
inlineScripts.push('console.log("test")');
styleSheets.push({href: 'test.css'});
inlineStyleSheets.push('body { color: red; }');
},
};
}

test('should not modify users params', () => {
const meta: Meta[] = [];
const links: Link[] = [];
const scripts: Script[] = [];
const inlineScripts: string[] = [];
const styleSheets: Stylesheet[] = [];
const inlineStyleSheets: string[] = [];

createRenderFunction([modifyPlugin()])({
title: 'test',
meta,
links,
scripts,
inlineScripts,
styleSheets,
inlineStyleSheets,
});

expect(meta).toEqual([]);
expect(links).toEqual([]);
expect(scripts).toEqual([]);
expect(inlineScripts).toEqual([]);
expect(styleSheets).toEqual([]);
expect(inlineStyleSheets).toEqual([]);
});
12 changes: 6 additions & 6 deletions src/utils/generateRenderContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ export function generateRenderContent<Plugins extends Plugin[], Data>(
): RenderContent {
const helpers = getRenderHelpers(params);
const htmlAttributes: Attributes = {...params.htmlAttributes};
const meta = params.meta ?? [];
const meta = [...(params.meta ?? [])];
// in terms of sets: meta = params.meta ∪ (defaultMeta ∖ params.meta)
defaultMeta.forEach((defaultMetaItem) => {
if (!meta.find(({name}) => name === defaultMetaItem.name)) {
meta.push(defaultMetaItem);
}
});
const styleSheets = params.styleSheets || [];
const scripts = params.scripts || [];
const inlineStyleSheets = params.inlineStyleSheets || [];
const inlineScripts = params.inlineScripts || [];
const links = params.links || [];
const styleSheets = [...(params.styleSheets || [])];
const scripts = [...(params.scripts || [])];
const inlineStyleSheets = [...(params.inlineStyleSheets || [])];
const inlineScripts = [...(params.inlineScripts || [])];
const links = [...(params.links || [])];

inlineScripts.unshift(`window.__DATA__ = ${htmlescape(params.data || {})};`);

Expand Down
Loading