@rr0/cms 0.3.71 → 0.3.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -24,7 +24,7 @@ export class TimeReplacer {
|
|
|
24
24
|
}
|
|
25
25
|
async replacement(context, origEl) {
|
|
26
26
|
let replacement;
|
|
27
|
-
if (origEl.dateTime) { // Already done
|
|
27
|
+
if (origEl.dateTime || origEl.dataset.format === "none") { // Already done, or not to be interpreted
|
|
28
28
|
replacement = origEl;
|
|
29
29
|
}
|
|
30
30
|
else {
|
|
@@ -32,7 +32,13 @@ export class TimeReplacer {
|
|
|
32
32
|
const timeStr = origEl.textContent;
|
|
33
33
|
const valid = context.time.updateFromStr(timeStr);
|
|
34
34
|
const between = context.messages.context.time.between.test(TimeReplacer.precedingText(origEl));
|
|
35
|
-
|
|
35
|
+
try {
|
|
36
|
+
replacement = valid && this.factory.create(context, previousContext, { url: true, contentOnly: true, between });
|
|
37
|
+
}
|
|
38
|
+
catch (e) { // One unrenderable time must not stop the others of the page
|
|
39
|
+
context.warn("Could not render time", timeStr, e.message);
|
|
40
|
+
replacement = undefined;
|
|
41
|
+
}
|
|
36
42
|
if (!replacement) {
|
|
37
43
|
replacement = origEl;
|
|
38
44
|
// replacement.setAttribute("datetime", context.time.toString())
|
|
@@ -102,6 +102,36 @@ describe("TimeReplacer", async () => {
|
|
|
102
102
|
expect(await renderIn("en", "from <time>1951/1955</time>")).toBe("1951 to 1955");
|
|
103
103
|
});
|
|
104
104
|
});
|
|
105
|
+
describe("renders an EDTF season", () => {
|
|
106
|
+
async function render(locale, value) {
|
|
107
|
+
const context = cmsTestUtil.time.newHtmlContext("1/9/9/0/08/index.html", "", locale);
|
|
108
|
+
const el = context.file.document.createElement("time");
|
|
109
|
+
el.textContent = value;
|
|
110
|
+
return (await replacer.replacement(context, el)).textContent;
|
|
111
|
+
}
|
|
112
|
+
test("in French and English, instead of failing on month 23", async () => {
|
|
113
|
+
expect(await render("fr", "1954-23")).toBe("automne 1954");
|
|
114
|
+
expect(await render("en", "1954-22")).toBe("summer 1954");
|
|
115
|
+
});
|
|
116
|
+
test("after another date", async () => {
|
|
117
|
+
const context = cmsTestUtil.time.newHtmlContext("1/9/9/0/08/index.html", "");
|
|
118
|
+
const previous = context.file.document.createElement("time");
|
|
119
|
+
previous.textContent = "1954-10-16";
|
|
120
|
+
await replacer.replacement(context, previous);
|
|
121
|
+
const season = context.file.document.createElement("time");
|
|
122
|
+
season.textContent = "1954-24";
|
|
123
|
+
expect((await replacer.replacement(context, season)).textContent).toBe("hiver 1954");
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
test("leaves a time with data-format=\"none\" as written", async () => {
|
|
127
|
+
const context = cmsTestUtil.time.newHtmlContext("1/9/9/0/08/index.html", "");
|
|
128
|
+
const el = context.file.document.createElement("time");
|
|
129
|
+
el.dataset.format = "none";
|
|
130
|
+
el.textContent = "Dans la nuit";
|
|
131
|
+
const replaced = await replacer.replacement(context, el);
|
|
132
|
+
expect(replaced).toBe(el);
|
|
133
|
+
expect(replaced.outerHTML).toBe(`<time data-format="none">Dans la nuit</time>`);
|
|
134
|
+
});
|
|
105
135
|
test("parses unsupported", async () => {
|
|
106
136
|
const interval = "moi";
|
|
107
137
|
const context = cmsTestUtil.time.newHtmlContext("1/9/9/0/08/index.html", "");
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TimeTextBuilder } from "./TimeTextBuilder.js";
|
|
1
2
|
import { Level0Duration } from "@rr0/time";
|
|
2
3
|
export class RelativeTimeTextBuilder {
|
|
3
4
|
constructor(timeTextBuilder) {
|
|
@@ -9,6 +10,9 @@ export class RelativeTimeTextBuilder {
|
|
|
9
10
|
const time = newContext.time;
|
|
10
11
|
const previousTime = time.equals(oldContext === null || oldContext === void 0 ? void 0 : oldContext.time) ? undefined : oldContext.time;
|
|
11
12
|
let options;
|
|
13
|
+
if (TimeTextBuilder.isSeason(time.getMonth()) || TimeTextBuilder.isSeason(previousTime === null || previousTime === void 0 ? void 0 : previousTime.getMonth())) {
|
|
14
|
+
return undefined; // A season has no position within a year: always render it in full
|
|
15
|
+
}
|
|
12
16
|
if (previousTime === null || previousTime === void 0 ? void 0 : previousTime.isDefined()) {
|
|
13
17
|
options = {};
|
|
14
18
|
const deltaMs = time.date.compare(previousTime.date);
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { RR0Context } from "../../RR0Context.js";
|
|
2
2
|
export declare class TimeTextBuilder {
|
|
3
3
|
readonly options: Intl.DateTimeFormatOptions;
|
|
4
|
+
/**
|
|
5
|
+
* EDTF season codes (21-24) and their names, per language.
|
|
6
|
+
*/
|
|
7
|
+
static readonly seasons: Record<string, Record<number, string>>;
|
|
8
|
+
static isSeason(month: number | undefined): boolean;
|
|
4
9
|
/**
|
|
5
10
|
* @param options The default date format.to use.
|
|
6
11
|
*/
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export class TimeTextBuilder {
|
|
2
|
+
static isSeason(month) {
|
|
3
|
+
return month >= 21 && month <= 24;
|
|
4
|
+
}
|
|
2
5
|
/**
|
|
3
6
|
* @param options The default date format.to use.
|
|
4
7
|
*/
|
|
@@ -9,10 +12,16 @@ export class TimeTextBuilder {
|
|
|
9
12
|
* Build a textual representation of context's time, according to context's locale.
|
|
10
13
|
*/
|
|
11
14
|
build(context, options = this.options) {
|
|
15
|
+
var _a;
|
|
12
16
|
const time = context.time;
|
|
13
17
|
const printOptions = {};
|
|
14
18
|
const date = new Date(undefined, undefined, undefined);
|
|
15
19
|
const year = time.getYear();
|
|
20
|
+
const season = time.getMonth();
|
|
21
|
+
if (TimeTextBuilder.isSeason(season)) { // "1954-23": no Date can hold it
|
|
22
|
+
const names = (_a = TimeTextBuilder.seasons[context.locale]) !== null && _a !== void 0 ? _a : TimeTextBuilder.seasons.en;
|
|
23
|
+
return year ? `${names[season]} ${year}` : names[season];
|
|
24
|
+
}
|
|
16
25
|
if (year) {
|
|
17
26
|
date.setFullYear(year);
|
|
18
27
|
printOptions.year = options.year;
|
|
@@ -79,3 +88,10 @@ export class TimeTextBuilder {
|
|
|
79
88
|
return text;
|
|
80
89
|
}
|
|
81
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* EDTF season codes (21-24) and their names, per language.
|
|
93
|
+
*/
|
|
94
|
+
TimeTextBuilder.seasons = {
|
|
95
|
+
fr: { 21: "printemps", 22: "été", 23: "automne", 24: "hiver" },
|
|
96
|
+
en: { 21: "spring", 22: "summer", 23: "autumn", 24: "winter" }
|
|
97
|
+
};
|
package/package.json
CHANGED