-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.go
More file actions
70 lines (67 loc) · 2.1 KB
/
Copy pathhtml.go
File metadata and controls
70 lines (67 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package fmt
// EscapeAttr returns a string safe to place inside an HTML attribute value.
//
// It escapes the following characters:
//
// & -> &
// " -> "
// ' -> '
// < -> <
// > -> >
//
// Example:
//
// s := Convert(`Tom & Jerry's "House" <tag>`).EscapeAttr()
// // s == `Tom & Jerry's "House" <tag>`
//
// Note: this method performs plain string replacements and does not detect
// existing HTML entities. Calling EscapeAttr on a string that already
// contains entities (for example `&`) will produce double-escaped
// output (`&amp;`). This behavior is intentional and matches a simple
// escape-for-attribute semantics.
func (c *Conv) EscapeAttr() string {
return c.Replace("&", "&").
Replace("\"", """).
Replace("'", "'").
Replace("<", "<").
Replace(">", ">").
String()
}
// EscapeHTML returns a string safe for inclusion into HTML content.
//
// It escapes the following characters:
//
// & -> &
// < -> <
// > -> >
// " -> "
// ' -> '
//
// Example:
//
// s := Convert(`<div class="x">Tom & Jerry's</div>`).EscapeHTML()
// // s == `<div class="x">Tom & Jerry's</div>`
//
// Like EscapeAttr, this method uses simple replacements and will double-escape
// existing entities.
func (c *Conv) EscapeHTML() string {
return c.Replace("&", "&").
Replace("<", "<").
Replace(">", ">").
Replace("\"", """).
Replace("'", "'").
String()
}
// Html creates a string for HTML content, similar to Translate but without automatic spacing.
// It supports two modes:
// 1. Format mode: If the first argument is a string containing '%', it behaves like Fmt.
// 2. Concatenation mode: Otherwise, it concatenates arguments (translating LocStr) without spaces.
//
// Usage:
// Html("div", "span").String() -> "divspan"
// Html("<div class='%s'>", "foo").String() -> "<div class='foo'>"
func Html(values ...any) *Conv {
// Use unified smart processing
// separator="", allowStringCode=false (to support 2-letter tags), detectFormat=true
return GetConv().SmartArgs(BuffOut, "", false, true, values...)
}