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

Add support for mixed rendering #49

Closed
wants to merge 5 commits into from
Closed
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
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "preact-render-to-string",
"name": "@yr/preact-render-to-string",
"amdName": "preactRenderToString",
"version": "3.6.3",
"version": "4.0.2",
"description": "Render JSX to an HTML string, with support for Preact components.",
"main": "dist/index.js",
"jsnext:main": "src/index.js",
Expand All @@ -20,15 +20,15 @@
"universal",
"isomorphic"
],
"author": "Jason Miller <jason@developit.ca>",
"author": "Jason Miller <jason@developit.ca>, Simen Sægrov <simen.sagrov@nrk.no>",
"license": "MIT",
"typings": "src/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/developit/preact-render-to-string.git"
"url": "https://github.com/yr/preact-render-to-string.git"
},
"bugs": {
"url": "https://github.com/developit/preact-render-to-string/issues"
"url": "https://github.com/yr/preact-render-to-string/issues"
},
"homepage": "https://github.com/developit/preact-render-to-string",
"peerDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ declare module render {
shallow:boolean;
xml:boolean;
pretty:boolean;
alwaysRenderedComponents: Array[String];
}

function render(vnode:VNode, context?:any, options?:Options):string;
function shallowRender(vnode:VNode, context?:any):string;
function mixedRender(vnode: VNode, alwaysRenderedComponents: Array[String], context?: any): string;
}

export = render;
26 changes: 23 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,27 @@ renderToString.render = renderToString;
let shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW);


/**
* Only render elements, leaving Components inline as `<ComponentName ... />`
* except those specified in fullyRenderedComponents.
* This method is just a convenience alias for `render(vnode, context, { shallow:true, alwaysRenderedComponented: [] })`
* @param {VNode} vnode JSX VNode to render.
* @param {Array} alwaysRenderedComponents List of components that should be rendered with shallow rendering
* @param {Object} [context={}] Optionally pass an initial context object through the render path.
*/
let mixedRender = (vnode, alwaysRenderedComponents = [], context) => {
const opts = Object.assign({ alwaysRenderedComponents }, SHALLOW);
return renderToString(vnode, context, opts);
};


/** The default export is an alias of `render()`. */
export default function renderToString(vnode, context, opts, inner, isSvgMode) {
let { nodeName, attributes, children } = vnode || EMPTY,
isComponent = false;
context = context || {};
opts = opts || {};
opts.alwaysRenderedComponents = opts.alwaysRenderedComponents || [];

let pretty = opts.pretty,
indentChar = typeof pretty==='string' ? pretty : '\t';
Expand All @@ -69,9 +84,12 @@ export default function renderToString(vnode, context, opts, inner, isSvgMode) {

// components
if (typeof nodeName==='function') {
let componentName = getComponentName(nodeName);
isComponent = true;
if (opts.shallow && (inner || opts.renderRootComponent===false)) {
nodeName = getComponentName(nodeName);
if (opts.shallow &&
(inner || opts.renderRootComponent===false) &&
!opts.alwaysRenderedComponents.includes(componentName)) {
nodeName = componentName;
}
else {
let props = getNodeProps(vnode),
Expand Down Expand Up @@ -230,10 +248,12 @@ function getFallbackComponentName(component) {
return name;
}
renderToString.shallowRender = shallowRender;
renderToString.mixedRender = mixedRender;


export {
renderToString as render,
renderToString,
shallowRender
shallowRender,
mixedRender
};
84 changes: 84 additions & 0 deletions test/mixedRender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { mixedRender } from '../src';
import { h, Component } from 'preact';
import chai, { expect } from 'chai';
import { spy, match } from 'sinon';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);

describe('mixedRender()', () => {
it('should not render nested components when not white listed', () => {
let Test = spy( ({ foo, children }) => <div bar={foo}><b>test child</b>{ children }</div> );
Test.displayName = 'Test';

let rendered = mixedRender(
<section>
<Test foo={1}><span>asdf</span></Test>
</section>
);

expect(rendered).to.equal(`<section><Test foo="1"><span>asdf</span></Test></section>`);
expect(Test).not.to.have.been.called;
});

it('should always render root component', () => {
let Test = spy( ({ foo, children }) => <div bar={foo}><b>test child</b>{ children }</div> );
Test.displayName = 'Test';

let rendered = mixedRender(
<Test foo={1}>
<span>asdf</span>
</Test>
);

expect(rendered).to.equal(`<div bar="1"><b>test child</b><span>asdf</span></div>`);
expect(Test).to.have.been.calledOnce;
});

it('should render nested components when they are white listed', () => {
let Test = spy( ({ foo, children }) => <div bar={foo}><b>test child</b>{ children }</div> );
Test.displayName = 'Test';

let rendered = mixedRender(
<section>
<Test foo={1}><span>asdf</span></Test>
</section>
, ['Test']);

expect(rendered).to.equal(`<section><div bar="1"><b>test child</b><span>asdf</span></div></section>`);
expect(Test).to.have.been.called;
});

it('should not render nested components inside a whitelisted component', () => {
let Test = spy( ({ foo, children }) => <div bar={foo}><b>test child</b>{ children }</div> );
let Ignored = spy( ({ title, children }) => <h2>This {title} should not be rendered</h2> );
Test.displayName = 'Test';
Ignored.displayName = 'Ignored';

let rendered = mixedRender(
<section>
<Test foo={1}><Ignored title={'FooBarTitle'}/></Test>
</section>
, ['Test']);

expect(rendered).to.equal(`<section><div bar="1"><b>test child</b><Ignored title="FooBarTitle"></Ignored></div></section>`);
expect(Test).to.have.been.called;
expect(Ignored).to.not.have.been.called;
});

it('should render deeply nested components when all are white listed', () => {
let Test = spy( ({ foo, children }) => <div bar={foo}><b>test child</b>{ children }</div> );
let Ignored = spy( ({ title, children }) => <h2>This {title} should be rendered</h2> );
Test.displayName = 'Test';
Ignored.displayName = 'Ignored';

let rendered = mixedRender(
<section>
<Test foo={1}><Ignored title={'FooBarTitle'}/></Test>
</section>
, ['Test', 'Ignored']);

expect(rendered).to.equal(`<section><div bar="1"><b>test child</b><h2>This FooBarTitle should be rendered</h2></div></section>`);
expect(Test).to.have.been.called;
expect(Ignored).to.have.been.called;
});
});