Sanitize CSS class names applied on Legend rows/entries

Previously, short names and numbers would be applied as CSS classes, but they would be invalid class names and it would thus be impossible to apply styles to them.

Close #1321.
Minor: `"   " -> ""`, but from rules it should be "_"