[PATCH] changeset: add filename derived anchors to file diffs (and use them)

Søren Løvborg sorenl at unity3d.com
Mon Feb 22 14:16:43 UTC 2016


Please change "path2anchor" to "path_to_anchor", we're not that pressed
for space. ;-)

Defining anchors using <a name="XYZ"> is deprecated; the code should be
using "id" attribute. (Since there can only be one id per element, it
can be placed on the next div.)

As to why browsers choke on special characters in anchor names, it's
probably related to this requirement in the HTML standard:

    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
    followed by any number of letters, digits ([0-9]), hyphens ("-"),
    underscores ("_"), colons (":"), and periods (".").

(When I follow the rule about starting with a letter, I observe no
problems with periods across browsers. Can you elaborate on where
you saw problems with periods?)

Besides introducing illegal characters into the attribute, the ad-hoc
escaping can also cause problems if filenames contain actual ! or \
characters. I'd prefer a standard solution that doesn't have weird
corner cases.

As for '/', that character only appears in the quoted output because
urllib.quote considers it "safe" by standard. If we disable that, then
replace '%' with a valid character and unconditionally add a letter in
front of the filename, we get a guaranteed valid ID:

    id = 'A' + urllib.quote(filename, '').replace('%', ':')

Alternatively, if we don't care about making the string semi-readable,
we could simply use the SHA-1 hash of the filename:

    id = hashlib.sha1(filename).hexdigest()

This eliminates all issues with special characters, and also guarantees
a consistent id length independent of filename length.

Best,
Søren


More information about the kallithea-general mailing list