Yeah it looks to me like URI.escape is not being smart here. I would not remove escaping altogether as it can lead to security issues, but it’s worth coming up with a failing test and fixing the escaping to work properly.
Note that # should not be encoded as jeff points out, but < and > and quotes definitely should be.