Skip to content

Commit 013a209

Browse files
committed
Use non-breakable spaces in options for the choice widget (bug 2026037)
Usual white spaces are collapsed.
1 parent 0128ead commit 013a209

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

src/display/annotation_layer.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,10 +2139,18 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
21392139
}
21402140
});
21412141

2142+
const fixDisplayValue = (option, value) => {
2143+
const newValue = value.replaceAll(" ", "\u00A0");
2144+
option.textContent = newValue;
2145+
if (newValue !== value) {
2146+
option.setAttribute("display-value", value);
2147+
}
2148+
};
2149+
21422150
// Insert the options into the choice field.
21432151
for (const option of this.data.options) {
21442152
const optionElement = document.createElement("option");
2145-
optionElement.textContent = option.displayValue;
2153+
fixDisplayValue(optionElement, option.displayValue);
21462154
optionElement.value = option.exportValue;
21472155
if (storedData.value.includes(option.exportValue)) {
21482156
optionElement.setAttribute("selected", true);
@@ -2185,7 +2193,8 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
21852193
const getItems = event => {
21862194
const options = event.target.options;
21872195
return Array.prototype.map.call(options, option => ({
2188-
displayValue: option.textContent,
2196+
displayValue:
2197+
option.getAttribute("display-value") || option.textContent,
21892198
exportValue: option.value,
21902199
}));
21912200
};
@@ -2239,7 +2248,7 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
22392248
const { index, displayValue, exportValue } = event.detail.insert;
22402249
const selectChild = selectElement.children[index];
22412250
const optionElement = document.createElement("option");
2242-
optionElement.textContent = displayValue;
2251+
fixDisplayValue(optionElement, displayValue);
22432252
optionElement.value = exportValue;
22442253

22452254
if (selectChild) {
@@ -2261,7 +2270,7 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
22612270
for (const item of items) {
22622271
const { displayValue, exportValue } = item;
22632272
const optionElement = document.createElement("option");
2264-
optionElement.textContent = displayValue;
2273+
fixDisplayValue(optionElement, displayValue);
22652274
optionElement.value = exportValue;
22662275
selectElement.append(optionElement);
22672276
}

test/integration/annotation_spec.mjs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,4 +920,50 @@ a dynamic compiler for JavaScript based on our`);
920920
);
921921
});
922922
});
923+
924+
describe("bug 2026037", () => {
925+
let pages;
926+
927+
beforeEach(async () => {
928+
pages = await loadAndWait("bug2026037.pdf", getAnnotationSelector("22R"));
929+
});
930+
931+
afterEach(async () => {
932+
await closePages(pages);
933+
});
934+
935+
it("must check that spaces in a choice option display value are preserved", async () => {
936+
await Promise.all(
937+
pages.map(async ([browserName, page]) => {
938+
// The option's displayValue contains multiple consecutive spaces
939+
// ("A B"). Browsers collapse spaces in textContent, so the
940+
// fix stores the original in a "display-value" attribute and uses
941+
// non-breaking spaces (\u00A0) in textContent.
942+
const displayAttr = await page.$eval(
943+
`${getSelector("22R")} option`,
944+
el => el.getAttribute("display-value")
945+
);
946+
expect(displayAttr)
947+
.withContext(`In ${browserName}`)
948+
.toEqual("A B");
949+
950+
const textContent = await page.$eval(
951+
`${getSelector("22R")} option`,
952+
el => el.textContent
953+
);
954+
expect(textContent)
955+
.withContext(`In ${browserName}`)
956+
.toEqual("A\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0B");
957+
958+
const exportValue = await page.$eval(
959+
`${getSelector("22R")} option`,
960+
el => el.value
961+
);
962+
expect(exportValue)
963+
.withContext(`In ${browserName}`)
964+
.toEqual("a b");
965+
})
966+
);
967+
});
968+
});
923969
});

test/pdfs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,3 +893,4 @@
893893
!text_rise_eol_bug.pdf
894894
!hello_world_rotated.pdf
895895
!bug2025674.pdf
896+
!bug2026037.pdf

test/pdfs/bug2026037.pdf

6.51 KB
Binary file not shown.

0 commit comments

Comments
 (0)