BaseXdb / basex

BaseX Main Repository.
http://basex.org
BSD 3-Clause "New" or "Revised" License
684 stars 265 forks source link

Suppress indentation adjacent to (X)HTML inline elements #2171

Closed GuntherRademacher closed 1 year ago

GuntherRademacher commented 1 year ago

More unwanted whitespace was found using (X)HTML serialization with indent=yes. An example of this is

<html><style type="text/css"><!--
body {font-size: 48px}
a {background: #CCC}
--></style><body><p><a name="x"><b>X</b></a> <a name="y"><b>X</b></a></p></body></html>

As is, browsers show it as

image

But when serialized with method=html and indent=yes, it becomes

<html>
  <style type="text/css">
    <!--
body {font-size: 48px}
a {background: #CCC}
-->
  </style>
  <body>
    <p>
      <a name="x">
        <b>X</b>
      </a>
      <a name="y">
        <b>X</b>
      </a>
    </p>
  </body>
</html>

and browsers show it as

image

The difference in this case is caused by the indentation whitespace that was added between the closing tags for b and a. According to the serialization spec, that whitespace should not be there, because a and b are "inline" elements - see

This fix keeps track of the most recent tag, in case it is a closing tag. When adding indentation, that and a following opening tag are checked for being inline elements. In case either of them is, the whitespace is suppressed.

As the spec suggests, the list of inline elements is taken from HTML and XHTML DTDs, and from HTML 5. Different lists could be used for HTML and XHTML, and in contrast to the implementation here, ins and del could additionally be checked for child elements before suppressing the whitespace. However for the sake of simplicity, I thought it is acceptable to use a single list and omit those extra checks. This would in the worst case add slightly less indentation whitespace than could be.

ChristianGruen commented 1 year ago

Just fine. I have merely fixed a few small formatting issues. If you use Eclipse, you can install Checkstyle. The resulting warnings will be displayed in the ·Problems· view of the IDE.

GuntherRademacher commented 1 year ago

Thanks! I will have a look at Checkstyle.