Hone logo
Hone
Problems

Enforcing Template Structure with Angular Structural Directives and Custom Directives

Angular's templates are powerful, but can become messy and inconsistent over time. This challenge focuses on creating a system to enforce strict template structures within your Angular components, improving maintainability and reducing errors. You'll leverage structural directives and custom directives to ensure specific elements are present and in the correct order.

Problem Description

You need to build a system that enforces a predefined template structure for a specific Angular component. This system should use a combination of Angular's built-in structural directives (*ngIf, *ngFor) and a custom directive to ensure that certain elements are always present and in a specific order. The goal is to prevent developers from accidentally removing or reordering crucial parts of the template, leading to broken functionality or visual inconsistencies.

What needs to be achieved:

  1. Define a Template Structure: You'll be given a simplified component representing a "Product Card." The required structure is:

    • A div with class product-card (root element).
    • Inside the product-card, a h2 element containing the product name.
    • Inside the product-card, an img element with src attribute bound to a productImage property.
    • Inside the product-card, a p element containing the product description.
    • Inside the product-card, a button element with text "Add to Cart".
  2. Create a Custom Directive: Develop a custom directive called templateEnforcer that checks if the required elements are present in the correct order within the component's template.

  3. Enforce the Structure: Apply the templateEnforcer directive to the root element of the Product Card component. The directive should log an error to the console if the template structure is violated.

Key Requirements:

  • The directive must accurately identify the presence and order of the required elements.
  • The directive should not interfere with the component's normal functionality when the template is valid.
  • Error messages should be clear and informative, indicating which element is missing or out of order.

Expected Behavior:

  • When the template adheres to the defined structure, the directive should not produce any errors.
  • When the template is missing an element or has elements in the wrong order, the directive should log an error message to the console. The error message should clearly state what is missing or out of order.

Edge Cases to Consider:

  • What happens if an element is present but has incorrect attributes (e.g., missing src on the img tag)? (This is not required to be handled in this challenge, but consider it for future improvements).
  • How should the directive handle dynamic content within the required elements? (e.g., *ngIf inside the h2 element). The directive should still check for the presence of the h2 element, regardless of its content.

Examples

Example 1:

Input: Valid Product Card Template
<div class="product-card">
  <h2>Product Name</h2>
  <img src="product.jpg">
  <p>Product Description</p>
  <button>Add to Cart</button>
</div>
Output: No errors logged to the console.
Explanation: The template matches the required structure.

Example 2:

Input: Missing Product Description
<div class="product-card">
  <h2>Product Name</h2>
  <img src="product.jpg">
  <button>Add to Cart</button>
</div>
Output: Error logged to the console: "Missing element: <p>Product Description</p>"
Explanation: The `<p>` element is missing from the template.

Example 3:

Input: Elements in Incorrect Order
<div class="product-card">
  <button>Add to Cart</button>
  <h2>Product Name</h2>
  <img src="product.jpg">
  <p>Product Description</p>
</div>
Output: Error logged to the console: "Elements out of order. Expected: <h2>, <img>, <p>, <button>. Found: <button>, <h2>, <img>, <p>"
Explanation: The order of the elements is incorrect.

Constraints

  • The solution must be written in TypeScript.
  • The custom directive should be reusable and not tightly coupled to the Product Card component.
  • The directive should not significantly impact the performance of the component. Simple DOM traversal is acceptable.
  • The error messages should be descriptive enough to help developers quickly identify and fix the issue.

Notes

  • Consider using QueryList to efficiently access the child elements of the component.
  • Think about how to make the directive configurable to support different template structures in the future. (This is beyond the scope of the core challenge, but a good consideration for extensibility).
  • Focus on the core functionality of enforcing the template structure. Styling or complex error handling is not required.
  • The order of elements is crucial. The directive must verify that elements appear in the specified sequence.
Loading editor...
typescript