View Source vs the DOM: Why Browser HTML Looks Different

November 08, 2026 · HTML.now Editorial · HTML Troubleshooting

Explain original response, parser correction, JavaScript mutation, DevTools and reliable capture/debugging.

How the HTML Parser Transforms Source into DOM

The HTML specification at https://html.spec.whatwg.org/ defines a parsing algorithm that every conforming browser implements. This algorithm does not simply read markup and build a matching tree. It applies error recovery, implicit element creation, and tag omission rules during the "tree construction" stage. These corrections happen before any JavaScript executes, and they are the first reason source and DOM diverge.

Implicit <tbody> Insertion

When the parser encounters a <tr> directly inside a <table> with no intervening <tbody>, <thead>, or <tfoot>, the tree construction algorithm inserts a <tbody> element automatically. This is specified in the "The tr element" section of the parsing spec.

Observable check: Open any page containing a table in DevTools. Right-click a <td> and select "Inspect." The ancestor chain will show <tbody> even if View Source (Ctrl+U) contains none. A CSS selector like table > tr will match nothing in the DOM despite appearing valid against the source.

Omitted and Implied Structural Tags

The specification makes <html>, <head>, and <body> tags optional in source. If omitted, the parser generates them. A document beginning with only <p>Hello produces a DOM containing <html>, <head>, and <body> wrappers. Similarly, a <p> element is automatically closed when the parser encounters certain block-level elements like <div>, <ul>, or another <p>.

Observable check: Save a text file containing only the line <p>First<p>Second with a .html extension and open it in a browser. Inspect the Elements panel. The DOM shows two separate <p> elements, not nested ones. View Source shows the original unclosed tags.

Self-Closing Tag Handling

HTML5 treats the trailing slash in void elements like <br/> and <img/> as a parse error in most contexts. The DOM representation drops the slash. Foreign content (SVG, MathML) follows different rules where self-closing syntax is meaningful.

Observable check: In DevTools, inspect an <img> tag written as <img src="example.png" /> in source. The Elements panel displays it as <img src="example.png">. The slash is absent from the DOM serialization.

JavaScript-Driven DOM Mutation

After the parser builds the initial DOM, scripts modify it. These modifications are not reflected in View Source, which remains a static snapshot of server-sent bytes.

Scripts can:

  • Replace the entire contents of an element via innerHTML or document.write
  • Add and remove nodes with appendChild, removeChild, or insertBefore
  • Fetch remote data and inject it as new DOM subtrees
  • Rewrite attributes with setAttribute or direct property assignment

Frameworks like React, Vue, and Angular often deliver source HTML containing a single mounting point such as <div id="root"></div>. The visible page is constructed entirely through JavaScript execution.

Observable check: Load a page built with a JavaScript framework. View Source shows the empty mount point. DevTools shows the full rendered tree. Disable JavaScript in browser settings and reload. The page displays only the empty container.

What DevTools Displays and What It Omits

The Elements panel in DevTools shows the live DOM tree, not the source. This is the representation the browser uses for layout, styling, and hit testing. The panel reflects all parser corrections and all script mutations that have occurred up to the moment of inspection.

Capabilities

  • Right-click any visible element and choose "Inspect" to see its computed tag name, attributes, and position in the tree.
  • Use "Copy" → "Copy outerHTML" to serialize the current DOM subtree, including all corrections and mutations.
  • Hover over nodes to highlight the corresponding rendered region on the page.

Limitations

  • DevTools shows a point-in-time snapshot. For pages with setInterval, requestAnimationFrame, or streaming updates, the tree changes continuously. A single copy captures only one state.
  • The DOM tree omits shadow DOM content unless explicitly enabled in DevTools settings. Shadow roots encapsulate internal structure for custom elements and some built-in controls.
  • Pseudo-elements (::before, ::after) appear in the Styles panel but not as distinct nodes in the Elements tree.

Capture Strategies and Their Failure Modes

Source HTML Capture

Use when the page is server-rendered and contains no meaningful client-side JavaScript. Suitable for archival of original markup, legal evidence, or static content.

Method: curl -O <url>, wget <url>, or browser "Save Page As" → "Web Page, HTML Only."

Failure modes:

  • Misses all JavaScript-generated content
  • Preserves relative resource URLs that may break when the file is opened outside its original context
  • Does not reflect any parser corrections, so CSS selectors tested against source may not match the DOM

Rendered DOM Capture

Use for JavaScript-heavy pages, single-page applications, infinite scroll layouts, or content behind interaction gates (tabs, accordions).

Method: Use a headless browser (Playwright, Puppeteer, Selenium). Load the page, wait for content to appear, then call page.content() to serialize the current DOM.

Observable check: In Playwright, await page.waitForSelector('.product-card') before await page.content() ensures the targeted content has loaded. Compare the output length to View Source. A DOM capture that is orders of magnitude larger confirms JavaScript-generated content.

Failure modes:

  • Timing: Capture before asynchronous content loads yields an incomplete DOM. Capture after a carousel advances or a modal closes yields a different state than intended.
  • Relative resource paths: Images, stylesheets, and scripts referenced with relative URLs (/assets/style.css) fail when the captured HTML is opened from a local filesystem. Convert to absolute URLs or inline assets.
  • Script dependency: The captured DOM still contains <script> tags referencing external files. Opening the file offline may trigger failed network requests, console errors, or infinite loading spinners if the page logic depends on script execution.
  • Dynamic state loss: Values in form fields, scroll positions, and CSS pseudo-class states (:hover, :focus) are not serialized in page.content().

What Conversion Tools Cannot Recover

Conversion tools operate on the input they receive. They cannot reconstruct missing data or re-execute application logic.

Capabilities:

  • Repair malformed markup (unclosed tags, improperly nested elements)
  • Resolve relative URLs to absolute paths if the base URL is known
  • Inline external CSS and images when resources remain accessible

Hard limitations:

  • Cannot re-run JavaScript to regenerate content that was not captured
  • Cannot re-fetch data from APIs that are unavailable or require authentication
  • Cannot reconstruct the original source HTML from a heavily mutated DOM
  • Cannot preserve interactive behavior (form submission, event handlers, animations) in static output formats like PDF or PNG

Diagnostic Sequence for Mismatched Output

When converted output does not match the rendered page, follow these steps:

  1. Compare source and DOM: Open View Source and DevTools side by side on the original page. Identify structural differences: missing elements, extra wrappers, changed tag names.
  2. Identify JavaScript involvement: Search View Source for <script> tags, inline event handlers (onclick, onload), or framework attributes (data-reactroot, ng-version, v-bind). Their presence indicates DOM capture is required.
  3. Capture with a headless browser: Use Playwright or Puppeteer to serialize the DOM after a deliberate wait. Compare the captured HTML against the input your converter received.
  4. Inspect the captured file: Open the saved HTML in a text editor. Verify it begins with <!DOCTYPE html>. Confirm <html> contains both <head> and <body>. Check that critical resource links use absolute URLs.
  5. Consult the specification: When a structural discrepancy persists, reference the parsing rules at https://html.spec.whatwg.org/. For example, a <div> placed inside a <p> causes the parser to implicitly close the <p> before the <div>, which explains why the nesting appears different in the DOM.