Skip to content

Latest commit

 

History

History
56 lines (41 loc) · 1.18 KB

File metadata and controls

56 lines (41 loc) · 1.18 KB

HTML Serializer

Problem

https://www.greatfrontend.com/questions/javascript/html-serializer

Problem Description

Given an object which resembles a DOM tree, implement a function that serializes the object into a formatted string with proper indentation (one tab per nesting level) and one tag per line.

Examples

const tree = {
  tag: 'body',
  children: [
    { tag: 'div', children: [{ tag: 'span', children: ['foo', 'bar'] }] },
    { tag: 'div', children: ['baz'] },
  ],
};

serializeNode(tree);
// Output:
`<body>
	<div>
		<span>
			foo
			bar
		</span>
	</div>
	<div>
		baz
	</div>
</body>`;

Solution

HTML Serializer