Skip to content

Commit

Permalink
amp-apester: Support keyword lookup in AmpDocShadow (#27345)
Browse files Browse the repository at this point in the history
* amp-apester: Support keyword lookup in AmpDocShadow

Query meta[name="keywords"] using method supported by
AmpDocShadow as well as other AmpDoc subtypes.

* extratctTitle --> extractTitle

* Add test note about clearing meta_
  • Loading branch information
mdmower committed Apr 3, 2020
1 parent 74c149a commit 22b70b0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion extensions/amp-apester-media/0.1/amp-apester-media.js
Expand Up @@ -152,7 +152,7 @@ class AmpApesterMedia extends AMP.BaseElement {
'data-apester-channel-id'
),
renderer: true,
tags: extractTags(this.getAmpDoc().getRootNode(), this.element),
tags: extractTags(this.getAmpDoc(), this.element),
};
}

Expand Down
Expand Up @@ -14,18 +14,23 @@
* limitations under the License.
*/

import {Services} from '../../../../src/services';
import {
extractArticleTags,
extractElementTags,
extractTags,
extratctTitle,
extractTitle,
} from '../utils';

describes.realWin('amp-apester-media-utils', {}, (unused) => {
beforeEach(() => {
document.body.textContent = '';
document.head.textContent = '';
});
afterEach(() => {
// Clear cached AmpDoc meta since document is reused in each test
Services.ampdoc(document).meta_ = null;
});

it('Extract element tags as empty array when there is no element', () => {
const expected = [];
Expand All @@ -51,7 +56,7 @@ describes.realWin('amp-apester-media-utils', {}, (unused) => {

it('Extract title works well with no dl json', () => {
const expected = [];
const tags = extratctTitle(document);
const tags = extractTitle(document);
expect(tags).to.deep.equal(expected);
});

Expand All @@ -76,7 +81,7 @@ describes.realWin('amp-apester-media-utils', {}, (unused) => {
}`;
element.text = jsonLd;
document.body.appendChild(element);
const tags = extratctTitle(document);
const tags = extractTitle(document);
expect(tags).to.deep.equal(expected);
});

Expand All @@ -101,7 +106,7 @@ describes.realWin('amp-apester-media-utils', {}, (unused) => {
}`;
element.text = jsonLd;
document.body.appendChild(element);
const tags = extratctTitle(document);
const tags = extractTitle(document);
expect(tags).to.deep.equal(expected);
});

Expand Down Expand Up @@ -129,7 +134,7 @@ describes.realWin('amp-apester-media-utils', {}, (unused) => {
document.body.appendChild(element);
}

const tags = extratctTitle(document);
const tags = extractTitle(document);
expect(tags).to.deep.equal(expected);
});

Expand All @@ -139,7 +144,8 @@ describes.realWin('amp-apester-media-utils', {}, (unused) => {
meta.setAttribute('name', 'keywords');
meta.setAttribute('content', '');
document.head.appendChild(meta);
const tags = extractArticleTags(document);
const ampdoc = Services.ampdoc(document);
const tags = extractArticleTags(ampdoc);
expect(tags).to.deep.equal(expected);
});

Expand All @@ -149,7 +155,8 @@ describes.realWin('amp-apester-media-utils', {}, (unused) => {
meta.setAttribute('name', 'keywords');
meta.setAttribute('content', '');
document.head.appendChild(meta);
const tags = extractArticleTags(document);
const ampdoc = Services.ampdoc(document);
const tags = extractArticleTags(ampdoc);
expect(tags).to.deep.equal(expected);
});

Expand All @@ -159,7 +166,8 @@ describes.realWin('amp-apester-media-utils', {}, (unused) => {
meta.setAttribute('name', 'keywords');
meta.setAttribute('content', 'this is, the, tag');
document.head.appendChild(meta);
const tags = extractArticleTags(document);
const ampdoc = Services.ampdoc(document);
const tags = extractArticleTags(ampdoc);
expect(tags).to.deep.equal(expected);
});

Expand Down Expand Up @@ -201,7 +209,8 @@ describes.realWin('amp-apester-media-utils', {}, (unused) => {
scriptElement.text = jsonLd;
document.body.appendChild(scriptElement);

const tags = extractTags(document, element);
const ampdoc = Services.ampdoc(document);
const tags = extractTags(ampdoc, element);
expect(tags).to.deep.equal(expected);
});

Expand Down Expand Up @@ -233,7 +242,8 @@ describes.realWin('amp-apester-media-utils', {}, (unused) => {
scriptElement.text = jsonLd;
document.body.appendChild(scriptElement);

const tags = extractTags(document, element);
const ampdoc = Services.ampdoc(document);
const tags = extractTags(ampdoc, element);
expect(tags).to.deep.equal(expected);
});
});
30 changes: 14 additions & 16 deletions extensions/amp-apester-media/0.1/utils.js
Expand Up @@ -88,10 +88,10 @@ export function extractElementTags(element) {

/**
* Extract article's first 5 words from the title and use them for tags.
* @param {!Node} root
* @param {!Document|!ShadowRoot} root
* @return {!Array<string>}
*/
export function extratctTitle(root) {
export function extractTitle(root) {
const scriptTags = toArray(
root.querySelectorAll('script[type="application/ld+json"]')
);
Expand All @@ -117,31 +117,29 @@ export function extratctTitle(root) {
}
/**
* Extracts article meta keywords
* @param {*} root
* @return {*} TODO(#23582): Specify return type
* @param {!../../../src/service/ampdoc-impl.AmpDoc} ampdoc
* @return {Array<string>}
*/
export function extractArticleTags(root) {
const metaKeywords = root.querySelector('meta[name="keywords"]') || {
content: '',
};
return metaKeywords.content
.trim()
export function extractArticleTags(ampdoc) {
return (ampdoc.getMetaByName('keywords') || '')
.split(',')
.filter((e) => e)
.map((e) => e.trim());
.map((e) => e.trim())
.filter((e) => e);
}

/**
* Extracts tags from a given element and document.
* @param {!Node} root
* @param {!../../../src/service/ampdoc-impl.AmpDoc} ampdoc
* @param {!Node} element
* @return {Array<string>}
*/
export function extractTags(root, element) {
export function extractTags(ampdoc, element) {
const extractedTags = extractElementTags(element) || [];
const articleMetaTags = extractArticleTags(root);
const articleMetaTags = extractArticleTags(ampdoc);
const concatedTags = extractedTags.concat(
articleMetaTags.length ? articleMetaTags : extratctTitle(root) || []
articleMetaTags.length
? articleMetaTags
: extractTitle(ampdoc.getRootNode()) || []
);
const loweredCase = concatedTags.map((tag) => tag.toLowerCase().trim());
const noDuplication = loweredCase.filter(
Expand Down

0 comments on commit 22b70b0

Please sign in to comment.