@@ -328,21 +328,18 @@ Element insertElementFor(final Token.StartTag startTag) {
328328 Element el = createElementFor (startTag , NamespaceHtml , false );
329329 doInsertElement (el );
330330
331- // handle self-closing tags. when the spec expects an empty tag, will directly hit insertEmpty, so won't generate this fake end tag.
331+ // handle self-closing tags. when the spec expects an empty (void) tag, will directly hit insertEmpty, so won't generate this fake end tag.
332332 if (startTag .isSelfClosing ()) {
333333 Tag tag = el .tag ();
334- if (tag .isKnownTag ()) {
335- if (!tag .isEmpty ())
336- tokeniser .error ("Tag [%s] cannot be self closing; not a void tag" , tag .normalName ());
337- // else: ok
334+ tag .setSeenSelfClose (); // can infer output if in xml syntax
335+ if (tag .isKnownTag () && (tag .isEmpty () || tag .isSelfClosing ())) {
336+ // ok, allow it. effectively a pop, but fiddles with the state. handles empty style, title etc which would otherwise leave us in data state
337+ tokeniser .transition (TokeniserState .Data ); // handles <script />, otherwise needs breakout steps from script data
338+ tokeniser .emit (emptyEnd .reset ().name (el .tagName ())); // ensure we get out of whatever state we are in. emitted for yielded processing
339+ } else {
340+ // error it, and leave the inserted element on
341+ tokeniser .error ("Tag [%s] cannot be self-closing; not a void tag" , tag .normalName ());
338342 }
339- else { // unknown tag: remember this is self-closing, for output
340- tag .setSelfClosing ();
341- }
342-
343- // effectively a pop, but fiddles with the state. handles empty style, title etc which would otherwise leave us in data state
344- tokeniser .transition (TokeniserState .Data ); // handles <script />, otherwise needs breakout steps from script data
345- tokeniser .emit (emptyEnd .reset ().name (el .tagName ())); // ensure we get out of whatever state we are in. emitted for yielded processing
346343 }
347344
348345 return el ;
@@ -355,8 +352,8 @@ Element insertForeignElementFor(final Token.StartTag startTag, String namespace)
355352 Element el = createElementFor (startTag , namespace , true );
356353 doInsertElement (el );
357354
358- if (startTag .isSelfClosing ()) {
359- el .tag ().setSelfClosing (); // remember this is self-closing for output
355+ if (startTag .isSelfClosing ()) { // foreign els are OK to self-close
356+ el .tag ().setSeenSelfClose (); // remember this is self-closing for output
360357 pop ();
361358 }
362359
0 commit comments