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

ampdoc: Add get/setMetaByName methods #26609

Merged
merged 5 commits into from Feb 6, 2020
Merged

Conversation

mdmower
Copy link
Contributor

@mdmower mdmower commented Feb 4, 2020

  • Add getMetaByName to AmpDoc to get the content value from a <meta name=... content=...> element. Override in AmpShadowDoc to perform the lookup in a name/content pair dictionary.
  • Add setMetaByName to AmpDoc (only implemented in AmpDocShadow) to store meta name/content values in a variable. This enables retention of information stored in <meta name=... content=...> tags since it would be invalid HTML to copy them into the ShadowRoot where there is no <head> under which they could reside.

- Add getMetaByName to AmpDoc with overrides implemented in
  AmpDocSingle and AmpDocShadow to get the content value of a meta[name]
  tag.
- Add setMetaByName ot AmpDoc with override implemented in AmpDocShadow
  to store meta[name] tag content values.
@mdmower
Copy link
Contributor Author

mdmower commented Feb 4, 2020

/cc @choumx Is this close to what you had in mind?

Copy link
Contributor Author

@mdmower mdmower left a comment

Choose a reason for hiding this comment

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

A test for AmpDocSingle would be good.

Does AmpDocFie need these methods? Unsure what this doc type looks like when loaded.

const mName = el.getAttribute('name');
let sanitizedName;
try {
sanitizedName = mName.replace(/[^\x00-\x7F]/gu, '_').toLowerCase();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is overkill. If a publisher puts Unicode characters in the value of name, then unexpected behavior is reasonable (like __ instead of _). Can just do sanitizedName = mName.replace(/[^\x00-\x7F]/g, '_').toLowerCase(); in all browsers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or maybe any sanitization at all is overkill. Thoughts?

Choose a reason for hiding this comment

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

Woo I appreciate the thoroughness!

Yea I think it's a bit overkill since this isn't a public API and there's a short list of valid meta[name] values (all of which are just ASCII). Probably better to save the bytes and CPU for users e.g. just use meta[name="..."] query selector.

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode
let sanitizedName;
try {
sanitizedName = name.replace(/[^\x00-\x7F]/gu, '_').toLowerCase();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same here

const mName = el.getAttribute('name');
let sanitizedName;
try {
sanitizedName = mName.replace(/[^\x00-\x7F]/gu, '_').toLowerCase();

Choose a reason for hiding this comment

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

Woo I appreciate the thoroughness!

Yea I think it's a bit overkill since this isn't a public API and there's a short list of valid meta[name] values (all of which are just ASCII). Probably better to save the bytes and CPU for users e.g. just use meta[name="..."] query selector.

* @return {?string}
*/
getMetaByName(unusedName) {
return /** @type {?} */ (devAssert(null, 'not implemented'));

Choose a reason for hiding this comment

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

Should querying the headNode() be the default implementation and overriden only by AmpDocShadow?

}

/**
* Stores the value of an ampdoc's meta tag content for a given name.

Choose a reason for hiding this comment

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

Let's add some language that dissuades users from calling this. It should only be used during setup of the AmpDoc.

- Simplify meta name/content lookups
- Add test for non-shadow getMetaByName
@mdmower
Copy link
Contributor Author

mdmower commented Feb 5, 2020

Here is a simple amp-script commit that can be used to test this feature: mdmower@6cec785

@micajuine-ho
Copy link
Contributor

@mdmower Looks like we're good, just needed to push a blank commit

@mdmower
Copy link
Contributor Author

mdmower commented Feb 6, 2020

@mdmower Looks like we're good, just needed to push a blank commit

Thanks for your help @micajuine-ho !

@mdmower
Copy link
Contributor Author

mdmower commented Feb 6, 2020

@ampproject/wg-runtime or @ampproject/wg-performance could you please review this PR and the bundle size increase? For context, see #26535 (comment) . Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants