Design a System to Render Web Frontend
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2021-08-23
Problem Statement
Design and implement a system that can render a web frontend given structured input (HTML, CSS, JavaScript). The system should parse input documents, build an internal representation, and render the UI components on the screen, handling layout, styling, and user interactions.
Functional Requirements (FR)
HTML Parsing & DOM Creation
•Parse an HTML string and create a DOM tree representation.
•Support common HTML elements (div, span, img, p, button, input, etc.).
CSS Parsing & Styling
•Parse CSS rules and apply them to the DOM elements.
•Support basic selectors (element, class, id).
•Handle style inheritance (font, color, etc.).
Layout Engine
•Compute box model (margin, border, padding, content).
•Position elements based on layout rules (block, inline, flex/grid if extended).
•Handle reflow when styles or content change.
Rendering Engine
•Convert DOM + CSSOM (style tree) into a render tree.
•Paint elements with applied styles (backgrounds, borders, text).
JavaScript Execution (Optional)
•Parse and execute simple JavaScript for interactivity (e.g., button click updates text).
•Support DOM APIs like getElementById, appendChild.
Events & Interactions
•Handle user events like onClick, onInput, onChange.
•Allow dynamic DOM updates via JS.
Non-Functional Requirements (NFR)
•Performance: Parsing and rendering should be efficient for reasonably sized pages.
•Extensibility: Should support adding new tags, CSS rules, or layouts easily.
•Correctness: Render output must respect HTML structure and CSS precedence rules.
•Isolation: JavaScript should not crash the rendering pipeline.
Core Entities & Attributes
•Node (DOM Node) → tagName, attributes, children, parent
•StyleRule → selector, properties
•CSSOM Node → mapped style properties for a DOM node
•RenderObject → dimensions, computed styles, position, children
•Event → type (click, input), targetNode, handler
Example Use Case
1.Input:
1
2<html>
3 <body>
4 <div id="main" style="color: red;">
5 <p>Hello World!</p>
6 <button>Click Me</button>
7 </div>
8 </body>
9</html>CSS:
1
2#main { font-size: 16px; }
3p { color: blue; }1.System parses HTML → builds DOM tree.
2.Parses CSS → builds CSSOM tree.
3.Combines → builds Render Tree.
4.Computes layout (div → p + button).
5.Renders text “Hello World!” in blue inside #main div.
Evaluation Criteria
•Clean class design (Node, DOMTree, CSSParser, LayoutEngine, RenderEngine, EventHandler).
•Correct handling of parsing and tree building.
•Ability to render nested elements with styles applied.
•Extensibility for advanced layouts (flexbox, grid, animations).