Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Possibility to add optional parameters #186

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions app-route.html
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,14 @@
if (!this.route) {
return;
}

var path = this.route.path;
var path = this.route.path || "";
var pattern = this.pattern;

if (!pattern) {
return;
}

if (!path) {
this.__resetProperties();
return;
}

var remainingPieces = path.split('/');
var patternPieces = pattern.split('/');

Expand All @@ -268,17 +263,25 @@
break;
}
var pathPiece = remainingPieces.shift();

// We don't match this path.
if (!pathPiece && pathPiece !== '') {
var patternPieceOptional = false;

if (patternPiece.slice(-1) == '?') {
// Optional parameter.
patternPiece = patternPiece.slice(0, -1);
patternPieceOptional = true;
} else if (!pathPiece && pathPiece !== '') {
// No optional parameter and we don't match this path.
this.__resetProperties();
return;
}
matched.push(pathPiece);

if (pathPiece !== undefined) {
matched.push(pathPiece);
}

if (patternPiece.charAt(0) == ':') {
namedMatches[patternPiece.slice(1)] = pathPiece;
} else if (patternPiece !== pathPiece) {
} else if (patternPiece !== pathPiece && !patternPieceOptional) {
this.__resetProperties();
return;
}
Expand Down
214 changes: 214 additions & 0 deletions test/app-route-optional-params.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
<!doctype html>
<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
<title>app-route-optional-params</title>

<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../web-component-tester/browser.js"></script>

<link rel="import" href="../../polymer/polymer.html">
<link rel="import" href="../app-route.html">
</head>

<body>

<test-fixture id="SimpleRoute">
<template>
<app-route
pattern="/:id?"></app-route>
</template>
</test-fixture>

<test-fixture id="ComplexRoute">
<template>
<app-route
pattern="/client/:id/:action?/:view?"></app-route>
</template>
</test-fixture>

<test-fixture id="NoParametersRoute">
<template>
<app-route
pattern="/client/list?"></app-route>
</template>
</test-fixture>

<script>
'use strict';

suite('simple optional parameter', function () {
var route;

setup(function() {
route = fixture('SimpleRoute');
});

test('active route with undefined optional parameter', function() {
route.route = {
prefix: '/client',
path: '',
__queryParams: {}
};

expect(route.active).to.be.equal(true);
expect(route.data).to.be.deep.equal({
id: undefined
});
expect(route.tail).to.be.deep.equal({
prefix: '/client',
path: '',
__queryParams: {}
});
});

test('active route with filled optional parameter', function() {
route.route = {
prefix: '/client',
path: '/c123',
__queryParams: {}
};

expect(route.active).to.be.equal(true);
expect(route.data).to.be.deep.equal({
id: 'c123'
});
expect(route.tail).to.be.deep.equal({
prefix: '/client/c123',
path: '',
__queryParams: {}
});
});

});

suite('complex optional parameter', function () {
var route;

setup(function() {
route = fixture('ComplexRoute');
});

test('path doesn\'t match the route because id parameter is required', function() {
route.route = {
prefix: '',
path: '/client',
__queryParams: {}
};

expect(route.active).to.be.equal(false);
expect(route.data).to.be.deep.equal({});
});

test('path matches the route because third and fourth parameters are optionals', function() {
route.route = {
prefix: '',
path: '/client/c123',
__queryParams: {}
};

expect(route.active).to.be.equal(true);
expect(route.data).to.be.deep.equal({
id: 'c123',
action: undefined,
view: undefined
});
expect(route.tail).to.be.deep.equal({
prefix: '/client/c123',
path: '',
__queryParams: {}
});
});

test('path matches the route because fourth parameters is optional', function() {
route.route = {
prefix: '',
path: '/client/c123/overview',
__queryParams: {}
};

expect(route.active).to.be.equal(true);
expect(route.data).to.be.deep.equal({
id: 'c123',
action: 'overview',
view: undefined
});
expect(route.tail).to.be.deep.equal({
prefix: '/client/c123/overview',
path: '',
__queryParams: {}
});
});

});

suite('no parameters router', function () {
var route;

setup(function() {
route = fixture('NoParametersRoute');
});

test('path match the route because \'list\' in second part of the path is optional', function() {
route.route = {
prefix: '',
path: '/client',
__queryParams: {}
};

expect(route.active).to.be.equal(true);
expect(route.data).to.be.deep.equal({});
expect(route.tail).to.be.deep.equal({
prefix: '/client',
path: '',
__queryParams: {}
});
});

test('path matches the route because second parameter is filled, although it is optional', function() {
route.route = {
prefix: '',
path: '/client/list',
__queryParams: {}
};

expect(route.active).to.be.equal(true);
expect(route.data).to.be.deep.equal({});
expect(route.tail).to.be.deep.equal({
prefix: '/client/list',
path: '',
__queryParams: {}
});
});

test('path matches the route because \'list\' in second part of the path is not required', function() {
route.route = {
prefix: '',
path: '/client/overview',
__queryParams: {}
};

expect(route.active).to.be.equal(true);
expect(route.data).to.be.deep.equal({});
expect(route.tail).to.be.deep.equal({
prefix: '/client/overview',
path: '',
__queryParams: {}
});
});

});

</script>

</body>

</html>
3 changes: 2 additions & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
'app-route.html?wc-ce=true&wc-shadydom=true',
'app-location.html?wc-ce=true&wc-shadydom=true',
'test-observer-app.html?wc-ce=true&wc-shadydom=true',
'test-app-example-1.html?wc-ce=true&wc-shadydom=true'
'test-app-example-1.html?wc-ce=true&wc-shadydom=true',
'app-route-optional-params.html?wc-ce=true&wc-shadydom=true'
]);
</script>
</body>
Expand Down