dashorst commented on code in PR #1876:
URL: https://github.com/apache/wicket/pull/1876#discussion_r4527832965


##########
wicket-core/src/main/java/org/apache/wicket/markup/parser/XmlPullParser.java:
##########
@@ -639,79 +679,113 @@ public String toString()
         * @param tag
         * @param tagText
         *            The text between tags
-        * @return false in case of an error
+        * @return false if the tag text does not start with a tag name
         * @throws ParseException
         */
        private boolean parseTagText(final XmlTag tag, final String tagText) 
throws ParseException
        {
-               // Get the length of the tagtext
-               final int tagTextLength = tagText.length();
-
-               // If we match tagname pattern
                final TagNameParser tagnameParser = new TagNameParser(tagText);
-               if (tagnameParser.matcher().lookingAt())
+               if (tagnameParser.matcher().lookingAt() == false)
                {
-                       // Extract the tag from the pattern matcher
-                       tag.name = tagnameParser.getName();
-                       tag.namespace = tagnameParser.getNamespace();
-
-                       // Are we at the end? Then there are no attributes, so 
we just
-                       // return the tag
-                       int pos = tagnameParser.matcher().end(0);
-                       if (pos == tagTextLength)
-                       {
-                               return true;
-                       }
-
-                       // Extract attributes
-                       final VariableAssignmentParser attributeParser = new 
VariableAssignmentParser(tagText);
-                       while (attributeParser.matcher().find(pos))
-                       {
-                               // Get key and value using attribute pattern
-                               String value = attributeParser.getValue();
-
-                               // In case like <html xmlns:wicket> will the 
value be null
-                               if (value == null)
-                               {
-                                       value = "";
-                               }
+                       return false;
+               }
 
-                               // Set new position to end of attribute
-                               pos = attributeParser.matcher().end(0);
+               tag.name = tagnameParser.getName();
+               tag.namespace = tagnameParser.getNamespace();
 
-                               // Chop off double quotes or single quotes
-                               if (value.startsWith("\"") || 
value.startsWith("\'"))
-                               {
-                                       value = value.substring(1, 
value.length() - 1);
-                               }
+               parseAttributes(tag, tagText, tagnameParser.matcher().end(0));
+               return true;
+       }
 
-                               // Trim trailing whitespace
-                               value = value.trim();
+       /**
+        * Reads the attributes the way the attribute states of the HTML 
tokenizer do, see the class
+        * documentation.
+        *
+        * @param tag
+        *            the tag to add the attributes to
+        * @param tagText
+        *            The text between tags
+        * @param pos
+        *            the index in the tag text right after the tag name
+        * @throws ParseException
+        */
+       private void parseAttributes(final XmlTag tag, final String tagText, 
int pos)
+               throws ParseException
+       {
+               final int length = tagText.length();
+               while (true)
+               {
+                       while ((pos < length) &&
+                               (isWhitespace(tagText.charAt(pos)) || 
(tagText.charAt(pos) == '/')))
+                       {
+                               pos++;
+                       }
+                       if (pos == length)
+                       {
+                               return;
+                       }
 
-                               // Unescape
-                               value = 
Strings.unescapeMarkup(value).toString();
+                       // The first character of a name may be '='
+                       final int nameStart = pos++;
+                       while ((pos < length) && 
(isWhitespace(tagText.charAt(pos)) == false) &&
+                               (tagText.charAt(pos) != '/') && 
(tagText.charAt(pos) != '='))
+                       {
+                               pos++;
+                       }
+                       final String key = tagText.substring(nameStart, pos);
 
-                               // Get key
-                               final String key = attributeParser.getKey();
+                       while ((pos < length) && 
isWhitespace(tagText.charAt(pos)))
+                       {
+                               pos++;
+                       }
 
-                               // Put the attribute in the attributes hash
-                               if (null != tag.getAttributes().put(key, value))
+                       String value = "";
+                       if ((pos < length) && (tagText.charAt(pos) == '='))
+                       {
+                               pos++;
+                               while ((pos < length) && 
isWhitespace(tagText.charAt(pos)))
                                {
-                                       throw new ParseException("Same 
attribute found twice: " + key +
-                                               getLineAndColumnText(), 
input.getPosition());
+                                       pos++;
                                }
 
-                               // The input has to match exactly (no left over 
junk after
-                               // attributes)
-                               if (pos == tagTextLength)
+                               if (pos < length)
                                {
-                                       return true;
+                                       final char quote = tagText.charAt(pos);
+                                       if ((quote == '"') || (quote == '\''))
+                                       {
+                                               final int closingQuote = 
tagText.indexOf(quote, pos + 1);
+                                               if (closingQuote == -1)
+                                               {
+                                                       throw new 
ParseException(
+                                                               
"Opening/closing quote not found for value of " + key +
+                                                                       
getLineAndColumnText(),
+                                                               
input.getPosition());
+                                               }
+                                               value = tagText.substring(pos + 
1, closingQuote);
+                                               pos = closingQuote + 1;
+                                       }
+                                       else
+                                       {
+                                               final int valueStart = pos;
+                                               while ((pos < length) && 
(isWhitespace(tagText.charAt(pos)) == false))
+                                               {
+                                                       pos++;
+                                               }
+                                               value = 
tagText.substring(valueStart, pos);
+                                       }
                                }
                        }
 
-                       return true;
+                       if (null != tag.getAttributes().put(key, 
Strings.unescapeMarkup(value).toString()))

Review Comment:
   Done in dfa946499e: `migration-to-12.md` has a section "Markup is parsed the 
way HTML parses it" that opens with this case (`wicket:id`, `wicket:message`, 
`wicket:for`, failing only at render time) and what to do, then lists the other 
behaviour changes. I merged master in first, because the branch predated the 
guide.



##########
wicket-core-tests/src/test/java/org/apache/wicket/markup/parser/XmlPullParserTest.java:
##########
@@ -486,4 +493,126 @@ final void downlevelRevealedConditionalComments() throws 
Exception
                type = parser.next();
                assertEquals(HttpTagType.CONDITIONAL_COMMENT_ENDIF, type);
        }
-}
\ No newline at end of file
+
+       @Test
+       void closeTagWithoutNameIsRejected()
+       {
+               XmlPullParser parser = new XmlPullParser();
+
+               assertThrows(ParseException.class, () -> {
+                       parser.parse("</>");
+                       parser.next();
+               });
+       }
+
+       @Test

Review Comment:
   Added `@Timeout(30)` in 8af85afa4e. The test runs in about 150 ms, so 30 
seconds only trips on a real hang.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to