I’m not sure if that is possible, and it doesn’t sound very practical to me.
For instance here is simple fragment with a heading. You can copy this into your browser console and see the output.
const fragment = new DocumentFragment()
const heading = document.createElement('h1')
heading.textContent = 'Read all about it!'
fragment.appendChild(heading)
console.dir(fragment)
This is just a small portion of that fragment logged out
baseURI: "chrome://new-tab-page/"
childElementCount: 1
childNodes: NodeList(1)
0: h1
length: 1
[[Prototype]]: NodeList
entries: ƒ entries()
forEach: ƒ forEach()
item: ƒ item()
keys: ƒ keys()
length: (...)
values: ƒ values()
constructor: ƒ NodeList()
Symbol(Symbol.iterator): ƒ values()
Symbol(Symbol.toStringTag): "NodeList"
get length: ƒ length()
[[Prototype]]: Object
children: HTMLCollection(1)
0: h1
length: 1
[[Prototype]]: HTMLCollection
item: ƒ item()
length: (...)
namedItem: ƒ namedItem()
constructor: ƒ HTMLCollection()
Symbol(Symbol.iterator): ƒ values()
Symbol(Symbol.toStringTag): "HTMLCollection"
get length: ƒ length()
[[Prototype]]: Object
...
Well it certainly rules out json, as json only copies array, objects and primitives values, no functions e.g. entries, forEach etc.
You then have built in prototypes NodeLists, HTMLCollection and symbols, getters and setters.
I am not sure it would even be possible to store this in a database.
That is as opposed to storing maybe a string template and parsing it.
'<h1>Read all about it!</h1>'
Alternative?
Another thought, if you want to stick to explicitly creating elements, is to possibly represent the process in object form.
If you look at React’s createElement it maybe gives us an idea.
React.createElement(
"div",
{ className: "red" },
React.createElement("div", { className: "blue" }) <-- child element
)
You could store a nested object, a basic representation of your Dom elements and then loop through it later creating your actual DOM elements.
Store just the data instead?
This is what I would go with. I know I sound like a broken record here, but then on retrieval I would feed that data into html template strings.