Skip to content

Validation broken in Genesis themes by Document::normalize_document_structure() #4104

@westonruter

Description

@westonruter

Bug Description

I was testing out a Genesis theme and I saw in the admin bar that it was ✅ valid AMP. When I clicked to validate the page, however, I unexpectedly saw two validation errors:

image

This made no sense to me and I was racking my brain trying to figure it out. It turns out to be that the link[rel=canonical] element was located in the body during validation, when for non-validation requests it is in the head as expected.

The issue turns out to be \Amp\AmpWP\Dom\Document::normalize_document_structure() introduced in #3758, namely that it is not currently written to account for HTML comments occuring before the doctype.

Normally, WordPress themes hard-code the <!DOCTYPE html>, <html>, and <head> tags. Genesis, however, uses genesis_doctype() to generate them. When a validation request is being performed, source stack comments are added before and after genesis_doctype() like so:

<!--amp-source-stack {"type":"theme","name":"genesis","file":"lib\/structure\/header.php","line":28,"function":"genesis_do_doctype","hook":"genesis_doctype","priority":10}--><!DOCTYPE html>
<html lang="en-US">
<head >
<meta charset="UTF-8" />
<!--/amp-source-stack {"type":"theme","name":"genesis","file":"lib\/structure\/header.php","line":28,"function":"genesis_do_doctype","hook":"genesis_doctype","priority":10}-->

This has the effect of Document::normalize_document_structure() normalizing the document to start as:

<!DOCTYPE html><html><head></head><body><!--amp-source-stack {"type":"theme","name":"genesis","file":"lib\/structure\/header.php","line":28,"function":"genesis_do_doctype","hook":"genesis_doctype","priority":10}--><!DOCTYPE html>
<html lang="en-US">
<head >

Expected Behaviour

The DOM used for validation should be normalized to be the same as the DOM used for rendering.

Steps to reproduce

  1. Activate Genesis.
  2. Enable Transitional or Standard mode in the AMP plugin.
  3. Validate a URL.
  4. Notice unexpected validation errors for link[rel=canonical] or other tags.

Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • Validating a page generated by Genesis should not result in validation errors when there are none on the frontend.
  • HTML comments that occur before and around the <!DOCTYPE>, <html>, and <head> tags must be preserved so that validation errors can be properly sourced.

Implementation brief

  • Perhaps capture the whitespace before/after the <!DOCTYPE>, <html>, and <head> and ensure that they are retained after normalization.

QA testing instructions

Demo

Changelog entry

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't workingP0High priorityQA passedHas passed QA and is done

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions