Unverified Commit cd1cc5c5 authored by Lyza Gardner's avatar Lyza Gardner Committed by GitHub

Merge pull request #1652 from hypothesis/is-highlight-util

Add `isHighlight` util function to `annotation-metadata`
parents 34d63c81 e6aadd79
......@@ -113,7 +113,7 @@ function oldHighlight() {
return {
id: 'annotation_id',
$highlight: undefined,
target: ['foo', 'bar'],
target: [{ source: 'source', selector: [] }],
references: [],
text: '',
tags: [],
......
......@@ -149,6 +149,43 @@ function isWaitingToAnchor(annotation) {
);
}
/**
* Is this annotation a highlight?
*
* Highlights are generally identifiable by having no text content AND no tags,
* but there is some nuance.
*
* @param {Object} annotation
* @return {boolean}
*/
function isHighlight(annotation) {
// `$highlight` is an ephemeral attribute set by the `annotator` on new
// annotation objects (created by clicking the "highlight" button).
// It is not persisted and cannot be relied upon, but if it IS present,
// this is definitely a highlight (one which is not yet saved).
if (annotation.$highlight) {
return true;
}
if (isNew(annotation)) {
// For new (unsaved-to-service) annotations, unless they have a truthy
// `$highlight` attribute, we don't know yet if they are a highlight.
return false;
}
// Note that it is possible to end up with an empty (no `text`) annotation
// that is not a highlight by adding at least one tag—thus, it is necessary
// to check for the existence of tags as well as text content.
return (
!isPageNote(annotation) &&
!isReply(annotation) &&
!annotation.hidden && // A hidden annotation has some form of objectionable content
!annotation.text &&
!(annotation.tags && annotation.tags.length)
);
}
/** Return `true` if the given annotation is an orphan. */
function isOrphan(annotation) {
return hasSelector(annotation) && annotation.$orphan;
......@@ -220,6 +257,7 @@ module.exports = {
domainAndTitle: domainAndTitle,
flagCount: flagCount,
isAnnotation: isAnnotation,
isHighlight: isHighlight,
isNew: isNew,
isOrphan: isOrphan,
isPageNote: isPageNote,
......
......@@ -234,6 +234,58 @@ describe('annotation-metadata', function() {
});
});
describe('.isHighlight', () => {
[
{
annotation: fixtures.newEmptyAnnotation(),
expect: false,
desc: 'new, empty annotation',
},
{
annotation: fixtures.newReply(),
expect: false,
desc: 'new, reply annotation',
},
{
annotation: fixtures.newAnnotation(),
expect: false,
desc: 'new, with some text',
},
{
annotation: fixtures.newHighlight(),
expect: true,
desc: 'new, marked as $highlight',
},
{
annotation: fixtures.oldAnnotation(),
expect: false,
desc: 'pre-existing annotation',
},
{
annotation: fixtures.oldHighlight(),
expect: true,
desc: 'pre-existing higlight',
},
{
annotation: fixtures.oldPageNote(),
expect: false,
desc: 'pre-existing page note',
},
{
annotation: fixtures.oldReply(),
expect: false,
desc: 'pre-existing reply',
},
].forEach(testcase => {
it(`returns ${testcase.expect} for isHighlight when annotation is: ${testcase.desc}`, () => {
assert.equal(
annotationMetadata.isHighlight(testcase.annotation),
testcase.expect
);
});
});
});
describe('.isPageNote', function() {
it('returns true for an annotation with an empty target', function() {
assert.isTrue(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment