This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git


The following commit(s) were added to refs/heads/master by this push:
     new 8bbf158  WICKET-6890 Render debug setting 
'outputMarkupContainerClassName` as an attribute
8bbf158 is described below

commit 8bbf158ebc4d12f3b317c0d607200441c0767162
Author: Martin Tzvetanov Grigorov <[email protected]>
AuthorDate: Fri May 28 12:16:21 2021 +0300

    WICKET-6890 Render debug setting 'outputMarkupContainerClassName` as an 
attribute
---
 .../src/main/java/org/apache/wicket/Component.java |  1 +
 .../java/org/apache/wicket/MarkupContainer.java    | 23 +++-----
 .../OutputMarkupContainerClassNameBehavior.java    | 61 ++++++++++++++++++++++
 ...OutputMarkupContainerClassNameBehaviorTest.java | 60 +++++++++++++++++++++
 4 files changed, 128 insertions(+), 17 deletions(-)

diff --git a/wicket-core/src/main/java/org/apache/wicket/Component.java 
b/wicket-core/src/main/java/org/apache/wicket/Component.java
index e1f522e..5456609 100644
--- a/wicket-core/src/main/java/org/apache/wicket/Component.java
+++ b/wicket-core/src/main/java/org/apache/wicket/Component.java
@@ -3933,6 +3933,7 @@ public abstract class Component
                        // apply behaviors that are attached to the component 
tag.
                        if (tag.hasBehaviors())
                        {
+                               tag = tag.mutable();
                                Iterator<? extends Behavior> tagBehaviors = 
tag.getBehaviors();
                                while (tagBehaviors.hasNext())
                                {
diff --git a/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java 
b/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
index d2531a5..3cb4ce9 100644
--- a/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
+++ b/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
@@ -32,6 +32,7 @@ import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
 
 import org.apache.commons.collections4.map.LinkedMap;
+import org.apache.wicket.behavior.OutputMarkupContainerClassNameBehavior;
 import org.apache.wicket.core.util.string.ComponentStrings;
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.markup.ComponentTag.IAutoComponentFactory;
@@ -786,29 +787,17 @@ public abstract class MarkupContainer extends Component 
implements Iterable<Comp
                try
                {
                        setIgnoreAttributeModifier(true);
-                       renderComponentTag(associatedMarkupOpenTag);
-                       associatedMarkupStream.next();
-
-                       String className = null;
-
                        final boolean outputClassName = 
getApplication().getDebugSettings()
-                               .isOutputMarkupContainerClassName();
+                                       .isOutputMarkupContainerClassName();
                        if (outputClassName)
                        {
-                               className = Classes.name(getClass());
-                               getResponse().write("<!-- MARKUP FOR ");
-                               getResponse().write(className);
-                               getResponse().write(" BEGIN -->");
+                               
associatedMarkupOpenTag.addBehavior(OutputMarkupContainerClassNameBehavior.INSTANCE);
                        }
 
-                       renderComponentTagBody(associatedMarkupStream, 
associatedMarkupOpenTag);
+                       renderComponentTag(associatedMarkupOpenTag);
+                       associatedMarkupStream.next();
 
-                       if (outputClassName)
-                       {
-                               getResponse().write("<!-- MARKUP FOR ");
-                               getResponse().write(className);
-                               getResponse().write(" END -->");
-                       }
+                       renderComponentTagBody(associatedMarkupStream, 
associatedMarkupOpenTag);
 
                        renderClosingComponentTag(associatedMarkupStream, 
associatedMarkupOpenTag, false);
                }
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/behavior/OutputMarkupContainerClassNameBehavior.java
 
b/wicket-core/src/main/java/org/apache/wicket/behavior/OutputMarkupContainerClassNameBehavior.java
new file mode 100644
index 0000000..8783d01
--- /dev/null
+++ 
b/wicket-core/src/main/java/org/apache/wicket/behavior/OutputMarkupContainerClassNameBehavior.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.behavior;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.MarkupContainer;
+import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.settings.DebugSettings;
+import org.apache.wicket.util.lang.Classes;
+
+/**
+ * <p>A behavior that applies to {@link MarkupContainer}s with associated 
markup.
+ * It adds an attribute named <em>namespace:className</em> to
+ * 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>
+ *
+ * @see DebugSettings#setOutputMarkupContainerClassName(boolean) 
+ */
+public class OutputMarkupContainerClassNameBehavior extends Behavior {
+
+    public static final OutputMarkupContainerClassNameBehavior INSTANCE = new 
OutputMarkupContainerClassNameBehavior();
+
+    private OutputMarkupContainerClassNameBehavior()
+    {}
+
+    @Override
+    public void onComponentTag(Component component, ComponentTag tag) {
+        super.onComponentTag(component, tag);
+
+        if (component instanceof MarkupContainer)
+        {
+            String namespace = tag.getNamespace();
+            if (namespace == null)
+            {
+                namespace = "wicket";
+            }
+            tag.put(namespace + ":className", 
Classes.name(component.getClass()));
+        }
+    }
+
+    @Override
+    public boolean isTemporary(Component component) {
+        return true;
+    }
+}
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/behavior/OutputMarkupContainerClassNameBehaviorTest.java
 
b/wicket-core/src/test/java/org/apache/wicket/behavior/OutputMarkupContainerClassNameBehaviorTest.java
new file mode 100644
index 0000000..77de9af
--- /dev/null
+++ 
b/wicket-core/src/test/java/org/apache/wicket/behavior/OutputMarkupContainerClassNameBehaviorTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.behavior;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.wicket.MockPanelWithLink;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.util.tester.WicketTestCase;
+import org.junit.jupiter.api.Test;
+
+class OutputMarkupContainerClassNameBehaviorTest extends WicketTestCase {
+
+    @Test
+    void whenDebugIsEnabled_thenRenderAttribute()
+    {
+        
tester.getApplication().getDebugSettings().setOutputMarkupContainerClassName(true);
+
+        MockPanelWithLink component = new MockPanelWithLink("test") {
+            @Override
+            protected void onLinkClick(AjaxRequestTarget target) {
+
+            }
+        };
+        tester.startComponentInPage(component);
+
+        assertTrue(tester.getLastResponseAsString().contains("<wicket:panel 
wicket:className=\"org.apache.wicket.MockPanelWithLink\">"));
+    }
+
+    @Test
+    void whenDebugIsDisabled_thenDontRenderAttribute()
+    {
+        
tester.getApplication().getDebugSettings().setOutputMarkupContainerClassName(false);
+
+        MockPanelWithLink component = new MockPanelWithLink("test") {
+            @Override
+            protected void onLinkClick(AjaxRequestTarget target) {
+
+            }
+        };
+        tester.startComponentInPage(component);
+
+        assertFalse(tester.getLastResponseAsString().contains("<wicket:panel 
wicket:className=\"org.apache.wicket.MockPanelWithLink\">"));
+    }
+}

Reply via email to