Skip to content

Commit 0d26557

Browse files
committed
Change the renderReactAttributes parameter to serverOnly. Fix the tests and all the spaces issues
1 parent 6c150ff commit 0d26557

File tree

5 files changed

+71
-71
lines changed

5 files changed

+71
-71
lines changed

src/React.AspNet/HtmlHelperExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* All rights reserved.
44
*
55
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
6+
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

@@ -66,25 +66,25 @@ private static IReactEnvironment Environment
6666
/// <param name="props">Props to initialise the component with</param>
6767
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
6868
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
69-
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
70-
/// <param name="renderReactAttributes">Indicates if the React data-attributes should be rendered during server side rendering</param>
69+
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
70+
/// <param name="serverOnly">Skip rendering React specific data-attributes during server side rendering. Defaults to <c>false</c></param>
7171
/// <returns>The component's HTML</returns>
7272
public static IHtmlString React<T>(
7373
this IHtmlHelper htmlHelper,
7474
string componentName,
7575
T props,
7676
string htmlTag = null,
77-
string containerId = null,
77+
string containerId = null,
7878
bool clientOnly = false,
79-
bool renderReactAttributes = true
79+
bool serverOnly = false
8080
)
8181
{
8282
var reactComponent = Environment.CreateComponent(componentName, props, containerId);
8383
if (!string.IsNullOrEmpty(htmlTag))
8484
{
8585
reactComponent.ContainerTag = htmlTag;
8686
}
87-
var result = reactComponent.RenderHtml(clientOnly, renderReactAttributes);
87+
var result = reactComponent.RenderHtml(clientOnly, serverOnly);
8888
return new HtmlString(result);
8989
}
9090

