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

WICKET-6890 Introduce strategy for class name debug output #781

Merged
merged 6 commits into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -21,15 +21,16 @@

import org.apache.wicket.MockPanelWithLink;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.settings.DebugSettings;
import org.apache.wicket.util.tester.WicketTestCase;
import org.junit.jupiter.api.Test;

class OutputMarkupContainerClassNameBehaviorTest extends WicketTestCase {

@Test
void whenDebugIsEnabled_thenRenderAttribute()
void whenDebugIsEnabledWithAttribute_thenRenderAttribute()
{
tester.getApplication().getDebugSettings().setOutputMarkupContainerClassName(true);
tester.getApplication().getDebugSettings().setOutputMarkupContainerClassNameStrategy(DebugSettings.ClassOutputStrategy.TAG_ATTRIBUTE);

MockPanelWithLink component = new MockPanelWithLink("test") {
@Override
Expand All @@ -43,9 +44,25 @@ protected void onLinkClick(AjaxRequestTarget target) {
}

@Test
void whenDebugIsDisabled_thenDontRenderAttribute()
void whenDebugIsEnabledWithComment_thenRenderComment()
{
tester.getApplication().getDebugSettings().setOutputMarkupContainerClassName(false);
tester.getApplication().getDebugSettings().setOutputMarkupContainerClassNameStrategy(DebugSettings.ClassOutputStrategy.HTML_COMMENT);

MockPanelWithLink component = new MockPanelWithLink("test") {
@Override
protected void onLinkClick(AjaxRequestTarget target) {

}
};
tester.startComponentInPage(component);

assertTrue(tester.getLastResponseAsString().contains("<!-- MARKUP FOR org.apache.wicket.MockPanelWithLink BEGIN -->"));
}

@Test
void whenDebugIsDisabled_thenDontRenderAttributeOrComment()
{
tester.getApplication().getDebugSettings().setOutputMarkupContainerClassNameStrategy(DebugSettings.ClassOutputStrategy.NONE);

MockPanelWithLink component = new MockPanelWithLink("test") {
@Override
Expand All @@ -56,5 +73,6 @@ protected void onLinkClick(AjaxRequestTarget target) {
tester.startComponentInPage(component);

assertFalse(tester.getLastResponseAsString().contains("<wicket:panel wicket:className=\"org.apache.wicket.MockPanelWithLink\">"));
assertFalse(tester.getLastResponseAsString().contains("<!-- MARKUP FOR org.apache.wicket.MockPanelWithLink BEGIN -->"));
}
}
23 changes: 20 additions & 3 deletions wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
Expand Up @@ -787,18 +787,35 @@ public final void renderAssociatedMarkup(final String openTagName)
try
{
setIgnoreAttributeModifier(true);
final boolean outputClassName = getApplication().getDebugSettings()
.isOutputMarkupContainerClassName();
if (outputClassName)

final DebugSettings.ClassOutputStrategy outputClassName = getApplication().getDebugSettings()
.getOutputMarkupContainerClassNameStrategy();
if (outputClassName == DebugSettings.ClassOutputStrategy.TAG_ATTRIBUTE)
{
associatedMarkupOpenTag.addBehavior(OutputMarkupContainerClassNameBehavior.INSTANCE);
}

renderComponentTag(associatedMarkupOpenTag);
associatedMarkupStream.next();

String className = null;
if (outputClassName == DebugSettings.ClassOutputStrategy.HTML_COMMENT)
{
className = Classes.name(getClass());
getResponse().write("<!-- MARKUP FOR ");
getResponse().write(className);
getResponse().write(" BEGIN -->");
}

renderComponentTagBody(associatedMarkupStream, associatedMarkupOpenTag);

if (outputClassName == DebugSettings.ClassOutputStrategy.HTML_COMMENT)
{
getResponse().write("<!-- MARKUP FOR ");
getResponse().write(className);
getResponse().write(" END -->");
}

renderClosingComponentTag(associatedMarkupStream, associatedMarkupOpenTag, false);
}
finally
Expand Down
2 changes: 1 addition & 1 deletion wicket-core/src/main/java/org/apache/wicket/Page.java
Expand Up @@ -829,7 +829,7 @@ public void component(final Component component, final IVisit<Void> visit)

if (getApplication().getDebugSettings().isOutputMarkupContainerClassName())
{
String className = Classes.name(getClass());
final String className = Classes.name(getClass());
getResponse().write("<!-- Page Class ");
getResponse().write(className);
getResponse().write(" END -->\n");
Expand Down
Expand Up @@ -28,9 +28,9 @@
* the markup element of {@link ComponentTag} with value the fully
* qualified class name of the markup container.</p>
*
* <p>It is used internally by Wicket when {@link DebugSettings#isOutputMarkupContainerClassName()} is active.</p>
* <p>It is used internally by Wicket when {@link org.apache.wicket.settings.DebugSettings.ClassOutputStrategy#TAG_ATTRIBUTE} is active.</p>
*
* @see DebugSettings#setOutputMarkupContainerClassName(boolean)
* @see DebugSettings#setOutputMarkupContainerClassNameStrategy(DebugSettings.ClassOutputStrategy)
*/
public class OutputMarkupContainerClassNameBehavior extends Behavior {

Expand Down
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.wicket.settings;

import org.apache.wicket.util.lang.Args;

/**
* Settings class for various debug settings
* <p>
Expand Down Expand Up @@ -54,7 +56,7 @@ public class DebugSettings
/**
* Whether the container's class name should be printed to response (in a html comment).
*/
private boolean outputMarkupContainerClassName = false;
private ClassOutputStrategy outputMarkupContainerClassNameStrategy = ClassOutputStrategy.NONE;

private String componentPathAttributeName = null;

Expand Down Expand Up @@ -104,14 +106,26 @@ public boolean isLinePreciseReportingOnNewComponentEnabled()
}

/**
* Returns whether the output of markup container's should be wrapped by comments containing the
* container's class name.
* Returns whether the output of markup container's should contain the
* container's Java class name.
*
* @return true if the markup container's class name should be written to response
* @return true if the markup container's Java class name should be written to response
* @deprecated use {@link #getOutputMarkupContainerClassNameStrategy()} instead
*/
@Deprecated(forRemoval = true)
public boolean isOutputMarkupContainerClassName()
{
return outputMarkupContainerClassName;
return outputMarkupContainerClassNameStrategy != ClassOutputStrategy.NONE;
}

/**
* Returns the strategy for outputting the Java class name of a markup container
*
* @return the strategy for outputting the Java class name of a markup container
*/
public ClassOutputStrategy getOutputMarkupContainerClassNameStrategy()
{
return outputMarkupContainerClassNameStrategy;
}

/**
Expand Down Expand Up @@ -173,13 +187,39 @@ public DebugSettings setLinePreciseReportingOnNewComponentEnabled(boolean enable
*
* @param enable
* @return {@code this} object for chaining
* @deprecated use {@link #setOutputMarkupContainerClassNameStrategy(ClassOutputStrategy)} instead
*/
@Deprecated(forRemoval = true)
public DebugSettings setOutputMarkupContainerClassName(boolean enable)
{
outputMarkupContainerClassName = enable;
outputMarkupContainerClassNameStrategy = enable ? ClassOutputStrategy.HTML_COMMENT : ClassOutputStrategy.NONE;
return this;
}

/**
* Sets the strategy for outputting the Java class name of a markup container in the HTML output.
*
* @param strategy
* @return {@code this} object for chaining
*/
public DebugSettings setOutputMarkupContainerClassNameStrategy(ClassOutputStrategy strategy)
{
outputMarkupContainerClassNameStrategy = Args.notNull(strategy, "strategy");
return this;
}

/**
* Sets the strategy for outputting the Java class name of a markup container in the HTML output.
*
* @param strategyName the enum name of the {@link ClassOutputStrategy} to use
* @return {@code this} object for chaining
*/
public DebugSettings setOutputMarkupContainerClassNameStrategy(String strategyName)
{
final ClassOutputStrategy strategy = Enum.valueOf(ClassOutputStrategy.class, strategyName);
return setOutputMarkupContainerClassNameStrategy(strategy);
}

/**
* If the parameter value is non-empty then Wicket will use it as the name of an attribute of the
* component tag to print the {@link org.apache.wicket.Component}'s path.
Expand Down Expand Up @@ -233,4 +273,23 @@ public boolean isDevelopmentUtilitiesEnabled()
{
return developmentUtilitiesEnabled;
}

/**
* Strategy for outputting the Java class name of a markup container
*/
public enum ClassOutputStrategy
{
/**
* Output the container's class name in an HTML comment
*/
HTML_COMMENT,
/**
* Output the container's class name in a tag attribute
*/
TAG_ATTRIBUTE,
/**
* Do not output the container's class name
*/
NONE
}
}
Expand Up @@ -72,14 +72,22 @@ public interface DebugSettingsMBean
* class name. (Useful for determining which part of page belongs to which markup file).
*
* @param enable
* @deprecated use {@link #setOutputMarkupContainerClassNameStrategy(String)} instead
*/
void setOutputMarkupContainerClassName(boolean enable);

/**
* Returns whether the output of markup container's should be wrapped by comments containing the
* container's class name.
* Sets the strategy for outputting the Java class name of a markup container in the HTML output.
* (Useful for determining which part of page belongs to which markup file).
*
* @param strategyName the enum name of the class output strategy to use
*/
void setOutputMarkupContainerClassNameStrategy(String strategyName);

/**
* Returns whether the markup container's Java class name should be written to the response.
*
* @return true if the markup container's class name should be written to response
* @return true if the markup container's Java class name should be written to response
*/
boolean isOutputMarkupContainerClassName();

Expand Down
Expand Up @@ -86,6 +86,12 @@ public void setOutputMarkupContainerClassName(final boolean enable)
application.getDebugSettings().setOutputMarkupContainerClassName(enable);
}

@Override
public void setOutputMarkupContainerClassNameStrategy(final String strategyName) {
application.getDebugSettings().setOutputMarkupContainerClassNameStrategy(strategyName);

}

@Override
public boolean isOutputMarkupContainerClassName()
{
Expand Down