kuchiki-rs / kuchiki

(朽木) HTML/XML tree manipulation library for Rust
MIT License
470 stars 54 forks source link

Sort Attributes when serializing #40

Closed dfrankland closed 6 years ago

dfrankland commented 6 years ago

I've been making a crate that relies on parsing, modifying, and serializing HTML. Testing can become complex because I currently have to traverse two trees and compare elements as well as attributes.

This change makes serialization deterministic, making testing of the resulting string possible.

SimonSapin commented 6 years ago

Another option could be to use BTreeMap instead of HashMap. I don’t know which is preferable. Do you have an opinion?

dfrankland commented 6 years ago

I re-ran my tests and benchmarks using both the sorting of the Vec from a HashMap, and also using a BTreeMap. I think that the BTreeMap is the more correct way to implement this. It makes it obvious that the outcome is deterministic and allows the user of the library to order the map if needed. Also, it seems to be a few milliseconds faster ⚡️

I've opened another PR #41 for the BTreeMap implementation, so you can choose which you prefer 👍

SimonSapin commented 6 years ago

Merged #41 instead, thanks.