Skip to content
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

Fix PDF quote anchoring #261

Merged
merged 4 commits into from
Feb 27, 2017
Merged

Fix PDF quote anchoring #261

merged 4 commits into from
Feb 27, 2017

Conversation

robertknight
Copy link
Member

270932b broke anchoring using quote selectors in PDFs because the re-implementation of the TextQuoteAnchor class was missing a method, toPositionAnchor, which PDF anchoring tried to call.

This did not affect the majority of annotations because it only affected cases where the position selector failed to anchor and so anchoring fell back to the quote selector.

Fixes hypothesis/product-backlog#146 . Note that after creating the annotation as described in the issue the highlighted region is offset. This issue existed in Hypothesis v0.50 as well.

This PR:

  • Adds the missing method and a test for it
  • Adds a higher-level test in the PDF anchoring tests
  • Adds some documentation to the PDF anchoring code to clarify what a few of the different functions do.

While implementing this fix, I noticed that quote anchoring is broken in PDFs in other ways too - if the quote is not found on the first page that anchoring checks then it will never be found at all. I'm going to fix this separately.

Make this file easier to understand for the next person who tries to
read it.
This method used to exist in the `dom-anchor-text-quote` library but got
lost when the class was moved to a wrapper in `types.coffee` after the
upgrade to dom-anchor-text-quote v3.x.

This method is used by PDF anchoring and its absence caused quote
anchoring to throw an exception. The majority of PDF annotations still
continued to anchor because the position anchor still worked.

This commit also adds some basic documentation to the `types` module and
some basic API tests.
…to anchor

This test fails without the `toPositionAnchor` fix in the previous
commit.
@codecov-io
Copy link

codecov-io commented Feb 24, 2017

Codecov Report

Merging #261 into master will increase coverage by 0.42%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #261      +/-   ##
==========================================
+ Coverage   75.62%   76.05%   +0.42%     
==========================================
  Files         116      116              
  Lines        5793     5796       +3     
  Branches      946      947       +1     
==========================================
+ Hits         4381     4408      +27     
+ Misses       1412     1388      -24
Impacted Files Coverage Δ
src/annotator/anchoring/pdf.coffee 96.8% <ø> (+24.79%)
src/annotator/anchoring/types.coffee 97.87% <100%> (+0.14%)
src/sidebar/directive/annotation-share-dialog.js 76.66% <0%> (-23.34%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ba6ef24...1dc6323. Read the comment docs.

@dwhly
Copy link
Member

dwhly commented Feb 24, 2017

Gracias!

@judell
Copy link
Contributor

judell commented Feb 24, 2017

"if the quote is not found on the first page that anchoring checks then it will never be found at all."

Whoa.

@seanh seanh self-assigned this Feb 27, 2017
@seanh
Copy link
Contributor

seanh commented Feb 27, 2017

Confirmed that I can reproduce hypothesis/product-backlog#146 on master and that this branch appears to fix it

@seanh
Copy link
Contributor

seanh commented Feb 27, 2017

"Make this file easier to understand for the next person who tries to
read it." Thanks!

Copy link
Contributor

@seanh seanh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, fixes the bug and also adds plenty of documentation and tests, steadily improving the codebase.

I've made a comment about the top-level describe() string, but I realise that this kind of description is used often in our tests (regrettably, in my opinion) so I'll approve this and leave it up to you to merge with or without making that change @robertknight

// These are primarily basic API tests for the anchoring classes. Tests for
// anchoring a variety of HTML and PDF content exist in `html-test` and
// `pdf-test`.
describe('Anchoring classes', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if describe('types.coffee') (or maybe just describe('types')) would be a more precise way of putting this? Or else rename types.coffee to anchoring.coffee. But there's a mismatch between the two names "types" and "anchoring" which I think makes it less immediately clear, from looking at for example the output of a failed test, what code was being tested.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'll change the name here to match the module. I think the closest we have to a convention is that the top-level describe matches:

  1. The name of the 'default' export if the file has one (ie. if it does module.exports = someFunction then the describe block is 'someFunction')
  2. Otherwise the name is the module filename

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like a pretty reasonable convention. Perhaps files should be named after their default exports too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps files should be named after their default exports too

This is a common convention. In our code we usually do that but hyphenate the name. eg. If the default export is serviceConfig we name the file service-config.

var quoteAnchor = new TextQuoteAnchor(container, 'some are more equal than others');
assert.throws(function () {
quoteAnchor.toPositionAnchor();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice - the tests are simple and easy to understand

#
# 1. Providing a consistent interface across different types of anchor.
# 2. Insulating the rest of the code from API changes in the underyling anchoring
# libraries.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much appreciated!

@robertknight robertknight merged commit cffcd25 into master Feb 27, 2017
@robertknight robertknight deleted the fix-pdf-quote-anchoring branch February 27, 2017 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Annotation on PDF is immediately orphaned
5 participants