pyquery icon indicating copy to clipboard operation
pyquery copied to clipboard

removeClass() in some cases changes the order of classes

Open mezorian opened this issue 4 years ago • 1 comments

Hi, first of all thank you for your great tool! I really like to have this as a python library :)

During some of my unit-tests I discovered some behavior of the removeClass() function. While converting the result of tag.get() into a python set at

  • https://github.com/gawel/pyquery/blob/master/pyquery/pyquery.py#L844

it might happen that the order of values is changed. I think the reason for this is that regarding to

  • https://www.w3schools.com/python/python_sets.asp

a set is a

unordered

collection which means that

items can appear in a different order every time you use them

AFAIK css classes are also defined as unordered (see : https://css-tricks.com/the-order-of-css-classes-in-html-doesnt-matter/ ) Maybe there are other opinions here and I open to a short discussion, but I personally would expect this function to only remove the specified class and keep the order as it is.

To give you an idea where this could be relevant, I can try to explain you why this is an issue for my unit-tests. I have written a small framework which in the end is generating some html reports to show if everything was successful or if there were any issues. To test this html generation I have unit-tests which call my framework and which compare the generated html with some expected html. In most of the cases these tests are successful, but sometimes the generated html differs in the order of classes. Always when this happens the comparison considers the two files as not equal and fails.

If you agree that this is unexpected behavior I am also open to try to fix this. To be honest I am not yet super experienced in python, but I think with some small info on how to build and test everything I could maybe manage this ;)

BR, mezorian

mezorian avatar Jan 11 '21 14:01 mezorian

Hi,

yes, it's definitely a bug

gawel avatar Jan 11 '21 20:01 gawel