Giter Club home page Giter Club logo

Comments (10)

oliverklee avatar oliverklee commented on September 15, 2024 1

Yes, we strive to follow Semantic Versioning.

This indeed is a gray area.

We didn't consider this as a breaking change as the functionality of the generated HTML is not affected by the formatting.

In general, I would recommend to write tests for HTML (and XML) to be formatting-independent in order to make them less brittle.

@JakeQZ What do you think?

from emogrifier.

oliverklee avatar oliverklee commented on September 15, 2024

And @mortenscheel, thanks for reporting this!

from emogrifier.

JakeQZ avatar JakeQZ commented on September 15, 2024

I notice tha the changelog entry says "Disable HTML formatting by default".

Although there are no methods or parameters in CssInliner to explicitly control the behaviour, there is a method to get the internal DOMDocument object, on which the formatOutput property can be set.

The following should achieve the previous behaviour, though I've not tested it (and setting the formatOutput property after loading the HTML might result in a different behaviour from when it is set before loading the HTML):

$cssInliner = CssInliner::fromHtml($html);
$cssInliner->getDomDocument()->formatOutput = true;
$visualHtml = $cssInliner->inlineCss($css)->render();

The old behaviour (in in which the rendered HTML is formatted) could be considered to be a bug, for example if list items are displayed as inline-block and do not have white space between them, but the formatting introduces some, or if some elements have the white-space property set (e.g. to pre), and extra whitespace is added by the formatting which then affects the layout, though I have not tested these scenarios.

from emogrifier.

mortenscheel avatar mortenscheel commented on September 15, 2024

In general, I would recommend to write tests for HTML (and XML) to be formatting-independent in order to make them less brittle.

Do you have any tips on how to do that? Currently I'm comparing the output html to a fixture, and I don't know how I can ignore only the redundant whitespace. Except maybe running both throw a DomDocument, but that seems like a hassle.

from emogrifier.

oliverklee avatar oliverklee commented on September 15, 2024

@mortenscheel The XML-specific assertions of PHPUnit might be helpful: https://docs.phpunit.de/en/10.4/assertions.html#xml

from emogrifier.

oliverklee avatar oliverklee commented on September 15, 2024

Or maybe https://github.com/caxy/php-htmldiff is worth a try.

from emogrifier.

JakeQZ avatar JakeQZ commented on September 15, 2024

The XML-specific assertions of PHPUnit might be helpful: https://docs.phpunit.de/en/10.4/assertions.html#xml

Looks like this internally uses DOMDocument, but it may only allow XML or XHTML if it uses the loadXML method and not the loadHTML method.

I was thinking you could just use regex to collapse all whitespace sequences to a single space:

$normalizedHtml = \preg_replace('/\\s++/', ' ', $html);

This would have the drawback of replacing all line breaks with regular spaces, making the differences diffucult to see. That could be mitigated against by changing spaces after tags to line breaks:

$normalizedHtml = \str_replace('> ', ">\n", \preg_replace('/\\s++/', ' ', $html));

Though differences where whitespace is important, such as preformatted text, wouldn't generate test failures, so you'd have to check those cases differently.

from emogrifier.

oliverklee avatar oliverklee commented on September 15, 2024

Please note that HTML cannot be fully parsed with regular expressions. 😉

from emogrifier.

JakeQZ avatar JakeQZ commented on September 15, 2024

Please note that HTML cannot be fully parsed with regular expressions. 😉

Indeed, though the above is not parsing it, just removing extra whitespace.

from emogrifier.

oliverklee avatar oliverklee commented on September 15, 2024

Converting to a discussion as this has turned from a potential bug report to a discussion. :-)

from emogrifier.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.