@@ -28,24 +28,30 @@ test('should return promise', t => {
2828test ( 'should add nanoid to style links' , async t => {
2929 const input = '<link rel="stylesheet" href="style.css">' ;
3030 const html = ( await processing ( input ) ) . html ;
31- const id = queryString . parse ( parser ( html ) [ 0 ] . attrs . href . split ( '?' ) [ 1 ] ) . v ;
31+
32+ const { url, id} = getParts ( html , 'href' ) ;
3233 t . truthy ( id ) ;
34+ t . is ( url , 'style.css' ) ;
3335 t . is ( id . length , 21 ) ;
3436} ) ;
3537
3638test ( 'should add nanoid to script links' , async t => {
3739 const input = '<script src="script.js"></script>' ;
3840 const html = ( await processing ( input ) ) . html ;
39- const id = queryString . parse ( parser ( html ) [ 0 ] . attrs . src . split ( '?' ) [ 1 ] ) . v ;
41+
42+ const { url, id} = getParts ( html , 'src' ) ;
4043 t . truthy ( id ) ;
44+ t . is ( url , 'script.js' ) ;
4145 t . is ( id . length , 21 ) ;
4246} ) ;
4347
4448test ( 'should add nanoid to iframe links' , async t => {
4549 const input = '<iframe src="index.html"></iframe>' ;
4650 const html = ( await processing ( input , { tags : [ 'iframe' ] } ) ) . html ;
47- const id = queryString . parse ( parser ( html ) [ 0 ] . attrs . src . split ( '?' ) [ 1 ] ) . v ;
51+
52+ const { url, id} = getParts ( html , 'src' ) ;
4853 t . truthy ( id ) ;
54+ t . is ( url , 'index.html' ) ;
4955 t . is ( id . length , 21 ) ;
5056} ) ;
5157
@@ -64,11 +70,12 @@ test('should not remove other attributes', async t => {
6470 const input = '<link rel="stylesheet" href="style.css">' ;
6571 const html = ( await processing ( input ) ) . html ;
6672
67- const id = queryString . parse ( parser ( html ) [ 0 ] . attrs . href . split ( '?' ) [ 1 ] ) . v ;
73+ const { url , id } = getParts ( html , ' href' ) ;
6874 const rel = parser ( html ) [ 0 ] . attrs . rel ;
6975 t . truthy ( id ) ;
7076 t . truthy ( rel ) ;
7177 t . is ( rel , 'stylesheet' ) ;
78+ t . is ( url , 'style.css' ) ;
7279 t . is ( id . length , 21 ) ;
7380} ) ;
7481
@@ -77,11 +84,12 @@ test('should not add nano id', async t => {
7784 const input = `<link rel="stylesheet" href="style.css?v=${ staticID } ">` ;
7885 const html = ( await processing ( input ) ) . html ;
7986
80- const id = queryString . parse ( parser ( html ) [ 0 ] . attrs . href . split ( '?' ) [ 1 ] ) . v ;
87+ const { url , id } = getParts ( html , ' href' ) ;
8188 const rel = parser ( html ) [ 0 ] . attrs . rel ;
8289 t . truthy ( id ) ;
8390 t . truthy ( rel ) ;
8491 t . is ( rel , 'stylesheet' ) ;
92+ t . is ( url , 'style.css' ) ;
8593 t . is ( id , staticID ) ;
8694 t . is ( id . length , 21 ) ;
8795} ) ;
@@ -91,11 +99,12 @@ test('should add nano id for relative path', async t => {
9199 const input = `<link rel="stylesheet" href="/?v=${ staticID } ">` ;
92100 const html = ( await processing ( input ) ) . html ;
93101
94- const id = queryString . parse ( parser ( html ) [ 0 ] . attrs . href . split ( '?' ) [ 1 ] ) . v ;
102+ const { url , id } = getParts ( html , ' href' ) ;
95103 const rel = parser ( html ) [ 0 ] . attrs . rel ;
96104 t . truthy ( id ) ;
97105 t . truthy ( rel ) ;
98106 t . is ( rel , 'stylesheet' ) ;
107+ t . is ( url , '/' ) ;
99108 t . is ( id , staticID ) ;
100109 t . is ( id . length , 21 ) ;
101110} ) ;
@@ -108,3 +117,23 @@ test('should not add nano id for not url', async t => {
108117 t . truthy ( href ) ;
109118 t . is ( href , 'sadsadsadsda' ) ;
110119} ) ;
120+
121+ test ( 'should add nano id for root path' , async t => {
122+ const input = '<link rel="stylesheet" href="/style.css">' ;
123+ const html = ( await processing ( input ) ) . html ;
124+
125+ const { url, id} = getParts ( html , 'href' ) ;
126+ const rel = parser ( html ) [ 0 ] . attrs . rel ;
127+ t . truthy ( id ) ;
128+ t . truthy ( rel ) ;
129+ t . is ( rel , 'stylesheet' ) ;
130+ t . is ( url , '/style.css' ) ;
131+ t . is ( id . length , 21 ) ;
132+ } ) ;
133+
134+ function getParts ( html , attributeName ) {
135+ const parts = parser ( html ) [ 0 ] . attrs [ attributeName ] . split ( '?' ) ;
136+ const url = parts [ 0 ] ;
137+ const id = queryString . parse ( parts [ 1 ] ) . v ;
138+ return { url, id} ;
139+ }
0 commit comments