Skip to content
Open
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
121 changes: 116 additions & 5 deletions packages/lexical-playground/__tests__/unit/DateTimeImport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ const DateTimeImportTestExtension = defineExtension({
});

interface ImportResult {
bold: boolean[];
dateTimes: number[];
hasDateTimeNode: boolean;
html: string;
text: string;
values: string[];
}

function importHtml(html: string): ImportResult {
Expand All @@ -45,18 +47,41 @@ function importHtml(html: string): ImportResult {
expect(() => editor.getEditorState().toJSON()).not.toThrow();

return editor.read(() => {
const dateTimes = $nodesOfType(DateTimeNode).map(node =>
node.getDateTime().getTime(),
);
const nodes = $nodesOfType(DateTimeNode);
return {
dateTimes,
hasDateTimeNode: dateTimes.length > 0,
bold: nodes.map(node => node.hasFormat('bold')),
dateTimes: nodes.map(node => node.getDateTime().getTime()),
hasDateTimeNode: nodes.length > 0,
html: $generateHtmlFromNodes(editor, null),
text: $getRoot().getTextContent(),
values: nodes.map(node => node.getDateTimeValue()),
};
});
}

// A date chip copied from Google Docs (Chrome, Korean UI). Google stores a
// chip without a time (dfie_tpt 0) as noon UTC of its day.
const GOOGLE_DOCS_DATE = {
dfie_dt: '2026년 11월 17일',
dfie_l: 'ko',
dfie_p: {fres_frt: 0, fres_usd: '', tres_tv: 'y년 MMM d일'},
dfie_pt: 3,
dfie_tpt: 0,
dfie_ts: {tv: {tv_n: 0, tv_s: 1794916800}},
dfie_tzi: '',
fres_frt: 1,
fres_usd: '',
};

function googleDocsHtml(datDf: object | null, style = ''): string {
const richLinks = JSON.stringify({dat_df: datDf, type: 'date'});
return `<p><span data-rich-links='${richLinks}' style="${style}">gd text</span></p>`;
}

function atSeconds(tvS: number) {
return {...GOOGLE_DOCS_DATE, dfie_ts: {tv: {tv_n: 0, tv_s: tvS}}};
}

describe('DateTime HTML import', () => {
it('keeps the element content when the date cannot be parsed', () => {
const result = importHtml(
Expand Down Expand Up @@ -122,4 +147,90 @@ describe('DateTime HTML import', () => {
`data-lexical-datetime="${new Date(seconds * 1000).toISOString()}"`,
);
});

it.each([
['a date chip', GOOGLE_DOCS_DATE, '2026-11-17'],
[
'a date chip whose time was removed',
{...GOOGLE_DOCS_DATE, dfie_tzi: 'ROK'},
'2026-11-17',
],
[
'a chip at 12:00 in Seoul',
{
...atSeconds(1794884400),
dfie_dt: '2026년 11월 17일 오후 12:00 GMT+9',
dfie_tpt: 2,
dfie_tzi: 'ROK',
},
'2026-11-17T03:00:00.000Z',
],
[
'a chip at 12:00 UTC',
{
...GOOGLE_DOCS_DATE,
dfie_dt: '2026년 11월 17일 오후 12:00 UTC',
dfie_tpt: 2,
dfie_tzi: 'UCT',
},
'2026-11-17T12:00:00.000Z',
],
[
// A 12:00 chip with its time zone hidden copies the same whether it was
// set in Seoul or in UTC: Google keeps only the wall clock time, written
// as UTC
'a chip at 12:00 without a time zone',
{
...GOOGLE_DOCS_DATE,
dfie_dt: '2026년 11월 17일 오후 12:00',
dfie_tpt: 1,
},
new Date(2026, 10, 17, 12).toISOString(),
],
Comment thread
rootnode-bot[bot] marked this conversation as resolved.
Resolved
[
'a date chip whose timestamp is 0',
{...atSeconds(0), dfie_dt: '2026-11-17'},
'2026-11-17T00:00:00.000Z',
],
['a date chip in year 9999', atSeconds(253382817600), '9999-05-20'],
['a date chip in year 0', atSeconds(-62155080000), '0000-05-20'],
[
'a date chip in year 10000',
atSeconds(253414440000),
'+010000-05-20T12:00:00.000Z',
],
[
'a date chip in year -1',
atSeconds(-62186702400),
'-000001-05-20T12:00:00.000Z',
],
])('imports %s from Google Docs', (_name, datDf, value) => {
const result = importHtml(googleDocsHtml(datDf));

expect(result.values).toEqual([value]);
});

it.each([
['a timestamp out of range', atSeconds(9e12)],
['no date', null],
])('keeps the text of a Google Docs chip with %s', (_name, datDf) => {
const result = importHtml(googleDocsHtml(datDf));

expect(result.hasDateTimeNode).toBe(false);
expect(result.text).toBe('gd text');
});

it.each([
['date chip', GOOGLE_DOCS_DATE, '2026-11-17'],
[
'chip with a time',
{...atSeconds(1794884400), dfie_tpt: 2, dfie_tzi: 'ROK'},
'2026-11-17T03:00:00.000Z',
],
])('keeps the format of a Google Docs %s', (_name, datDf, value) => {
const result = importHtml(googleDocsHtml(datDf, 'font-weight: 700'));

expect(result.values).toEqual([value]);
expect(result.bold).toEqual([true]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ interface ChildResult {
exportedText: string[];
getDateTime: number[];
getDateTime1942: number[];
googleDocsImport: string[];
googleDocsTime: [[string, number], string[]];
htmlImport: string[];
offsetless: [string, string];
setAllDayDate: string[];
Expand Down Expand Up @@ -68,6 +70,11 @@ describe('DateTimeNode in other time zones', () => {
expect(result.text).toEqual(ALL_DAY_TEXT);
expect(result.exportedText).toEqual(ALL_DAY_TEXT);
expect(result.htmlImport).toEqual(['2011-12-30']);
expect(result.googleDocsImport).toEqual(['2026-11-17']);
expect(result.googleDocsTime).toEqual([
['12:00', 17],
['2026-11-17T03:00:00.000Z'],
]);
expect(result.setAllDayDate).toEqual(['2026-11-04', '2027-01-01']);
expect(result.decoratedDateTime).toEqual([2026, 10, 4]);
expect(result.e2eDateValue).toEqual(['2026-11-04', '2027-01-01']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ const result = withDOM(() => {
const dateTime = node.getDateTime();
return [dateTime.getDate(), dateTime.getHours()];
}),
// A Google Docs date chip: noon UTC, which is the next day from UTC+12 east
googleDocsImport: importHtmlValue(
`<p><span data-rich-links='{"type":"date","dat_df":{"dfie_ts":{"tv":{"tv_s":1794916800}},"dfie_tpt":0}}'>x</span></p>`,
),
// A Google Docs 12:00 chip without a time zone pastes as 12:00 here, and
// one with a time zone keeps its instant
googleDocsTime: [
...importHtmlValue(
`<p><span data-rich-links='{"type":"date","dat_df":{"dfie_ts":{"tv":{"tv_s":1794916800}},"dfie_tpt":1}}'>x</span></p>`,
).map(value => [getTimeValue(value), new Date(value).getDate()]),
importHtmlValue(
`<p><span data-rich-links='{"type":"date","dat_df":{"dfie_ts":{"tv":{"tv_s":1794884400}},"dfie_tpt":2}}'>x</span></p>`,
),
],
htmlImport: importHtmlValue(
'<p><span data-lexical-datetime="2011-12-30">x</span></p>',
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,46 @@ const DateTimeRule = defineImportRule({

const GoogleDocsDateRule = defineImportRule({
$import: (_ctx, el, $next) => {
let parsed: {dat_df?: {dfie_ts?: {tv?: {tv_s?: number}}; dfie_dt?: string}};
let parsed: {
dat_df?: {
dfie_dt?: string;
dfie_tpt?: number;
dfie_ts?: {tv?: {tv_s?: number}};
} | null;
};
try {
parsed = JSON.parse(el.getAttribute('data-rich-links') ?? '{}');
} catch {
return $next();
}
if (parsed?.dat_df === undefined) {
const {dat_df} = parsed;
if (dat_df == null) {
return $next();
}
const parsedDate =
(parsed.dat_df.dfie_ts?.tv?.tv_s ?? 0) * 1000 ||
Date.parse(parsed.dat_df.dfie_dt ?? '');
if (isNaN(parsedDate)) {
const seconds = dat_df.dfie_ts?.tv?.tv_s;
const date = new Date(
(seconds ?? 0) * 1000 || Date.parse(dat_df.dfie_dt ?? ''),
);
if (isNaN(date.getTime())) {
return $next();
}
return [
applyFormatFromStyle($createDateTimeNode(new Date(parsedDate)), el.style),
];
// Google Docs writes what some chips show as UTC: noon of the day for a
// date without a time (dfie_tpt 0), and the wall clock time for a time
// without a time zone (dfie_tpt 1). The time is kept without an offset,
// so it is read as local time where it is pasted.
const year = date.getUTCFullYear();
const shown =
seconds && year >= 0 && year <= 9999 ? date.toISOString() : '';
const value =
dat_df.dfie_tpt === 0
? shown.slice(0, 10)
: dat_df.dfie_tpt === 1
? shown.slice(0, 19)
: '';
const node =
(value !== '' ? $createDateTimeNodeFromValue(value) : null) ??
$createDateTimeNode(date);
return [applyFormatFromStyle(node, el.style)];
},
match: sel.tag('span').attr('data-rich-links', /"type"\s*:\s*"date"/),
name: '@lexical/playground/datetime-google-docs',
Expand Down
Loading