Try it yourself with our free Html Beautifier tool — runs entirely in your browser, no signup needed.

How to Format HTML in JavaScript

How to format HTML in JavaScript

Formatting HTML in JavaScript is a common requirement for web development, especially when working with dynamic content, templating engines, or server-side rendering. Properly formatting HTML ensures that the output is readable, maintainable, and efficient. In this guide, we will explore how to format HTML in JavaScript, covering the basics, handling edge cases, and providing performance tips.

Quick Example

Here is a minimal example of how to format HTML in JavaScript using the DOMParser API:

const htmlString = '<div>  <p>Hello, World!</p>  </div>';
const parser = new DOMParser();
const doc = parser.parseFromString(htmlString, 'text/html');
const formattedHtml = doc.documentElement.outerHTML;
console.log(formattedHtml);
// Output: <div><p>Hello, World!</p></div>

This code takes an HTML string, parses it using the DOMParser API, and then extracts the formatted HTML using the outerHTML property.

Step-by-Step Breakdown

Let's break down the code:

  1. const htmlString = '<div> <p>Hello, World!</p> </div>';: This line defines the input HTML string.
  2. const parser = new DOMParser();: This line creates a new instance of the DOMParser API.
  3. const doc = parser.parseFromString(htmlString, 'text/html');: This line parses the input HTML string using the parseFromString method, specifying the MIME type as text/html.
  4. const formattedHtml = doc.documentElement.outerHTML;: This line extracts the formatted HTML using the outerHTML property of the parsed document's root element.
  5. console.log(formattedHtml);: This line logs the formatted HTML to the console.

Handling Edge Cases

Empty/Null Input

When dealing with empty or null input, it's essential to handle it properly to avoid errors. Here's an example:

function formatHtml(htmlString) {
  if (!htmlString) {
    return '';
  }
  const parser = new DOMParser();
  const doc = parser.parseFromString(htmlString, 'text/html');
  return doc.documentElement.outerHTML;
}

In this example, we added a simple null check at the beginning of the function. If the input is empty or null, the function returns an empty string.

Invalid Input

Invalid input can cause the DOMParser API to throw an error. To handle this, we can use a try-catch block:

function formatHtml(htmlString) {
  try {
    const parser = new DOMParser();
    const doc = parser.parseFromString(htmlString, 'text/html');
    return doc.documentElement.outerHTML;
  } catch (error) {
    console.error('Error formatting HTML:', error);
    return '';
  }
}

In this example, we wrapped the parsing code in a try-catch block. If an error occurs, we log the error to the console and return an empty string.

Large Input

When dealing with large input, it's essential to consider performance. One approach is to use a streaming parser, but for simplicity, we can use the DOMParser API with a timeout:

function formatHtml(htmlString) {
  const parser = new DOMParser();
  const timeout = 1000; // 1 second
  const startTime = Date.now();
  try {
    const doc = parser.parseFromString(htmlString, 'text/html');
    if (Date.now() - startTime > timeout) {
      console.warn('Formatting took too long, returning original HTML');
      return htmlString;
    }
    return doc.documentElement.outerHTML;
  } catch (error) {
    console.error('Error formatting HTML:', error);
    return '';
  }
}

In this example, we added a timeout of 1 second. If the parsing takes longer than the timeout, we log a warning and return the original HTML.

Unicode/Special Characters

When dealing with Unicode or special characters, it's essential to ensure that the parser handles them correctly. The DOMParser API supports Unicode characters, but it's crucial to specify the correct encoding:

const htmlString = '<div>  <p>Hello, </p>  </div>';
const parser = new DOMParser();
const doc = parser.parseFromString(htmlString, 'text/html; charset=utf-8');
const formattedHtml = doc.documentElement.outerHTML;
console.log(formattedHtml);
// Output: <div><p>Hello, </p></div>

In this example, we specified the charset=utf-8 parameter when parsing the HTML string.

Common Mistakes

1. Not Handling Empty/Null Input

Wrong code:

const formattedHtml = doc.documentElement.outerHTML;

Corrected code:

if (!htmlString) {
  return '';
}
const formattedHtml = doc.documentElement.outerHTML;

2. Not Handling Invalid Input

Wrong code:

const doc = parser.parseFromString(htmlString, 'text/html');

Corrected code:

try {
  const doc = parser.parseFromString(htmlString, 'text/html');
} catch (error) {
  console.error('Error formatting HTML:', error);
  return '';
}

3. Not Considering Performance

Wrong code:

const doc = parser.parseFromString(htmlString, 'text/html');
const formattedHtml = doc.documentElement.outerHTML;

Corrected code:

const timeout = 1000; // 1 second
const startTime = Date.now();
try {
  const doc = parser.parseFromString(htmlString, 'text/html');
  if (Date.now() - startTime > timeout) {
    console.warn('Formatting took too long, returning original HTML');
    return htmlString;
  }
  const formattedHtml = doc.documentElement.outerHTML;
} catch (error) {
  console.error('Error formatting HTML:', error);
  return '';
}

Performance Tips

  1. Use a streaming parser: When dealing with large input, consider using a streaming parser to improve performance.
  2. Specify the correct encoding: Ensure that you specify the correct encoding when parsing the HTML string to avoid character encoding issues.
  3. Use a timeout: Consider using a timeout to prevent the parser from taking too long to parse the HTML string.

FAQ

Q: What is the best way to format HTML in JavaScript?

A: The best way to format HTML in JavaScript is to use the DOMParser API, which provides a built-in way to parse and format HTML.

Q: How do I handle empty or null input?

A: You should add a null check at the beginning of the function and return an empty string if the input is empty or null.

Q: How do I handle invalid input?

A: You should use a try-catch block to catch any errors that occur during parsing and return an empty string or an error message.

Q: How do I improve performance when dealing with large input?

A: You can use a streaming parser, specify the correct encoding, and use a timeout to prevent the parser from taking too long.

Q: How do I handle Unicode or special characters?

A: You should specify the correct encoding when parsing the HTML string, such as charset=utf-8.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp