| Age | Commit message (Collapse) | Author |
|
Putting "html" under "net" package make no sense.
Another reason is to make the package flat under "lib/" directory.
|
|
HTTP request now implicitly create request with context.
Any false positive related to not closing HTTP response body has been
annotated with "nolint:bodyclose".
In the example code, use consistent "// Output:" comment format, by
prefixing with single space.
Any comment on code now also prefixing with single space.
An error returned without variables now use [errors.New] instead of
[fmt.Errorf].
Any error returned using [fmt.Errorf] now wrapped using "%w" instead of
"%s".
Also, replace error checking using [errors.Is] or [errors.As], instead
of using equal/not-equal operator.
Any statement like "x = x OP y" now replaced with "x OP= y".
Also, swap statement is simplified using "x, y = y, x".
Any switch statement with single case now replaced with if-condition.
Any call to defer on function or program that call [os.Exit], now
replaced by calling the deferred function directly.
Any if-else condition now replaced with switch statement, if possible.
|
|
Instead of using bytes.Replace, three times, iterate the plain text
manually to clean up the white and multiple spaces.
Benchmark result,
name old time/op new time/op delta
Sanitize-8 4.27µs ±10% 2.64µs ±13% -38.21% (p=0.000 n=10+10)
name old alloc/op new alloc/op delta
Sanitize-8 4.84kB ± 0% 4.45kB ± 0% -7.94% (p=0.000 n=10+10)
name old allocs/op new allocs/op delta
Sanitize-8 13.0 ± 0% 6.0 ± 0% -53.85% (p=0.000 n=10+10)
|
|
Since the sanitize package only contains HTML function, and the html
package already exist, we move the function into html package.
|
|
While at it, minimize allocation by using the input []byte as
output.
|
|
Given an input string, The NormalizeForID normalize it to HTML ID.
The normalization follow Mozilla specification [1] rules,
- it must not contain whitespace (spaces, tabs etc.),
- only ASCII letters, digits, '_', and '-' should be used, and
- it should start with a letter.
An empty string is equal to "_".
Any other unknown characters will be replaced with '_'.
If the input does not start with letter, it will be prefixed with
'_', unless it start with '_'.
[1] https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id.
|