forked from brianvoe/gofakeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.go
174 lines (147 loc) · 6.39 KB
/
html.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package gofakeit
import (
"errors"
"strconv"
"strings"
"github.com/brianvoe/gofakeit/v7/data"
)
// InputName will return a random input field name
func InputName() string {
return inputName(GlobalFaker)
}
// InputName will return a random input field name
func (f *Faker) InputName() string {
return inputName(f)
}
func inputName(f *Faker) string {
return getRandValue(f, []string{"html", "input_name"})
}
type SVGOptions struct {
Height int
Width int
Type string
Colors []string
}
// Generate a random svg generator
func Svg(options *SVGOptions) string { return svg(GlobalFaker, options) }
// Generate a random svg generator
func (f *Faker) Svg(options *SVGOptions) string { return svg(f, options) }
func svg(f *Faker, options *SVGOptions) string {
// If options is nil, set it to empty struct
if options == nil {
options = &SVGOptions{}
}
// If options height and weight is not set, set it to random number between 100 and 500
if options.Width == 0 {
options.Width = number(f, 100, 500)
}
widthStr := strconv.Itoa(options.Width)
if options.Height == 0 {
options.Height = number(f, 100, 500)
}
heightStr := strconv.Itoa(options.Height)
// Check if type is set, if not set to random type
if options.Type == "" {
options.Type = randomString(f, data.GetSubData("html", "svg"))
}
// If the colors are not set, set it to a set of nice colors
if len(options.Colors) == 0 {
options.Colors = niceColors(f)
}
// Start svg string
svgStr := `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ` + widthStr + ` ` + heightStr + `" width="` + widthStr + `" height="` + heightStr + `">`
// Add a rect for the background
svgStr += `<rect x="0" y="0" width="100%" height="100%" fill="` + randomString(f, options.Colors) + `" />`
// Add a random number of shapes
for i := 0; i < number(f, 10, 20); i++ {
// Add a random shape
switch options.Type {
case "rect":
svgStr += `<rect x="` + strconv.Itoa(number(f, 0, options.Width)) + `" y="` + strconv.Itoa(number(f, 0, options.Height)) + `" width="` + strconv.Itoa(number(f, 0, options.Width)) + `" height="` + strconv.Itoa(number(f, 0, options.Height)) + `" fill="` + randomString(f, options.Colors) + `" />`
case "circle":
svgStr += `<circle cx="` + strconv.Itoa(number(f, 0, options.Width)) + `" cy="` + strconv.Itoa(number(f, 0, options.Height)) + `" r="` + strconv.Itoa(number(f, 0, options.Width)) + `" fill="` + randomString(f, options.Colors) + `" />`
case "ellipse":
svgStr += `<ellipse cx="` + strconv.Itoa(number(f, 0, options.Width)) + `" cy="` + strconv.Itoa(number(f, 0, options.Height)) + `" rx="` + strconv.Itoa(number(f, 0, options.Width)) + `" ry="` + strconv.Itoa(number(f, 0, options.Height)) + `" fill="` + randomString(f, options.Colors) + `" />`
case "line":
svgStr += `<line x1="` + strconv.Itoa(number(f, 0, options.Width)) + `" y1="` + strconv.Itoa(number(f, 0, options.Height)) + `" x2="` + strconv.Itoa(number(f, 0, options.Width)) + `" y2="` + strconv.Itoa(number(f, 0, options.Height)) + `" stroke="` + randomString(f, options.Colors) + `" />`
case "polyline":
svgStr += `<polyline points="` + strconv.Itoa(number(f, 0, options.Width)) + `,` + strconv.Itoa(number(f, 0, options.Height)) + ` ` + strconv.Itoa(number(f, 0, options.Width)) + `,` + strconv.Itoa(number(f, 0, options.Height)) + ` ` + strconv.Itoa(number(f, 0, options.Width)) + `,` + strconv.Itoa(number(f, 0, options.Height)) + `" fill="` + randomString(f, options.Colors) + `" />`
case "polygon":
svgStr += `<polygon points="` + strconv.Itoa(number(f, 0, options.Width)) + `,` + strconv.Itoa(number(f, 0, options.Height)) + ` ` + strconv.Itoa(number(f, 0, options.Width)) + `,` + strconv.Itoa(number(f, 0, options.Height)) + ` ` + strconv.Itoa(number(f, 0, options.Width)) + `,` + strconv.Itoa(number(f, 0, options.Height)) + `" fill="` + randomString(f, options.Colors) + `" />`
}
}
// End svg string
svgStr += `</svg>`
return svgStr
}
func addHtmlLookup() {
AddFuncLookup("inputname", Info{
Display: "Input Name",
Category: "html",
Description: "Attribute used to define the name of an input element in web forms",
Example: "first_name",
Output: "string",
Generate: func(f *Faker, m *MapParams, info *Info) (any, error) {
return inputName(f), nil
},
})
AddFuncLookup("svg", Info{
Display: "Image SVG",
Category: "html",
Description: "Scalable Vector Graphics used to display vector images in web content",
Example: `<svg width="369" height="289">
<rect fill="#4f2958" />
<polygon points="382,87 418,212 415,110" fill="#fffbb7" />
</svg>`,
Output: "string",
ContentType: "image/svg+xml",
Params: []Param{
{Field: "width", Display: "Width", Type: "int", Default: "500", Description: "Width in px"},
{Field: "height", Display: "Height", Type: "int", Default: "500", Description: "Height in px"},
{Field: "type", Display: "Type", Type: "string", Optional: true, Options: data.GetSubData("html", "svg"), Description: "Sub child element type"},
{Field: "colors", Display: "Colors", Type: "[]string", Optional: true, Description: "Hex or RGB array of colors to use"},
},
Generate: func(f *Faker, m *MapParams, info *Info) (any, error) {
// Setup new options
options := SVGOptions{}
var err error
options.Width, err = info.GetInt(m, "width")
if err != nil {
return nil, err
}
if options.Width < 10 || options.Width >= 1000 {
return nil, errors.New("invalid image width, must be greater than 10, less than 1000")
}
options.Height, err = info.GetInt(m, "height")
if err != nil {
return nil, err
}
if options.Height < 10 || options.Height >= 1000 {
return nil, errors.New("invalid image height, must be greater than 10, less than 1000")
}
options.Type, err = info.GetString(m, "type")
svgData := data.GetSubData("html", "svg")
if err != nil {
return nil, err
}
// If type is empty, set with random type
if options.Type == "" {
options.Type = randomString(f, svgData)
}
// If not in date html svg type array, return error
if !stringInSlice(options.Type, svgData) {
return nil, errors.New("invalid svg type, must be one of " + strings.Join(svgData, ","))
}
// Get colors
options.Colors, err = info.GetStringArray(m, "colors")
if err != nil {
return nil, err
}
// If colors is empty, set with random colors
if len(options.Colors) == 0 {
options.Colors = niceColors(f)
}
return svg(f, &options), nil
},
})
}