Browse Source

Widget annotation: implement field name according to the specification

The original code is difficult to read and, more importantly, performs
actions that are not described in the specification. It replaces empty
names with a backtick and an index, but this behavior is not described
in the specification. While the specification is not entirely clear
about what should happen in this case, it does specify that the `T`
field is optional and that multiple field dictionaries may have the same
fully qualified name, so to achieve this it makes the most sense to
ignore missing `T` fields during construction of the field name. This is
the most specification-compliant solution and, judging by opened issue #6623, also the required and expected behavior.
Tim van der Meij 9 years ago
parent
commit
1d96854019
  1. 2
      examples/acroforms/forms.js
  2. 65
      src/core/annotation.js
  3. 68
      test/unit/annotation_layer_spec.js

2
examples/acroforms/forms.js

@ -91,7 +91,7 @@ function setupForm(div, content, viewport) {
// select box is not supported // select box is not supported
} }
input.className = 'inputControl'; input.className = 'inputControl';
input.name = item.fullName; input.name = item.fieldName;
input.title = item.alternativeText; input.title = item.alternativeText;
assignFontStyle(input, item); assignFontStyle(input, item);
bindInputItem(input, item); bindInputItem(input, item);

65
src/core/annotation.js

@ -621,6 +621,7 @@ var WidgetAnnotation = (function WidgetAnnotationClosure() {
var data = this.data; var data = this.data;
data.annotationType = AnnotationType.WIDGET; data.annotationType = AnnotationType.WIDGET;
data.fieldName = this._constructFieldName(dict);
data.fieldValue = Util.getInheritableProperty(dict, 'V', data.fieldValue = Util.getInheritableProperty(dict, 'V',
/* getArray = */ true); /* getArray = */ true);
data.alternativeText = stringToPDFString(dict.get('TU') || ''); data.alternativeText = stringToPDFString(dict.get('TU') || '');
@ -640,41 +641,49 @@ var WidgetAnnotation = (function WidgetAnnotationClosure() {
if (data.fieldType === 'Sig') { if (data.fieldType === 'Sig') {
this.setFlags(AnnotationFlag.HIDDEN); this.setFlags(AnnotationFlag.HIDDEN);
} }
}
// Building the full field name by collecting the field and Util.inherit(WidgetAnnotation, Annotation, {
// its ancestors 'T' data and joining them using '.'. /**
var fieldName = []; * Construct the (fully qualified) field name from the (partial) field
var namedItem = dict; * names of the field and its ancestors.
var ref = params.ref; *
while (namedItem) { * @private
var parent = namedItem.get('Parent'); * @memberof WidgetAnnotation
var parentRef = namedItem.getRaw('Parent'); * @param {Dict} dict - Complete widget annotation dictionary
var name = namedItem.get('T'); * @return {string}
if (name) { */
fieldName.unshift(stringToPDFString(name)); _constructFieldName: function WidgetAnnotation_constructFieldName(dict) {
} else if (parent && ref) { // Both the `Parent` and `T` fields are optional. While at least one of
// The field name is absent, that means more than one field // them should be provided, bad PDF generators may fail to do so.
// with the same name may exist. Replacing the empty name if (!dict.has('T') && !dict.has('Parent')) {
// with the '`' plus index in the parent's 'Kids' array. warn('Unknown field name, falling back to empty field name.');
// This is not in the PDF spec but necessary to id the return '';
// 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;
} }
// If no parent exists, the partial and fully qualified names are equal.
if (!dict.has('Parent')) {
return stringToPDFString(dict.get('T'));
} }
fieldName.unshift('`' + j);
// 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')));
} }
namedItem = parent;
ref = parentRef; var loopDict = dict;
while (loopDict.has('Parent')) {
loopDict = loopDict.get('Parent');
if (loopDict.has('T')) {
fieldName.unshift(stringToPDFString(loopDict.get('T')));
} }
data.fullName = fieldName.join('.');
} }
return fieldName.join('.');
},
Util.inherit(WidgetAnnotation, Annotation, {
/** /**
* Check if a provided field flag is set. * Check if a provided field flag is set.
* *

68
test/unit/annotation_layer_spec.js

@ -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() { describe('TextWidgetAnnotation', function() {
var textWidgetDict; var textWidgetDict;

Loading…
Cancel
Save