@gravity-ui/page-constructor 1.16.3 → 1.16.4

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.16.4](https://github.com/gravity-ui/page-constructor/compare/v1.16.3...v1.16.4) (2023-02-22)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * **BasicCard:** add target ([#162](https://github.com/gravity-ui/page-constructor/issues/162)) ([569038c](https://github.com/gravity-ui/page-constructor/commit/569038c8ef1c43b7c8b9d6462126997155605b4c))
9
+ * do not set "rel: 'noopener noreferrer'" for local links ([#160](https://github.com/gravity-ui/page-constructor/issues/160)) ([81173a7](https://github.com/gravity-ui/page-constructor/commit/81173a7e237104463971a80e386f5bad37caa551))
10
+
3
11
  ## [1.16.3](https://github.com/gravity-ui/page-constructor/compare/v1.16.2...v1.16.3) (2023-02-17)
4
12
 
5
13
 
@@ -106,6 +106,7 @@ export interface BackgroundCardProps extends CardBaseProps, Omit<ContentBlockPro
106
106
  export interface BasicCardProps extends CardBaseProps, Omit<ContentBlockProps, 'colSizes' | 'centered' | 'size' | 'theme'> {
107
107
  url: string;
108
108
  icon?: ImageProps;
109
+ target?: string;
109
110
  }
110
111
  export interface BannerCardProps {
111
112
  title: string;
@@ -9,9 +9,9 @@ const __1 = require("../");
9
9
  const utils_2 = require("../../components/Media/Image/utils");
10
10
  const b = (0, utils_1.block)('basic-card');
11
11
  const BasicCard = (props) => {
12
- const { url, title, text, border, icon, additionalInfo, links, buttons } = props;
12
+ const { title, text, icon, additionalInfo, links, buttons } = props, cardParams = tslib_1.__rest(props, ["title", "text", "icon", "additionalInfo", "links", "buttons"]);
13
13
  const iconProps = icon && (0, utils_2.getMediaImage)(icon);
14
- return (react_1.default.createElement(CardBase_1.default, { className: b(), url: url, border: border },
14
+ return (react_1.default.createElement(CardBase_1.default, Object.assign({ className: b() }, cardParams),
15
15
  react_1.default.createElement(CardBase_1.default.Content, null,
16
16
  iconProps && react_1.default.createElement(Image_1.default, Object.assign({}, iconProps, { className: b('icon') })),
17
17
  react_1.default.createElement(__1.Content, { title: title, text: text, additionalInfo: additionalInfo, links: links, buttons: buttons, colSizes: { all: 12, md: 12 }, size: "s" }))));
@@ -19,6 +19,10 @@ export declare const BasicCard: {
19
19
  pattern: string;
20
20
  })[];
21
21
  };
22
+ target: {
23
+ type: string;
24
+ enum: string[];
25
+ };
22
26
  title: {
23
27
  oneOf: ({
24
28
  type: string;
@@ -13,6 +13,9 @@ exports.BasicCard = {
13
13
  required: ['url'],
14
14
  properties: Object.assign(Object.assign(Object.assign(Object.assign({}, common_1.BaseProps), common_1.CardBase), BasicCardContentProps), { url: {
15
15
  type: 'string',
16
- }, icon: schema_2.ImageProps }),
16
+ }, icon: schema_2.ImageProps, target: {
17
+ type: 'string',
18
+ enum: ['_blank', '_parent', '_top', '_self'],
19
+ } }),
17
20
  },
18
21
  };
@@ -6,6 +6,7 @@ export declare const EXTERNAL_LINK_PROPS: {
6
6
  export declare function getLinkProps(url: string, hostname?: string, target?: string): {
7
7
  target: string | undefined;
8
8
  };
9
+ export declare function isAbsoluteUrl(url: string | URL): boolean;
9
10
  export declare function isLinkExternal(url: string, routerHostname?: string): boolean;
10
11
  export declare function getNonLocaleHostName(hostname: string): string;
11
12
  export declare function setUrlTld(url: string, tld?: string): string;
@@ -1,25 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getAbsolutePath = exports.getPageSearchParams = exports.setUrlTld = exports.getNonLocaleHostName = exports.isLinkExternal = exports.getLinkProps = exports.EXTERNAL_LINK_PROPS = void 0;
3
+ exports.getAbsolutePath = exports.getPageSearchParams = exports.setUrlTld = exports.getNonLocaleHostName = exports.isLinkExternal = exports.isAbsoluteUrl = exports.getLinkProps = exports.EXTERNAL_LINK_PROPS = void 0;
4
4
  const url_1 = require("url");
5
+ const EXAMPLE_URL = 'https://example.org';
5
6
  exports.EXTERNAL_LINK_PROPS = { target: '_blank', rel: 'noopener noreferrer' };
6
7
  function getLinkProps(url, hostname, target) {
7
8
  let linkProps = { target };
8
- if (target === '_blank' || isLinkExternal(url, hostname)) {
9
+ if (isLinkExternal(url, hostname)) {
9
10
  linkProps = Object.assign(Object.assign({}, linkProps), exports.EXTERNAL_LINK_PROPS);
10
11
  }
11
12
  return linkProps;
12
13
  }
13
14
  exports.getLinkProps = getLinkProps;
15
+ function isAbsoluteUrl(url) {
16
+ // Using example URL as base for relative links
17
+ const urlObj = new URL(url, EXAMPLE_URL);
18
+ return (
19
+ // Compare url origin with example and check that original url was not example one
20
+ urlObj.origin !== EXAMPLE_URL || (typeof url === 'string' && url.startsWith(EXAMPLE_URL)));
21
+ }
22
+ exports.isAbsoluteUrl = isAbsoluteUrl;
14
23
  function isLinkExternal(url, routerHostname) {
15
- if (!routerHostname) {
16
- return true;
17
- }
18
- const { hostname } = (0, url_1.parse)(url);
19
- if (!hostname) {
20
- return false;
21
- }
22
- return getNonLocaleHostName(hostname) !== getNonLocaleHostName(routerHostname);
24
+ return (isAbsoluteUrl(url) &&
25
+ getNonLocaleHostName(new URL(url).hostname) !== getNonLocaleHostName(routerHostname !== null && routerHostname !== void 0 ? routerHostname : ''));
23
26
  }
24
27
  exports.isLinkExternal = isLinkExternal;
25
28
  function getNonLocaleHostName(hostname) {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const url_1 = require("./url");
4
+ describe('URL utils check', () => {
5
+ test.each([
6
+ ['https://user:pass@sub.example.com:8080/p/a/t/h?query=string&query2=1#hash', true],
7
+ ['http://example.net/path', true],
8
+ ['/p/a/t/h?query=string&query2=1#hash', false],
9
+ ['/path', false],
10
+ ['path', false],
11
+ ])("isAbsoluteUrl('%s') should return '%s'", (url, result) => {
12
+ expect((0, url_1.isAbsoluteUrl)(url)).toEqual(result);
13
+ });
14
+ test.each([
15
+ [
16
+ 'https://user:pass@sub.example.com:8080/p/a/t/h?query=string&query2=1#hash',
17
+ 'example.net',
18
+ true,
19
+ ],
20
+ [
21
+ 'https://user:pass@sub.example.com:8080/p/a/t/h?query=string&query2=1#hash',
22
+ 'sub.example.com',
23
+ false,
24
+ ],
25
+ [
26
+ 'https://user:pass@sub.example.com:8080/p/a/t/h?query=string&query2=1#hash',
27
+ undefined,
28
+ true,
29
+ ],
30
+ ['http://example.net/path', 'example.net', false],
31
+ ['http://example.net/path', 'sub.example.com', true],
32
+ ['http://example.net/path', undefined, true],
33
+ ['/p/a/t/h?query=string&query2=1#hash', 'example.net', false],
34
+ ['/p/a/t/h?query=string&query2=1#hash', undefined, false],
35
+ ['/path', 'example.net', false],
36
+ ['/path', undefined, false],
37
+ ['path', 'example.net', false],
38
+ ['path', undefined, false],
39
+ ])("isLinkExternal('%s', '%s') should return '%s'", (url, hostname, result) => {
40
+ expect((0, url_1.isLinkExternal)(url, hostname)).toEqual(result);
41
+ });
42
+ test.each([
43
+ ['http://example.net/path', 'example.net', '_blank', { target: '_blank' }],
44
+ ['http://example.net/path', 'example.net', undefined, {}],
45
+ [
46
+ 'http://example.net/path',
47
+ 'example.com',
48
+ '_blank',
49
+ { target: '_blank', rel: 'noopener noreferrer' },
50
+ ],
51
+ [
52
+ 'http://example.net/path',
53
+ 'example.com',
54
+ undefined,
55
+ { target: '_blank', rel: 'noopener noreferrer' },
56
+ ],
57
+ [
58
+ 'http://example.net/path',
59
+ undefined,
60
+ '_blank',
61
+ { target: '_blank', rel: 'noopener noreferrer' },
62
+ ],
63
+ [
64
+ 'http://example.net/path',
65
+ undefined,
66
+ undefined,
67
+ { target: '_blank', rel: 'noopener noreferrer' },
68
+ ],
69
+ ['/path', 'example.net', '_blank', { target: '_blank' }],
70
+ ['/path', 'example.net', undefined, {}],
71
+ ['/path', undefined, '_blank', { target: '_blank' }],
72
+ ['/path', undefined, undefined, {}],
73
+ ])("getLinkProps('%s', '%s', '%s') should return '%s'", (url, hostname, target, result) => {
74
+ expect((0, url_1.getLinkProps)(url, hostname, target)).toEqual(result);
75
+ });
76
+ });
@@ -106,6 +106,7 @@ export interface BackgroundCardProps extends CardBaseProps, Omit<ContentBlockPro
106
106
  export interface BasicCardProps extends CardBaseProps, Omit<ContentBlockProps, 'colSizes' | 'centered' | 'size' | 'theme'> {
107
107
  url: string;
108
108
  icon?: ImageProps;
109
+ target?: string;
109
110
  }
110
111
  export interface BannerCardProps {
111
112
  title: string;
@@ -1,3 +1,4 @@
1
+ import { __rest } from "tslib";
1
2
  import React from 'react';
2
3
  import { block } from '../../utils';
3
4
  import CardBase from '../../components/CardBase/CardBase';
@@ -7,9 +8,9 @@ import { getMediaImage } from '../../components/Media/Image/utils';
7
8
  import './BasicCard.css';
8
9
  const b = block('basic-card');
9
10
  const BasicCard = (props) => {
10
- const { url, title, text, border, icon, additionalInfo, links, buttons } = props;
11
+ const { title, text, icon, additionalInfo, links, buttons } = props, cardParams = __rest(props, ["title", "text", "icon", "additionalInfo", "links", "buttons"]);
11
12
  const iconProps = icon && getMediaImage(icon);
12
- return (React.createElement(CardBase, { className: b(), url: url, border: border },
13
+ return (React.createElement(CardBase, Object.assign({ className: b() }, cardParams),
13
14
  React.createElement(CardBase.Content, null,
14
15
  iconProps && React.createElement(Image, Object.assign({}, iconProps, { className: b('icon') })),
15
16
  React.createElement(Content, { title: title, text: text, additionalInfo: additionalInfo, links: links, buttons: buttons, colSizes: { all: 12, md: 12 }, size: "s" }))));
@@ -19,6 +19,10 @@ export declare const BasicCard: {
19
19
  pattern: string;
20
20
  })[];
21
21
  };
22
+ target: {
23
+ type: string;
24
+ enum: string[];
25
+ };
22
26
  title: {
23
27
  oneOf: ({
24
28
  type: string;
@@ -9,6 +9,9 @@ export const BasicCard = {
9
9
  required: ['url'],
10
10
  properties: Object.assign(Object.assign(Object.assign(Object.assign({}, BaseProps), CardBase), BasicCardContentProps), { url: {
11
11
  type: 'string',
12
- }, icon: ImageProps }),
12
+ }, icon: ImageProps, target: {
13
+ type: 'string',
14
+ enum: ['_blank', '_parent', '_top', '_self'],
15
+ } }),
13
16
  },
14
17
  };
@@ -6,6 +6,7 @@ export declare const EXTERNAL_LINK_PROPS: {
6
6
  export declare function getLinkProps(url: string, hostname?: string, target?: string): {
7
7
  target: string | undefined;
8
8
  };
9
+ export declare function isAbsoluteUrl(url: string | URL): boolean;
9
10
  export declare function isLinkExternal(url: string, routerHostname?: string): boolean;
10
11
  export declare function getNonLocaleHostName(hostname: string): string;
11
12
  export declare function setUrlTld(url: string, tld?: string): string;
@@ -1,21 +1,23 @@
1
1
  import { parse, format } from 'url';
2
+ const EXAMPLE_URL = 'https://example.org';
2
3
  export const EXTERNAL_LINK_PROPS = { target: '_blank', rel: 'noopener noreferrer' };
3
4
  export function getLinkProps(url, hostname, target) {
4
5
  let linkProps = { target };
5
- if (target === '_blank' || isLinkExternal(url, hostname)) {
6
+ if (isLinkExternal(url, hostname)) {
6
7
  linkProps = Object.assign(Object.assign({}, linkProps), EXTERNAL_LINK_PROPS);
7
8
  }
8
9
  return linkProps;
9
10
  }
11
+ export function isAbsoluteUrl(url) {
12
+ // Using example URL as base for relative links
13
+ const urlObj = new URL(url, EXAMPLE_URL);
14
+ return (
15
+ // Compare url origin with example and check that original url was not example one
16
+ urlObj.origin !== EXAMPLE_URL || (typeof url === 'string' && url.startsWith(EXAMPLE_URL)));
17
+ }
10
18
  export function isLinkExternal(url, routerHostname) {
11
- if (!routerHostname) {
12
- return true;
13
- }
14
- const { hostname } = parse(url);
15
- if (!hostname) {
16
- return false;
17
- }
18
- return getNonLocaleHostName(hostname) !== getNonLocaleHostName(routerHostname);
19
+ return (isAbsoluteUrl(url) &&
20
+ getNonLocaleHostName(new URL(url).hostname) !== getNonLocaleHostName(routerHostname !== null && routerHostname !== void 0 ? routerHostname : ''));
19
21
  }
20
22
  export function getNonLocaleHostName(hostname) {
21
23
  return hostname.replace(/\.(ru|com)$/, '');
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,74 @@
1
+ import { getLinkProps, isAbsoluteUrl, isLinkExternal } from './url';
2
+ describe('URL utils check', () => {
3
+ test.each([
4
+ ['https://user:pass@sub.example.com:8080/p/a/t/h?query=string&query2=1#hash', true],
5
+ ['http://example.net/path', true],
6
+ ['/p/a/t/h?query=string&query2=1#hash', false],
7
+ ['/path', false],
8
+ ['path', false],
9
+ ])("isAbsoluteUrl('%s') should return '%s'", (url, result) => {
10
+ expect(isAbsoluteUrl(url)).toEqual(result);
11
+ });
12
+ test.each([
13
+ [
14
+ 'https://user:pass@sub.example.com:8080/p/a/t/h?query=string&query2=1#hash',
15
+ 'example.net',
16
+ true,
17
+ ],
18
+ [
19
+ 'https://user:pass@sub.example.com:8080/p/a/t/h?query=string&query2=1#hash',
20
+ 'sub.example.com',
21
+ false,
22
+ ],
23
+ [
24
+ 'https://user:pass@sub.example.com:8080/p/a/t/h?query=string&query2=1#hash',
25
+ undefined,
26
+ true,
27
+ ],
28
+ ['http://example.net/path', 'example.net', false],
29
+ ['http://example.net/path', 'sub.example.com', true],
30
+ ['http://example.net/path', undefined, true],
31
+ ['/p/a/t/h?query=string&query2=1#hash', 'example.net', false],
32
+ ['/p/a/t/h?query=string&query2=1#hash', undefined, false],
33
+ ['/path', 'example.net', false],
34
+ ['/path', undefined, false],
35
+ ['path', 'example.net', false],
36
+ ['path', undefined, false],
37
+ ])("isLinkExternal('%s', '%s') should return '%s'", (url, hostname, result) => {
38
+ expect(isLinkExternal(url, hostname)).toEqual(result);
39
+ });
40
+ test.each([
41
+ ['http://example.net/path', 'example.net', '_blank', { target: '_blank' }],
42
+ ['http://example.net/path', 'example.net', undefined, {}],
43
+ [
44
+ 'http://example.net/path',
45
+ 'example.com',
46
+ '_blank',
47
+ { target: '_blank', rel: 'noopener noreferrer' },
48
+ ],
49
+ [
50
+ 'http://example.net/path',
51
+ 'example.com',
52
+ undefined,
53
+ { target: '_blank', rel: 'noopener noreferrer' },
54
+ ],
55
+ [
56
+ 'http://example.net/path',
57
+ undefined,
58
+ '_blank',
59
+ { target: '_blank', rel: 'noopener noreferrer' },
60
+ ],
61
+ [
62
+ 'http://example.net/path',
63
+ undefined,
64
+ undefined,
65
+ { target: '_blank', rel: 'noopener noreferrer' },
66
+ ],
67
+ ['/path', 'example.net', '_blank', { target: '_blank' }],
68
+ ['/path', 'example.net', undefined, {}],
69
+ ['/path', undefined, '_blank', { target: '_blank' }],
70
+ ['/path', undefined, undefined, {}],
71
+ ])("getLinkProps('%s', '%s', '%s') should return '%s'", (url, hostname, target, result) => {
72
+ expect(getLinkProps(url, hostname, target)).toEqual(result);
73
+ });
74
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/page-constructor",
3
- "version": "1.16.3",
3
+ "version": "1.16.4",
4
4
  "description": "Gravity UI Page Constructor",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -106,6 +106,7 @@ export interface BackgroundCardProps extends CardBaseProps, Omit<ContentBlockPro
106
106
  export interface BasicCardProps extends CardBaseProps, Omit<ContentBlockProps, 'colSizes' | 'centered' | 'size' | 'theme'> {
107
107
  url: string;
108
108
  icon?: ImageProps;
109
+ target?: string;
109
110
  }
110
111
  export interface BannerCardProps {
111
112
  title: string;
@@ -6,6 +6,7 @@ export declare const EXTERNAL_LINK_PROPS: {
6
6
  export declare function getLinkProps(url: string, hostname?: string, target?: string): {
7
7
  target: string | undefined;
8
8
  };
9
+ export declare function isAbsoluteUrl(url: string | URL): boolean;
9
10
  export declare function isLinkExternal(url: string, routerHostname?: string): boolean;
10
11
  export declare function getNonLocaleHostName(hostname: string): string;
11
12
  export declare function setUrlTld(url: string, tld?: string): string;
@@ -1,25 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getAbsolutePath = exports.getPageSearchParams = exports.setUrlTld = exports.getNonLocaleHostName = exports.isLinkExternal = exports.getLinkProps = exports.EXTERNAL_LINK_PROPS = void 0;
3
+ exports.getAbsolutePath = exports.getPageSearchParams = exports.setUrlTld = exports.getNonLocaleHostName = exports.isLinkExternal = exports.isAbsoluteUrl = exports.getLinkProps = exports.EXTERNAL_LINK_PROPS = void 0;
4
4
  const url_1 = require("url");
5
+ const EXAMPLE_URL = 'https://example.org';
5
6
  exports.EXTERNAL_LINK_PROPS = { target: '_blank', rel: 'noopener noreferrer' };
6
7
  function getLinkProps(url, hostname, target) {
7
8
  let linkProps = { target };
8
- if (target === '_blank' || isLinkExternal(url, hostname)) {
9
+ if (isLinkExternal(url, hostname)) {
9
10
  linkProps = Object.assign(Object.assign({}, linkProps), exports.EXTERNAL_LINK_PROPS);
10
11
  }
11
12
  return linkProps;
12
13
  }
13
14
  exports.getLinkProps = getLinkProps;
15
+ function isAbsoluteUrl(url) {
16
+ // Using example URL as base for relative links
17
+ const urlObj = new URL(url, EXAMPLE_URL);
18
+ return (
19
+ // Compare url origin with example and check that original url was not example one
20
+ urlObj.origin !== EXAMPLE_URL || (typeof url === 'string' && url.startsWith(EXAMPLE_URL)));
21
+ }
22
+ exports.isAbsoluteUrl = isAbsoluteUrl;
14
23
  function isLinkExternal(url, routerHostname) {
15
- if (!routerHostname) {
16
- return true;
17
- }
18
- const { hostname } = (0, url_1.parse)(url);
19
- if (!hostname) {
20
- return false;
21
- }
22
- return getNonLocaleHostName(hostname) !== getNonLocaleHostName(routerHostname);
24
+ return (isAbsoluteUrl(url) &&
25
+ getNonLocaleHostName(new URL(url).hostname) !== getNonLocaleHostName(routerHostname !== null && routerHostname !== void 0 ? routerHostname : ''));
23
26
  }
24
27
  exports.isLinkExternal = isLinkExternal;
25
28
  function getNonLocaleHostName(hostname) {