@rulecms/widget-angular 0.5.0 → 0.6.0

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.
@@ -3373,12 +3373,144 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImpo
3373
3373
  type: Input
3374
3374
  }] } });
3375
3375
 
3376
+ /**
3377
+ * Missing/undefined on a widget GET payload means include — the default, and
3378
+ * the shape older clients already understand. Only an explicit `false` turns
3379
+ * emission off, even if `classDefinitions` is still present on the payload.
3380
+ */
3381
+ function shouldIncludeClassDefinitionsOnPublishedWidget(includeClassDefinitions) {
3382
+ return includeClassDefinitions !== false;
3383
+ }
3384
+
3385
+ /**
3386
+ * Attribute-selector escape so a published key cannot break out of
3387
+ * `[data-rulecms-published-key="…"]`. `CSS.escape` is not assumed (SSR / Node).
3388
+ */
3389
+ const escapeCssAttributeValue$1 = (value) => value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
3390
+ /** Escape a class name for use after `.` (`/` → `\/`). */
3391
+ const escapeCssClassName = (name) => name.replace(/[^A-Za-z0-9_-]/g, (char) => `\\${char}`);
3392
+ const REJECTED_IN_BODY = ['</', '<script', '@import', 'javascript:', 'vbscript:', 'expression('];
3393
+ // eslint-disable-next-line no-control-regex -- control characters are what this matches
3394
+ const REJECTED_CONTROL_CHARACTERS = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/;
3395
+ const DISALLOWED_IN_NAME = /[\s{};"]/;
3396
+ const ALLOWED_NAME = /^[A-Za-z0-9_:%./[\]%-]+$/;
3397
+ const isBraceBalanced = (value) => {
3398
+ let depth = 0;
3399
+ for (const char of value) {
3400
+ if (char === '{') {
3401
+ depth += 1;
3402
+ }
3403
+ else if (char === '}') {
3404
+ depth -= 1;
3405
+ if (depth < 0) {
3406
+ return false;
3407
+ }
3408
+ }
3409
+ }
3410
+ return depth === 0;
3411
+ };
3412
+ const isCssClassName = (value) => {
3413
+ const name = value.startsWith('.') ? value.slice(1) : value;
3414
+ if (!name || name.length > 200 || name.startsWith('.') || DISALLOWED_IN_NAME.test(name)) {
3415
+ return false;
3416
+ }
3417
+ return ALLOWED_NAME.test(name);
3418
+ };
3419
+ const isCssClassDeclarationBody = (value) => {
3420
+ const trimmed = value.trim();
3421
+ if (!trimmed || trimmed.length > 4000) {
3422
+ return false;
3423
+ }
3424
+ if (REJECTED_CONTROL_CHARACTERS.test(trimmed) || !isBraceBalanced(trimmed)) {
3425
+ return false;
3426
+ }
3427
+ const lowered = trimmed.toLowerCase();
3428
+ return !REJECTED_IN_BODY.some((rejected) => lowered.includes(rejected));
3429
+ };
3430
+ const sanitizeForStylesheet$1 = (classes) => {
3431
+ if (!classes) {
3432
+ return {};
3433
+ }
3434
+ const sanitized = {};
3435
+ for (const [rawName, rawValue] of Object.entries(classes)) {
3436
+ const name = rawName.startsWith('.') ? rawName.slice(1) : rawName.trim();
3437
+ if (!isCssClassName(name) || typeof rawValue !== 'string') {
3438
+ continue;
3439
+ }
3440
+ const value = rawValue.trim();
3441
+ if (!isCssClassDeclarationBody(value)) {
3442
+ continue;
3443
+ }
3444
+ sanitized[name] = value;
3445
+ }
3446
+ return sanitized;
3447
+ };
3448
+ const indentDeclarationBody = (body) => body
3449
+ .split('\n')
3450
+ .map((line) => ` ${line}`)
3451
+ .join('\n');
3452
+ /**
3453
+ * Descendant class rules under this widget’s published-key span.
3454
+ * Empty / flag-off / unsanitary catalogs return null so the caller omits `<style>`.
3455
+ */
3456
+ const formatProjectCssClassesStylesheet = (publishedKey, classes, includeClassDefinitions) => {
3457
+ if (!shouldIncludeClassDefinitionsOnPublishedWidget(includeClassDefinitions)) {
3458
+ return null;
3459
+ }
3460
+ const sanitized = sanitizeForStylesheet$1(classes);
3461
+ const names = Object.keys(sanitized);
3462
+ if (names.length === 0 || !publishedKey) {
3463
+ return null;
3464
+ }
3465
+ const ancestor = `[data-rulecms-published-key="${escapeCssAttributeValue$1(publishedKey)}"]`;
3466
+ return names
3467
+ .sort((left, right) => left.localeCompare(right))
3468
+ .map((name) => `${ancestor} .${escapeCssClassName(name)} {\n${indentDeclarationBody(sanitized[name])}\n}`)
3469
+ .join('\n');
3470
+ };
3471
+
3472
+ /**
3473
+ * Emits one scoped `<style data-rulecms-project-class-definitions>` from the
3474
+ * project catalog, or nothing when the catalog is empty / unsanitary.
3475
+ */
3476
+ class ProjectCssClassesStylesheetComponent {
3477
+ get css() {
3478
+ return formatProjectCssClassesStylesheet(this.publishedKey, this.classes);
3479
+ }
3480
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: ProjectCssClassesStylesheetComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
3481
+ static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "19.2.25", type: ProjectCssClassesStylesheetComponent, isStandalone: true, selector: "rulecms-project-css-classes-stylesheet", inputs: { publishedKey: "publishedKey", classes: "classes" }, ngImport: i0, template: `
3482
+ @if (css) {
3483
+ <style data-rulecms-project-class-definitions="" [innerText]="css"></style>
3484
+ }
3485
+ `, isInline: true }); }
3486
+ }
3487
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: ProjectCssClassesStylesheetComponent, decorators: [{
3488
+ type: Component,
3489
+ args: [{
3490
+ selector: 'rulecms-project-css-classes-stylesheet',
3491
+ standalone: true,
3492
+ template: `
3493
+ @if (css) {
3494
+ <style data-rulecms-project-class-definitions="" [innerText]="css"></style>
3495
+ }
3496
+ `,
3497
+ }]
3498
+ }], propDecorators: { publishedKey: [{
3499
+ type: Input,
3500
+ args: [{ required: true }]
3501
+ }], classes: [{
3502
+ type: Input
3503
+ }] } });
3504
+
3376
3505
  /**
3377
3506
  * When true, a non-empty `cssVariables` object on the widget GET payload is
3378
3507
  * emitted as one scoped `<style>` on the published widget.
3379
3508
  *
3380
3509
  * TODO: project setting — the customer turns this off if the host stylesheet
3381
3510
  * already defines these tokens. Hardcoded on for this iteration.
3511
+ *
3512
+ * Class definitions (`classDefinitions` / `includeClassDefinitions` on the
3513
+ * same GET) are a separate catalog; see `project-css-classes/`.
3382
3514
  */
