This repository was archived by the owner on Oct 22, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed
Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff 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 */
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 ) ;
You can’t perform that action at this time.
0 commit comments