gofakeit icon indicating copy to clipboard operation
gofakeit copied to clipboard

Add html

Open brianvoe opened this issue 3 years ago • 17 comments

Add ability to add random html elements.

brianvoe avatar Jan 13 '22 20:01 brianvoe

Yo! I'm back after a week+ off. What's the deal with this? Would this be like generating individual <p>, <h1>, <div>, <li>, etc. elements with fake info inside of them? Nesting? They wouldn't be styled at all, would they? Is this similar to xml.go?

petergeorgas avatar Apr 19 '22 00:04 petergeorgas

Hey buddy! Ready for round 2?

So my thought for this is to start with a simple function. Something like HtmlElement() string. and returns a random html element. Just like you described in your previous comment <p>, <h1>, <div>

And then next function would take that a step further and do a random complete element <div>random sentence</div> <h1>random sentence</h1>

And then take that another step further and do a group of element that could be randomly nested

<div>
    <p>random sentence</p>
    <h1>
        <li>random something</li>
    </h1>
</div>

brianvoe avatar Apr 19 '22 14:04 brianvoe

Those were just my thoughts. Not sure if anyone would actually use it but I thought it would be interesting to add. Eventually my thoughts were to see how far I could take the idea of generating html to resemble at least something that looks like a website.

brianvoe avatar Apr 19 '22 14:04 brianvoe

So if I'm correctly understanding, we have one function that basically just generates an HTML stub (nothing in-between), another function that generates single HTML elements with some fake text inside of them, and then another function that generates nested HTML elements with fake data inside of them?

petergeorgas avatar Apr 19 '22 15:04 petergeorgas

ya that was my thoughts. each proceeding can use the previous function to get data it needs.

brianvoe avatar Apr 19 '22 16:04 brianvoe

Cool! I started work on a new branch, see html.go. So far I have stubs and single elements with fields being generated, next I'll do nesting.

Should we be able to pass in multiple fields for single elements, or just one?

petergeorgas avatar Apr 19 '22 17:04 petergeorgas

Also, something I think may be cool is if there are also functions for populating specific elements with fake data, for example, perhaps we have a P() func that will always output a <p> element, populated with whatever fields specified.

What do you think?

petergeorgas avatar Apr 19 '22 17:04 petergeorgas

ya thats a pretty good idea. maybe one function that takes in the element. cause otherwise we would have soooo many function to get all the primary html elements that exist

brianvoe avatar Apr 19 '22 18:04 brianvoe

Sorry! It's been a bit. Finals week and all that good stuff. I am having some trouble right now comprehending exactly how nested HTML would work. I am looking at json.go, and it is not really clear to me based on the tests written nor the code how exactly you handle nested objects.

For example, I don't understand how this:

func ExampleFaker_JSON_object() {
	f := New(11)

	value, err := f.JSON(&JSONOptions{
		Type: "object",
		Fields: []Field{
			{Name: "first_name", Function: "firstname"},
			{Name: "last_name", Function: "lastname"},
			{Name: "address", Function: "address"},
			{Name: "password", Function: "password", Params: MapParams{"special": {"false"}}},
		},
		Indent: true,
	})
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(string(value))

	// Output: {
	//     "first_name": "Markus",
	//     "last_name": "Moen",
	//     "address": {
	//         "address": "4599 Dale ton, Norfolk, New Jersey 36906",
	//         "street": "4599 Dale ton",
	//         "city": "Norfolk",
	//         "state": "New Jersey",
	//         "zip": "36906",
	//         "country": "Tokelau",
	//         "latitude": 23.058758,
	//         "longitude": 89.022594
	//     },
	//     "password": "qjXy56JHcVlZ"
	// }
}

Produces the output there.

petergeorgas avatar May 01 '22 20:05 petergeorgas

Hey! No problem! So instead of looking at the json functions, the more similar one to look at in this situation would be the sentence and paragraph functions. If that makes sense.

brianvoe avatar May 02 '22 14:05 brianvoe

Gotcha, in that case, do we not care about what is inside each HTML element? Will we just generate stubs? Do we want nested HTML as a feature?

petergeorgas avatar May 03 '22 04:05 petergeorgas

https://github.com/brianvoe/gofakeit/issues/194#issuecomment-1102723787

So I think this one best explains the 3 functions I was thinking about. Let me know if I need to better describe it.

brianvoe avatar May 03 '22 14:05 brianvoe

Are the random things within the HTML elements user-specified or arbitrary? I.e. sentences?

petergeorgas avatar May 03 '22 23:05 petergeorgas

arbitrary

brianvoe avatar May 04 '22 03:05 brianvoe

Add input fields and svg generators

brianvoe avatar Dec 18 '22 21:12 brianvoe

I have been having a play around with the HTML generator Just testing out ideas. Generating random elements based on element groups Generating elements with random attributes, I need to add a random elements Generator Generating page elements or a full HTML page.

https://github.com/Mrpye/gofakeit/blob/experimental/html_elements.go https://github.com/Mrpye/gofakeit/blob/experimental/html_elements_test.go

I might be overkill, got a bit carried away.

Mrpye avatar Nov 17 '23 18:11 Mrpye

Ill take a look at those as soon as we finalize the template stuff.

brianvoe avatar Nov 17 '23 18:11 brianvoe