Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct schema.org meta script by appending text node #1220

Merged
merged 1 commit into from
Jun 20, 2018

Conversation

westonruter
Copy link
Member

I found on an environment that the Schema.org meta was being output empty as:

<script type="application/ld+json"></script>

When looking at the logs I saw an error:

PHP Warning: DOMDocument::createElement(): unterminated entity reference d=mm&r=g","width":200,"height":200}} in …/amp/includes/class-amp-theme-support.php on line 984

When looking up the docs for DOMDocument::createElement() I found that while it does take a second parameter for the value, it apparently does not do escaping (❓ ):

The value of the element. By default, an empty element will be created. The value can also be set later with DOMElement::$nodeValue.

The value is used verbatim except that the < and > entity references will escaped. Note that & has to be manually escaped; otherwise it is regarded as starting an entity reference. Also " won't be escaped.

This is even noted in the second example. This was causing problems for a value like, specifically for the ampersands in the URL:

{"@context":"http:\/\/schema.org","publisher":{"@type":"Organization","name":"WCEU 2018 AMP Demos"},"@type":"WebPage","mainEntityOfPage":"https:\/\/amp-demo.xwp.io\/","headline":"Home","datePublished":"2018-06-10T23:06:03+00:00","dateModified":"2018-06-10T23:06:03+00:00","author":{"@type":"Person","name":"Weston Ruter"},"image":{"@type":"ImageObject","url":"https:\/\/secure.gravatar.com\/avatar\/bb16e7904f2f335b6c9f524cc533d2d2?s=200&d=mm&r=g","width":200,"height":200}}

The issue is fixed if the text node is explicitly created and appended to the script element.

@westonruter westonruter added this to the v1.0 milestone Jun 20, 2018
Copy link
Contributor

@kienstra kienstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

Hi @westonruter,
This looks good, and works as expected.

Like you mentioned, with this PR, image URLs that have ampersands appear as expected. Like:

https://example.com/?foo=1&bar=2

with-pr-ampersands

I also saw that before this PR, an image URL with ampersands caused the schema.org data to not appear:

before-pr-schema

@westonruter westonruter merged commit ceb4aeb into develop Jun 20, 2018
@westonruter westonruter deleted the fix/schemaorg-meta branch June 20, 2018 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants