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

Interactive forms: values for checkboxes #8079

Closed
Closed
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
40 changes: 21 additions & 19 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,28 +784,30 @@ var TextWidgetAnnotation = (function TextWidgetAnnotationClosure() {
var ButtonWidgetAnnotation = (function ButtonWidgetAnnotationClosure() {
function ButtonWidgetAnnotation(params) {
WidgetAnnotation.call(this, params);

// Bool: is button widget a checkbox
this.data.checkBox = !this.hasFieldFlag(AnnotationFieldFlag.RADIO) &&
!this.hasFieldFlag(AnnotationFieldFlag.PUSHBUTTON);
if (this.data.checkBox) {
if (!isName(this.data.fieldValue)) {
return;
}
this.data.fieldValue = this.data.fieldValue.name;
}

// Bool: is button widget a radio button
this.data.radioButton = this.hasFieldFlag(AnnotationFieldFlag.RADIO) &&
!this.hasFieldFlag(AnnotationFieldFlag.PUSHBUTTON);
if (this.data.radioButton) {
this.data.fieldValue = this.data.buttonValue = null;

// The parent field's `V` entry holds a `Name` object with the appearance
// state of whichever child field is currently in the "on" state.
var fieldParent = params.dict.get('Parent');
if (isDict(fieldParent) && fieldParent.has('V')) {
var fieldParentValue = fieldParent.get('V');
if (isName(fieldParentValue)) {
this.data.fieldValue = fieldParentValue.name;
!this.hasFieldFlag(AnnotationFieldFlag.PUSHBUTTON);

if (this.data.checkBox || this.data.radioButton) {
// Set fieldValue
if (this.data.checkBox) { // For checkbox button widgets
if (isName(this.data.fieldValue)) {
this.data.fieldValue = this.data.fieldValue.name;
}
} else { // For radio button widgets
this.data.fieldValue = this.data.buttonValue = null;

// The parent fiel's `V` entry holds a `Name` object with the appearance
// state of whichever child field is currently in the "on" state.
var fieldParent = params.dict.get('Parent');
if (isDict(fieldParent) && fieldParent.has('V')) {
var fieldParentValue = fieldParent.get('V');
if (isName(fieldParentValue)) {
this.data.fieldValue = fieldParentValue.name;
}
}
}

Expand Down
40 changes: 38 additions & 2 deletions test/unit/annotation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,8 +970,18 @@ describe('annotation', function() {
buttonWidgetDict = null;
});

it('should handle checkboxes', function() {
buttonWidgetDict.set('V', Name.get('1'));
it('should handle checkboxes with a field value', function() {
var parentDict = new Dict();
parentDict.set('V', Name.get('1'));

var normalAppearanceStateDict = new Dict();
normalAppearanceStateDict.set('2', null);

var appearanceStatesDict = new Dict();
appearanceStatesDict.set('N', normalAppearanceStateDict);

buttonWidgetDict.set('Parent', parentDict);
buttonWidgetDict.set('AP', appearanceStatesDict);

var buttonWidgetRef = new Ref(124, 0);
var xref = new XRefMock([
Expand All @@ -984,8 +994,34 @@ describe('annotation', function() {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);

expect(data.checkBox).toEqual(true);
expect(data.radioButton).toEqual(false);
expect(data.fieldValue).toEqual('1');
expect(data.buttonValue).toEqual('2');
});

it('should handle checkboxes without a field value', function() {
var normalAppearanceStateDict = new Dict();
normalAppearanceStateDict.set('2', null);

var appearanceStatesDict = new Dict();
appearanceStatesDict.set('N', normalAppearanceStateDict);

buttonWidgetDict.set('AP', appearanceStatesDict);

var buttonWidgetRef = new Ref(124, 0);
var xref = new XRefMock([
{ ref: buttonWidgetRef, data: buttonWidgetDict, }
]);

var annotation = annotationFactory.create(xref, buttonWidgetRef,
pdfManagerMock, idFactoryMock);
var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET);

expect(data.checkBox).toEqual(true);
expect(data.radioButton).toEqual(false);
expect(data.fieldValue).toEqual(null);
expect(data.buttonValue).toEqual('2');
});

it('should handle radio buttons with a field value', function() {
Expand Down