1
1
/* eslint-disable */
2
2
// External URL (Protocol URL)
3
3
const 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
8
5
const ATTRS = [
9
6
{ attrs : { src : true } } ,
10
7
{ attrs : { href : true } } ,
11
8
{ attrs : { srcset : true } } ,
12
9
] ;
13
10
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 ;
18
29
} ;
19
30
20
31
export default function ( options = { } ) {
@@ -30,7 +41,7 @@ export default function (options = {}) {
30
41
}
31
42
32
43
// Ignore external && filtered urls
33
- if ( filter ( node . attrs . src ) ) {
44
+ if ( filter ( node . attrs . src , options ) ) {
34
45
return node ;
35
46
}
36
47
@@ -48,10 +59,11 @@ export default function (options = {}) {
48
59
49
60
return node ;
50
61
}
62
+
51
63
// <tag href="path/to/file.ext">
52
64
if ( node . attrs . href ) {
53
65
// Ignore external && filtered urls
54
- if ( filter ( node . attrs . href ) ) {
66
+ if ( filter ( node . attrs . href , options ) ) {
55
67
return node ;
56
68
}
57
69
@@ -72,7 +84,7 @@ export default function (options = {}) {
72
84
// <tag srcset="path/to/file.ext">
73
85
if ( node . attrs . srcset ) {
74
86
// Ignore external && filtered urls
75
- if ( filter ( node . attrs . srcset ) ) {
87
+ if ( filter ( node . attrs . srcset , options ) ) {
76
88
return node ;
77
89
}
78
90
0 commit comments