-
Notifications
You must be signed in to change notification settings - Fork 204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query from url #254
Query from url #254
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Extracts an annotation selection or default filter from a url. | ||
* | ||
* @param {string} url - The URL which may contain a '#annotations:<ID>' | ||
* fragment. | ||
* @return {Object} - An object with either an annotation ID or a filter string. | ||
*/ | ||
function extractAnnotationQuery(url) { | ||
var filter = {}; | ||
try { | ||
// Annotation IDs are url-safe-base64 identifiers | ||
// See https://tools.ietf.org/html/rfc4648#page-7 | ||
var annotFragmentMatch = url.match(/#annotations:([A-Za-z0-9_-]+)$/); | ||
var queryFragmentMatch = url.match(/#annotations:(query|q):(.+)$/i); | ||
if (queryFragmentMatch) { | ||
filter.query = queryFragmentMatch[2]; | ||
} else if (annotFragmentMatch) { | ||
filter.annotations = annotFragmentMatch[1]; | ||
} else { | ||
filter = null; | ||
} | ||
} catch (err) { | ||
filter = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What specific error are you trying to guard against here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was just following the previous codebase (function extractURLFromID formerly) I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was looking at the code and the only possible error is a URIError which would not be caught by this code. I'll remove the try/catch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two tests that fail if the try-catch code is not included, so I kept it in. |
||
} | ||
return filter; | ||
} | ||
|
||
module.exports = { | ||
extractAnnotationQuery: extractAnnotationQuery, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually if a module exports a single function then we use |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
var annotationIds = require('../extract-annotation-query'); | ||
|
||
describe('annotation queries', function () { | ||
var annotation = annotationIds.extractAnnotationQuery('http://localhost:3000#annotations:alphanum3ric_-only'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a utility called |
||
var queryVarA = annotationIds.extractAnnotationQuery('http://localhost:3000#annotations:q:user:USERNAME'); | ||
var queryVarB = annotationIds.extractAnnotationQuery('http://localhost:3000#annotations:QuerY:user:USERNAME'); | ||
var invalid = annotationIds.extractAnnotationQuery('http://localhost:3000#annotations:\"TRYINGTOGETIN\";EVILSCRIPT()'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A general comment about these tests is that it is easier to follow if the preparation/arrangement is part of the test case instead of being a separate step that happens in |
||
|
||
it ('accepts regular annotation id', function () { | ||
assert.equal(annotation.annotations, 'alphanum3ric_-only'); | ||
}); | ||
|
||
it ('returns null for query when annotation id exists', function() { | ||
assert.equal(annotation.query, null); | ||
}); | ||
|
||
it ('returns null on invalid query / id', function() { | ||
assert.equal(invalid, null); | ||
}); | ||
|
||
it ('produces a null annotation when valid query exists', function () { | ||
assert.equal(queryVarA.annotations, null); | ||
}); | ||
|
||
it ('accepts query style A ("q:")', function () { | ||
assert.equal(queryVarA.query, 'user:USERNAME'); | ||
}); | ||
|
||
it ('accepts query style B ("query:")', function () { | ||
assert.equal (queryVarB.query, 'user:USERNAME'); | ||
}); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
var queryUrl = require('../validate-query'); | ||
|
||
describe('queryUrl', function () { | ||
var qURL; | ||
var longqURL; | ||
var trickyqURL; | ||
var upperURL; | ||
|
||
beforeEach(function () { | ||
qURL = queryUrl('user:user_name'); | ||
longqURL = queryUrl('user:user_nameany:hello'); | ||
trickyqURL = queryUrl('user_bob__helloany:hello'); | ||
upperURL = queryUrl('something'); | ||
}); | ||
|
||
it ('returns false on a non-query', function () { | ||
assert.equal(queryUrl({foo:'bar'}), null); | ||
}); | ||
|
||
it('returns an annotation string as a query', function () { | ||
assert.equal(qURL, 'user:user_name'); | ||
}); | ||
|
||
it('accepts longer queries', function () { | ||
assert.equal(longqURL, 'user:user_name any: hello'); | ||
}); | ||
|
||
it ('is not tricked by confounding usernames or queries', function() { | ||
assert.equal(trickyqURL, 'user_bob__hello any: hello'); | ||
}); | ||
|
||
it ('accepts upper and lower case values', function () { | ||
assert.equal(upperURL, 'something'); | ||
}); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
// A set of functions to prepare a query from a url request | ||
// | ||
// These functions take values from var annotations | ||
// produced in a url fragment | ||
// ( e.g. http://www.example.com/path/to/file | ||
// #annotations:query__user__username) | ||
// in settings | ||
// and converts it to a search query, detecting | ||
// tags like "user:", "any:" and "tag:". | ||
// | ||
|
||
function returnQueryFromAnnotationFragment (query){ | ||
var result = null; | ||
try { | ||
if (query) { | ||
result = query.replace(/(user|any|tag|text):/gi, | ||
function (tag) { | ||
// temporarily fix bug where | ||
// any:term does not work | ||
if (tag === 'any:') { | ||
return ' ' + tag + ' '; | ||
} else { | ||
return ' ' + tag; | ||
} | ||
}).trim(); | ||
} | ||
} catch (e) { | ||
result = null; | ||
} | ||
return (result); | ||
} | ||
|
||
module.exports = returnQueryFromAnnotationFragment; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of this variable needs an update.