3383
3515
  const INCLUDE_PROJECT_CSS_VARIABLES_ON_PUBLISHED_WIDGET = true;
3384
3516
 
@@ -3498,6 +3630,11 @@ function prepareRuleCMSWidgetData(raw) {
3498
3630
  ...(raw.cssVariables && Object.keys(raw.cssVariables).length > 0
3499
3631
  ? { cssVariables: raw.cssVariables }
3500
3632
  : {}),
3633
+ ...(shouldIncludeClassDefinitionsOnPublishedWidget(raw.includeClassDefinitions) &&
3634
+ raw.classDefinitions &&
3635
+ Object.keys(raw.classDefinitions).length > 0
3636
+ ? { classDefinitions: raw.classDefinitions }
3637
+ : {}),
3501
3638
  };
3502
3639
  }
3503
3640
 
@@ -3742,6 +3879,10 @@ class RuleCMSWidgetComponent {
3742
3879
  [publishedKey]="publishedKey"
3743
3880
  [variables]="preparedData?.cssVariables"
3744
3881
  />
3882
+ <rulecms-project-css-classes-stylesheet
3883
+ [publishedKey]="publishedKey"
3884
+ [classes]="preparedData?.classDefinitions"
3885
+ />
3745
3886
  @if (mode !== 'pre-fetched' && loading) {
3746
3887
  <div [ngStyle]="loadingContainerStyle">
3747
3888
  <div [ngStyle]="spinnerContainerStyle">
@@ -3782,7 +3923,7 @@ class RuleCMSWidgetComponent {
3782
3923
  }
3783
3924
  }
3784
3925
  </span>
3785
- `, isInline: true, dependencies: [{ kind: "directive", type: NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }, { kind: "component", type: BreakpointVariantRenderComponent, selector: "rulecms-breakpoint-variants", inputs: ["displayItemList", "widgetCollections", "plugins", "componentProps", "mounts", "scopeIdInput"] }, { kind: "component", type: RenderComponentsComponent, selector: "rulecms-render-components", inputs: ["displayItemList", "isComposeMode", "isWithinCollection", "widgetCollections", "showDropContainers", "reduxDeviceDisplayType", "plugins", "componentProps", "mounts"] }, { kind: "component", type: ProjectCssVariablesStylesheetComponent, selector: "rulecms-project-css-variables-stylesheet", inputs: ["publishedKey", "variables"] }] }); }
3926
+ `, isInline: true, dependencies: [{ kind: "directive", type: NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }, { kind: "component", type: BreakpointVariantRenderComponent, selector: "rulecms-breakpoint-variants", inputs: ["displayItemList", "widgetCollections", "plugins", "componentProps", "mounts", "scopeIdInput"] }, { kind: "component", type: RenderComponentsComponent, selector: "rulecms-render-components", inputs: ["displayItemList", "isComposeMode", "isWithinCollection", "widgetCollections", "showDropContainers", "reduxDeviceDisplayType", "plugins", "componentProps", "mounts"] }, { kind: "component", type: ProjectCssVariablesStylesheetComponent, selector: "rulecms-project-css-variables-stylesheet", inputs: ["publishedKey", "variables"] }, { kind: "component", type: ProjectCssClassesStylesheetComponent, selector: "rulecms-project-css-classes-stylesheet", inputs: ["publishedKey", "classes"] }] }); }
3786
3927
  }
3787
3928
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: RuleCMSWidgetComponent, decorators: [{
3788
3929
  type: Component,
@@ -3794,6 +3935,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImpo
3794
3935
  BreakpointVariantRenderComponent,
3795
3936
  RenderComponentsComponent,
3796
3937
  ProjectCssVariablesStylesheetComponent,
3938
+ ProjectCssClassesStylesheetComponent,
3797
3939
  ],
3798
3940
  template: `
3799
3941
  <span [attr.data-rulecms-published-key]="publishedKey" style="display: contents">
@@ -3801,6 +3943,10 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImpo
3801
3943
  [publishedKey]="publishedKey"
3802
3944
  [variables]="preparedData?.cssVariables"
3803
3945
  />
3946
+ <rulecms-project-css-classes-stylesheet
3947
+ [publishedKey]="publishedKey"
3948
+ [classes]="preparedData?.classDefinitions"
3949
+ />
3804
3950
  @if (mode !== 'pre-fetched' && loading) {
3805
3951
  <div [ngStyle]="loadingContainerStyle">
3806
3952
  <div [ngStyle]="spinnerContainerStyle">