import type { Selector } from "@iiif/parser/presentation-3/types";
import type { Selector as SelectorV4 } from "@iiif/parser/presentation-4/types";
import { JSDOM } from "jsdom";
import { describe, expect, test } from "vitest";
import ghentChoices from "../fixtures/presentation-3/ghent-choices.json";
import { expandTarget } from "../src";
import { parseSelector } from "../src/annotation-targets/parse-selector";
describe("parse selector", () => {
test("Parsing a 3D PointSelector (including zeros + z)", () => {
expect(
parseSelector({
type: "PointSelector",
x: 0,
y: 0,
z: 0,
} as SelectorV4)
).toMatchInlineSnapshot(`
{
"selector": {
"spatial": {
"x": 0,
"y": 0,
"z": 0,
},
"type": "PointSelector",
},
"selectors": [
{
"spatial": {
"x": 0,
"y": 0,
"z": 0,
},
"type": "PointSelector",
},
],
}
`);
});
test("Parsing a PolygonZSelector WKT", () => {
expect(
parseSelector({
type: "PolygonZSelector",
value: "POLYGONZ((-1.0843 2.8273 -2, 1.0843 2.8273 -2, 1.0843 0 -2, -1.0843 0 -2, -1.0843 2.8273 -2))",
} as SelectorV4)
).toMatchInlineSnapshot(`
{
"selector": {
"points3d": [
[
-1.0843,
2.8273,
-2,
],
[
1.0843,
2.8273,
-2,
],
[
1.0843,
0,
-2,
],
[
-1.0843,
0,
-2,
],
[
-1.0843,
2.8273,
-2,
],
],
"spatial": {
"height": 2.8273,
"unit": "pixel",
"width": 2.1686,
"x": -1.0843,
"y": 0,
},
"type": "PolygonZSelector",
"value": "POLYGONZ((-1.0843 2.8273 -2, 1.0843 2.8273 -2, 1.0843 0 -2, -1.0843 0 -2, -1.0843 2.8273 -2))",
},
"selectors": [
{
"points3d": [
[
-1.0843,
2.8273,
-2,
],
[
1.0843,
2.8273,
-2,
],
[
1.0843,
0,
-2,
],
[
-1.0843,
0,
-2,
],
[
-1.0843,
2.8273,
-2,
],
],
"spatial": {
"height": 2.8273,
"unit": "pixel",
"width": 2.1686,
"x": -1.0843,
"y": 0,
},
"type": "PolygonZSelector",
"value": "POLYGONZ((-1.0843 2.8273 -2, 1.0843 2.8273 -2, 1.0843 0 -2, -1.0843 0 -2, -1.0843 2.8273 -2))",
},
],
}
`);
});
test("does not produce infinite bounds for an empty WKT polygon", () => {
const result = parseSelector({ type: "WktSelector", value: "POLYGON EMPTY" } as SelectorV4);
expect(result.selector).toMatchObject({ points3d: [] });
expect(result.selector).not.toHaveProperty("spatial");
});
describe("SVG Selectors", () => {
test("Malformed SVG path data returns an empty selector", () => {
expect(
parseSelector(
{
type: "SvgSelector",
value: "",
},
{ domParser: new new JSDOM().window.DOMParser() }
)
).toEqual({ selector: null, selectors: [] });
});
test("Parsing a rectangle selector", () => {
expect(
parseSelector({
type: "SvgSelector",
value:
'',
})
).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
100,
100,
],
[
400,
100,
],
[
400,
400,
],
[
100,
400,
],
[
100,
100,
],
],
"spatial": {
"height": 300,
"unit": "pixel",
"width": 300,
"x": 100,
"y": 100,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "rect",
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
100,
100,
],
[
400,
100,
],
[
400,
400,
],
[
100,
400,
],
[
100,
100,
],
],
"spatial": {
"height": 300,
"unit": "pixel",
"width": 300,
"x": 100,
"y": 100,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "rect",
"type": "SvgSelector",
},
],
}
`);
});
test("Parsing svg with temporal element", () => {
expect(
parseSelector([
{
type: "SvgSelector",
value:
'',
},
{
type: "FragmentSelector",
conformsTo: "http://www.w3.org/TR/media-frags/",
value: "t=10.46,10.46",
},
])
).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {},
"points": [
[
3069,
1586,
],
[
3069,
1616,
],
[
3100,
1586,
],
[
3100,
1616,
],
[
3069,
1586,
],
],
"spatial": {
"height": 30,
"unit": "pixel",
"width": 31,
"x": 3069,
"y": 1586,
},
"style": undefined,
"svg": "",
"svgShape": "polygon",
"temporal": {
"endTime": 10.46,
"startTime": 10.46,
},
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {},
"points": [
[
3069,
1586,
],
[
3069,
1616,
],
[
3100,
1586,
],
[
3100,
1616,
],
[
3069,
1586,
],
],
"spatial": {
"height": 30,
"unit": "pixel",
"width": 31,
"x": 3069,
"y": 1586,
},
"style": undefined,
"svg": "",
"svgShape": "polygon",
"temporal": {
"endTime": 10.46,
"startTime": 10.46,
},
"type": "SvgSelector",
},
{
"temporal": {
"endTime": 10.46,
"startTime": 10.46,
},
"type": "TemporalSelector",
},
],
}
`);
});
test("Parsing svg without size", () => {
expect(
parseSelector({
type: "SvgSelector",
value:
'',
})
).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"backgroundColor": "#FF0055",
"borderColor": "#FF0055",
"borderWidth": "1",
},
"points": [
[
2596,
6948,
],
[
2571,
6952,
],
[
2597,
6959,
],
[
2616,
6953,
],
[
2596,
6948,
],
[
2596,
6948,
],
],
"spatial": {
"height": 11,
"unit": "pixel",
"width": 45,
"x": 2571,
"y": 6948,
},
"style": {
"fill": "#FF0055",
"fillOpacity": 0.1,
"stroke": "#FF0055",
"strokeWidth": "1",
},
"svg": "",
"svgShape": "polygon",
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {
"backgroundColor": "#FF0055",
"borderColor": "#FF0055",
"borderWidth": "1",
},
"points": [
[
2596,
6948,
],
[
2571,
6952,
],
[
2597,
6959,
],
[
2616,
6953,
],
[
2596,
6948,
],
[
2596,
6948,
],
],
"spatial": {
"height": 11,
"unit": "pixel",
"width": 45,
"x": 2571,
"y": 6948,
},
"style": {
"fill": "#FF0055",
"fillOpacity": 0.1,
"stroke": "#FF0055",
"strokeWidth": "1",
},
"svg": "",
"svgShape": "polygon",
"type": "SvgSelector",
},
],
}
`);
});
test("Parsing a circle selector", () => {
expect(
parseSelector({
type: "SvgSelector",
value:
'',
})
).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
373,
199.83333,
],
[
369.77677110823635,
230.50030439561948,
],
[
360.2479550022836,
259.82698485368053,
],
[
344.8300066703048,
286.5316547131398,
],
[
324.1967644379316,
309.4471917579157,
],
[
299.25,
327.5720770582047,
],
[
271.0800066703047,
340.11416615353517,
],
[
240.9179483319789,
346.5253095668203,
],
[
210.08205166802114,
346.5253095668203,
],
[
179.91999332969527,
340.11416615353517,
],
[
151.75000000000003,
327.5720770582047,
],
[
126.80323556206841,
309.4471917579157,
],
[
106.16999332969527,
286.5316547131398,
],
[
90.75204499771638,
259.82698485368053,
],
[
81.22322889176365,
230.50030439561948,
],
[
78,
199.83333000000002,
],
[
81.22322889176365,
169.16635560438053,
],
[
90.75204499771633,
139.8396751463195,
],
[
106.16999332969525,
113.13500528686022,
],
[
126.80323556206838,
90.21946824208437,
],
[
151.74999999999994,
72.09458294179531,
],
[
179.91999332969525,
59.55249384646484,
],
[
210.08205166802114,
53.14135043317967,
],
[
240.9179483319788,
53.14135043317967,
],
[
271.0800066703047,
59.55249384646481,
],
[
299.25,
72.0945829417953,
],
[
324.1967644379315,
90.21946824208429,
],
[
344.8300066703047,
113.13500528686016,
],
[
360.2479550022837,
139.83967514631945,
],
[
369.7767711082363,
169.1663556043804,
],
[
373,
199.83332999999996,
],
],
"spatial": {
"height": 293.3839591336406,
"unit": "pixel",
"width": 295,
"x": 78,
"y": 53.14135043317967,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "circle",
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
373,
199.83333,
],
[
369.77677110823635,
230.50030439561948,
],
[
360.2479550022836,
259.82698485368053,
],
[
344.8300066703048,
286.5316547131398,
],
[
324.1967644379316,
309.4471917579157,
],
[
299.25,
327.5720770582047,
],
[
271.0800066703047,
340.11416615353517,
],
[
240.9179483319789,
346.5253095668203,
],
[
210.08205166802114,
346.5253095668203,
],
[
179.91999332969527,
340.11416615353517,
],
[
151.75000000000003,
327.5720770582047,
],
[
126.80323556206841,
309.4471917579157,
],
[
106.16999332969527,
286.5316547131398,
],
[
90.75204499771638,
259.82698485368053,
],
[
81.22322889176365,
230.50030439561948,
],
[
78,
199.83333000000002,
],
[
81.22322889176365,
169.16635560438053,
],
[
90.75204499771633,
139.8396751463195,
],
[
106.16999332969525,
113.13500528686022,
],
[
126.80323556206838,
90.21946824208437,
],
[
151.74999999999994,
72.09458294179531,
],
[
179.91999332969525,
59.55249384646484,
],
[
210.08205166802114,
53.14135043317967,
],
[
240.9179483319788,
53.14135043317967,
],
[
271.0800066703047,
59.55249384646481,
],
[
299.25,
72.0945829417953,
],
[
324.1967644379315,
90.21946824208429,
],
[
344.8300066703047,
113.13500528686016,
],
[
360.2479550022837,
139.83967514631945,
],
[
369.7767711082363,
169.1663556043804,
],
[
373,
199.83332999999996,
],
],
"spatial": {
"height": 293.3839591336406,
"unit": "pixel",
"width": 295,
"x": 78,
"y": 53.14135043317967,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "circle",
"type": "SvgSelector",
},
],
}
`);
});
test("Parsing an ellipse selector", () => {
expect(
parseSelector({
type: "SvgSelector",
value:
'',
})
).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
746,
77.83333,
],
[
738.4281436542635,
84.38254826075942,
],
[
716.0435010731612,
90.64553425688771,
],
[
679.8243885509194,
96.3485654472129,
],
[
631.3537551033444,
101.24239200253791,
],
[
572.75,
105.11313021920982,
],
[
506.5743885509193,
107.79161026329734,
],
[
435.71911252224197,
109.16076970410062,
],
[
363.2808874777581,
109.1607697041006,
],
[
292.42561144908075,
107.79161026329734,
],
[
226.25000000000006,
105.11313021920982,
],
[
167.6462448966557,
101.24239200253793,
],
[
119.17561144908075,
96.3485654472129,
],
[
82.95649892683878,
90.6455342568877,
],
[
60.57185634573631,
84.38254826075942,
],
[
53,
77.83333,
],
[
60.57185634573631,
71.28411173924059,
],
[
82.95649892683872,
65.02112574311231,
],
[
119.17561144908069,
59.3180945527871,
],
[
167.64624489665556,
54.424267997462096,
],
[
226.24999999999983,
50.5535297807902,
],
[
292.42561144908063,
47.875049736702664,
],
[
363.28088747775786,
46.5058902958994,
],
[
435.7191125222421,
46.50589029589939,
],
[
506.57438855091925,
47.875049736702664,
],
[
572.75,
50.553529780790186,
],
[
631.3537551033445,
54.424267997462096,
],
[
679.8243885509191,
59.318094552787095,
],
[
716.0435010731612,
65.0211257431123,
],
[
738.4281436542635,
71.28411173924057,
],
[
746,
77.83332999999999,
],
],
"spatial": {
"height": 62.654879408201225,
"unit": "pixel",
"width": 693,
"x": 53,
"y": 46.50589029589939,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "ellipse",
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
746,
77.83333,
],
[
738.4281436542635,
84.38254826075942,
],
[
716.0435010731612,
90.64553425688771,
],
[
679.8243885509194,
96.3485654472129,
],
[
631.3537551033444,
101.24239200253791,
],
[
572.75,
105.11313021920982,
],
[
506.5743885509193,
107.79161026329734,
],
[
435.71911252224197,
109.16076970410062,
],
[
363.2808874777581,
109.1607697041006,
],
[
292.42561144908075,
107.79161026329734,
],
[
226.25000000000006,
105.11313021920982,
],
[
167.6462448966557,
101.24239200253793,
],
[
119.17561144908075,
96.3485654472129,
],
[
82.95649892683878,
90.6455342568877,
],
[
60.57185634573631,
84.38254826075942,
],
[
53,
77.83333,
],
[
60.57185634573631,
71.28411173924059,
],
[
82.95649892683872,
65.02112574311231,
],
[
119.17561144908069,
59.3180945527871,
],
[
167.64624489665556,
54.424267997462096,
],
[
226.24999999999983,
50.5535297807902,
],
[
292.42561144908063,
47.875049736702664,
],
[
363.28088747775786,
46.5058902958994,
],
[
435.7191125222421,
46.50589029589939,
],
[
506.57438855091925,
47.875049736702664,
],
[
572.75,
50.553529780790186,
],
[
631.3537551033445,
54.424267997462096,
],
[
679.8243885509191,
59.318094552787095,
],
[
716.0435010731612,
65.0211257431123,
],
[
738.4281436542635,
71.28411173924057,
],
[
746,
77.83332999999999,
],
],
"spatial": {
"height": 62.654879408201225,
"unit": "pixel",
"width": 693,
"x": 53,
"y": 46.50589029589939,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "ellipse",
"type": "SvgSelector",
},
],
}
`);
});
test("Parsing a cubic bézier selector", () => {
expect(
parseSelector({
type: "SvgSelector",
value:
'',
})
).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
200.00001,
83.33333,
],
[
189.04971975752,
92.24393388116,
],
[
178.80567039746,
100.43438771718,
],
[
169.23560567209,
107.94149345373,
],
[
160.30805901686,
114.80115206594,
],
[
151.99235355031,
121.0483635583,
],
[
144.25860207391,
126.71722696451,
],
[
137.07770707199,
131.84094034727,
],
[
130.42136071156,
136.45180079809,
],
[
123.61277681833,
141.00964477236,
],
[
113.97516170199,
147.21988073928,
],
[
105.33190309671,
152.52755936099,
],
[
97.61454532645,
157.00342065752,
],
[
90.75699356146,
160.71576500807,
],
[
84.69551381893,
163.73045314736,
],
[
79.3687329639,
166.11090616086,
],
[
71.80644334526,
168.98320166403,
],
[
64.26398773056,
171.19491342159,
],
[
58.37447362501,
172.16184015701,
],
[
53.95206877442,
172.04690989659,
],
[
47.30957775492,
168.43492134597,
],
[
46.33866301795,
160.54259107233,
],
[
49.20284444659,
153.2402160926,
],
[
53.18074876445,
147.44703936037,
],
[
54.94958995593,
145.39344791461,
],
[
56.87747882523,
143.4189243797,
],
[
58.97158482534,
141.53028695154,
],
[
61.2392133287,
139.73448309529,
],
[
63.68780562698,
138.03858954433,
],
[
66.32493893077,
136.4498122992,
],
[
69.153584424,
134.99077366543,
],
[
72.10941134425,
133.81175249426,
],
[
75.1745132003,
132.96710613581,
],
[
78.35180398922,
132.47987074175,
],
[
81.64425923663,
132.37356891368,
],
[
85.054915997,
132.67220970143,
],
[
88.8462153964,
133.49590584219,
],
[
93.13397532373,
135.35476109534,
],
[
97.37963547339,
138.32629590014,
],
[
101.57393111067,
142.48205447481,
],
[
105.70739342858,
147.89515688303,
],
[
111.0901563458,
157.92532868215,
],
[
115.93172618217,
172.1048667021,
],
[
119.94023,
190.33333,
],
],
"spatial": {
"height": 106.99999999999999,
"unit": "pixel",
"width": 153.66134698205002,
"x": 46.33866301795,
"y": 83.33333,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "path",
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
200.00001,
83.33333,
],
[
189.04971975752,
92.24393388116,
],
[
178.80567039746,
100.43438771718,
],
[
169.23560567209,
107.94149345373,
],
[
160.30805901686,
114.80115206594,
],
[
151.99235355031,
121.0483635583,
],
[
144.25860207391,
126.71722696451,
],
[
137.07770707199,
131.84094034727,
],
[
130.42136071156,
136.45180079809,
],
[
123.61277681833,
141.00964477236,
],
[
113.97516170199,
147.21988073928,
],
[
105.33190309671,
152.52755936099,
],
[
97.61454532645,
157.00342065752,
],
[
90.75699356146,
160.71576500807,
],
[
84.69551381893,
163.73045314736,
],
[
79.3687329639,
166.11090616086,
],
[
71.80644334526,
168.98320166403,
],
[
64.26398773056,
171.19491342159,
],
[
58.37447362501,
172.16184015701,
],
[
53.95206877442,
172.04690989659,
],
[
47.30957775492,
168.43492134597,
],
[
46.33866301795,
160.54259107233,
],
[
49.20284444659,
153.2402160926,
],
[
53.18074876445,
147.44703936037,
],
[
54.94958995593,
145.39344791461,
],
[
56.87747882523,
143.4189243797,
],
[
58.97158482534,
141.53028695154,
],
[
61.2392133287,
139.73448309529,
],
[
63.68780562698,
138.03858954433,
],
[
66.32493893077,
136.4498122992,
],
[
69.153584424,
134.99077366543,
],
[
72.10941134425,
133.81175249426,
],
[
75.1745132003,
132.96710613581,
],
[
78.35180398922,
132.47987074175,
],
[
81.64425923663,
132.37356891368,
],
[
85.054915997,
132.67220970143,
],
[
88.8462153964,
133.49590584219,
],
[
93.13397532373,
135.35476109534,
],
[
97.37963547339,
138.32629590014,
],
[
101.57393111067,
142.48205447481,
],
[
105.70739342858,
147.89515688303,
],
[
111.0901563458,
157.92532868215,
],
[
115.93172618217,
172.1048667021,
],
[
119.94023,
190.33333,
],
],
"spatial": {
"height": 106.99999999999999,
"unit": "pixel",
"width": 153.66134698205002,
"x": 46.33866301795,
"y": 83.33333,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "path",
"type": "SvgSelector",
},
],
}
`);
});
test("Parsing a quadratic bézier selector", () => {
expect(
parseSelector({
type: "SvgSelector",
value:
'',
})
).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
200.00001,
83.33333,
],
[
162.45341289069,
113.44052265292,
],
[
130.23963379446,
137.53241475664,
],
[
103.07514493893,
155.94441519035,
],
[
80.6823522931,
169.00491330697,
],
[
62.78959556985,
177.03527892978,
],
[
49.13114822878,
180.34986234844,
],
[
39.44721747963,
179.25599431439,
],
[
33.48394428626,
174.05398603533,
],
[
30.99340337135,
165.03712916881,
],
[
31.73360322201,
152.49169581455,
],
[
35.46848609646,
136.69693850527,
],
[
41.96792803201,
117.92509019573,
],
[
51.00773885472,
96.44136424955,
],
[
62.36966219099,
72.50395442325,
],
[
75.84137548181,
46.36403484682,
],
[
91.21649,
18.26576,
],
],
"spatial": {
"height": 162.08410234844,
"unit": "pixel",
"width": 169.00660662865,
"x": 30.99340337135,
"y": 18.26576,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "path",
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
200.00001,
83.33333,
],
[
162.45341289069,
113.44052265292,
],
[
130.23963379446,
137.53241475664,
],
[
103.07514493893,
155.94441519035,
],
[
80.6823522931,
169.00491330697,
],
[
62.78959556985,
177.03527892978,
],
[
49.13114822878,
180.34986234844,
],
[
39.44721747963,
179.25599431439,
],
[
33.48394428626,
174.05398603533,
],
[
30.99340337135,
165.03712916881,
],
[
31.73360322201,
152.49169581455,
],
[
35.46848609646,
136.69693850527,
],
[
41.96792803201,
117.92509019573,
],
[
51.00773885472,
96.44136424955,
],
[
62.36966219099,
72.50395442325,
],
[
75.84137548181,
46.36403484682,
],
[
91.21649,
18.26576,
],
],
"spatial": {
"height": 162.08410234844,
"unit": "pixel",
"width": 169.00660662865,
"x": 30.99340337135,
"y": 18.26576,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "path",
"type": "SvgSelector",
},
],
}
`);
});
test("Parsing a polygon selector", () => {
expect(
parseSelector({
type: "SvgSelector",
value:
'',
})
).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
100,
100,
],
[
150,
25,
],
[
150,
75,
],
[
200,
0,
],
[
100,
100,
],
],
"spatial": {
"height": 100,
"unit": "pixel",
"width": 100,
"x": 100,
"y": 0,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "polygon",
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
100,
100,
],
[
150,
25,
],
[
150,
75,
],
[
200,
0,
],
[
100,
100,
],
],
"spatial": {
"height": 100,
"unit": "pixel",
"width": 100,
"x": 100,
"y": 0,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "polygon",
"type": "SvgSelector",
},
],
}
`);
});
test("Parsing a polyline selector", () => {
expect(
parseSelector({
type: "SvgSelector",
value:
'',
})
).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
100,
100,
],
[
150,
25,
],
[
150,
75,
],
[
200,
0,
],
],
"spatial": {
"height": 100,
"unit": "pixel",
"width": 100,
"x": 100,
"y": 0,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "polyline",
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
100,
100,
],
[
150,
25,
],
[
150,
75,
],
[
200,
0,
],
],
"spatial": {
"height": 100,
"unit": "pixel",
"width": 100,
"x": 100,
"y": 0,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "polyline",
"type": "SvgSelector",
},
],
}
`);
});
test("Parsing the cookbook polygon selector", () => {
expect(
parseSelector({
type: "SvgSelector",
value:
"",
})
).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {},
"points": [
[
270,
1900,
],
[
1530,
1900,
],
[
1530,
1610,
],
[
1315,
1300,
],
[
1200,
986,
],
[
904,
661,
],
[
600,
986,
],
[
500,
1300,
],
[
270,
1630,
],
[
270,
1900,
],
],
"spatial": {
"height": 1239,
"unit": "pixel",
"width": 1260,
"x": 270,
"y": 661,
},
"style": undefined,
"svg": "",
"svgShape": "polygon",
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {},
"points": [
[
270,
1900,
],
[
1530,
1900,
],
[
1530,
1610,
],
[
1315,
1300,
],
[
1200,
986,
],
[
904,
661,
],
[
600,
986,
],
[
500,
1300,
],
[
270,
1630,
],
[
270,
1900,
],
],
"spatial": {
"height": 1239,
"unit": "pixel",
"width": 1260,
"x": 270,
"y": 661,
},
"style": undefined,
"svg": "",
"svgShape": "polygon",
"type": "SvgSelector",
},
],
}
`);
});
test("Parsing a cubic bézier selector with jsdom", () => {
expect(
parseSelector(
{
type: "SvgSelector",
value:
'',
},
{ domParser: new new JSDOM().window.DOMParser() }
)
).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
200.00001,
83.33333,
],
[
189.04971975752,
92.24393388116,
],
[
178.80567039746,
100.43438771718,
],
[
169.23560567209,
107.94149345373,
],
[
160.30805901686,
114.80115206594,
],
[
151.99235355031,
121.0483635583,
],
[
144.25860207391,
126.71722696451,
],
[
137.07770707199,
131.84094034727,
],
[
130.42136071156,
136.45180079809,
],
[
123.61277681833,
141.00964477236,
],
[
113.97516170199,
147.21988073928,
],
[
105.33190309671,
152.52755936099,
],
[
97.61454532645,
157.00342065752,
],
[
90.75699356146,
160.71576500807,
],
[
84.69551381893,
163.73045314736,
],
[
79.3687329639,
166.11090616086,
],
[
71.80644334526,
168.98320166403,
],
[
64.26398773056,
171.19491342159,
],
[
58.37447362501,
172.16184015701,
],
[
53.95206877442,
172.04690989659,
],
[
47.30957775492,
168.43492134597,
],
[
46.33866301795,
160.54259107233,
],
[
49.20284444659,
153.2402160926,
],
[
53.18074876445,
147.44703936037,
],
[
54.94958995593,
145.39344791461,
],
[
56.87747882523,
143.4189243797,
],
[
58.97158482534,
141.53028695154,
],
[
61.2392133287,
139.73448309529,
],
[
63.68780562698,
138.03858954433,
],
[
66.32493893077,
136.4498122992,
],
[
69.153584424,
134.99077366543,
],
[
72.10941134425,
133.81175249426,
],
[
75.1745132003,
132.96710613581,
],
[
78.35180398922,
132.47987074175,
],
[
81.64425923663,
132.37356891368,
],
[
85.054915997,
132.67220970143,
],
[
88.8462153964,
133.49590584219,
],
[
93.13397532373,
135.35476109534,
],
[
97.37963547339,
138.32629590014,
],
[
101.57393111067,
142.48205447481,
],
[
105.70739342858,
147.89515688303,
],
[
111.0901563458,
157.92532868215,
],
[
115.93172618217,
172.1048667021,
],
[
119.94023,
190.33333,
],
],
"spatial": {
"height": 106.99999999999999,
"unit": "pixel",
"width": 153.66134698205002,
"x": 46.33866301795,
"y": 83.33333,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "path",
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#000",
},
"points": [
[
200.00001,
83.33333,
],
[
189.04971975752,
92.24393388116,
],
[
178.80567039746,
100.43438771718,
],
[
169.23560567209,
107.94149345373,
],
[
160.30805901686,
114.80115206594,
],
[
151.99235355031,
121.0483635583,
],
[
144.25860207391,
126.71722696451,
],
[
137.07770707199,
131.84094034727,
],
[
130.42136071156,
136.45180079809,
],
[
123.61277681833,
141.00964477236,
],
[
113.97516170199,
147.21988073928,
],
[
105.33190309671,
152.52755936099,
],
[
97.61454532645,
157.00342065752,
],
[
90.75699356146,
160.71576500807,
],
[
84.69551381893,
163.73045314736,
],
[
79.3687329639,
166.11090616086,
],
[
71.80644334526,
168.98320166403,
],
[
64.26398773056,
171.19491342159,
],
[
58.37447362501,
172.16184015701,
],
[
53.95206877442,
172.04690989659,
],
[
47.30957775492,
168.43492134597,
],
[
46.33866301795,
160.54259107233,
],
[
49.20284444659,
153.2402160926,
],
[
53.18074876445,
147.44703936037,
],
[
54.94958995593,
145.39344791461,
],
[
56.87747882523,
143.4189243797,
],
[
58.97158482534,
141.53028695154,
],
[
61.2392133287,
139.73448309529,
],
[
63.68780562698,
138.03858954433,
],
[
66.32493893077,
136.4498122992,
],
[
69.153584424,
134.99077366543,
],
[
72.10941134425,
133.81175249426,
],
[
75.1745132003,
132.96710613581,
],
[
78.35180398922,
132.47987074175,
],
[
81.64425923663,
132.37356891368,
],
[
85.054915997,
132.67220970143,
],
[
88.8462153964,
133.49590584219,
],
[
93.13397532373,
135.35476109534,
],
[
97.37963547339,
138.32629590014,
],
[
101.57393111067,
142.48205447481,
],
[
105.70739342858,
147.89515688303,
],
[
111.0901563458,
157.92532868215,
],
[
115.93172618217,
172.1048667021,
],
[
119.94023,
190.33333,
],
],
"spatial": {
"height": 106.99999999999999,
"unit": "pixel",
"width": 153.66134698205002,
"x": 46.33866301795,
"y": 83.33333,
},
"style": {
"fill": "#fff",
"stroke": "#000",
},
"svg": "",
"svgShape": "path",
"type": "SvgSelector",
},
],
}
`);
});
test("Parsing a real world example selector", () => {
const annotations: any[] = ghentChoices.items[0]!.annotations[0].items;
const firstTarget = annotations[0].target;
expect(expandTarget(firstTarget)).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {},
"points": [
[
905.7663277587891,
824.1576701660156,
],
[
891.7238851318359,
878.9430197753907,
],
[
890.8197407226562,
905.6661394042969,
],
[
825.7214197998047,
905.6661394042969,
],
[
798.2355216064453,
852.1408363037109,
],
[
827.1680661621094,
820.3150755615235,
],
[
883.5865854492188,
801.5089024658204,
],
[
905.7663277587891,
824.1576701660156,
],
],
"spatial": {
"height": 104.15723693847656,
"unit": "pixel",
"width": 107.53080615234376,
"x": 798.2355216064453,
"y": 801.5089024658204,
},
"style": undefined,
"svg": "",
"svgShape": "polygon",
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {},
"points": [
[
905.7663277587891,
824.1576701660156,
],
[
891.7238851318359,
878.9430197753907,
],
[
890.8197407226562,
905.6661394042969,
],
[
825.7214197998047,
905.6661394042969,
],
[
798.2355216064453,
852.1408363037109,
],
[
827.1680661621094,
820.3150755615235,
],
[
883.5865854492188,
801.5089024658204,
],
[
905.7663277587891,
824.1576701660156,
],
],
"spatial": {
"height": 104.15723693847656,
"unit": "pixel",
"width": 107.53080615234376,
"x": 798.2355216064453,
"y": 801.5089024658204,
},
"style": undefined,
"svg": "",
"svgShape": "polygon",
"type": "SvgSelector",
},
],
"source": "https://iiif.ghentcdh.ugent.be/iiif/manifests/cune-iiif-orm:sde:O.0219/canvas/0001",
"type": "SpecificResource",
}
`);
});
test("Parsing svg target", () => {
const target = {
source: "https://data.globalise.huygens.knaw.nl/manifests/maps/4.VEL/B/B.3/B.3.3/229.json/canvas/p1",
selector: {
type: "SvgSelector",
value:
'',
},
};
expect(expandTarget(target as any)).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"backgroundColor": "#FF0055",
"borderColor": "#FF0055",
"borderWidth": "1",
},
"points": [
[
29234,
89,
],
[
29234,
89,
],
],
"spatial": {
"height": 0,
"unit": "pixel",
"width": 0,
"x": 29234,
"y": 89,
},
"style": {
"fill": "#FF0055",
"fillOpacity": 0.1,
"stroke": "#FF0055",
"strokeWidth": "1",
},
"svg": "",
"svgShape": "polygon",
"type": "SvgSelector",
},
"selectors": [
{
"boxStyle": {
"backgroundColor": "#FF0055",
"borderColor": "#FF0055",
"borderWidth": "1",
},
"points": [
[
29234,
89,
],
[
29234,
89,
],
],
"spatial": {
"height": 0,
"unit": "pixel",
"width": 0,
"x": 29234,
"y": 89,
},
"style": {
"fill": "#FF0055",
"fillOpacity": 0.1,
"stroke": "#FF0055",
"strokeWidth": "1",
},
"svg": "",
"svgShape": "polygon",
"type": "SvgSelector",
},
],
"source": "https://data.globalise.huygens.knaw.nl/manifests/maps/4.VEL/B/B.3/B.3.3/229.json/canvas/p1",
"type": "SpecificResource",
}
`);
});
});
describe("temporal selectors", () => {
test("temporal with start only", () => {
const selector: Selector = {
type: "FragmentSelector",
value: "t=10.5",
};
expect(parseSelector(selector)).toMatchInlineSnapshot(`
{
"selector": {
"temporal": {
"endTime": undefined,
"startTime": 10.5,
},
"type": "TemporalSelector",
},
"selectors": [
{
"temporal": {
"endTime": undefined,
"startTime": 10.5,
},
"type": "TemporalSelector",
},
],
}
`);
});
test("temporal with start and end", () => {
const selector: Selector = {
type: "FragmentSelector",
value: "t=10.5,20.5",
};
expect(parseSelector(selector)).toMatchInlineSnapshot(`
{
"selector": {
"temporal": {
"endTime": 20.5,
"startTime": 10.5,
},
"type": "TemporalSelector",
},
"selectors": [
{
"temporal": {
"endTime": 20.5,
"startTime": 10.5,
},
"type": "TemporalSelector",
},
],
}
`);
});
test("temporal with start offset", () => {
const selector: Selector = {
type: "FragmentSelector",
value: "t=302.05,3971.24",
};
expect(parseSelector(selector)).toMatchInlineSnapshot(`
{
"selector": {
"temporal": {
"endTime": 3971.24,
"startTime": 302.05,
},
"type": "TemporalSelector",
},
"selectors": [
{
"temporal": {
"endTime": 3971.24,
"startTime": 302.05,
},
"type": "TemporalSelector",
},
],
}
`);
});
test("temporal with start and end decimals", () => {
const selector: Selector = {
type: "FragmentSelector",
value: "t=0.1,302.05",
};
expect(parseSelector(selector)).toMatchInlineSnapshot(`
{
"selector": {
"temporal": {
"endTime": 302.05,
"startTime": 0.1,
},
"type": "TemporalSelector",
},
"selectors": [
{
"temporal": {
"endTime": 302.05,
"startTime": 0.1,
},
"type": "TemporalSelector",
},
],
}
`);
});
});
describe("temporal box selector", () => {
test("temporal box selector", () => {
const target =
"https://preview.iiif.io/cookbook/0489-multimedia-canvas/recipe/0489-multimedia-canvas/canvas#xywh=200,200,480,360&t=27,57";
const expanded = expandTarget(target);
expect(expanded).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {},
"spatial": {
"height": 360,
"unit": "pixel",
"width": 480,
"x": 200,
"y": 200,
},
"temporal": {
"endTime": 57,
"startTime": 27,
},
"type": "TemporalBoxSelector",
},
"selectors": [
{
"boxStyle": {},
"spatial": {
"height": 360,
"unit": "pixel",
"width": 480,
"x": 200,
"y": 200,
},
"temporal": {
"endTime": 57,
"startTime": 27,
},
"type": "TemporalBoxSelector",
},
],
"source": {
"id": "https://preview.iiif.io/cookbook/0489-multimedia-canvas/recipe/0489-multimedia-canvas/canvas",
"type": "Canvas",
},
"type": "SpecificResource",
}
`);
});
});
describe("point selector", () => {
test("point selector", () => {
const examplePointSelector: Selector = {
type: "PointSelector",
x: 3385,
y: 1464,
};
expect(parseSelector(examplePointSelector)).toMatchInlineSnapshot(`
{
"selector": {
"spatial": {
"x": 3385,
"y": 1464,
},
"type": "PointSelector",
},
"selectors": [
{
"spatial": {
"x": 3385,
"y": 1464,
},
"type": "PointSelector",
},
],
}
`);
});
});
describe("parse selector with stylesheet", () => {
test("box selector with css", () => {
const exampleSelector: Selector = {
type: "FragmentSelector",
value: "xywh=100,140,480,360",
};
const parsed = parseSelector(
exampleSelector,
{
loadedStylesheets: { "https://example.org/styles": ".test { background: red }" },
},
{
styleClass: "test",
}
);
expect(parsed).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"background": "red",
},
"spatial": {
"height": 360,
"unit": "pixel",
"width": 480,
"x": 100,
"y": 140,
},
"type": "BoxSelector",
},
"selectors": [
{
"boxStyle": {
"background": "red",
},
"spatial": {
"height": 360,
"unit": "pixel",
"width": 480,
"x": 100,
"y": 140,
},
"type": "BoxSelector",
},
],
}
`);
});
test("box selector with css transform", () => {
const exampleSelector: Selector = {
type: "FragmentSelector",
value: "xywh=0,0,2105,1523",
};
const parsed = parseSelector(
exampleSelector,
{
loadedStylesheets: {
"https://example.org/styles": `
.rotated {
transform-origin: 761px 1344px;
transform: rotate(90deg) translateY(-582px);
}
`,
},
},
{
styleClass: "rotated",
}
);
expect(parsed).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"transform": "rotate(90deg) translateY(-582px)",
"transformOrigin": "761px 1344px",
},
"rotation": 90,
"rotationOrigin": {
"unit": "pixel",
"x": 761,
"y": 1344,
},
"spatial": {
"height": 1523,
"unit": "pixel",
"width": 2105,
"x": 0,
"y": 0,
},
"transform": {
"rotation": 90,
"rotationOrigin": {
"unit": "pixel",
"x": 761,
"y": 1344,
},
"transform": "rotate(90deg) translateY(-582px)",
"transformOrigin": "761px 1344px",
"translate": {
"unit": "pixel",
"x": 0,
"y": -582,
},
},
"translate": {
"unit": "pixel",
"x": 0,
"y": -582,
},
"type": "BoxSelector",
},
"selectors": [
{
"boxStyle": {
"transform": "rotate(90deg) translateY(-582px)",
"transformOrigin": "761px 1344px",
},
"rotation": 90,
"rotationOrigin": {
"unit": "pixel",
"x": 761,
"y": 1344,
},
"spatial": {
"height": 1523,
"unit": "pixel",
"width": 2105,
"x": 0,
"y": 0,
},
"transform": {
"rotation": 90,
"rotationOrigin": {
"unit": "pixel",
"x": 761,
"y": 1344,
},
"transform": "rotate(90deg) translateY(-582px)",
"transformOrigin": "761px 1344px",
"translate": {
"unit": "pixel",
"x": 0,
"y": -582,
},
},
"translate": {
"unit": "pixel",
"x": 0,
"y": -582,
},
"type": "BoxSelector",
},
],
}
`);
});
test("expandTarget with CSS", () => {
const expanded = expandTarget(
{
type: "SpecificResource",
source: "https://preview.iiif.io/cookbook/0045-css/recipe/0045-css/canvas/p1#xywh=170,160,2200,1000",
styleClass: "author2",
},
{
loadedStylesheets: {
"https://preview.iiif.io/cookbook/0045-css/recipe/0045-css/style.css": `
.author1 {
color: #f00;
background-color: #fff;
border-color: #f00;
}
.author2 {
color: #1a1;
background-color: #fff;
border-color: #0f0;
}`,
},
}
);
expect(expanded).toMatchInlineSnapshot(`
{
"selector": {
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#0f0",
},
"spatial": {
"height": 1000,
"unit": "pixel",
"width": 2200,
"x": 170,
"y": 160,
},
"type": "BoxSelector",
},
"selectors": [
{
"boxStyle": {
"backgroundColor": "#fff",
"borderColor": "#0f0",
},
"spatial": {
"height": 1000,
"unit": "pixel",
"width": 2200,
"x": 170,
"y": 160,
},
"type": "BoxSelector",
},
],
"source": {
"id": "https://preview.iiif.io/cookbook/0045-css/recipe/0045-css/canvas/p1",
"type": "Canvas",
},
"type": "SpecificResource",
}
`);
});
});
});