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

Widget annotation: implement field name according to the specification #7775

Merged
merged 1 commit into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/acroforms/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function setupForm(div, content, viewport) {
// select box is not supported
}
input.className = 'inputControl';
input.name = item.fullName;
input.name = item.fieldName;
input.title = item.alternativeText;
assignFontStyle(input, item);
bindInputItem(input, item);
Expand Down
71 changes: 40 additions & 31 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ var WidgetAnnotation = (function WidgetAnnotationClosure() {
var data = this.data;

data.annotationType = AnnotationType.WIDGET;
data.fieldName = this._constructFieldName(dict);
data.fieldValue = Util.getInheritableProperty(dict, 'V',
/* getArray = */ true);
data.alternativeText = stringToPDFString(dict.get('TU') || '');
Expand All @@ -640,41 +641,49 @@ var WidgetAnnotation = (function WidgetAnnotationClosure() {
if (data.fieldType === 'Sig') {
this.setFlags(AnnotationFlag.HIDDEN);
}
}

// Building the full field name by collecting the field and
// its ancestors 'T' data and joining them using '.'.
var fieldName = [];
var namedItem = dict;
var ref = params.ref;
while (namedItem) {
var parent = namedItem.get('Parent');
var parentRef = namedItem.getRaw('Parent');
var name = namedItem.get('T');
if (name) {
fieldName.unshift(stringToPDFString(name));
} else if (parent && ref) {
// The field name is absent, that means more than one field
// with the same name may exist. Replacing the empty name
// with the '`' plus index in the parent's 'Kids' array.
// This is not in the PDF spec but necessary to id the
// the input controls.
var kids = parent.get('Kids');
var j, jj;
for (j = 0, jj = kids.length; j < jj; j++) {
var kidRef = kids[j];
if (kidRef.num === ref.num && kidRef.gen === ref.gen) {
break;
}
Util.inherit(WidgetAnnotation, Annotation, {
/**
* Construct the (fully qualified) field name from the (partial) field
* names of the field and its ancestors.
*
* @private
* @memberof WidgetAnnotation
* @param {Dict} dict - Complete widget annotation dictionary
* @return {string}
*/
_constructFieldName: function WidgetAnnotation_constructFieldName(dict) {
// Both the `Parent` and `T` fields are optional. While at least one of
// them should be provided, bad PDF generators may fail to do so.
if (!dict.has('T') && !dict.has('Parent')) {
warn('Unknown field name, falling back to empty field name.');
return '';
Copy link
Collaborator

Choose a reason for hiding this comment

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

I completely agree that it's better to just ignore these bad PDF files for now (since we don't have any broken examples to test with)!

}

// If no parent exists, the partial and fully qualified names are equal.
if (!dict.has('Parent')) {
return stringToPDFString(dict.get('T'));
}

// Form the fully qualified field name by appending the partial name to
// the parent's fully qualified name, separated by a period.
var fieldName = [];
if (dict.has('T')) {
fieldName.unshift(stringToPDFString(dict.get('T')));
}

var loopDict = dict;
while (loopDict.has('Parent')) {
loopDict = loopDict.get('Parent');

if (loopDict.has('T')) {
fieldName.unshift(stringToPDFString(loopDict.get('T')));
}
fieldName.unshift('`' + j);
}
namedItem = parent;
ref = parentRef;
}
data.fullName = fieldName.join('.');
}
return fieldName.join('.');
},

Util.inherit(WidgetAnnotation, Annotation, {
/**
* Check if a provided field flag is set.
*
Expand Down
68 changes: 68 additions & 0 deletions test/unit/annotation_layer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,74 @@ describe('Annotation layer', function() {
});
});

describe('WidgetAnnotation', function() {
var widgetDict;

beforeEach(function (done) {
widgetDict = new Dict();
widgetDict.set('Type', Name.get('Annot'));
widgetDict.set('Subtype', Name.get('Widget'));

done();
});

afterEach(function () {
widgetDict = null;
});

it('should handle unknown field names', function() {
var widgetRef = new Ref(20, 0);
var xref = new XRefMock([
{ ref: widgetRef, data: widgetDict, }
]);

var widgetAnnotation = annotationFactory.create(xref, widgetRef,
pdfManagerMock);
var data = widgetAnnotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.fieldName).toEqual('');
});

it('should construct the field name when there are no ancestors',
function() {
widgetDict.set('T', 'foo');

var widgetRef = new Ref(21, 0);
var xref = new XRefMock([
{ ref: widgetRef, data: widgetDict, }
]);

var widgetAnnotation = annotationFactory.create(xref, widgetRef,
pdfManagerMock);
var data = widgetAnnotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.fieldName).toEqual('foo');
});

it('should construct the field name when there are ancestors', function() {
var firstParent = new Dict();
firstParent.set('T', 'foo');

var secondParent = new Dict();
secondParent.set('Parent', firstParent);
secondParent.set('T', 'bar');

widgetDict.set('Parent', secondParent);
widgetDict.set('T', 'baz');

var widgetRef = new Ref(22, 0);
var xref = new XRefMock([
{ ref: widgetRef, data: widgetDict, }
]);

var widgetAnnotation = annotationFactory.create(xref, widgetRef,
pdfManagerMock);
var data = widgetAnnotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.fieldName).toEqual('foo.bar.baz');
});
});

describe('TextWidgetAnnotation', function() {
var textWidgetDict;

Expand Down