Skip to content

Commit

Permalink
Add an empty entry in combo list when nothing is selected (bug 1773680)
Browse files Browse the repository at this point in the history
- it aims to fix https://bugzilla.mozilla.org/show_bug.cgi?id=1773680
- the empty is removed once something is selected.
  • Loading branch information
calixteman committed Jun 10, 2022
1 parent 808a55e commit 072e0d4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,8 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
selectElement.setAttribute("id", id);
selectElement.tabIndex = DEFAULT_TAB_INDEX;

let hasAnEmptyEntry = true;

if (!this.data.combo) {
// List boxes have a size and (optionally) multiple selection.
selectElement.size = this.data.options.length;
Expand All @@ -1486,10 +1488,27 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
}
if (storedData.value.includes(option.exportValue)) {
optionElement.setAttribute("selected", true);
hasAnEmptyEntry = false;
}
selectElement.appendChild(optionElement);
}

let removeEmptyEntry = null;
if (hasAnEmptyEntry && this.data.combo && this.data.options.length > 0) {
const noneOptionElement = document.createElement("option");
noneOptionElement.value = " ";
noneOptionElement.setAttribute("hidden", true);
noneOptionElement.setAttribute("selected", true);
selectElement.insertBefore(noneOptionElement, selectElement.firstChild);

removeEmptyEntry = () => {
noneOptionElement.remove();
selectElement.removeEventListener("input", removeEmptyEntry);
removeEmptyEntry = null;
};
selectElement.addEventListener("input", removeEmptyEntry);
}

const getValue = (event, isExport) => {
const name = isExport ? "value" : "textContent";
const options = event.target.options;
Expand All @@ -1514,6 +1533,7 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
selectElement.addEventListener("updatefromsandbox", jsEvent => {
const actions = {
value(event) {
removeEmptyEntry?.();
const value = event.detail.value;
const values = new Set(Array.isArray(value) ? value : [value]);
for (const option of selectElement.options) {
Expand Down
2 changes: 2 additions & 0 deletions test/pdfs/bug1773680.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://bugzilla.mozilla.org/attachment.cgi?id=9280675

8 changes: 8 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6567,5 +6567,13 @@
"rounds": 1,
"type": "eq",
"annotations": true
},
{ "id": "bug1773680.pdf",
"file": "pdfs/bug1773680.pdf",
"md5": "6099fc695fe018ce444752929d86f9c8",
"rounds": 1,
"link": true,
"type": "eq",
"annotations": true
}
]

0 comments on commit 072e0d4

Please sign in to comment.