Browse Source

Merge pull request #7775 from timvandermeij/widget-annotation-name

Widget annotation: implement field name according to the specification
Tim van der Meij 9 years ago committed by GitHub
parent
commit
9f8d67475e
  1. 2
      examples/acroforms/forms.js
  2. 71
      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);

71
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; // If no parent exists, the partial and fully qualified names are equal.
for (j = 0, jj = kids.length; j < jj; j++) { if (!dict.has('Parent')) {
var kidRef = kids[j]; return stringToPDFString(dict.get('T'));
if (kidRef.num === ref.num && kidRef.gen === ref.gen) { }
break;
} // 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; return fieldName.join('.');
ref = parentRef; },
}
data.fullName = 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

@ -616,6 +616,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