Skip to content

Commit f349943

Browse files
committed
Add support for disabling function and/or function name comparison in JSX mode
1 parent 49abb38 commit f349943

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ let shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW);
5050

5151
/** The default export is an alias of `render()`. */
5252
export default function renderToString(vnode, context, opts, inner) {
53-
let { nodeName, attributes, children } = vnode || EMPTY;
53+
let { nodeName, attributes, children } = vnode || EMPTY,
54+
isComponent = false;
5455
context = context || {};
5556
opts = opts || {};
5657

@@ -68,6 +69,7 @@ export default function renderToString(vnode, context, opts, inner) {
6869

6970
// components
7071
if (typeof nodeName==='function') {
72+
isComponent = true;
7173
if (opts.shallow && (inner || opts.renderRootComponent===false)) {
7274
nodeName = getComponentName(nodeName);
7375
}
@@ -111,7 +113,7 @@ export default function renderToString(vnode, context, opts, inner) {
111113
if (name==='children') continue;
112114
if (!(opts && opts.allAttributes) && (name==='key' || name==='ref')) continue;
113115

114-
let hooked = opts.attributeHook && opts.attributeHook(name, v, context, opts);
116+
let hooked = opts.attributeHook && opts.attributeHook(name, v, context, opts, isComponent);
115117
if (hooked || hooked==='') {
116118
s += hooked;
117119
continue;

src/jsx.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,24 @@ let prettyFormatOpts = {
2020
};
2121

2222

23-
function attributeHook(name, value, context, opts) {
24-
if (value==null) return '';
23+
function attributeHook(name, value, context, opts, isComponent) {
24+
let type = typeof value;
25+
26+
// always skip null & undefined values, skip false DOM attributes, skip functions if told to
27+
if (value==null || (!isComponent && value===false) || (type==='function' && !opts.functions)) return '';
2528

2629
let indentChar = typeof opts.pretty==='string' ? opts.pretty : '\t';
27-
if (typeof value!=='string') {
28-
preactPlugin.context = context;
29-
preactPlugin.opts = opts;
30-
value = prettyFormat(value, prettyFormatOpts);
31-
if (~value.indexOf('\n')) {
32-
value = `${indent('\n'+value, indentChar)}\n`;
30+
if (type!=='string') {
31+
if (type==='function' && !opts.functionNames) {
32+
value = 'Function';
33+
}
34+
else {
35+
preactPlugin.context = context;
36+
preactPlugin.opts = opts;
37+
value = prettyFormat(value, prettyFormatOpts);
38+
if (~value.indexOf('\n')) {
39+
value = `${indent('\n'+value, indentChar)}\n`;
40+
}
3341
}
3442
return indent(`\n${name}={${value}}`, indentChar);
3543
}
@@ -41,6 +49,8 @@ let defaultOpts = {
4149
attributeHook,
4250
jsx: true,
4351
xml: false,
52+
functions: true,
53+
functionNames: true,
4454
pretty: ' '
4555
};
4656

0 commit comments

Comments
 (0)