Skip to content

Commit

Permalink
ampdoc: Add get/setMetaByName methods (#26609)
Browse files Browse the repository at this point in the history
* ampdoc: Add get/setMetaByName methods

- 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.

* ampdoc: Correct jsdoc override

* ampdoc: Revisions based on review (relax)

- Simplify meta name/content lookups
- Add test for non-shadow getMetaByName
  • Loading branch information
mdmower committed Feb 6, 2020
1 parent 3d21e45 commit 6e9b7ab
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/multidoc-manager.js
Expand Up @@ -358,6 +358,9 @@ export class MultidocManager {
// Ignore.
} else if (name == 'viewport') {
// Ignore.
} else if (name) {
// Store meta name/content pairs.
ampdoc.setMetaByName(name, n.getAttribute('content') || '');
} else {
// TODO(dvoytenko): copy other meta tags.
dev().warn(TAG, 'meta ignored: ', n);
Expand Down
52 changes: 52 additions & 0 deletions src/service/ampdoc-impl.js
Expand Up @@ -24,6 +24,7 @@ import {
removeDocumentVisibilityChangeListener,
} from '../utils/document-visibility';
import {dev, devAssert} from '../log';
import {escapeCssSelectorIdent} from '../css';
import {getParentWindowFrameElement, registerServiceBuilder} from '../service';
import {getShadowRootNode} from '../shadow-embed';
import {isDocumentReady, whenDocumentReady} from '../document-ready';
Expand Down Expand Up @@ -317,6 +318,9 @@ export class AmpDoc {
/** @private {!Object<string, string>} */
this.params_ = (opt_options && opt_options.params) || map();

/** @protected {!Object<string, string>} */
this.meta_ = (opt_options && opt_options.meta) || map();

/** @private @const {!Array<string>} */
this.declaredExtensions_ = [];

Expand Down Expand Up @@ -414,6 +418,38 @@ export class AmpDoc {
return v == null ? null : v;
}

/**
* Returns the value of an ampdoc's meta tag content for a given name, or
* `null` if the meta tag does not exist.
* @param {string} name
* @return {?string}
*/
getMetaByName(name) {
if (!name) {
return null;
}

const el = dev()
.assertElement(this.win.document.head)
.querySelector(`meta[name="${escapeCssSelectorIdent(name)}"]`);

return el ? el.getAttribute('content') || '' : null;
}

/**
* Stores the value of an ampdoc's meta tag content for a given name. To be
* implemented by subclasses.
* @param {string} unusedName
* @param {string} unusedContent
*
* Avoid using this method in components. It is only meant to be used by the
* runtime for AmpDoc subclasses where <meta> elements do not exist and name/
* content pairs must be stored in this.meta_.
*/
setMetaByName(unusedName, unusedContent) {
devAssert(null, 'not implemented');
}

/**
* Returns whether the specified extension has been declared on this ampdoc.
* @param {string} extensionId
Expand Down Expand Up @@ -867,6 +903,22 @@ export class AmpDocShadow extends AmpDoc {
whenReady() {
return this.readyPromise_;
}

/** @override */
getMetaByName(name) {
return this.meta_[name] !== undefined ? this.meta_[name] : null;
}

/** @override */
setMetaByName(name, content) {
if (!name) {
throw dev().createError(
'Attempted to store invalid meta name/content pair'
);
}

this.meta_[name] = content;
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions test/unit/test-ampdoc.js
Expand Up @@ -121,6 +121,14 @@ describes.sandboxed('AmpDocService', {}, () => {
expect(service.getAmpDoc(div)).to.equal(service.getSingleDoc());
});

it('should return meta content values', () => {
const meta = document.createElement('meta');
meta.setAttribute('name', 'abc');
meta.setAttribute('content', '123');
document.head.appendChild(meta);
expect(service.getAmpDoc(meta).getMetaByName('abc')).to.equal('123');
});

// For example, <amp-next-page> creates shadow documents in single-doc
// mode.
describe('shadow documents', () => {
Expand Down
9 changes: 9 additions & 0 deletions test/unit/test-runtime.js
Expand Up @@ -1345,6 +1345,15 @@ describes.realWin(
).to.not.exist;
});

it('should import meta content', () => {
const metaEl = win.document.createElement('meta');
metaEl.setAttribute('name', 'abc');
metaEl.setAttribute('content', '123');
importDoc.head.appendChild(metaEl);
const amp = win.AMP.attachShadowDoc(hostElement, importDoc, docUrl);
expect(amp.ampdoc.getMetaByName('abc')).to.equal('123');
});

it('should start as visible by default', () => {
win.AMP.attachShadowDoc(hostElement, importDoc, docUrl);
expect(ampdoc.getVisibilityState()).to.equal('visible');
Expand Down

0 comments on commit 6e9b7ab

Please sign in to comment.