11/* eslint-disable */
22// External URL (Protocol URL)
33const URL = / ^ \w + : \/ \/ / ;
4- // TODO(michael-ciniawsky)
5- // extend with custom matchers
6- // e.g <custom-element custom-src="">
7- // (`options.url.filter`) (#159)
4+ // Attributes Matcher
85const ATTRS = [
96 { attrs : { src : true } } ,
107 { attrs : { href : true } } ,
118 { attrs : { srcset : true } } ,
129] ;
1310
14- // TODO(michael-ciniawsky)
15- // add filter method for urls (e.g `options.url.filter`) (#158)
16- const filter = ( url ) => {
17- return URL . test ( url ) || url . startsWith ( '//' ) ;
11+ const filter = ( url , options ) => {
12+ if ( URL . test ( url ) ) {
13+ return true ;
14+ }
15+
16+ if ( url . startsWith ( '//' ) ) {
17+ return true ;
18+ }
19+
20+ if ( options . url instanceof RegExp ) {
21+ return options . url . test ( url ) ;
22+ }
23+
24+ if ( typeof options . url === 'function' ) {
25+ return options . url ( url ) ;
26+ }
27+
28+ return false ;
1829} ;
1930
2031export default function ( options = { } ) {
@@ -30,7 +41,7 @@ export default function (options = {}) {
3041 }
3142
3243 // Ignore external && filtered urls
33- if ( filter ( node . attrs . src ) ) {
44+ if ( filter ( node . attrs . src , options ) ) {
3445 return node ;
3546 }
3647
@@ -48,10 +59,11 @@ export default function (options = {}) {
4859
4960 return node ;
5061 }
62+
5163 // <tag href="path/to/file.ext">
5264 if ( node . attrs . href ) {
5365 // Ignore external && filtered urls
54- if ( filter ( node . attrs . href ) ) {
66+ if ( filter ( node . attrs . href , options ) ) {
5567 return node ;
5668 }
5769
@@ -72,7 +84,7 @@ export default function (options = {}) {
7284 // <tag srcset="path/to/file.ext">
7385 if ( node . attrs . srcset ) {
7486 // Ignore external && filtered urls
75- if ( filter ( node . attrs . srcset ) ) {
87+ if ( filter ( node . attrs . srcset , options ) ) {
7688 return node ;
7789 }
7890
0 commit comments