[Pkg-javascript-commits] [pdf.js] 113/157: Refactor annotation rectangle code and add unit tests
David Prévot
taffit at moszumanska.debian.org
Tue Aug 11 06:46:46 UTC 2015
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository pdf.js.
commit 980aa10e0443166babee31aef0ec491cbd22805c
Author: Tim van der Meij <timvandermeij at gmail.com>
Date: Mon Jul 20 22:01:47 2015 +0200
Refactor annotation rectangle code and add unit tests
This patch refactors the code responsible for setting the annotation's rectangle. Its goal is to:
- Actually check that the input array is actually an array, and if so, that it contains exactly four elements.
- Only call `normalizeRect` if the input array is valid, i.e., we do not call it for the default rectangle anymore.
Unit tests are provided just like with the other patches in this series.
---
src/core/annotation.js | 20 ++++++++++++++++++--
test/unit/annotation_layer_spec.js | 18 ++++++++++++++++++
2 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/src/core/annotation.js b/src/core/annotation.js
index a88acaf..d5ad2e9 100644
--- a/src/core/annotation.js
+++ b/src/core/annotation.js
@@ -75,10 +75,11 @@ var Annotation = (function AnnotationClosure() {
var data = this.data = {};
data.subtype = dict.get('Subtype').name;
- var rect = dict.get('Rect') || [0, 0, 0, 0];
- data.rect = Util.normalizeRect(rect);
data.annotationFlags = dict.get('F');
+ this.setRectangle(dict.get('Rect'));
+ data.rect = this.rectangle;
+
this.setColor(dict.get('C'));
data.color = this.color;
@@ -92,6 +93,21 @@ var Annotation = (function AnnotationClosure() {
Annotation.prototype = {
/**
+ * Set the rectangle.
+ *
+ * @public
+ * @memberof Annotation
+ * @param {Array} rectangle - The rectangle array with exactly four entries
+ */
+ setRectangle: function Annotation_setRectangle(rectangle) {
+ if (isArray(rectangle) && rectangle.length === 4) {
+ this.rectangle = Util.normalizeRect(rectangle);
+ } else {
+ this.rectangle = [0, 0, 0, 0];
+ }
+ },
+
+ /**
* Set the color and take care of color space conversion.
*
* @public
diff --git a/test/unit/annotation_layer_spec.js b/test/unit/annotation_layer_spec.js
index 1c45b63..a8ec906 100644
--- a/test/unit/annotation_layer_spec.js
+++ b/test/unit/annotation_layer_spec.js
@@ -7,6 +7,24 @@
describe('Annotation layer', function() {
describe('Annotation', function() {
+ it('should set and get a valid rectangle', function() {
+ var dict = new Dict();
+ dict.set('Subtype', '');
+ var annotation = new Annotation({ dict: dict, ref: 0 });
+ annotation.setRectangle([117, 694, 164.298, 720]);
+
+ expect(annotation.rectangle).toEqual([117, 694, 164.298, 720]);
+ });
+
+ it('should not set and get an invalid rectangle', function() {
+ var dict = new Dict();
+ dict.set('Subtype', '');
+ var annotation = new Annotation({ dict: dict, ref: 0 });
+ annotation.setRectangle([117, 694, 164.298]);
+
+ expect(annotation.rectangle).toEqual([0, 0, 0, 0]);
+ });
+
it('should reject a color if it is not an array', function() {
var dict = new Dict();
dict.set('Subtype', '');
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/pdf.js.git
More information about the Pkg-javascript-commits
mailing list