From 323e86c442782f12c137f62f6d530bc920e6fa8d Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Tue, 13 Sep 2016 14:57:11 +0200 Subject: [PATCH] Text widget annotations: implement unit testing and sanitize data values --- src/core/annotation.js | 15 ++++++-- src/display/annotation_layer.js | 5 ++- test/unit/annotation_layer_spec.js | 56 ++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index 490877818..0527d17f4 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -671,8 +671,19 @@ var TextWidgetAnnotation = (function TextWidgetAnnotationClosure() { function TextWidgetAnnotation(params) { WidgetAnnotation.call(this, params); - this.data.textAlignment = Util.getInheritableProperty(params.dict, 'Q'); - this.data.maxLen = Util.getInheritableProperty(params.dict, 'MaxLen'); + // Determine the alignment of text in the field. + var alignment = Util.getInheritableProperty(params.dict, 'Q'); + if (!isInt(alignment) || alignment < 0 || alignment > 2) { + alignment = null; + } + this.data.textAlignment = alignment; + + // Determine the maximum length of text in the field. + var maximumLength = Util.getInheritableProperty(params.dict, 'MaxLen'); + if (!isInt(maximumLength) || maximumLength < 0) { + maximumLength = null; + } + this.data.maxLen = maximumLength; } Util.inherit(TextWidgetAnnotation, WidgetAnnotation, { diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index 559d1c1a4..60c8f4618 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -29,7 +29,6 @@ var AnnotationBorderStyleType = sharedUtil.AnnotationBorderStyleType; var AnnotationType = sharedUtil.AnnotationType; -var isInt = sharedUtil.isInt; var Util = sharedUtil.Util; var addLinkAttributes = displayDOMUtils.addLinkAttributes; var LinkTarget = displayDOMUtils.LinkTarget; @@ -451,7 +450,7 @@ var TextWidgetAnnotationElement = ( element.type = 'text'; element.value = this.data.fieldValue; - if (isInt(this.data.maxLen)) { + if (this.data.maxLen !== null) { element.maxLength = this.data.maxLen; } } else { @@ -467,7 +466,7 @@ var TextWidgetAnnotationElement = ( this._setTextStyle(element, font); } - if (isInt(this.data.textAlignment)) { + if (this.data.textAlignment !== null) { element.style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment]; } diff --git a/test/unit/annotation_layer_spec.js b/test/unit/annotation_layer_spec.js index db37855e9..6971e6fca 100644 --- a/test/unit/annotation_layer_spec.js +++ b/test/unit/annotation_layer_spec.js @@ -453,6 +453,62 @@ describe('Annotation layer', function() { }); }); + describe('TextWidgetAnnotation', function() { + var textWidgetDict; + + beforeEach(function (done) { + textWidgetDict = new Dict(); + textWidgetDict.set('Type', Name.get('Annot')); + textWidgetDict.set('Subtype', Name.get('Widget')); + textWidgetDict.set('FT', Name.get('Tx')); + + done(); + }); + + afterEach(function () { + textWidgetDict = null; + }); + + it('should handle unknown text alignment and maximum length', function() { + var textWidgetRef = new Ref(124, 0); + var xref = new XRefMock([ + { ref: textWidgetRef, data: textWidgetDict, } + ]); + + var textWidgetAnnotation = annotationFactory.create(xref, textWidgetRef); + expect(textWidgetAnnotation.data.textAlignment).toEqual(null); + expect(textWidgetAnnotation.data.maxLen).toEqual(null); + }); + + it('should not set invalid text alignment and maximum length', function() { + textWidgetDict.set('Q', 'center'); + textWidgetDict.set('MaxLen', 'five'); + + var textWidgetRef = new Ref(43, 0); + var xref = new XRefMock([ + { ref: textWidgetRef, data: textWidgetDict, } + ]); + + var textWidgetAnnotation = annotationFactory.create(xref, textWidgetRef); + expect(textWidgetAnnotation.data.textAlignment).toEqual(null); + expect(textWidgetAnnotation.data.maxLen).toEqual(null); + }); + + it('should set valid text alignment and maximum length', function() { + textWidgetDict.set('Q', 1); + textWidgetDict.set('MaxLen', 20); + + var textWidgetRef = new Ref(84, 0); + var xref = new XRefMock([ + { ref: textWidgetRef, data: textWidgetDict, } + ]); + + var textWidgetAnnotation = annotationFactory.create(xref, textWidgetRef); + expect(textWidgetAnnotation.data.textAlignment).toEqual(1); + expect(textWidgetAnnotation.data.maxLen).toEqual(20); + }); + }); + describe('FileAttachmentAnnotation', function() { var loadingTask; var annotations;