From fa7018a0c32798e3bcb5e05d2bdd6cc3051be76f Mon Sep 17 00:00:00 2001 From: Valerii Sidorenko Date: Tue, 9 Apr 2024 12:12:38 +0200 Subject: [PATCH] fix: do not modify input params --- src/render.test.ts | 44 +++++++++++++++++++++++++++++- src/utils/generateRenderContent.ts | 12 ++++---- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/render.test.ts b/src/render.test.ts index 1e06563..9e993aa 100644 --- a/src/render.test.ts +++ b/src/render.test.ts @@ -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 { return { @@ -53,3 +53,45 @@ test('should render body classes', () => { /\s*
\s*content\s*<\/div>\s*<\/body>/, ); }); + +function modifyPlugin(): Plugin { + 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([]); +}); diff --git a/src/utils/generateRenderContent.ts b/src/utils/generateRenderContent.ts index b1c1080..e7f4c17 100644 --- a/src/utils/generateRenderContent.ts +++ b/src/utils/generateRenderContent.ts @@ -36,18 +36,18 @@ export function generateRenderContent( ): 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 || {})};`);