Skip to content

Commit

Permalink
fix: do not modify input params (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS committed Apr 9, 2024
1 parent f06907f commit 793bfd0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
44 changes: 43 additions & 1 deletion src/render.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 {
Expand Down Expand Up @@ -53,3 +53,45 @@ test('should render body classes', () => {
/<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

0 comments on commit 793bfd0

Please sign in to comment.