preact-render-to-string 6.0.0 → 6.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +147 -147
- package/dist/commonjs.js +1 -1
- package/dist/commonjs.js.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.module.js +1 -1
- package/dist/index.module.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/jsx.d.ts +24 -21
- package/dist/jsx.js +4 -1
- package/dist/jsx.mjs.map +1 -1
- package/dist/jsx.modern.js +2 -0
- package/dist/jsx.modern.js.map +1 -0
- package/dist/jsx.module.js.map +1 -1
- package/dist/jsx.umd.js.map +1 -1
- package/jsx.js +1 -1
- package/package.json +152 -152
- package/src/constants.js +16 -16
- package/src/index.d.ts +7 -3
- package/src/index.js +385 -381
- package/src/jsx.d.ts +24 -21
- package/src/jsx.js +104 -104
- package/src/polyfills.js +8 -8
- package/src/pretty.js +433 -433
- package/src/util.js +150 -150
- package/typings.json +5 -5
- package/dist/internal.d.ts +0 -38
package/src/jsx.d.ts
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
import { VNode } from 'preact';
|
|
2
|
-
|
|
3
|
-
interface Options {
|
|
4
|
-
jsx?: boolean;
|
|
5
|
-
xml?: boolean;
|
|
6
|
-
pretty?: boolean | string;
|
|
7
|
-
shallow?: boolean;
|
|
8
|
-
functions?: boolean;
|
|
9
|
-
functionNames?: boolean;
|
|
10
|
-
skipFalseAttributes?: boolean;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default function renderToStringPretty(
|
|
14
|
-
vnode: VNode,
|
|
15
|
-
context?: any,
|
|
16
|
-
options?: Options
|
|
17
|
-
): string;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
import { VNode } from 'preact';
|
|
2
|
+
|
|
3
|
+
interface Options {
|
|
4
|
+
jsx?: boolean;
|
|
5
|
+
xml?: boolean;
|
|
6
|
+
pretty?: boolean | string;
|
|
7
|
+
shallow?: boolean;
|
|
8
|
+
functions?: boolean;
|
|
9
|
+
functionNames?: boolean;
|
|
10
|
+
skipFalseAttributes?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default function renderToStringPretty(
|
|
14
|
+
vnode: VNode,
|
|
15
|
+
context?: any,
|
|
16
|
+
options?: Options
|
|
17
|
+
): string;
|
|
18
|
+
export function render(vnode: VNode, context?: any, options?: Options): string;
|
|
19
|
+
|
|
20
|
+
export function shallowRender(
|
|
21
|
+
vnode: VNode,
|
|
22
|
+
context?: any,
|
|
23
|
+
options?: Options
|
|
24
|
+
): string;
|
package/src/jsx.js
CHANGED
|
@@ -1,104 +1,104 @@
|
|
|
1
|
-
import './polyfills';
|
|
2
|
-
import renderToString from './pretty';
|
|
3
|
-
import { indent, encodeEntities } from './util';
|
|
4
|
-
import prettyFormat from 'pretty-format';
|
|
5
|
-
|
|
6
|
-
/** @typedef {import('preact').VNode} VNode */
|
|
7
|
-
|
|
8
|
-
// we have to patch in Array support, Possible issue in npm.im/pretty-format
|
|
9
|
-
let preactPlugin = {
|
|
10
|
-
test(object) {
|
|
11
|
-
return (
|
|
12
|
-
object &&
|
|
13
|
-
typeof object === 'object' &&
|
|
14
|
-
'type' in object &&
|
|
15
|
-
'props' in object &&
|
|
16
|
-
'key' in object
|
|
17
|
-
);
|
|
18
|
-
},
|
|
19
|
-
print(val, print, indent) {
|
|
20
|
-
return renderToString(val, preactPlugin.context, preactPlugin.opts, true);
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
let prettyFormatOpts = {
|
|
25
|
-
plugins: [preactPlugin]
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
function attributeHook(name, value, context, opts, isComponent) {
|
|
29
|
-
let type = typeof value;
|
|
30
|
-
|
|
31
|
-
// Use render-to-string's built-in handling for these properties
|
|
32
|
-
if (name === 'dangerouslySetInnerHTML') return false;
|
|
33
|
-
|
|
34
|
-
// always skip null & undefined values, skip false DOM attributes, skip functions if told to
|
|
35
|
-
if (value == null || (type === 'function' && !opts.functions)) return '';
|
|
36
|
-
|
|
37
|
-
if (
|
|
38
|
-
opts.skipFalseAttributes &&
|
|
39
|
-
!isComponent &&
|
|
40
|
-
(value === false ||
|
|
41
|
-
((name === 'class' || name === 'style') && value === ''))
|
|
42
|
-
)
|
|
43
|
-
return '';
|
|
44
|
-
|
|
45
|
-
let indentChar = typeof opts.pretty === 'string' ? opts.pretty : '\t';
|
|
46
|
-
if (type !== 'string') {
|
|
47
|
-
if (type === 'function' && !opts.functionNames) {
|
|
48
|
-
value = 'Function';
|
|
49
|
-
} else {
|
|
50
|
-
preactPlugin.context = context;
|
|
51
|
-
preactPlugin.opts = opts;
|
|
52
|
-
value = prettyFormat(value, prettyFormatOpts);
|
|
53
|
-
if (~value.indexOf('\n')) {
|
|
54
|
-
value = `${indent('\n' + value, indentChar)}\n`;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return indent(`\n${name}={${value}}`, indentChar);
|
|
58
|
-
}
|
|
59
|
-
return `\n${indentChar}${name}="${encodeEntities(value)}"`;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
let defaultOpts = {
|
|
63
|
-
attributeHook,
|
|
64
|
-
jsx: true,
|
|
65
|
-
xml: false,
|
|
66
|
-
functions: true,
|
|
67
|
-
functionNames: true,
|
|
68
|
-
skipFalseAttributes: true,
|
|
69
|
-
pretty: ' '
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Render Preact JSX + Components to a pretty-printed HTML-like string.
|
|
74
|
-
* @param {VNode} vnode JSX Element / VNode to render
|
|
75
|
-
* @param {Object} [context={}] Initial root context object
|
|
76
|
-
* @param {Object} [options={}] Rendering options
|
|
77
|
-
* @param {Boolean} [options.jsx=true] Generate JSX/XML output instead of HTML
|
|
78
|
-
* @param {Boolean} [options.xml=false] Use self-closing tags for elements without children
|
|
79
|
-
* @param {Boolean} [options.shallow=false] Serialize nested Components (`<Foo a="b" />`) instead of rendering
|
|
80
|
-
* @param {Boolean} [options.pretty=false] Add whitespace for readability
|
|
81
|
-
* @param {RegExp|undefined} [options.voidElements] RegeEx to define which element types are self-closing
|
|
82
|
-
* @returns {String} a pretty-printed HTML-like string
|
|
83
|
-
*/
|
|
84
|
-
export default function renderToStringPretty(vnode, context, options) {
|
|
85
|
-
const opts = Object.assign({}, defaultOpts, options || {});
|
|
86
|
-
if (!opts.jsx) opts.attributeHook = null;
|
|
87
|
-
return renderToString(vnode, context, opts);
|
|
88
|
-
}
|
|
89
|
-
export { renderToStringPretty as render };
|
|
90
|
-
|
|
91
|
-
const SHALLOW = { shallow: true };
|
|
92
|
-
|
|
93
|
-
/** Only render elements, leaving Components inline as `<ComponentName ... />`.
|
|
94
|
-
* This method is just a convenience alias for `render(vnode, context, { shallow:true })`
|
|
95
|
-
* @name shallow
|
|
96
|
-
* @function
|
|
97
|
-
* @param {VNode} vnode JSX VNode to render.
|
|
98
|
-
* @param {Object} [context={}] Optionally pass an initial context object through the render path.
|
|
99
|
-
* @param {Parameters<typeof renderToStringPretty>[2]} [options] Optionally pass an initial context object through the render path.
|
|
100
|
-
*/
|
|
101
|
-
export function shallowRender(vnode, context, options) {
|
|
102
|
-
const opts = Object.assign({}, SHALLOW, options || {});
|
|
103
|
-
return renderToStringPretty(vnode, context, opts);
|
|
104
|
-
}
|
|
1
|
+
import './polyfills';
|
|
2
|
+
import renderToString from './pretty';
|
|
3
|
+
import { indent, encodeEntities } from './util';
|
|
4
|
+
import prettyFormat from 'pretty-format';
|
|
5
|
+
|
|
6
|
+
/** @typedef {import('preact').VNode} VNode */
|
|
7
|
+
|
|
8
|
+
// we have to patch in Array support, Possible issue in npm.im/pretty-format
|
|
9
|
+
let preactPlugin = {
|
|
10
|
+
test(object) {
|
|
11
|
+
return (
|
|
12
|
+
object &&
|
|
13
|
+
typeof object === 'object' &&
|
|
14
|
+
'type' in object &&
|
|
15
|
+
'props' in object &&
|
|
16
|
+
'key' in object
|
|
17
|
+
);
|
|
18
|
+
},
|
|
19
|
+
print(val, print, indent) {
|
|
20
|
+
return renderToString(val, preactPlugin.context, preactPlugin.opts, true);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
let prettyFormatOpts = {
|
|
25
|
+
plugins: [preactPlugin]
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function attributeHook(name, value, context, opts, isComponent) {
|
|
29
|
+
let type = typeof value;
|
|
30
|
+
|
|
31
|
+
// Use render-to-string's built-in handling for these properties
|
|
32
|
+
if (name === 'dangerouslySetInnerHTML') return false;
|
|
33
|
+
|
|
34
|
+
// always skip null & undefined values, skip false DOM attributes, skip functions if told to
|
|
35
|
+
if (value == null || (type === 'function' && !opts.functions)) return '';
|
|
36
|
+
|
|
37
|
+
if (
|
|
38
|
+
opts.skipFalseAttributes &&
|
|
39
|
+
!isComponent &&
|
|
40
|
+
(value === false ||
|
|
41
|
+
((name === 'class' || name === 'style') && value === ''))
|
|
42
|
+
)
|
|
43
|
+
return '';
|
|
44
|
+
|
|
45
|
+
let indentChar = typeof opts.pretty === 'string' ? opts.pretty : '\t';
|
|
46
|
+
if (type !== 'string') {
|
|
47
|
+
if (type === 'function' && !opts.functionNames) {
|
|
48
|
+
value = 'Function';
|
|
49
|
+
} else {
|
|
50
|
+
preactPlugin.context = context;
|
|
51
|
+
preactPlugin.opts = opts;
|
|
52
|
+
value = prettyFormat(value, prettyFormatOpts);
|
|
53
|
+
if (~value.indexOf('\n')) {
|
|
54
|
+
value = `${indent('\n' + value, indentChar)}\n`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return indent(`\n${name}={${value}}`, indentChar);
|
|
58
|
+
}
|
|
59
|
+
return `\n${indentChar}${name}="${encodeEntities(value)}"`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let defaultOpts = {
|
|
63
|
+
attributeHook,
|
|
64
|
+
jsx: true,
|
|
65
|
+
xml: false,
|
|
66
|
+
functions: true,
|
|
67
|
+
functionNames: true,
|
|
68
|
+
skipFalseAttributes: true,
|
|
69
|
+
pretty: ' '
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Render Preact JSX + Components to a pretty-printed HTML-like string.
|
|
74
|
+
* @param {VNode} vnode JSX Element / VNode to render
|
|
75
|
+
* @param {Object} [context={}] Initial root context object
|
|
76
|
+
* @param {Object} [options={}] Rendering options
|
|
77
|
+
* @param {Boolean} [options.jsx=true] Generate JSX/XML output instead of HTML
|
|
78
|
+
* @param {Boolean} [options.xml=false] Use self-closing tags for elements without children
|
|
79
|
+
* @param {Boolean} [options.shallow=false] Serialize nested Components (`<Foo a="b" />`) instead of rendering
|
|
80
|
+
* @param {Boolean} [options.pretty=false] Add whitespace for readability
|
|
81
|
+
* @param {RegExp|undefined} [options.voidElements] RegeEx to define which element types are self-closing
|
|
82
|
+
* @returns {String} a pretty-printed HTML-like string
|
|
83
|
+
*/
|
|
84
|
+
export default function renderToStringPretty(vnode, context, options) {
|
|
85
|
+
const opts = Object.assign({}, defaultOpts, options || {});
|
|
86
|
+
if (!opts.jsx) opts.attributeHook = null;
|
|
87
|
+
return renderToString(vnode, context, opts);
|
|
88
|
+
}
|
|
89
|
+
export { renderToStringPretty as render };
|
|
90
|
+
|
|
91
|
+
const SHALLOW = { shallow: true };
|
|
92
|
+
|
|
93
|
+
/** Only render elements, leaving Components inline as `<ComponentName ... />`.
|
|
94
|
+
* This method is just a convenience alias for `render(vnode, context, { shallow:true })`
|
|
95
|
+
* @name shallow
|
|
96
|
+
* @function
|
|
97
|
+
* @param {VNode} vnode JSX VNode to render.
|
|
98
|
+
* @param {Object} [context={}] Optionally pass an initial context object through the render path.
|
|
99
|
+
* @param {Parameters<typeof renderToStringPretty>[2]} [options] Optionally pass an initial context object through the render path.
|
|
100
|
+
*/
|
|
101
|
+
export function shallowRender(vnode, context, options) {
|
|
102
|
+
const opts = Object.assign({}, SHALLOW, options || {});
|
|
103
|
+
return renderToStringPretty(vnode, context, opts);
|
|
104
|
+
}
|
package/src/polyfills.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
if (typeof Symbol !== 'function') {
|
|
2
|
-
let c = 0;
|
|
3
|
-
// eslint-disable-next-line
|
|
4
|
-
Symbol = function (s) {
|
|
5
|
-
return `@@${s}${++c}`;
|
|
6
|
-
};
|
|
7
|
-
Symbol.for = (s) => `@@${s}`;
|
|
8
|
-
}
|
|
1
|
+
if (typeof Symbol !== 'function') {
|
|
2
|
+
let c = 0;
|
|
3
|
+
// eslint-disable-next-line
|
|
4
|
+
Symbol = function (s) {
|
|
5
|
+
return `@@${s}${++c}`;
|
|
6
|
+
};
|
|
7
|
+
Symbol.for = (s) => `@@${s}`;
|
|
8
|
+
}
|