File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -21,6 +21,8 @@ const maybeMetaRedirect = (visit, {page}) => {
2121
2222window . addEventListener ( 'DOMContentLoaded' , emitExdocLoaded )
2323
24+ export const LINK_SELECTOR = 'a[href]:not([href^="http"]):not([href*="/"]):is([href$=".html"], [href*=".html#"], [href^="#"])'
25+
2426if ( ! isEmbedded && window . location . protocol !== 'file:' ) {
2527 new Swup ( {
2628 animationSelector : false ,
@@ -30,7 +32,7 @@ if (!isEmbedded && window.location.protocol !== 'file:') {
3032 return path === window . location . pathname ||
3133 path === window . location . pathname + '.html'
3234 } ,
33- linkSelector : 'a[href]:not([href^="/"]):not([href^="http"])[href$=".html"]' ,
35+ linkSelector : LINK_SELECTOR ,
3436 hooks : {
3537 'page:load' : maybeMetaRedirect ,
3638 'page:view' : emitExdocLoaded
Original file line number Diff line number Diff line change 1+ import { el } from '../js/helpers'
2+ import { isCrossBuild , LINK_SELECTOR } from '../js/swup'
3+
4+ const linkMatches = ( href ) => el ( 'a' , { href} ) . matches ( LINK_SELECTOR )
5+
6+ describe ( 'swup' , ( ) => {
7+ it ( 'should absolute links' , ( ) => {
8+ expect ( linkMatches ( '/foo.html' ) ) . toBe ( false )
9+ } )
10+
11+ it ( 'should ignore external links' , ( ) => {
12+ expect ( linkMatches ( 'http://example.com/foo.html' ) ) . toBe ( false )
13+ expect ( linkMatches ( 'https://example.com/foo.html' ) ) . toBe ( false )
14+ } )
15+
16+ it ( 'should ignore links with slashes' , ( ) => {
17+ expect ( linkMatches ( 'foo/bar.html' ) ) . toBe ( false )
18+ } )
19+
20+ it ( 'should ignore any page not ending with .html' , ( ) => {
21+ expect ( linkMatches ( 'foo' ) ) . toBe ( false )
22+ expect ( linkMatches ( 'foo.js' ) ) . toBe ( false )
23+ } )
24+
25+ it ( 'should match relative links to other pages' , ( ) => {
26+ expect ( linkMatches ( 'bar.html' ) ) . toBe ( true )
27+ } )
28+
29+ it ( 'should match local links with anchors' , ( ) => {
30+ expect ( linkMatches ( 'foo.html#section' ) ) . toBe ( true )
31+ expect ( linkMatches ( 'foo#section' ) ) . toBe ( false )
32+ expect ( linkMatches ( '#section' ) ) . toBe ( true )
33+ expect ( linkMatches ( 'bar/foo.html#section' ) ) . toBe ( false )
34+ } )
35+ } )
You can’t perform that action at this time.
0 commit comments