Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to sort attributes in HTML output. #2097

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lmartelli
Copy link

This is helpful to compare documents and consider documents equal if the only difference is attributes order.

@jhy
Copy link
Owner

jhy commented Jan 6, 2024

Hi,

If the goal is to:

consider documents equal if the only difference is attributes order

Why not make this a comparator function of an Element / node? E.g. we have equals() and hasSameValue().

And can you provide some detail on why that's useful / required? What documents do you have that you are testing for this and why is the attribute sort order the only unstable (?) thing about them? Trying to understand the utility of it.

@lmartelli
Copy link
Author

Hi,

If the goal is to:

consider documents equal if the only difference is attributes order

Why not make this a comparator function of an Element / node? E.g. we have equals() and hasSameValue().

And can you provide some detail on why that's useful / required? What documents do you have that you are testing for this and why is the attribute sort order the only unstable (?) thing about them? Trying to understand the utility of it.

I would like to use this for approval tests. The order of attributes can be changed by the developer in a template, or it can change depending on the version of the template engine, but it should have no impact on the outcome of the test. But if the value of an attribute changes, the HTML must validated again, because it could have an impact on the rendered document. Ideally, I would render the HTML, take a snapshot image, and compare the images pixel by pixel, but that would be slower and more complicated. Comparing the HTML while ignoring changes that are known not to have an impact on the rendered document looks like a good compromise.

With the proposed patch, I could do somehting like this :

val actualHtml = Jsoup.parse(actual);
actualHtml.outputSettings().sortAttributes(false);
val expectedHtml = Jsoup.parse(expected);
expectedHtml.outputSettings().sortAttributes(true);
if (actualHtml.outerHtml().equals(expectedHtml.outerHtml()))
	return VerifyResult.SUCCESS;
else
	return VerifyResult.FAILURE;

But it could indeed be a comparator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants