Sha256: 5b7ee11c46d5dc413a87f51a8a399a6c8283d0959655158a7b77cc62c72b2e6b

Contents?: true

Size: 1.96 KB

Versions: 81

Compression:

Stored size: 1.96 KB

Contents

import './polyfills';
import renderToString from './index';
import { indent, encodeEntities, assign } from './util';
import prettyFormat from 'pretty-format';

// we have to patch in Array support, Possible issue in npm.im/pretty-format
let preactPlugin = {
	test(object) {
		return (
			object &&
			typeof object === 'object' &&
			'type' in object &&
			'props' in object &&
			'key' in object
		);
	},
	print(val, print, indent) {
		return renderToString(val, preactPlugin.context, preactPlugin.opts, true);
	}
};

let prettyFormatOpts = {
	plugins: [preactPlugin]
};

function attributeHook(name, value, context, opts, isComponent) {
	let type = typeof value;

	// Use render-to-string's built-in handling for these properties
	if (name === 'dangerouslySetInnerHTML') return false;

	// always skip null & undefined values, skip false DOM attributes, skip functions if told to
	if (value == null || (type === 'function' && !opts.functions)) return '';

	if (
		opts.skipFalseAttributes &&
		!isComponent &&
		(value === false ||
			((name === 'class' || name === 'style') && value === ''))
	)
		return '';

	let indentChar = typeof opts.pretty === 'string' ? opts.pretty : '\t';
	if (type !== 'string') {
		if (type === 'function' && !opts.functionNames) {
			value = 'Function';
		} else {
			preactPlugin.context = context;
			preactPlugin.opts = opts;
			value = prettyFormat(value, prettyFormatOpts);
			if (~value.indexOf('\n')) {
				value = `${indent('\n' + value, indentChar)}\n`;
			}
		}
		return indent(`\n${name}={${value}}`, indentChar);
	}
	return `\n${indentChar}${name}="${encodeEntities(value)}"`;
}

let defaultOpts = {
	attributeHook,
	jsx: true,
	xml: false,
	functions: true,
	functionNames: true,
	skipFalseAttributes: true,
	pretty: '  '
};

function renderToJsxString(vnode, context, opts, inner) {
	opts = assign(assign({}, defaultOpts), opts || {});
	return renderToString(vnode, context, opts, inner);
}

export default renderToJsxString;
export { renderToJsxString as render };

Version data entries

81 entries across 81 versions & 1 rubygems

Version Path
isomorfeus-preact-10.9.0 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.8.2 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.8.1 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.8.0 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.7.3 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.7.2 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.7.1 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.7.0 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.6.62 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.6.61 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.6.60 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.6.59 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.6.58 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.6.57 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.6.56 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.6.55 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.6.54 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.6.53 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.6.52 node_modules/preact-render-to-string/src/jsx.js
isomorfeus-preact-10.6.51 node_modules/preact-render-to-string/src/jsx.js