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

Commit

Permalink
Match entered paths better
Browse files Browse the repository at this point in the history
Matches things better if the user puts a / in the input
  • Loading branch information
bbondy committed May 18, 2017
1 parent c4cdd46 commit c5d656a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
23 changes: 18 additions & 5 deletions app/common/lib/suggestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,16 @@ const sortBySimpleURL = (s1, s2) => {
if (!url1IsSimple && url2IsSimple) {
return 1
}
const url1IsSecure = s1.parsedUrl.protocol === 'https:'
const url2IsSecure = s2.parsedUrl.protocol === 'https:'
if (url1IsSimple && url2IsSimple) {
if (url1IsSecure && !url2IsSecure) {
return -1
}
if (!url1IsSecure && url2IsSecure) {
return 1
}
}
return 0
}

Expand Down Expand Up @@ -320,6 +330,7 @@ const getSortForSuggestions = (userInputLower) => {
userInputLower = userInputLower.replace(/^https:\/\//, '')
const userInputParts = userInputLower.split('/')
const userInputHost = userInputParts[0]
const userInputValue = userInputParts[1] || ''
const sortByDomain = getSortByDomain(userInputLower, userInputHost)
const sortByPath = getSortByPath(userInputLower)
const {sortByAccessCountWithAgeDecay} = require('./suggestion')
Expand All @@ -328,13 +339,15 @@ const getSortForSuggestions = (userInputLower) => {
s1.parsedUrl = s1.parsedUrl || urlParse(getURL(s1) || '')
s2.parsedUrl = s2.parsedUrl || urlParse(getURL(s2) || '')

const sortByDomainResult = sortByDomain(s1, s2)
if (sortByDomainResult !== 0) {
return sortByDomainResult
if (!userInputValue) {
const sortByDomainResult = sortByDomain(s1, s2)
if (sortByDomainResult !== 0) {
return sortByDomainResult
}
}

const path1 = s1.parsedUrl.path + (s1.parsedUrl.hash || '')
const path2 = s2.parsedUrl.path + (s2.parsedUrl.hash || '')
const path1 = s1.parsedUrl.host + s1.parsedUrl.path + (s1.parsedUrl.hash || '')
const path2 = s2.parsedUrl.host + s2.parsedUrl.path + (s2.parsedUrl.hash || '')
const sortByPathResult = sortByPath(path1, path2)
if (sortByPathResult !== 0) {
return sortByPathResult
Expand Down
5 changes: 2 additions & 3 deletions app/renderer/components/navigation/urlBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class UrlBar extends React.Component {
this.onContextMenu = this.onContextMenu.bind(this)
this.keyPressed = false
this.showAutocompleteResult = debounce(() => {
if (this.keyPressed || !this.urlInput || this.props.locationValueSuffix.length === 0) {
if (this.keyPressed || !this.urlInput) {
return
}
this.updateAutocomplete(this.lastVal)
Expand Down Expand Up @@ -392,7 +392,7 @@ class UrlBar extends React.Component {
} else if (this.props.location !== prevProps.location) {
// This is a url nav change
this.setValue(UrlUtil.getDisplayLocation(this.props.location, pdfjsEnabled))
} else if (this.props.hasLocationValueSuffix &&
} else if (this.props.hasSuggestionMatch &&
this.props.isActive &&
this.props.locationValueSuffix !== this.lastSuffix) {
this.showAutocompleteResult()
Expand Down Expand Up @@ -508,7 +508,6 @@ class UrlBar extends React.Component {
props.title = activeFrame.get('title') || ''
props.scriptsBlocked = activeFrame.getIn(['noScript', 'blocked'])
props.isSecure = activeFrame.getIn(['security', 'isSecure'])
props.hasLocationValueSuffix = urlbar.getIn(['suggestions', 'urlSuffix'])
props.hasSuggestionMatch = urlbar.getIn(['suggestions', 'hasSuggestionMatch'])
props.startLoadTime = activeFrame.get('startLoadTime')
props.endLoadTime = activeFrame.get('endLoadTime')
Expand Down
49 changes: 49 additions & 0 deletions test/unit/app/common/lib/suggestionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ describe('suggestion unit tests', function () {
it('trailing hash is considered simple', function () {
assert.equal(this.sort('https://brave.com/#', 'https://twitter.com'), 0)
})
it('Prefers https sipmle URLs', function () {
assert(this.sort('https://brave.com', 'http://brave.com') < 0)
})
})
describe('getSortByDomain', function () {
before(function () {
Expand Down Expand Up @@ -248,5 +251,51 @@ describe('suggestion unit tests', function () {
assert(this.sort('https://www.google.com', 'https://www.google.com/extra') < 0)
})
})
describe('getSortForSuggestions', function () {
describe('with url entered as path', function () {
before(function () {
const userInputLower = 'brianbondy.com/projects'
const userInputParts = userInputLower.split('/')
const userInputHost = userInputParts[0]
const internalSort = suggestion.getSortForSuggestions(userInputLower, userInputHost)
this.sort = (url1, url2) => {
return internalSort(
{ location: url1, parsedUrl: urlParse(url1) },
{ location: url2, parsedUrl: urlParse(url2) }
)
}
})
it('returns 0 when both urls are the same', function () {
assert.equal(this.sort('https://www.google.com', 'https://www.google.com'), 0)
})
it('matches exact path if more specific path is specified', function () {
assert(this.sort('https://brianbondy.com', 'https://www.brianbondy.com/projects/2') > 0)
})
})
describe('with single string entered', function () {
before(function () {
const userInputLower = 'brianbondy.c'
const userInputParts = userInputLower.split('/')
const userInputHost = userInputParts[0]
const internalSort = suggestion.getSortForSuggestions(userInputLower, userInputHost)
this.sort = (url1, url2) => {
return internalSort(
{ location: url1, parsedUrl: urlParse(url1) },
{ location: url2, parsedUrl: urlParse(url2) }
)
}
})
it('matches on domain name first', function () {
assert(this.sort('https://www.brianbondy.com', 'https://www.google.com/brianbondy.co') < 0)
})
it('matches with or without protocol', function () {
assert(this.sort('https://www.2brianbondy.com', 'http://www.brianbondy.com') > 0)
assert(this.sort('https://brianbondy.com', 'www.brianbondy.com') < 0)
})
it('non-wwww. matches before www.', function () {
assert(this.sort('https://brianbondy.com', 'www.brianbondy.com') < 0)
})
})
})
})
})

0 comments on commit c5d656a

Please sign in to comment.