11/* eslint-disable */
22// External URL (Protocol URL)
3- const TEST_URL = / ^ \w + : \/ \/ / ;
4- // TODO(michael-ciniawsky)
5- // extend with custom matchers
6- // e.g <custom-element custom-src="">
7- // (`options.url.filter`) (#159)
8- const MATCH_ATTRS = [
3+ const URL = / ^ \w + : \/ \/ / ;
4+ const ATTRS = [
95 { attrs : { src : true } } ,
106 { attrs : { href : true } } ,
117 { attrs : { srcset : true } } ,
128] ;
139
14- // TODO(michael-ciniawsky)
15- // add filter method for urls (e.g `options.url.filter`) (#158)
1610const filter = ( url , options ) => {
17- return TEST_URL . test ( url ) || url . startsWith ( '//' ) ;
11+ if ( URL . test ( url ) ) {
12+ return true ;
13+ }
14+
15+ if ( url . startsWith ( '//' ) ) {
16+ return true ;
17+ }
18+
19+ if ( typeof options . url === 'string' ) {
20+ return url . includes ( options . url ) ;
21+ }
22+
23+ if ( options . url instanceof RegExp ) {
24+ return options . url . test ( url ) ;
25+ }
26+
27+ if ( typeof options . url === 'function' ) {
28+ return options . url ( url ) ;
29+ }
30+
31+ return false
1832} ;
1933
2034export default function ( options = { } ) {
2135 return function ( tree ) {
2236 let idx = 0 ;
2337 const urls = { } ;
2438
25- tree . match ( MATCH_ATTRS , ( node ) => {
39+ tree . match ( ATTRS , ( node ) => {
2640 // <tag src="path/to/file.ext">
2741 if ( node . attrs . src ) {
2842 // Ignore <import>/<include
29- if ( node . tag === 'import' || node . tag === 'include' ) return node ;
30- // Ignore external && filtered urls
31- if ( filter ( node . attrs . src , options ) ) return node ;
32- // Add url to messages.urls
43+ if ( node . tag === 'import' || node . tag === 'include' ) {
44+ return node ;
45+ }
46+
47+ // Ignore external && filtered URLs
48+ if ( options . url && filter ( node . attrs . src , options ) ) {
49+ return node ;
50+ }
51+
52+ // Add URL to result.messages.urls
3353 urls [ `HTML__URL__${ idx } ` ] = node . attrs . src ;
34- // Add content placeholders to HTML
54+
55+ // Add URL content placeholders to HTML
3556 node . attrs . src = '${' + `HTML__URL__${ idx } ` + '}' ;
3657
3758 idx ++ ;
3859
3960 return node ;
4061 }
62+
4163 // <tag href="path/to/file.ext">
4264 if ( node . attrs . href ) {
43- // Ignore external && filtered urls
44- if ( filter ( node . attrs . href , options ) ) return node ;
45- // Add url to messages.urls
65+ // Ignore external && filtered URLs
66+ if ( filter ( node . attrs . href , options ) ) {
67+ return node ;
68+ }
69+
70+ // Add URL to result.messages.urls
4671 urls [ `HTML__URL__${ idx } ` ] = node . attrs . href ;
47- // Add content placeholder to HTML
72+
73+ // Add URL content placeholder to HTML
4874 node . attrs . href = '${' + `HTML__URL__${ idx } ` + '}' ;
4975
5076 idx ++ ;
@@ -53,11 +79,14 @@ export default function(options = {}) {
5379 }
5480 // <tag srcset="path/to/file.ext">
5581 if ( node . attrs . srcset ) {
56- // Ignore external && filtered urls
57- if ( filter ( node . attrs . srcset , options ) ) return node ;
58- // Add url to messages.urls
82+ // Ignore external && filtered URLs
83+ if ( filter ( node . attrs . srcset , options ) ) {
84+ return node ;
85+ }
86+ // Add URL to messages.urls
5987 urls [ `HTML__URL__${ idx } ` ] = node . attrs . srcset ;
60- // Add content placeholder to HTML
88+
89+ // Add URL content placeholder to HTML
6190 node . attrs . srcset = '${' + `HTML__URL__${ idx } ` + '}' ;
6291
6392 idx ++ ;
@@ -66,7 +95,7 @@ export default function(options = {}) {
6695 }
6796 } ) ;
6897
69- // Add urls to result.messages
98+ // Add URLs to result.messages
7099 tree . messages . push ( urls ) ;
71100
72101 return tree ;
0 commit comments