@@ -99,7 +99,7 @@ public static IHtmlString React<T>(
9999
/// <param name="props">Props to initialise the component with</param>
100100
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
101101
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
102-
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
102+
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
103103
/// <returns>The component's HTML</returns>
104104
public static IHtmlString ReactWithInit<T>(
105105
this IHtmlHelper htmlHelper,
@@ -124,7 +124,7 @@ public static IHtmlString ReactWithInit<T>(
124124
}
125125

126126
/// <summary>
127-
/// Renders the JavaScript required to initialise all components client-side. This will
127+
/// Renders the JavaScript required to initialise all components client-side. This will
128128
/// attach event handlers to the server-rendered HTML.
129129
/// </summary>
130130
/// <returns>JavaScript for all components</returns>

src/React.Core/IReactComponent.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* All rights reserved.
44
*
55
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
6+
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

@@ -39,13 +39,13 @@ public interface IReactComponent
3939
/// return the rendered HTML.
4040
/// </summary>
4141
/// <param name="renderContainerOnly">Only renders component container. Used for client-side only rendering.</param>
42-
/// <param name="renderReactAttributes">Indicates if the React data-attributes should be rendered during server side rendering</param>
42+
/// <param name="renderServerOnly">Only renders the common HTML mark up and not any React specific data attributes. Used for server-side only rendering.</param>
4343
/// <returns>HTML</returns>
44-
string RenderHtml(bool renderContainerOnly = false, bool renderReactAttributes = true);
44+
string RenderHtml(bool renderContainerOnly = false, bool renderServerOnly = false);
4545

4646
/// <summary>
47-
/// Renders the JavaScript required to initialise this component client-side. This will
48-
/// initialise the React component, which includes attach event handlers to the
47+
/// Renders the JavaScript required to initialise this component client-side. This will
48+
/// initialise the React component, which includes attach event handlers to the
4949
/// server-rendered HTML.
5050
/// </summary>
5151
/// <returns>JavaScript</returns>

src/React.Core/ReactComponent.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* All rights reserved.
44
*
55
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
6+
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

@@ -79,20 +79,20 @@ public ReactComponent(IReactEnvironment environment, IReactSiteConfiguration con
7979
/// return the rendered HTML.
8080
/// </summary>
8181
/// <param name="renderContainerOnly">Only renders component container. Used for client-side only rendering.</param>
82-
/// <param name="renderReactAttributes">Indicates if the React data-attributes should be rendered during server side rendering</param>
82+
/// <param name="renderServerOnly">Only renders the common HTML mark up and not any React specific data attributes. Used for server-side only rendering.</param>
8383
/// <returns>HTML</returns>
84-
public virtual string RenderHtml(bool renderContainerOnly = false, bool renderReactAttributes = true)
84+
public virtual string RenderHtml(bool renderContainerOnly = false, bool renderServerOnly = false)
8585
{
8686
EnsureComponentExists();
8787
try
88-
{
89-
var html = string.Empty;
90-
if (!renderContainerOnly)
88+
{
89+
var html = string.Empty;
90+
if (!renderContainerOnly)
9191
{
92-
var reactRenderCommand = renderReactAttributes
93-
? string.Format("React.renderToString({0})", GetComponentInitialiser())
94-
: string.Format("React.renderToStaticMarkup({0})", GetComponentInitialiser());
95-
html = _environment.Execute<string>(reactRenderCommand);
92+
var reactRenderCommand = renderServerOnly
93+
? string.Format("React.renderToStaticMarkup({0})", GetComponentInitialiser())
94+
: string.Format("React.renderToString({0})", GetComponentInitialiser());
95+
html = _environment.Execute<string>(reactRenderCommand);
9696
}
9797
return string.Format(
9898
"<{2} id=\"{0}\">{1}</{2}>",
@@ -113,8 +113,8 @@ public virtual string RenderHtml(bool renderContainerOnly = false, bool renderRe
113113
}
114114

115115
/// <summary>
116-
/// Renders the JavaScript required to initialise this component client-side. This will
117-
/// initialise the React component, which includes attach event handlers to the
116+
/// Renders the JavaScript required to initialise this component client-side. This will
117+
/// initialise the React component, which includes attach event handlers to the
118118
/// server-rendered HTML.
119119
/// </summary>
120120
/// <returns>JavaScript</returns>

src/React.Tests/Core/ReactComponentTest.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* All rights reserved.
44
*
55
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
6+
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

@@ -82,21 +82,21 @@ public void RenderHtmlShouldNotRenderComponentHtml()
8282
environment.Verify(x => x.Execute(It.IsAny<string>()), Times.Never);
8383
}
8484

85-
[Test]
86-
public void RenderHtmlShouldNotRenderReactAttributes()
87-
{
88-
var environment = new Mock<IReactEnvironment>();
89-
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
90-
var config = new Mock<IReactSiteConfiguration>();
91-
92-
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
93-
{
94-
Props = new { hello = "World" }
95-
};
96-
component.RenderHtml(renderReactAttributes: false);
97-
98-
environment.Verify(x => x.Execute<string>(@"React.renderToStaticMarkup(React.createElement(Foo, {""hello"":""World""}))"));
99-
}
85+
[Test]
86+
public void RenderHtmlShouldNotRenderClientSideAttributes()
87+
{
88+
var environment = new Mock<IReactEnvironment>();
89+
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
90+
var config = new Mock<IReactSiteConfiguration>();
91+
92+
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
93+
{
94+
Props = new { hello = "World" }
95+
};
96+
component.RenderHtml(renderServerOnly: true);
97+
98+
environment.Verify(x => x.Execute<string>(@"React.renderToStaticMarkup(React.createElement(Foo, {""hello"":""World""}))"));
99+
}
100100

101101
[Test]
102102
public void RenderHtmlShouldWrapComponentInCustomElement()

src/React.Tests/Mvc/HtmlHelperExtensionsTests.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* All rights reserved.
44
*
55
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
6+
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

@@ -19,7 +19,7 @@ class HtmlHelperExtensionsTests
1919
{
2020
/// <summary>
2121
/// Creates a mock <see cref="IReactEnvironment"/> and registers it with the IoC container
22-
/// This is only required because <see cref="HtmlHelperExtensions"/> can not be
22+
/// This is only required because <see cref="HtmlHelperExtensions"/> can not be
2323
/// injected :(
2424
/// </summary>
2525
private Mock<IReactEnvironment> ConfigureMockEnvironment()
@@ -33,7 +33,7 @@ private Mock<IReactEnvironment> ConfigureMockEnvironment()
3333
public void ReactWithInitShouldReturnHtmlAndScript()
3434
{
3535
var component = new Mock<IReactComponent>();
36-
component.Setup(x => x.RenderHtml(false, true)).Returns("HTML");
36+
component.Setup(x => x.RenderHtml(false, false)).Returns("HTML");
3737
component.Setup(x => x.RenderJavaScript()).Returns("JS");
3838
var environment = ConfigureMockEnvironment();
3939
environment.Setup(x => x.CreateComponent(
@@ -44,12 +44,12 @@ public void ReactWithInitShouldReturnHtmlAndScript()
4444

4545
var result = HtmlHelperExtensions.ReactWithInit(
4646
htmlHelper: null,
47-
componentName: "ComponentName",
48-
props: new { },
47+
componentName: "ComponentName",
48+
props: new { },
4949
htmlTag: "span"
5050
);
5151
Assert.AreEqual(
52-
"HTML" + System.Environment.NewLine + "<script>JS</script>",
52+
"HTML" + System.Environment.NewLine + "<script>JS</script>",
5353
result.ToString()
5454
);
5555

@@ -69,35 +69,35 @@ public void ReactWithClientOnlyTrueShouldCallRenderHtmlWithTrue()
6969

7070
var result = HtmlHelperExtensions.React(
7171
htmlHelper: null,
72-
componentName: "ComponentName",
73-
props: new { },
72+
componentName: "ComponentName",
73+
props: new { },
7474
htmlTag: "span",
7575
clientOnly: true,
76-
renderReactAttributes: true
76+
serverOnly: true
7777
);
78-
component.Verify(x => x.RenderHtml(It.Is<bool>(y => y == true), It.Is<bool>(z => z == true)), Times.Once);
78+
component.Verify(x => x.RenderHtml(It.Is<bool>(y => y == true), It.Is<bool>(z => z == true)), Times.Once);
7979
}
8080

81-
[Test]
82-
public void ReactWithRenderReactAttributesfalseShouldCallRenderHtmlWithfalse() {
83-
var component = new Mock<IReactComponent>();
84-
component.Setup(x => x.RenderHtml(true, false)).Returns("HTML");
85-
var environment = ConfigureMockEnvironment();
86-
environment.Setup(x => x.CreateComponent(
87-
"ComponentName",
88-
new { },
89-
null
90-
)).Returns(component.Object);
81+
[Test]
82+
public void ReactWithServerOnlyTrueShouldCallRenderHtmlWithTrue() {
83+
var component = new Mock<IReactComponent>();
84+
component.Setup(x => x.RenderHtml(true, true)).Returns("HTML");
85+
var environment = ConfigureMockEnvironment();
86+
environment.Setup(x => x.CreateComponent(
87+
"ComponentName",
88+
new { },
89+
null
90+
)).Returns(component.Object);
9191

92-
var result = HtmlHelperExtensions.React(
93-
htmlHelper: null,
94-
componentName: "ComponentName",
95-
props: new { },
96-
htmlTag: "span",
97-
clientOnly: true,
98-
renderReactAttributes: false
99-
);
100-
component.Verify(x => x.RenderHtml(It.Is<bool>(y => y == true), It.Is<bool>(z => z == false)), Times.Once);
101-
}
92+
var result = HtmlHelperExtensions.React(
93+
htmlHelper: null,
94+
componentName: "ComponentName",
95+
props: new { },
96+
htmlTag: "span",
97+
clientOnly: true,
98+
serverOnly: true
99+
);
100+
component.Verify(x => x.RenderHtml(It.Is<bool>(y => y == true), It.Is<bool>(z => z == true)), Times.Once);
101+
}
102102
}
103103
}

0 commit comments

Comments
 (0)