From 7bf5371f53c96170f2b8113fc32abd55c3af2806 Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Fri, 18 Oct 2024 09:27:31 +0200 Subject: [PATCH 1/5] Add alpha version support to injector. --- src/cdn/ck/createCKCdnBaseBundlePack.ts | 3 +- src/cdn/ck/createCKCdnUrl.ts | 7 +-- src/cdn/ck/isCKCdnVersion.ts | 55 +++++++++++++++++++ src/cdn/loadCKEditorCloud.ts | 9 ++- .../getCKBaseBundleInstallationInfo.ts | 6 +- src/installation-info/types.ts | 4 +- tests/cdn/ck/isCKCdnVersion.test.ts | 32 +++++++++++ tests/cdn/loadCKEditorCloud.test.ts | 24 ++++++++ 8 files changed, 127 insertions(+), 13 deletions(-) create mode 100644 src/cdn/ck/isCKCdnVersion.ts create mode 100644 tests/cdn/ck/isCKCdnVersion.test.ts diff --git a/src/cdn/ck/createCKCdnBaseBundlePack.ts b/src/cdn/ck/createCKCdnBaseBundlePack.ts index eb29381..7f2600d 100644 --- a/src/cdn/ck/createCKCdnBaseBundlePack.ts +++ b/src/cdn/ck/createCKCdnBaseBundlePack.ts @@ -12,7 +12,8 @@ import { without } from '../../utils/without.js'; import { getCKBaseBundleInstallationInfo } from '../../installation-info/getCKBaseBundleInstallationInfo.js'; import { createCKDocsUrl } from '../../docs/createCKDocsUrl.js'; -import { createCKCdnUrl, type CKCdnVersion } from './createCKCdnUrl.js'; +import { createCKCdnUrl } from './createCKCdnUrl.js'; +import type { CKCdnVersion } from './isCKCdnVersion.js'; import './globals.js'; diff --git a/src/cdn/ck/createCKCdnUrl.ts b/src/cdn/ck/createCKCdnUrl.ts index 0e78171..aa7408a 100644 --- a/src/cdn/ck/createCKCdnUrl.ts +++ b/src/cdn/ck/createCKCdnUrl.ts @@ -3,18 +3,13 @@ * For licensing, see LICENSE.md. */ -import type { SemanticVersion } from '../../utils/version/isSemanticVersion.js'; +import type { CKCdnVersion } from './isCKCdnVersion.js'; /** * The URL of the CKEditor CDN. */ export const CK_CDN_URL = 'https://cdn.ckeditor.com'; -/** - * A version of a file on the CKEditor CDN. - */ -export type CKCdnVersion = SemanticVersion; - /** * Creates a URL to a file on the CKEditor CDN. * diff --git a/src/cdn/ck/isCKCdnVersion.ts b/src/cdn/ck/isCKCdnVersion.ts new file mode 100644 index 0000000..79ea5f4 --- /dev/null +++ b/src/cdn/ck/isCKCdnVersion.ts @@ -0,0 +1,55 @@ +/** + * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved. + * For licensing, see LICENSE.md. + */ + +import { isSemanticVersion, type SemanticVersion } from '../../utils/isSemanticVersion.js'; + +/** + * A version of a file on the CKEditor CDN that is used for testing purposes. + */ +export type CKCdnTestingVersion = + | 'alpha' + | `${ SemanticVersion }-nightly-${ string }` + | `rc-${ string }`; + +/** + * A version of a file on the CKEditor CDN. + */ +export type CKCdnVersion = + | SemanticVersion + | CKCdnTestingVersion; + +/** + * Checks if the given string is a version of a file on the CKEditor CDN. + * + * @param version - The string to check. + * @returns `true` if the string is a version of a file on the CKEditor CDN, `false` otherwise. + * @example + * ```ts + * isCKCdnVersion( 'nightly' ); // -> true + * isCKCdnVersion( 'alpha' ); // -> true + * isCKCdnVersion( 'rc-1.2.3' ); // -> true + * isCKCdnVersion( '1.2.3' ); // -> false + * ``` + */ +export function isCKCdnTestingVersion( version: string ): version is CKCdnTestingVersion { + return version === 'alpha' || version === 'nightly' || version.startsWith( 'rc-' ); +} + +/** + * Checks if the given string is a version of a file on the CKEditor CDN. + * + * @param version - The string to check. + * @returns `true` if the string is a version of a file on the CKEditor CDN, `false` otherwise. + * @example + * ```ts + * isCKCdnVersion( 'nightly' ); // -> true + * isCKCdnVersion( 'alpha' ); // -> true + * isCKCdnVersion( 'rc-1.2.3' ); // -> true + * isCKCdnVersion( '1.2.3' ); // -> true + * ``` + */ +export function isCKCdnVersion( version: string ): version is CKCdnVersion { + return isSemanticVersion( version ) || isCKCdnTestingVersion( version ); +} diff --git a/src/cdn/loadCKEditorCloud.ts b/src/cdn/loadCKEditorCloud.ts index 8984325..dfa59e1 100644 --- a/src/cdn/loadCKEditorCloud.ts +++ b/src/cdn/loadCKEditorCloud.ts @@ -13,7 +13,8 @@ import { } from './ckbox/createCKBoxCdnBundlePack.js'; import type { ConditionalBlank } from '../types/ConditionalBlank.js'; -import type { CKCdnVersion, createCKCdnUrl } from './ck/createCKCdnUrl.js'; +import { isCKCdnTestingVersion, type CKCdnVersion } from './ck/isCKCdnVersion.js'; +import { createCKCdnUrl } from './ck/createCKCdnUrl.js'; import { loadCKCdnResourcesPack, @@ -83,6 +84,12 @@ export function loadCKEditorCloud( } } = config; + if ( isCKCdnTestingVersion( version ) ) { + console.warn( + 'You are using a testing version of CKEditor 5. Please remember that it is not suitable for production environments.' + ); + } + const pack = combineCKCdnBundlesPacks( { CKEditor: createCKCdnBaseBundlePack( { version, diff --git a/src/installation-info/getCKBaseBundleInstallationInfo.ts b/src/installation-info/getCKBaseBundleInstallationInfo.ts index 41272b8..d22cde6 100644 --- a/src/installation-info/getCKBaseBundleInstallationInfo.ts +++ b/src/installation-info/getCKBaseBundleInstallationInfo.ts @@ -5,15 +5,15 @@ import type { BundleInstallationInfo } from './types.js'; -import { isSemanticVersion } from '../utils/version/isSemanticVersion.js'; +import { isCKCdnVersion, type CKCdnVersion } from '../cdn/ck/isCKCdnVersion.js'; /** * Returns information about the base CKEditor bundle installation. */ -export function getCKBaseBundleInstallationInfo(): BundleInstallationInfo | null { +export function getCKBaseBundleInstallationInfo(): BundleInstallationInfo | null { const { CKEDITOR_VERSION, CKEDITOR } = window; - if ( !isSemanticVersion( CKEDITOR_VERSION ) ) { + if ( !isCKCdnVersion( CKEDITOR_VERSION ) ) { return null; } diff --git a/src/installation-info/types.ts b/src/installation-info/types.ts index 3442dbf..7e878b7 100644 --- a/src/installation-info/types.ts +++ b/src/installation-info/types.ts @@ -13,7 +13,7 @@ type BundleInstallationSource = 'npm' | 'cdn'; /** * Information about the currently installed CKEditor. */ -export type BundleInstallationInfo = { +export type BundleInstallationInfo = { /** * The source from which CKEditor was installed. @@ -23,5 +23,5 @@ export type BundleInstallationInfo = { /** * The version of CKEditor. */ - version: SemanticVersion; + version: V; }; diff --git a/tests/cdn/ck/isCKCdnVersion.test.ts b/tests/cdn/ck/isCKCdnVersion.test.ts new file mode 100644 index 0000000..c48194b --- /dev/null +++ b/tests/cdn/ck/isCKCdnVersion.test.ts @@ -0,0 +1,32 @@ +/** + * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved. + * For licensing, see LICENSE.md. + */ + +import { describe, it, expect } from 'vitest'; +import { isCKCdnVersion, isCKCdnTestingVersion } from '@/cdn/ck/isCKCdnVersion.js'; + +const testCases = [ + { version: 'alpha', isTesting: true, isVersion: true }, + { version: 'nightly', isTesting: true, isVersion: true }, + { version: 'rc-1.2.3', isTesting: true, isVersion: true }, + { version: '1.2.3', isTesting: false, isVersion: true }, + { version: 'beta', isTesting: false, isVersion: false }, + { version: '1.2', isTesting: false, isVersion: false } +]; + +describe( 'isCKCdnTestingVersion', () => { + for ( const { version, isTesting } of testCases ) { + it( `should return ${ isTesting } for "${ version }"`, () => { + expect( isCKCdnTestingVersion( version ) ).toBe( isTesting ); + } ); + } +} ); + +describe( 'isCKCdnVersion', () => { + for ( const { version, isVersion } of testCases ) { + it( `should return ${ isVersion } for "${ version }"`, () => { + expect( isCKCdnVersion( version ) ).toBe( isVersion ); + } ); + } +} ); diff --git a/tests/cdn/loadCKEditorCloud.test.ts b/tests/cdn/loadCKEditorCloud.test.ts index f8e8463..bfa9240 100644 --- a/tests/cdn/loadCKEditorCloud.test.ts +++ b/tests/cdn/loadCKEditorCloud.test.ts @@ -27,6 +27,8 @@ describe( 'loadCKEditorCloud', () => { removeAllCkCdnResources(); vi.spyOn( console, 'error' ).mockImplementation( () => undefined ); + vi.spyOn( console, 'warn' ).mockImplementation( () => undefined ); + window.FakePlugin = { fake: 'fake' }; } ); @@ -34,6 +36,28 @@ describe( 'loadCKEditorCloud', () => { vi.restoreAllMocks(); } ); + for ( const version of [ 'alpha', 'nightly' ] as const ) { + it( `should raise warning if ${ version } version is used`, async () => { + const { CKEditor } = await loadCKEditorCloud( { + version + } ); + + expect( CKEditor.ClassicEditor ).toBeDefined(); + expect( console.warn ).toBeCalledWith( + 'You are using a testing version of CKEditor 5. Please remember that it is not suitable for production environments.' + ); + } ); + } + + it( 'should not raise a warning if non-testing version is passed', async () => { + const { CKEditor } = await loadCKEditorCloud( { + version: '43.0.0' + } ); + + expect( CKEditor.ClassicEditor ).toBeDefined(); + expect( console.warn ).not.toBeCalled(); + } ); + it( 'should be possible to load base ckeditor with base features', async () => { const { CKEditor, CKBox, CKEditorPremiumFeatures } = await loadCKEditorCloud( { version: '43.0.0' From 3a16cb966c4a933ce5dad090d3e623e095192dbf Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Fri, 18 Oct 2024 09:36:45 +0200 Subject: [PATCH 2/5] Export `CKCdnUrlCreator` type --- src/cdn/ck/createCKCdnBaseBundlePack.ts | 4 ++-- src/cdn/ck/createCKCdnUrl.ts | 5 +++++ src/cdn/loadCKEditorCloud.ts | 5 +++-- src/index.ts | 3 ++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/cdn/ck/createCKCdnBaseBundlePack.ts b/src/cdn/ck/createCKCdnBaseBundlePack.ts index 7f2600d..94b5616 100644 --- a/src/cdn/ck/createCKCdnBaseBundlePack.ts +++ b/src/cdn/ck/createCKCdnBaseBundlePack.ts @@ -12,7 +12,7 @@ import { without } from '../../utils/without.js'; import { getCKBaseBundleInstallationInfo } from '../../installation-info/getCKBaseBundleInstallationInfo.js'; import { createCKDocsUrl } from '../../docs/createCKDocsUrl.js'; -import { createCKCdnUrl } from './createCKCdnUrl.js'; +import { createCKCdnUrl, type CKCdnUrlCreator } from './createCKCdnUrl.js'; import type { CKCdnVersion } from './isCKCdnVersion.js'; import './globals.js'; @@ -119,5 +119,5 @@ export type CKCdnBaseBundlePackConfig = { /** * The function that creates custom CDN URLs. */ - createCustomCdnUrl?: typeof createCKCdnUrl; + createCustomCdnUrl?: CKCdnUrlCreator; }; diff --git a/src/cdn/ck/createCKCdnUrl.ts b/src/cdn/ck/createCKCdnUrl.ts index aa7408a..4f86345 100644 --- a/src/cdn/ck/createCKCdnUrl.ts +++ b/src/cdn/ck/createCKCdnUrl.ts @@ -27,3 +27,8 @@ export const CK_CDN_URL = 'https://cdn.ckeditor.com'; export function createCKCdnUrl( bundle: string, file: string, version: CKCdnVersion ): string { return `${ CK_CDN_URL }/${ bundle }/${ version }/${ file }`; } + +/** + * A function that creates a URL to a file on the CKEditor CDN. + */ +export type CKCdnUrlCreator = typeof createCKCdnUrl; diff --git a/src/cdn/loadCKEditorCloud.ts b/src/cdn/loadCKEditorCloud.ts index dfa59e1..168db55 100644 --- a/src/cdn/loadCKEditorCloud.ts +++ b/src/cdn/loadCKEditorCloud.ts @@ -12,9 +12,10 @@ import { type CKBoxCdnBundlePackConfig } from './ckbox/createCKBoxCdnBundlePack.js'; +import type { CKCdnUrlCreator } from './ck/createCKCdnUrl.js'; import type { ConditionalBlank } from '../types/ConditionalBlank.js'; + import { isCKCdnTestingVersion, type CKCdnVersion } from './ck/isCKCdnVersion.js'; -import { createCKCdnUrl } from './ck/createCKCdnUrl.js'; import { loadCKCdnResourcesPack, @@ -194,5 +195,5 @@ export type CKEditorCloudConfig Date: Fri, 18 Oct 2024 09:39:30 +0200 Subject: [PATCH 3/5] Fix typings in tests --- tests/cdn/ck/createCKCdnBaseBundlePack.test.ts | 2 +- tests/cdn/ck/createCKCdnPremiumBundlePack.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cdn/ck/createCKCdnBaseBundlePack.test.ts b/tests/cdn/ck/createCKCdnBaseBundlePack.test.ts index 7b11d6d..39ac2fa 100644 --- a/tests/cdn/ck/createCKCdnBaseBundlePack.test.ts +++ b/tests/cdn/ck/createCKCdnBaseBundlePack.test.ts @@ -5,7 +5,7 @@ import { describe, it, expect, beforeEach } from 'vitest'; -import type { CKCdnVersion } from '@/cdn/ck/createCKCdnUrl.js'; +import type { CKCdnVersion } from '@/cdn/ck/isCKCdnVersion.js'; import { createCKCdnBaseBundlePack, diff --git a/tests/cdn/ck/createCKCdnPremiumBundlePack.test.ts b/tests/cdn/ck/createCKCdnPremiumBundlePack.test.ts index ee19f06..b5aca16 100644 --- a/tests/cdn/ck/createCKCdnPremiumBundlePack.test.ts +++ b/tests/cdn/ck/createCKCdnPremiumBundlePack.test.ts @@ -5,7 +5,7 @@ import { describe, it, expect, beforeEach } from 'vitest'; -import type { CKCdnVersion } from '@/cdn/ck/createCKCdnUrl.js'; +import type { CKCdnVersion } from '@/cdn/ck/isCKCdnVersion.js'; import { loadCKCdnResourcesPack } from '@/cdn/utils/loadCKCdnResourcesPack.js'; import { removeAllCkCdnResources } from '@/test-utils/cdn/removeAllCkCdnResources.js'; From e517a9e05489c3a37a3daba01e4dfd01e01f47b1 Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Mon, 4 Nov 2024 13:13:51 +0100 Subject: [PATCH 4/5] Fix tests --- src/cdn/ck/isCKCdnVersion.ts | 27 +++++++++++-------- ...SupportedLicenseVersionInstallationInfo.ts | 8 ++++++ tests/cdn/ck/isCKCdnVersion.test.ts | 7 +++-- tests/cdn/loadCKEditorCloud.test.ts | 2 +- ...rtedLicenseVersionInstallationInfo.test.ts | 7 +++++ 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/cdn/ck/isCKCdnVersion.ts b/src/cdn/ck/isCKCdnVersion.ts index 79ea5f4..537723c 100644 --- a/src/cdn/ck/isCKCdnVersion.ts +++ b/src/cdn/ck/isCKCdnVersion.ts @@ -3,15 +3,15 @@ * For licensing, see LICENSE.md. */ -import { isSemanticVersion, type SemanticVersion } from '../../utils/isSemanticVersion.js'; +import { isSemanticVersion, type SemanticVersion } from '../../utils/version/isSemanticVersion.js'; /** - * A version of a file on the CKEditor CDN that is used for testing purposes. + * A version of the CKEditor that is used for testing purposes. */ export type CKCdnTestingVersion = + | 'nightly' | 'alpha' - | `${ SemanticVersion }-nightly-${ string }` - | `rc-${ string }`; + | 'internal'; /** * A version of a file on the CKEditor CDN. @@ -27,14 +27,19 @@ export type CKCdnVersion = * @returns `true` if the string is a version of a file on the CKEditor CDN, `false` otherwise. * @example * ```ts - * isCKCdnVersion( 'nightly' ); // -> true - * isCKCdnVersion( 'alpha' ); // -> true - * isCKCdnVersion( 'rc-1.2.3' ); // -> true - * isCKCdnVersion( '1.2.3' ); // -> false + * isCKCdnTestingVersion( '1.2.3-nightly-abc' ); // -> true + * isCKCdnTestingVersion( '1.2.3-internal-abc' ); // -> true + * isCKCdnTestingVersion( '1.2.3-alpha.1' ); // -> true + * isCKCdnTestingVersion( '1.2.3' ); // -> false + * isCKCdnTestingVersion( 'nightly' ); // -> true * ``` */ -export function isCKCdnTestingVersion( version: string ): version is CKCdnTestingVersion { - return version === 'alpha' || version === 'nightly' || version.startsWith( 'rc-' ); +export function isCKCdnTestingVersion( version: string | undefined ): version is CKCdnTestingVersion { + if ( !version ) { + return false; + } + + return [ 'nightly', 'alpha', 'internal' ].some( testVersion => version.includes( testVersion ) ); } /** @@ -50,6 +55,6 @@ export function isCKCdnTestingVersion( version: string ): version is CKCdnTestin * isCKCdnVersion( '1.2.3' ); // -> true * ``` */ -export function isCKCdnVersion( version: string ): version is CKCdnVersion { +export function isCKCdnVersion( version: string | undefined ): version is CKCdnVersion { return isSemanticVersion( version ) || isCKCdnTestingVersion( version ); } diff --git a/src/installation-info/getSupportedLicenseVersionInstallationInfo.ts b/src/installation-info/getSupportedLicenseVersionInstallationInfo.ts index 9d7482d..e050a94 100644 --- a/src/installation-info/getSupportedLicenseVersionInstallationInfo.ts +++ b/src/installation-info/getSupportedLicenseVersionInstallationInfo.ts @@ -3,6 +3,7 @@ * For licensing, see LICENSE.md. */ +import { isCKCdnTestingVersion } from 'cdn/ck/isCKCdnVersion.js'; import type { LicenseKeyVersion } from '../license/LicenseKey.js'; import { destructureSemanticVersion } from '../utils/version/destructureSemanticVersion.js'; @@ -22,6 +23,13 @@ export function getSupportedLicenseVersionInstallationInfo(): LicenseKeyVersion } const { version } = installationInfo; + + // Assume that the testing version is always the newest one + // so we can return the highest supported license version. + if ( isCKCdnTestingVersion( version ) ) { + return 3; + } + const { major } = destructureSemanticVersion( version ); // License V3 was released in CKEditor 44.0.0. diff --git a/tests/cdn/ck/isCKCdnVersion.test.ts b/tests/cdn/ck/isCKCdnVersion.test.ts index c48194b..29c457b 100644 --- a/tests/cdn/ck/isCKCdnVersion.test.ts +++ b/tests/cdn/ck/isCKCdnVersion.test.ts @@ -9,10 +9,13 @@ import { isCKCdnVersion, isCKCdnTestingVersion } from '@/cdn/ck/isCKCdnVersion.j const testCases = [ { version: 'alpha', isTesting: true, isVersion: true }, { version: 'nightly', isTesting: true, isVersion: true }, - { version: 'rc-1.2.3', isTesting: true, isVersion: true }, + { version: 'rc-1.2.3', isTesting: false, isVersion: false }, { version: '1.2.3', isTesting: false, isVersion: true }, { version: 'beta', isTesting: false, isVersion: false }, - { version: '1.2', isTesting: false, isVersion: false } + { version: '1.2', isTesting: false, isVersion: false }, + { version: '0.0.0-nightly-20241104.0', isTesting: true, isVersion: true }, + { version: '0.0.0-internal-20241104.0', isTesting: true, isVersion: true }, + { version: '43.3.0-alpha.12 ', isTesting: true, isVersion: true } ]; describe( 'isCKCdnTestingVersion', () => { diff --git a/tests/cdn/loadCKEditorCloud.test.ts b/tests/cdn/loadCKEditorCloud.test.ts index bfa9240..04d6f63 100644 --- a/tests/cdn/loadCKEditorCloud.test.ts +++ b/tests/cdn/loadCKEditorCloud.test.ts @@ -36,7 +36,7 @@ describe( 'loadCKEditorCloud', () => { vi.restoreAllMocks(); } ); - for ( const version of [ 'alpha', 'nightly' ] as const ) { + for ( const version of [ 'alpha', 'internal' ] as const ) { it( `should raise warning if ${ version } version is used`, async () => { const { CKEditor } = await loadCKEditorCloud( { version diff --git a/tests/installation-info/getSupportedLicenseVersionInstallationInfo.test.ts b/tests/installation-info/getSupportedLicenseVersionInstallationInfo.test.ts index 158f5a1..4046f26 100644 --- a/tests/installation-info/getSupportedLicenseVersionInstallationInfo.test.ts +++ b/tests/installation-info/getSupportedLicenseVersionInstallationInfo.test.ts @@ -51,6 +51,13 @@ describe( 'getSupportedLicenseVersionInstallationInfo', () => { mockEditorVersion( '37.0.0' ); expect( getSupportedLicenseVersionInstallationInfo() ).toEqual( 1 ); } ); + + it( 'should return license V3 for test versions', () => { + for ( const version of [ 'alpha', 'nightly', 'internal' ] ) { + mockEditorVersion( version ); + expect( getSupportedLicenseVersionInstallationInfo() ).toEqual( 3 ); + } + } ); } ); function mockEditorVersion( version: string ): void { From d3fde1a982fdee4783d1c605202aecedaf123328 Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Mon, 4 Nov 2024 13:26:24 +0100 Subject: [PATCH 5/5] Fix typo --- .../getSupportedLicenseVersionInstallationInfo.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/installation-info/getSupportedLicenseVersionInstallationInfo.ts b/src/installation-info/getSupportedLicenseVersionInstallationInfo.ts index e050a94..f91a2a1 100644 --- a/src/installation-info/getSupportedLicenseVersionInstallationInfo.ts +++ b/src/installation-info/getSupportedLicenseVersionInstallationInfo.ts @@ -3,7 +3,7 @@ * For licensing, see LICENSE.md. */ -import { isCKCdnTestingVersion } from 'cdn/ck/isCKCdnVersion.js'; +import { isCKCdnTestingVersion } from '../cdn/ck/isCKCdnVersion.js'; import type { LicenseKeyVersion } from '../license/LicenseKey.js'; import { destructureSemanticVersion } from '../utils/version/destructureSemanticVersion.js';