Skip to content

Commit 1279448

Browse files
fixed: Computed path attributes and invalid query selector strings autoscrolling
1 parent 660a0a7 commit 1279448

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

src/components/route-hoc.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
import $ from 'bianco.query'
1010
import getCurrentRoute from '../get-current-route.js'
1111
import { get as getAttr } from 'bianco.attr'
12-
import { createDefaultSlot, getAttribute } from '../util.js'
12+
import {
13+
createDefaultSlot,
14+
getAttribute,
15+
isValidQuerySelectorString,
16+
} from '../util.js'
1317
import compose from 'cumpa'
1418

1519
const getInitialRouteValue = (pathToRegexp, path, options) => {
@@ -42,7 +46,8 @@ export const routeHoc = ({ slots, attributes }) => {
4246
// create the component state
4347
const currentRoute = getCurrentRoute()
4448
const path =
45-
getAttribute(attributes, PATH_ATTRIBUTE) || getAttr(el, PATH_ATTRIBUTE)
49+
getAttribute(attributes, PATH_ATTRIBUTE)?.evaluate(context) ||
50+
getAttr(el, PATH_ATTRIBUTE)
4651
const pathToRegexp = toRegexp(path, [])
4752
const state = {
4853
pathToRegexp,
@@ -127,11 +132,12 @@ export const routeHoc = ({ slots, attributes }) => {
127132

128133
// if this route component was already mounted we need to update it
129134
if (prevRoute) this.slot.update({}, this.context)
130-
// this route component was never mounted so we need to create its DOM
135+
// this route component was never mounted, so we need to create its DOM
131136
else this.mountSlot()
132137

133138
// emulate the default browser anchor links behaviour
134-
if (route.hash) $(route.hash)?.[0].scrollIntoView()
139+
if (route.hash && isValidQuerySelectorString(route.hash))
140+
$(route.hash)?.[0].scrollIntoView()
135141
},
136142
callLifecycleProperty(method, ...params) {
137143
const attr = getAttribute(attributes, method)

src/util.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ export const createDefaultSlot = (attributes = []) => {
4141
},
4242
])
4343
}
44+
45+
// True if the selector string is valid
46+
export const isValidQuerySelectorString = (selector) =>
47+
/^([a-zA-Z0-9-_*#.:[\]\s>+~()='"]|\\.)+$/.test(selector)

test/components.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import NestedUpdates from './components/nested-updates.riot'
44
import RecursiveUpdatesBugRouter from './components/recursive-updates-bug-router.riot'
55
import StaticBasePath from './components/static-base-path.riot'
66
import SameRouteMatches from './components/same-route-matches.riot'
7+
import ComputedRoutes from './components/computed-routes.riot'
78
import { component } from 'riot'
89
import { expect } from 'chai'
910
import { router, defaults } from '../src/index.js'
@@ -105,4 +106,27 @@ describe('components', function () {
105106

106107
comp.unmount()
107108
})
109+
110+
it('Computed routes get properly rendered', async function () {
111+
const el = document.createElement('div')
112+
const comp = component(ComputedRoutes)(el)
113+
114+
expect(comp.$('p')).to.be.not.ok
115+
116+
router.push('/home')
117+
118+
await sleep()
119+
120+
expect(comp.$('p')).to.be.ok
121+
122+
await sleep()
123+
124+
router.push('/')
125+
126+
await sleep()
127+
128+
expect(comp.$('p')).to.be.not.ok
129+
130+
comp.unmount()
131+
})
108132
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<computed-routes>
2+
<router>
3+
<route path={state.home}>
4+
<p>{state.name}</p>
5+
</route>
6+
</router>
7+
<script>
8+
import { Router, Route } from '../../src/index.js'
9+
10+
export default {
11+
components: {
12+
Router,
13+
Route,
14+
},
15+
state: {
16+
home: '/home',
17+
name: 'hello'
18+
},
19+
}
20+
</script>
21+
</computed-routes>

0 commit comments

Comments
 (0)