Skip to content

Commit

Permalink
Merge pull request TryGhost#6153 from kevinansfield/fix-subdir-nav-is…
Browse files Browse the repository at this point in the history
…sues

Fix sub-dir being added to navigation urls when installed in sub-dir
  • Loading branch information
ErisDS committed Dec 9, 2015
2 parents 16c71dd + 81765c6 commit 70a481b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 33 deletions.
25 changes: 20 additions & 5 deletions core/client/app/components/gh-navitem-url-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,13 @@ export default TextField.extend({
},

notifyUrlChanged() {
let value = this.get('value').trim();
let url = this.get('value').trim();
let urlParts = document.createElement('a');
let baseUrl = this.get('baseUrl');
let baseUrlParts = document.createElement('a');
let url = value;

// ensure value property is trimmed
this.set('value', value);
this.set('value', url);

// leverage the browser's native URI parsing
urlParts.href = url;
Expand All @@ -113,12 +112,28 @@ export default TextField.extend({
this.set('value', url);
}

// remove the base url before sending to action
if (urlParts.host === baseUrlParts.host && !url.match(/^#/)) {
// get our baseUrl relativity checks in order
let isOnSameHost = urlParts.host === baseUrlParts.host;
let isAnchorLink = url.match(/^#/);
let isRelativeToBasePath = urlParts.pathname.indexOf(baseUrlParts.pathname) === 0;

// if our pathname is only missing a trailing / mark it as relative
if (`${urlParts.pathname}/` === baseUrlParts.pathname) {
isRelativeToBasePath = true;
}

// if relative to baseUrl, remove the base url before sending to action
if (!isAnchorLink && isOnSameHost && isRelativeToBasePath) {
url = url.replace(/^[a-zA-Z0-9\-]+:/, '');
url = url.replace(/^\/\//, '');
url = url.replace(baseUrlParts.host, '');
url = url.replace(baseUrlParts.pathname, '');

// handle case where url path is same as baseUrl path but missing trailing slash
if (urlParts.pathname.slice(-1) !== '/') {
url = url.replace(baseUrlParts.pathname.slice(0, -1), '');
}

if (!url.match(/^\//)) {
url = `/${url}`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,10 @@ describeComponent(
testUrl('test/nested');
});

it('handles a baseUrl with a path component', function () {
it('handles links to subdomains of blog domain', function () {
let expectedUrl = '';

this.set('baseUrl', `${currentUrl}blog/`);
this.set('baseUrl', 'http://example.com/');

this.on('updateUrl', (url) => {
expect(url).to.equal(expectedUrl);
Expand All @@ -334,40 +334,78 @@ describeComponent(
`);
let $input = this.$('input');

let testUrl = (url) => {
expectedUrl = url;
run(() => {
$input.val(`${currentUrl}blog${url}`).trigger('input');
});
run(() => {
$input.trigger('blur');
});
};

testUrl('/about');
testUrl('/about#contact');
testUrl('/test/nested');
expectedUrl = 'http://test.example.com/';
run(() => {
$input.val(expectedUrl).trigger('input').trigger('blur');
});
expect($input.val()).to.equal(expectedUrl);
});

it('handles links to subdomains of blog domain', function () {
let expectedUrl = '';
describe('with sub-folder baseUrl', function () {
beforeEach(function () {
this.set('baseUrl', `${currentUrl}blog/`);
});

this.set('baseUrl', 'http://example.com/');
it('handles URLs relative to base url', function () {
let expectedUrl = '';

this.on('updateUrl', (url) => {
expect(url).to.equal(expectedUrl);
this.on('updateUrl', (url) => {
expect(url).to.equal(expectedUrl);
});

this.render(hbs `
{{gh-navitem-url-input baseUrl=baseUrl url=url last=isLast change="updateUrl"}}
`);
let $input = this.$('input');

let testUrl = (url) => {
expectedUrl = url;
run(() => {
$input.val(`${currentUrl}blog${url}`).trigger('input');
});
run(() => {
$input.trigger('blur');
});
};

testUrl('/about');
testUrl('/about#contact');
testUrl('/test/nested');
});

this.render(hbs `
{{gh-navitem-url-input baseUrl=baseUrl url=url last=isLast change="updateUrl"}}
`);
let $input = this.$('input');
it('handles URLs relative to base host', function () {
let expectedUrl = '';

expectedUrl = 'http://test.example.com/';
run(() => {
$input.val(expectedUrl).trigger('input').trigger('blur');
this.on('updateUrl', (url) => {
expect(url).to.equal(expectedUrl);
});

this.render(hbs `
{{gh-navitem-url-input baseUrl=baseUrl url=url last=isLast change="updateUrl"}}
`);
let $input = this.$('input');

let testUrl = (url) => {
expectedUrl = url;
run(() => {
$input.val(url).trigger('input');
});
run(() => {
$input.trigger('blur');
});
};

testUrl(`http://${window.location.host}`);
testUrl(`https://${window.location.host}`);
testUrl(`http://${window.location.host}/`);
testUrl(`https://${window.location.host}/`);
testUrl(`http://${window.location.host}/test`);
testUrl(`https://${window.location.host}/test`);
testUrl(`http://${window.location.host}/#test`);
testUrl(`https://${window.location.host}/#test`);
testUrl(`http://${window.location.host}/another/folder`);
testUrl(`https://${window.location.host}/another/folder`);
});
expect($input.val()).to.equal(expectedUrl);
});
}
);

0 comments on commit 70a481b

Please sign in to comment.