Skip to content

Commit 741cd07

Browse files
committed
router-links: convert from kebab case to camel case param-* and query-* attributes
1 parent 5ae185c commit 741cd07

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/middlewares/router-links.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ const undelegate = function (el, handler) {
3838
el.removeEventListener(eventName, handler, false)
3939
}
4040

41+
const camelize = (str) => {
42+
if (str.indexOf('-') === -1) return str
43+
const words = str.split('-')
44+
let result = ''
45+
for (let i = 0; i < words.length; i++) {
46+
const word = words[i]
47+
result += i ? word.charAt(0).toUpperCase() + word.slice(1) : word
48+
}
49+
return result
50+
}
51+
4152
function mutationHandler (mutations, observer) {
4253
mutations.forEach(function (mutation) {
4354
if (mutation.type === 'attributes') {
@@ -64,7 +75,7 @@ function getAttributeValues (el, prefix, result) {
6475
for (let i = 0; i < attributes.length; i++) {
6576
const attr = attributes[i]
6677
if (attr.name.indexOf(prefix) === 0) {
67-
const paramName = attr.name.slice(prefix.length)
78+
const paramName = camelize(attr.name.slice(prefix.length))
6879
result[paramName] = attr.value
6980
}
7081
}

tests/functional/routerLinksTest.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class ParentView extends BaseParentView {
4040
<a id="a-grandchildlink" route="grandchild" query-name="test"></a>
4141
<a id="a-rootlink2" route="root" param-id="2"></a>
4242
<a id="a-rootlink3" route="root"></a>
43+
<a id="a-secondrootlink" route="secondroot" param-person-id="2" query-test-value="yyy"></a>
4344
<a id="a-replace" route="parent" replace></a>
4445
<a id="a-childlink" route="child" query-name="test"></a>
4546
<div id="div-a-parent" route="parent"><a id="childanchor"></a><a id="childanchor2"></a><div><a id="childanchor3"></a></div></div>
@@ -101,6 +102,7 @@ describe('routerLinks', () => {
101102
})
102103
})
103104
route('root', { path: 'root/:id', component: ParentView })
105+
route('secondroot', { path: 'secondroot/:personId', component: ParentView })
104106
}
105107
parentComponent = ParentView
106108
router = new Router({ location: 'memory', outlet, routes })
@@ -119,12 +121,29 @@ describe('routerLinks', () => {
119121
const parentEl = document.querySelector(parentTag)
120122
await parentEl.updateComplete
121123

122-
expect($('#a-parentlink').attr('href')).to.be.equal('/parent')
124+
expect($('#a-parentlink').attr('href')).to.be.equal('/parent')
125+
})
126+
})
127+
128+
it('should use param-* and query-* attributes to generate href', async function () {
129+
return router.transitionTo('parent').then(async function () {
130+
const parentEl = document.querySelector(parentTag)
131+
await parentEl.updateComplete
132+
123133
expect($('#a-rootlink2').attr('href')).to.be.equal('/root/2')
124134
expect($('#a-grandchildlink').attr('href')).to.be.equal('/parent/child/grandchild?name=test')
125135
})
126136
})
127137

138+
it('should convert param-* and query-* attributes from kebab to camel case', async function () {
139+
return router.transitionTo('parent').then(async function () {
140+
const parentEl = document.querySelector(parentTag)
141+
await parentEl.updateComplete
142+
143+
expect($('#a-secondrootlink').attr('href')).to.be.equal('/secondroot/2?testValue=yyy')
144+
})
145+
})
146+
128147
it('should update href attributes in anchor tags when attribute is changed', function () {
129148
return router.transitionTo('parent').then(async function () {
130149
const parentEl = document.querySelector(parentTag)

0 commit comments

Comments
 (0)