You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by th...@apache.org on 2022/04/28 09:01:20 UTC

[wicket] branch master updated: WICKET-6963 Singleton markup sourcing strategy (Take 2) (#517)

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

theigl 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 76685914cd WICKET-6963 Singleton markup sourcing strategy (Take 2) (#517)
76685914cd is described below

commit 76685914cdad327afa41ecd7c2d3820297c1c092
Author: Thomas Heigl <th...@gmail.com>
AuthorDate: Thu Apr 28 11:01:16 2022 +0200

    WICKET-6963 Singleton markup sourcing strategy (Take 2) (#517)
    
    * Revert "Revert "WICKET-6963 Use singletons for PanelMarkupSourcingStrategy (#503)""
    
    This reverts commit 85fab8969a694b46c5b5ea59a37e21f4e0317a00.
    
    * WICKET-6963 Remove `noMoreWicketHeadTagsAllowed` from AssociatedMarkupSourcingStrategy
---
 .../wicket/markup/html/border/BorderPanel.java     |  2 +-
 .../markup/html/form/FormComponentPanel.java       |  2 +-
 .../panel/AssociatedMarkupSourcingStrategy.java    | 49 +++-------------------
 .../org/apache/wicket/markup/html/panel/Panel.java |  2 +-
 .../html/panel/PanelMarkupSourcingStrategy.java    | 19 +++++++++
 .../markup/parser/filter/HeaderSectionTest.java    | 29 ++++++-------
 6 files changed, 41 insertions(+), 62 deletions(-)

diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/border/BorderPanel.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/border/BorderPanel.java
index c194333c78..997372affb 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/border/BorderPanel.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/border/BorderPanel.java
@@ -92,7 +92,7 @@ public abstract class BorderPanel extends Panel
 	@Override
 	protected IMarkupSourcingStrategy newMarkupSourcingStrategy()
 	{
-		return new PanelMarkupSourcingStrategy(true);
+		return PanelMarkupSourcingStrategy.get(true);
 	}
 
 	/**
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponentPanel.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponentPanel.java
index 5fe284fa78..50c4846376 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponentPanel.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponentPanel.java
@@ -146,7 +146,7 @@ public abstract class FormComponentPanel<T> extends FormComponent<T> implements
 	@Override
 	protected IMarkupSourcingStrategy newMarkupSourcingStrategy()
 	{
-		return new PanelMarkupSourcingStrategy(false);
+		return PanelMarkupSourcingStrategy.get(false);
 	}
 
 	@Override
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/AssociatedMarkupSourcingStrategy.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/AssociatedMarkupSourcingStrategy.java
index 58d70135d1..618afc8f07 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/AssociatedMarkupSourcingStrategy.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/AssociatedMarkupSourcingStrategy.java
@@ -22,7 +22,6 @@ import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.markup.IMarkupFragment;
 import org.apache.wicket.markup.MarkupElement;
-import org.apache.wicket.markup.MarkupException;
 import org.apache.wicket.markup.MarkupNotFoundException;
 import org.apache.wicket.markup.MarkupStream;
 import org.apache.wicket.markup.TagUtils;
@@ -42,9 +41,6 @@ import org.apache.wicket.util.lang.Classes;
  */
 public abstract class AssociatedMarkupSourcingStrategy extends AbstractMarkupSourcingStrategy
 {
-	/** <wicket:head> is only allowed before <body>, </head>, <wicket:panel> etc. */
-	private boolean noMoreWicketHeadTagsAllowed = false;
-
 	private final String tagName;
 
 	/**
@@ -95,7 +91,7 @@ public abstract class AssociatedMarkupSourcingStrategy extends AbstractMarkupSou
 		if (associatedMarkup == null)
 		{
 			throw new MarkupNotFoundException("Failed to find markup file associated. " +
-				Classes.simpleName(parent.getClass()) + ": " + parent.toString());
+				Classes.simpleName(parent.getClass()) + ": " + parent);
 		}
 
 		// Find <wicket:panel>
@@ -103,7 +99,7 @@ public abstract class AssociatedMarkupSourcingStrategy extends AbstractMarkupSou
 		if (markup == null)
 		{
 			throw new MarkupNotFoundException("Expected to find <wicket:" + tagName +
-				"> in associated markup file. Markup: " + associatedMarkup.toString());
+				"> in associated markup file. Markup: " + associatedMarkup);
 		}
 		
 		// If child == null, than return the markup fragment starting with <wicket:panel>
@@ -167,13 +163,13 @@ public abstract class AssociatedMarkupSourcingStrategy extends AbstractMarkupSou
 			{
 				if (tag.getMarkupClass() == null)
 				{
-					// find() can still fail an return null => continue the search
+					// find() can still fail and return null => continue the search
 					childMarkup = stream.getMarkupFragment().find(child.getId());
 				}
 			}
 			else if (TagUtils.isHeadTag(tag))
 			{
-				// find() can still fail an return null => continue the search
+				// find() can still fail and return null => continue the search
 				childMarkup = stream.getMarkupFragment().find(child.getId());
 			}
 
@@ -219,9 +215,6 @@ public abstract class AssociatedMarkupSourcingStrategy extends AbstractMarkupSou
 	public final void renderHeadFromAssociatedMarkupFile(final WebMarkupContainer container,
 		final HtmlHeaderContainer htmlContainer)
 	{
-		// reset for each render in case the strategy is re-used
-		noMoreWicketHeadTagsAllowed = false;
-
 		// Gracefully getAssociateMarkupStream. Throws no exception in case
 		// markup is not found
 		final MarkupStream markupStream = container.getAssociatedMarkupStream(false);
@@ -231,7 +224,6 @@ public abstract class AssociatedMarkupSourcingStrategy extends AbstractMarkupSou
 		}
 
 		// Position pointer at current (first) header
-		noMoreWicketHeadTagsAllowed = false;
 		while (nextHeaderMarkup(markupStream) != -1)
 		{
 			// found <wicket:head>
@@ -301,15 +293,14 @@ public abstract class AssociatedMarkupSourcingStrategy extends AbstractMarkupSou
 		if (element instanceof WicketTag)
 		{
 			final WicketTag wTag = (WicketTag)element;
-			if ((wTag.isHeadTag() == true) && (wTag.getNamespace() != null))
+			if ((wTag.isHeadTag()) && (wTag.getNamespace() != null))
 			{
 				// Create the header container and associate the markup with it
 				return new HeaderPartContainer(id, container, markup);
 			}
 		}
 
-		throw new WicketRuntimeException("Programming error: expected a WicketTag: " +
-			markup.toString());
+		throw new WicketRuntimeException("Programming error: expected a WicketTag: " + markup);
 	}
 
 	/**
@@ -335,36 +326,8 @@ public abstract class AssociatedMarkupSourcingStrategy extends AbstractMarkupSou
 				WicketTag tag = (WicketTag)elem;
 				if (tag.isOpen() && tag.isHeadTag())
 				{
-					if (noMoreWicketHeadTagsAllowed == true)
-					{
-						throw new MarkupException(
-							"<wicket:head> tags are only allowed before <body>, </head>, <wicket:panel> etc. tag");
-					}
 					return associatedMarkupStream.getCurrentIndex();
 				}
-				// wicket:head must be before border, panel or extend
-				// @TODO why is that? Why can't it be anywhere? (except inside wicket:fragment)
-				else if (tag.isOpen() &&
-					(tag.isPanelTag() || tag.isBorderTag() || tag.isExtendTag()))
-				{
-					noMoreWicketHeadTagsAllowed = true;
-				}
-			}
-			else if (elem instanceof ComponentTag)
-			{
-				ComponentTag tag = (ComponentTag)elem;
-				// wicket:head must be before </head>
-				// @TODO why??
-				if (tag.isClose() && TagUtils.isHeadTag(tag))
-				{
-					noMoreWicketHeadTagsAllowed = true;
-				}
-				// wicket:head must be before <body>
-				// @TODO why??
-				else if (tag.isOpen() && TagUtils.isBodyTag(tag))
-				{
-					noMoreWicketHeadTagsAllowed = true;
-				}
 			}
 			elem = associatedMarkupStream.next();
 		}
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/Panel.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/Panel.java
index 1520c07dfe..ba3d9d9945 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/Panel.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/Panel.java
@@ -81,7 +81,7 @@ public abstract class Panel extends WebMarkupContainer implements IQueueRegion
 	@Override
 	protected IMarkupSourcingStrategy newMarkupSourcingStrategy()
 	{
-		return new PanelMarkupSourcingStrategy(false);
+		return PanelMarkupSourcingStrategy.get(false);
 	}
 	
 	/**
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/PanelMarkupSourcingStrategy.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/PanelMarkupSourcingStrategy.java
index 50c80c79b6..fe0418f1f2 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/PanelMarkupSourcingStrategy.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/PanelMarkupSourcingStrategy.java
@@ -33,9 +33,28 @@ import org.apache.wicket.markup.MarkupStream;
  */
 public class PanelMarkupSourcingStrategy extends AssociatedMarkupSourcingStrategy
 {
+	private static final PanelMarkupSourcingStrategy PANEL_INSTANCE = new PanelMarkupSourcingStrategy(
+		false);
+	private static final PanelMarkupSourcingStrategy BORDER_INSTANCE = new PanelMarkupSourcingStrategy(
+		true);
+
 	// False for Panel and true for Border components.
 	private final boolean allowWicketComponentsInBodyMarkup;
 
+	/**
+	 * @param allowWicketComponentsInBodyMarkup
+	 *            {@code false} for Panel and {@code true} for Border components. If Panel then the
+	 *            body markup should only contain raw markup, which is ignored (removed), but no
+	 *            Wicket Component. With Border components, the body markup will be associated with
+	 *            the Body Component.
+	 * 
+	 * @return A singleton of the strategy
+	 */
+	public static PanelMarkupSourcingStrategy get(final boolean allowWicketComponentsInBodyMarkup)
+	{
+		return allowWicketComponentsInBodyMarkup ? BORDER_INSTANCE : PANEL_INSTANCE;
+	}
+
 	/**
 	 * Constructor.
 	 * 
diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/parser/filter/HeaderSectionTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/parser/filter/HeaderSectionTest.java
index d8225c2006..b8f610bb27 100644
--- a/wicket-core/src/test/java/org/apache/wicket/markup/parser/filter/HeaderSectionTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/markup/parser/filter/HeaderSectionTest.java
@@ -16,10 +16,10 @@
  */
 package org.apache.wicket.markup.parser.filter;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
-import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.markup.MarkupException;
 import org.apache.wicket.util.tester.WicketTestCase;
 import org.junit.jupiter.api.Test;
@@ -140,22 +140,16 @@ class HeaderSectionTest extends WicketTestCase
 		executeTest(HeaderSectionPage_11.class, "HeaderSectionPageExpectedResult_11.html");
 	}
 
-	/**
-	 * @throws Exception
-	 */
 	@Test
-	void renderHomePage_13() throws Exception
+	void renderHomePage_13()
 	{
-		boolean hit = false;
-		try
-		{
-			executeTest(HeaderSectionPage_13.class, "HeaderSectionPageExpectedResult_13.html");
-		}
-		catch (WicketRuntimeException ex)
-		{
-			hit = true;
-		}
-		assertTrue(hit, "Expected a MarkupException to be thrown");
+		final MarkupException markupException = assertThrows(MarkupException.class,
+			() -> executeTest(HeaderSectionPage_13.class,
+				"HeaderSectionPageExpectedResult_13.html"));
+
+		assertTrue(markupException.getMessage()
+			.startsWith(
+				"Mis-placed <wicket:head>. <wicket:head> must be outside of <wicket:panel>, <wicket:border>, and <wicket:extend>."));
 	}
 
 	/**
@@ -227,8 +221,11 @@ class HeaderSectionTest extends WicketTestCase
 	@Test
 	void doubleHeadTagPage()
 	{
-		assertThrows(MarkupException.class, () -> {
+		final MarkupException markupException = assertThrows(MarkupException.class, () -> {
 			tester.startPage(DoubleHeadTagPage.class);
 		});
+
+		assertEquals(markupException.getMessage(),
+			"Tag <head> is not allowed at this position (do you have multiple <head> tags in your markup?).");
 	}
 }