Skip to content
This repository was archived by the owner on Oct 22, 2019. It is now read-only.

Commit fbbbd0f

Browse files
fstanissebastianbenz
authored andcommitted
Adds formats() to Document (#2039)
* move format logic, add filter <> format check * move logic for filter checks
1 parent 79d8d89 commit fbbbd0f

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

lib/Document.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ module.exports = class Document {
7979
return '';
8080
}
8181

82+
formats() {
83+
if (this.metadata.formats) {
84+
return this.metadata.formats;
85+
}
86+
87+
if (this.isAmpStory) {
88+
return ['stories'];
89+
}
90+
if (this.isAmpAds) {
91+
return ['ads'];
92+
}
93+
if (this.isAmpEmail) {
94+
return ['email'];
95+
}
96+
97+
// Use websites as fallback as isAmpWebsites could be true for all formats
98+
return ['websites'];
99+
}
100+
82101
/**
83102
* Returns true if the document contains a canonical link
84103
*/

lib/DocumentParser.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,19 @@ class DocumentParser {
246246
return string.replace('https://cdn.ampproject.org/amp4ads-v0.js', 'https://cdn.ampproject.org/v0.js');
247247
}
248248

249+
verifySectionFilters(section) {
250+
if (!section || !section.filters) {
251+
return;
252+
}
253+
for (const filter of section.filters) {
254+
if (!this.document.formats().includes(filter)) {
255+
throw new Error(`Section uses filter that's not listed in formats: ${filter}`);
256+
}
257+
}
258+
}
259+
249260
endSection() {
261+
this.verifySectionFilters(this.section);
250262
this.section = null;
251263
}
252264

spec/compiler/DocumentSpec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ describe("Document", function() {
8080

8181
});
8282

83+
describe("formats", function() {
84+
it("is determined automatically ", function() {
85+
doc.isAmpEmail = true;
86+
expect(doc.formats()).toEqual(['email']);
87+
});
88+
it("is extracted from metadata", function() {
89+
doc.isAmpEmail = true;
90+
doc.metadata.formats = ['websites', 'email'];
91+
expect(doc.formats()).toEqual(['websites', 'email']);
92+
});
93+
});
94+
8395
describe("hasCanonical is", function() {
8496
it("true if head contains canonical link ", function() {
8597
doc.appendHead(CANONICAL);

0 commit comments

Comments
 (0)