@atlaskit/code 14.1.5 → 14.2.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.
Files changed (40) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/bidi-warning/package.json +7 -0
  3. package/bidi-warning-decorator/package.json +7 -0
  4. package/dist/cjs/bidi-warning/bidi-warning-decorator.js +73 -0
  5. package/dist/cjs/bidi-warning/index.js +15 -0
  6. package/dist/cjs/bidi-warning/ui/index.js +46 -0
  7. package/dist/cjs/bidi-warning/ui/styled.js +74 -0
  8. package/dist/cjs/bidi-warning/ui/types.js +5 -0
  9. package/dist/cjs/code-block.js +9 -2
  10. package/dist/cjs/code.js +59 -2
  11. package/dist/cjs/react-syntax-highlighter-bidi-warning-renderer.js +185 -0
  12. package/dist/cjs/version.json +1 -1
  13. package/dist/es2019/bidi-warning/bidi-warning-decorator.js +42 -0
  14. package/dist/es2019/bidi-warning/index.js +1 -0
  15. package/dist/es2019/bidi-warning/ui/index.js +32 -0
  16. package/dist/es2019/bidi-warning/ui/styled.js +65 -0
  17. package/dist/es2019/bidi-warning/ui/types.js +1 -0
  18. package/dist/es2019/code-block.js +7 -2
  19. package/dist/es2019/code.js +49 -2
  20. package/dist/es2019/react-syntax-highlighter-bidi-warning-renderer.js +155 -0
  21. package/dist/es2019/version.json +1 -1
  22. package/dist/esm/bidi-warning/bidi-warning-decorator.js +61 -0
  23. package/dist/esm/bidi-warning/index.js +1 -0
  24. package/dist/esm/bidi-warning/ui/index.js +32 -0
  25. package/dist/esm/bidi-warning/ui/styled.js +64 -0
  26. package/dist/esm/bidi-warning/ui/types.js +1 -0
  27. package/dist/esm/code-block.js +8 -2
  28. package/dist/esm/code.js +50 -2
  29. package/dist/esm/react-syntax-highlighter-bidi-warning-renderer.js +167 -0
  30. package/dist/esm/version.json +1 -1
  31. package/dist/types/bidi-warning/bidi-warning-decorator.d.ts +5 -0
  32. package/dist/types/bidi-warning/index.d.ts +1 -0
  33. package/dist/types/bidi-warning/ui/index.d.ts +2 -0
  34. package/dist/types/bidi-warning/ui/styled.d.ts +7 -0
  35. package/dist/types/bidi-warning/ui/types.d.ts +21 -0
  36. package/dist/types/code.d.ts +3 -2
  37. package/dist/types/internal/types.d.ts +11 -0
  38. package/dist/types/react-syntax-highlighter-bidi-warning-renderer.d.ts +4 -0
  39. package/dist/types/types.d.ts +11 -0
  40. package/package.json +5 -2
@@ -0,0 +1,65 @@
1
+ /** @jsx jsx */
2
+ import { css, jsx } from '@emotion/core';
3
+ import { Y75 } from '@atlaskit/theme/colors';
4
+ const decoration = css({
5
+ // Required as otherwise the following bidi characters cause the span
6
+ // to not receive hover events.
7
+ //
8
+ // U+2066 LEFT-TO-RIGHT ISOLATE (when using pseudo element before)
9
+ // U+202E RIGHT-TO-LEFT OVERRIDE' (when using pseudo element after)
10
+ position: 'relative',
11
+ ':before': {
12
+ /* layout */
13
+ display: 'inline-flex',
14
+ flexDirection: 'row',
15
+ justifyContent: 'center',
16
+ alignItems: 'center',
17
+ padding: '0 4px',
18
+ fontSize: '14px',
19
+ lineHeight: '18px',
20
+ background: Y75,
21
+
22
+ /**
23
+ * Ensures the decoration receives pointer events when it occurs with
24
+ * an ancestor that disables them.
25
+ */
26
+ pointerEvents: 'auto',
27
+
28
+ /* contents */
29
+ content: '"<"attr(data-bidi-character-code)">"',
30
+ // This color is Y800 which is not yet rolled out
31
+ // https://hello.atlassian.net/wiki/spaces/~tswan/pages/1366555782?focusedCommentId=1370387374#comment-1370387374
32
+ color: '#7F5F01',
33
+ fontStyle: 'normal'
34
+ },
35
+ ':hover:before': {
36
+ color: '#533F04'
37
+ }
38
+ });
39
+ export function Decorator({
40
+ bidiCharacter,
41
+ children,
42
+ testId
43
+ }) {
44
+ const bidiCharacterCode = getBidiCharacterCode(bidiCharacter);
45
+ return jsx("span", {
46
+ "aria-label": bidiCharacterCode
47
+ }, jsx("span", {
48
+ css: decoration,
49
+ "data-testid": testId,
50
+ "data-bidi-character-code": bidiCharacterCode // This is set to true so that the content is not read out by
51
+ // screen readers as the content includes angle brackets for
52
+ // visual decoration purposes.
53
+ // We use a span with the aria-label set to the bidi character code
54
+ // above this span for screen readers.
55
+ ,
56
+ "aria-hidden": "true"
57
+ }, children));
58
+ }
59
+
60
+ function getBidiCharacterCode(bidiCharacter) {
61
+ var _bidiCharacter$codePo;
62
+
63
+ const bidiCharacterCode = (_bidiCharacter$codePo = bidiCharacter.codePointAt(0)) === null || _bidiCharacter$codePo === void 0 ? void 0 : _bidiCharacter$codePo.toString(16);
64
+ return `U+${bidiCharacterCode}`;
65
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -6,6 +6,7 @@ import { useGlobalTheme } from '@atlaskit/theme/components';
6
6
  import { useHighlightLines } from './internal/hooks/use-highlight';
7
7
  import { getCodeBlockStyles, getCodeBlockTheme } from './internal/theme/styles';
8
8
  import { normalizeLanguage } from './internal/utils/get-normalized-language';
9
+ import { createBidiWarningRenderer } from './react-syntax-highlighter-bidi-warning-renderer';
9
10
  const CodeBlock = /*#__PURE__*/memo(function CodeBlock({
10
11
  showLineNumbers = true,
11
12
  language: providedLanguage = 'text',
@@ -13,7 +14,9 @@ const CodeBlock = /*#__PURE__*/memo(function CodeBlock({
13
14
  highlightedStartText = 'Highlight start',
14
15
  highlightedEndText = 'Highlight end',
15
16
  testId,
16
- text
17
+ text,
18
+ codeBidiWarnings = true,
19
+ codeBidiWarningLabel
17
20
  }) {
18
21
  const numLines = (text || '').split('\n').length;
19
22
  const globalTheme = useGlobalTheme();
@@ -31,6 +34,7 @@ const CodeBlock = /*#__PURE__*/memo(function CodeBlock({
31
34
  const language = useMemo(() => normalizeLanguage(providedLanguage), [providedLanguage]); // https://product-fabric.atlassian.net/browse/DST-2472
32
35
 
33
36
  const languageToUse = text ? language : 'text';
37
+ const renderer = codeBidiWarnings ? createBidiWarningRenderer(codeBidiWarningLabel) : undefined;
34
38
  return jsx(SyntaxHighlighter, {
35
39
  "data-testid": testId,
36
40
  "data-code-lang": language,
@@ -42,7 +46,8 @@ const CodeBlock = /*#__PURE__*/memo(function CodeBlock({
42
46
  ,
43
47
  wrapLines: highlight.length > 0 || !!testId,
44
48
  lineProps: getLineProps,
45
- useInlineStyles: false
49
+ useInlineStyles: false,
50
+ renderer: renderer
46
51
  }, text);
47
52
  });
48
53
  CodeBlock.displayName = 'CodeBlock';
@@ -1,9 +1,11 @@
1
1
  import _extends from "@babel/runtime/helpers/extends";
2
2
 
3
3
  /** @jsx jsx */
4
- import { forwardRef, memo, useMemo } from 'react';
4
+ import React, { forwardRef, memo, useMemo } from 'react';
5
5
  import { jsx } from '@emotion/core';
6
6
  import { useGlobalTheme } from '@atlaskit/theme/components';
7
+ import CodeBidiWarning from './bidi-warning';
8
+ import codeBidiWarningDecorator from './bidi-warning/bidi-warning-decorator';
7
9
  import { getCodeStyles } from './internal/theme/styles';
8
10
  const Code = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Code({
9
11
  testId,
@@ -11,12 +13,57 @@ const Code = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Code({
11
13
  }, ref) {
12
14
  const theme = useGlobalTheme();
13
15
  const styles = useMemo(() => getCodeStyles(theme), [theme]);
16
+ const {
17
+ children,
18
+ codeBidiWarnings = true,
19
+ codeBidiWarningLabel,
20
+ ...otherProps
21
+ } = props;
22
+ const decoratedChildren = codeBidiWarnings ? jsx(RenderCodeChildrenWithBidiWarnings, {
23
+ codeBidiWarningLabel: codeBidiWarningLabel
24
+ }, children) : children;
14
25
  return jsx("code", _extends({
15
26
  ref: ref,
16
27
  "data-testid": testId,
17
28
  css: styles
18
- }, props));
29
+ }, otherProps), decoratedChildren);
19
30
  }));
31
+
32
+ function RenderCodeChildrenWithBidiWarnings({
33
+ children,
34
+ codeBidiWarningLabel
35
+ }) {
36
+ const replacedChildren = React.Children.map(children, childNode => {
37
+ if (typeof childNode === 'string') {
38
+ const decorated = codeBidiWarningDecorator(childNode, ({
39
+ bidiCharacter,
40
+ index
41
+ }) => jsx(CodeBidiWarning, {
42
+ bidiCharacter: bidiCharacter,
43
+ key: index,
44
+ label: codeBidiWarningLabel
45
+ }));
46
+ return decorated;
47
+ }
48
+
49
+ if (isReactElement(childNode) && childNode.props.children) {
50
+ const newChildNode = /*#__PURE__*/React.cloneElement(childNode, {
51
+ children: jsx(RenderCodeChildrenWithBidiWarnings, {
52
+ codeBidiWarningLabel: codeBidiWarningLabel
53
+ }, childNode.props.children)
54
+ });
55
+ return newChildNode;
56
+ }
57
+
58
+ return childNode;
59
+ });
60
+ return jsx(React.Fragment, null, replacedChildren);
61
+ }
62
+
63
+ function isReactElement(child) {
64
+ return !!child.type;
65
+ }
66
+
20
67
  Code.displayName = 'Code';
21
68
  export { getCodeStyles };
22
69
  export default Code;
@@ -0,0 +1,155 @@
1
+ import _extends from "@babel/runtime/helpers/extends";
2
+ // @ts-nocheck
3
+ import React from 'react';
4
+ import CodeBidiWarning from './bidi-warning';
5
+ import codeBidiWarningDecorator from './bidi-warning/bidi-warning-decorator'; // File mostly vendored from react-syntax-highlighter
6
+ //
7
+ // - https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/efc3f7b7537d1729193b7a472067bcbe6cbecaf1/src/highlight.js#L272-L281
8
+ // - https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/efc3f7b7537d1729193b7a472067bcbe6cbecaf1/src/create-element.js
9
+ //
10
+ // Patching react syntax-highlighter with a decoration feature is likely preferable
11
+
12
+ export function createBidiWarningRenderer(codeBidiWarningLabel) {
13
+ return function bidiWarningRenderer({
14
+ rows,
15
+ stylesheet,
16
+ useInlineStyles
17
+ }) {
18
+ return rows.map((node, i) => createElement({
19
+ node,
20
+ stylesheet,
21
+ useInlineStyles,
22
+ codeBidiWarningLabel,
23
+ key: `code-segement${i}`
24
+ }));
25
+ };
26
+ } // Get all possible permutations of all power sets
27
+ //
28
+ // Super simple, non-algorithmic solution since the
29
+ // number of class names will not be greater than 4
30
+
31
+ function powerSetPermutations(arr) {
32
+ const arrLength = arr.length;
33
+
34
+ if (arrLength === 0 || arrLength === 1) {
35
+ return arr;
36
+ }
37
+
38
+ if (arrLength === 2) {
39
+ // prettier-ignore
40
+ return [arr[0], arr[1], `${arr[0]}.${arr[1]}`, `${arr[1]}.${arr[0]}`];
41
+ }
42
+
43
+ if (arrLength === 3) {
44
+ return [arr[0], arr[1], arr[2], `${arr[0]}.${arr[1]}`, `${arr[0]}.${arr[2]}`, `${arr[1]}.${arr[0]}`, `${arr[1]}.${arr[2]}`, `${arr[2]}.${arr[0]}`, `${arr[2]}.${arr[1]}`, `${arr[0]}.${arr[1]}.${arr[2]}`, `${arr[0]}.${arr[2]}.${arr[1]}`, `${arr[1]}.${arr[0]}.${arr[2]}`, `${arr[1]}.${arr[2]}.${arr[0]}`, `${arr[2]}.${arr[0]}.${arr[1]}`, `${arr[2]}.${arr[1]}.${arr[0]}`];
45
+ }
46
+
47
+ if (arrLength >= 4) {
48
+ // Currently does not support more than 4 extra
49
+ // class names (after `.token` has been removed)
50
+ return [arr[0], arr[1], arr[2], arr[3], `${arr[0]}.${arr[1]}`, `${arr[0]}.${arr[2]}`, `${arr[0]}.${arr[3]}`, `${arr[1]}.${arr[0]}`, `${arr[1]}.${arr[2]}`, `${arr[1]}.${arr[3]}`, `${arr[2]}.${arr[0]}`, `${arr[2]}.${arr[1]}`, `${arr[2]}.${arr[3]}`, `${arr[3]}.${arr[0]}`, `${arr[3]}.${arr[1]}`, `${arr[3]}.${arr[2]}`, `${arr[0]}.${arr[1]}.${arr[2]}`, `${arr[0]}.${arr[1]}.${arr[3]}`, `${arr[0]}.${arr[2]}.${arr[1]}`, `${arr[0]}.${arr[2]}.${arr[3]}`, `${arr[0]}.${arr[3]}.${arr[1]}`, `${arr[0]}.${arr[3]}.${arr[2]}`, `${arr[1]}.${arr[0]}.${arr[2]}`, `${arr[1]}.${arr[0]}.${arr[3]}`, `${arr[1]}.${arr[2]}.${arr[0]}`, `${arr[1]}.${arr[2]}.${arr[3]}`, `${arr[1]}.${arr[3]}.${arr[0]}`, `${arr[1]}.${arr[3]}.${arr[2]}`, `${arr[2]}.${arr[0]}.${arr[1]}`, `${arr[2]}.${arr[0]}.${arr[3]}`, `${arr[2]}.${arr[1]}.${arr[0]}`, `${arr[2]}.${arr[1]}.${arr[3]}`, `${arr[2]}.${arr[3]}.${arr[0]}`, `${arr[2]}.${arr[3]}.${arr[1]}`, `${arr[3]}.${arr[0]}.${arr[1]}`, `${arr[3]}.${arr[0]}.${arr[2]}`, `${arr[3]}.${arr[1]}.${arr[0]}`, `${arr[3]}.${arr[1]}.${arr[2]}`, `${arr[3]}.${arr[2]}.${arr[0]}`, `${arr[3]}.${arr[2]}.${arr[1]}`, `${arr[0]}.${arr[1]}.${arr[2]}.${arr[3]}`, `${arr[0]}.${arr[1]}.${arr[3]}.${arr[2]}`, `${arr[0]}.${arr[2]}.${arr[1]}.${arr[3]}`, `${arr[0]}.${arr[2]}.${arr[3]}.${arr[1]}`, `${arr[0]}.${arr[3]}.${arr[1]}.${arr[2]}`, `${arr[0]}.${arr[3]}.${arr[2]}.${arr[1]}`, `${arr[1]}.${arr[0]}.${arr[2]}.${arr[3]}`, `${arr[1]}.${arr[0]}.${arr[3]}.${arr[2]}`, `${arr[1]}.${arr[2]}.${arr[0]}.${arr[3]}`, `${arr[1]}.${arr[2]}.${arr[3]}.${arr[0]}`, `${arr[1]}.${arr[3]}.${arr[0]}.${arr[2]}`, `${arr[1]}.${arr[3]}.${arr[2]}.${arr[0]}`, `${arr[2]}.${arr[0]}.${arr[1]}.${arr[3]}`, `${arr[2]}.${arr[0]}.${arr[3]}.${arr[1]}`, `${arr[2]}.${arr[1]}.${arr[0]}.${arr[3]}`, `${arr[2]}.${arr[1]}.${arr[3]}.${arr[0]}`, `${arr[2]}.${arr[3]}.${arr[0]}.${arr[1]}`, `${arr[2]}.${arr[3]}.${arr[1]}.${arr[0]}`, `${arr[3]}.${arr[0]}.${arr[1]}.${arr[2]}`, `${arr[3]}.${arr[0]}.${arr[2]}.${arr[1]}`, `${arr[3]}.${arr[1]}.${arr[0]}.${arr[2]}`, `${arr[3]}.${arr[1]}.${arr[2]}.${arr[0]}`, `${arr[3]}.${arr[2]}.${arr[0]}.${arr[1]}`, `${arr[3]}.${arr[2]}.${arr[1]}.${arr[0]}`];
51
+ }
52
+ }
53
+
54
+ const classNameCombinations = {};
55
+
56
+ function getClassNameCombinations(classNames) {
57
+ if (classNames.length === 0 || classNames.length === 1) {
58
+ return classNames;
59
+ }
60
+
61
+ const key = classNames.join('.');
62
+
63
+ if (!classNameCombinations[key]) {
64
+ classNameCombinations[key] = powerSetPermutations(classNames);
65
+ }
66
+
67
+ return classNameCombinations[key];
68
+ }
69
+
70
+ export function createStyleObject(classNames, elementStyle = {}, stylesheet) {
71
+ const nonTokenClassNames = classNames.filter(className => className !== 'token');
72
+ const classNamesCombinations = getClassNameCombinations(nonTokenClassNames);
73
+ return classNamesCombinations.reduce((styleObject, className) => {
74
+ return { ...styleObject,
75
+ ...stylesheet[className]
76
+ };
77
+ }, elementStyle);
78
+ }
79
+ export function createClassNameString(classNames) {
80
+ return classNames.join(' ');
81
+ }
82
+ export function createChildren(stylesheet, useInlineStyles, codeBidiWarningLabel) {
83
+ let childrenCount = 0;
84
+ return children => {
85
+ childrenCount += 1;
86
+ return children.map((child, i) => createElement({
87
+ node: child,
88
+ stylesheet,
89
+ useInlineStyles,
90
+ codeBidiWarningLabel,
91
+ key: `code-segment-${childrenCount}-${i}`
92
+ }));
93
+ };
94
+ }
95
+
96
+ function createElement({
97
+ node,
98
+ stylesheet,
99
+ style = {},
100
+ useInlineStyles,
101
+ codeBidiWarningLabel,
102
+ key
103
+ }) {
104
+ const {
105
+ properties,
106
+ type,
107
+ tagName: TagName,
108
+ value
109
+ } = node;
110
+
111
+ if (type === 'text') {
112
+ // occasionally react-syntax-highlighter passes a numeric value when the
113
+ // type is text
114
+ const textValue = value + '';
115
+ const decorated = codeBidiWarningDecorator(textValue, ({
116
+ bidiCharacter,
117
+ index
118
+ }) => /*#__PURE__*/React.createElement(CodeBidiWarning, {
119
+ bidiCharacter: bidiCharacter,
120
+ key: index,
121
+ label: codeBidiWarningLabel
122
+ }));
123
+ return decorated;
124
+ } else if (TagName) {
125
+ const childrenCreator = createChildren(stylesheet, useInlineStyles, codeBidiWarningLabel);
126
+ let props;
127
+
128
+ if (!useInlineStyles) {
129
+ props = { ...properties,
130
+ className: createClassNameString(properties.className)
131
+ };
132
+ } else {
133
+ const allStylesheetSelectors = Object.keys(stylesheet).reduce((classes, selector) => {
134
+ selector.split('.').forEach(className => {
135
+ if (!classes.includes(className)) {
136
+ classes.push(className);
137
+ }
138
+ });
139
+ return classes;
140
+ }, []); // For compatibility with older versions of react-syntax-highlighter
141
+
142
+ const startingClassName = properties.className && properties.className.includes('token') ? ['token'] : [];
143
+ const className = properties.className && startingClassName.concat(properties.className.filter(className => !allStylesheetSelectors.includes(className)));
144
+ props = { ...properties,
145
+ className: createClassNameString(className) || undefined,
146
+ style: createStyleObject(properties.className, Object.assign({}, properties.style, style), stylesheet)
147
+ };
148
+ }
149
+
150
+ const children = childrenCreator(node.children);
151
+ return /*#__PURE__*/React.createElement(TagName, _extends({
152
+ key: key
153
+ }, props), children);
154
+ }
155
+ }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/code",
3
- "version": "14.1.5",
3
+ "version": "14.2.0",
4
4
  "sideEffects": false
5
5
  }
@@ -0,0 +1,61 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+
3
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
4
+
5
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
6
+
7
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
8
+
9
+ export var bidiCharacterRegex = /[\u202A-\u202E\u2066-\u2069]/g;
10
+ export default function codeBidiWarningDecorator(originalText, decorate) {
11
+ var matches = _toConsumableArray(originalText.matchAll(bidiCharacterRegex));
12
+
13
+ if (matches.length === 0) {
14
+ // No matches encountered, so we return the originalText value
15
+ return originalText;
16
+ }
17
+
18
+ var children = [];
19
+ var mappedTo = 0;
20
+
21
+ var _iterator = _createForOfIteratorHelper(matches),
22
+ _step;
23
+
24
+ try {
25
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
26
+ var match = _step.value;
27
+
28
+ if (mappedTo !== match.index) {
29
+ // There were unmatched characters prior to this match which haven't been
30
+ // mapped to the children.
31
+ // Add them as plain text.
32
+ children.push(originalText.substring(mappedTo, match.index));
33
+ }
34
+
35
+ children.push(decorate({
36
+ bidiCharacter: match[0],
37
+ index: match.index
38
+ })); // While index is guaranteed to be present, it needs to be asserted due
39
+ // to a limitation of typescripts regex handling
40
+ //
41
+ // https://github.com/microsoft/TypeScript/issues/36788
42
+ // Decorate bidi character
43
+
44
+ mappedTo = match.index + match[0].length;
45
+ }
46
+ } catch (err) {
47
+ _iterator.e(err);
48
+ } finally {
49
+ _iterator.f();
50
+ }
51
+
52
+ if (mappedTo !== originalText.length) {
53
+ // There is text following the final match, which needs to be mapped
54
+ // to the children.
55
+ // Added as plain text.
56
+ children.push(originalText.substring(mappedTo, originalText.length));
57
+ } // return the mapped children with decorated bidi characters
58
+
59
+
60
+ return children;
61
+ }
@@ -0,0 +1 @@
1
+ export { default } from './ui';
@@ -0,0 +1,32 @@
1
+ import _extends from "@babel/runtime/helpers/extends";
2
+ import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
+ import React from 'react';
4
+ import Tooltip from '@atlaskit/tooltip';
5
+ import { Decorator } from './styled';
6
+ export default function BidiWarning(_ref) {
7
+ var testId = _ref.testId,
8
+ bidiCharacter = _ref.bidiCharacter,
9
+ skipChildren = _ref.skipChildren,
10
+ _ref$label = _ref.label,
11
+ label = _ref$label === void 0 ? 'Bidirectional characters change the order that text is rendered. This could be used to obscure malicious code.' : _ref$label;
12
+ return (
13
+ /*#__PURE__*/
14
+ // Following patches, this should be updated to use the render props signature which will provide aria attributes.
15
+ // Note: this should be tested, as initial testing did not see attributes work with current tooltip implementation.
16
+ React.createElement(Tooltip, {
17
+ content: label,
18
+ tag: CustomizedTagWithRef
19
+ }, /*#__PURE__*/React.createElement(Decorator, {
20
+ testId: testId,
21
+ bidiCharacter: bidiCharacter
22
+ }, skipChildren ? null : bidiCharacter))
23
+ );
24
+ }
25
+ var CustomizedTagWithRef = /*#__PURE__*/React.forwardRef(function (props, ref) {
26
+ var children = props.children,
27
+ rest = _objectWithoutProperties(props, ["children"]);
28
+
29
+ return /*#__PURE__*/React.createElement("span", _extends({}, rest, {
30
+ ref: ref
31
+ }), children);
32
+ });
@@ -0,0 +1,64 @@
1
+ /** @jsx jsx */
2
+ import { css, jsx } from '@emotion/core';
3
+ import { Y75 } from '@atlaskit/theme/colors';
4
+ var decoration = css({
5
+ // Required as otherwise the following bidi characters cause the span
6
+ // to not receive hover events.
7
+ //
8
+ // U+2066 LEFT-TO-RIGHT ISOLATE (when using pseudo element before)
9
+ // U+202E RIGHT-TO-LEFT OVERRIDE' (when using pseudo element after)
10
+ position: 'relative',
11
+ ':before': {
12
+ /* layout */
13
+ display: 'inline-flex',
14
+ flexDirection: 'row',
15
+ justifyContent: 'center',
16
+ alignItems: 'center',
17
+ padding: '0 4px',
18
+ fontSize: '14px',
19
+ lineHeight: '18px',
20
+ background: Y75,
21
+
22
+ /**
23
+ * Ensures the decoration receives pointer events when it occurs with
24
+ * an ancestor that disables them.
25
+ */
26
+ pointerEvents: 'auto',
27
+
28
+ /* contents */
29
+ content: '"<"attr(data-bidi-character-code)">"',
30
+ // This color is Y800 which is not yet rolled out
31
+ // https://hello.atlassian.net/wiki/spaces/~tswan/pages/1366555782?focusedCommentId=1370387374#comment-1370387374
32
+ color: '#7F5F01',
33
+ fontStyle: 'normal'
34
+ },
35
+ ':hover:before': {
36
+ color: '#533F04'
37
+ }
38
+ });
39
+ export function Decorator(_ref) {
40
+ var bidiCharacter = _ref.bidiCharacter,
41
+ children = _ref.children,
42
+ testId = _ref.testId;
43
+ var bidiCharacterCode = getBidiCharacterCode(bidiCharacter);
44
+ return jsx("span", {
45
+ "aria-label": bidiCharacterCode
46
+ }, jsx("span", {
47
+ css: decoration,
48
+ "data-testid": testId,
49
+ "data-bidi-character-code": bidiCharacterCode // This is set to true so that the content is not read out by
50
+ // screen readers as the content includes angle brackets for
51
+ // visual decoration purposes.
52
+ // We use a span with the aria-label set to the bidi character code
53
+ // above this span for screen readers.
54
+ ,
55
+ "aria-hidden": "true"
56
+ }, children));
57
+ }
58
+
59
+ function getBidiCharacterCode(bidiCharacter) {
60
+ var _bidiCharacter$codePo;
61
+
62
+ var bidiCharacterCode = (_bidiCharacter$codePo = bidiCharacter.codePointAt(0)) === null || _bidiCharacter$codePo === void 0 ? void 0 : _bidiCharacter$codePo.toString(16);
63
+ return "U+".concat(bidiCharacterCode);
64
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -6,6 +6,7 @@ import { useGlobalTheme } from '@atlaskit/theme/components';
6
6
  import { useHighlightLines } from './internal/hooks/use-highlight';
7
7
  import { getCodeBlockStyles, getCodeBlockTheme } from './internal/theme/styles';
8
8
  import { normalizeLanguage } from './internal/utils/get-normalized-language';
9
+ import { createBidiWarningRenderer } from './react-syntax-highlighter-bidi-warning-renderer';
9
10
  var CodeBlock = /*#__PURE__*/memo(function CodeBlock(_ref) {
10
11
  var _ref$showLineNumbers = _ref.showLineNumbers,
11
12
  showLineNumbers = _ref$showLineNumbers === void 0 ? true : _ref$showLineNumbers,
@@ -18,7 +19,10 @@ var CodeBlock = /*#__PURE__*/memo(function CodeBlock(_ref) {
18
19
  _ref$highlightedEndTe = _ref.highlightedEndText,
19
20
  highlightedEndText = _ref$highlightedEndTe === void 0 ? 'Highlight end' : _ref$highlightedEndTe,
20
21
  testId = _ref.testId,
21
- text = _ref.text;
22
+ text = _ref.text,
23
+ _ref$codeBidiWarnings = _ref.codeBidiWarnings,
24
+ codeBidiWarnings = _ref$codeBidiWarnings === void 0 ? true : _ref$codeBidiWarnings,
25
+ codeBidiWarningLabel = _ref.codeBidiWarningLabel;
22
26
  var numLines = (text || '').split('\n').length;
23
27
  var globalTheme = useGlobalTheme();
24
28
  var theme = useMemo(function () {
@@ -46,6 +50,7 @@ var CodeBlock = /*#__PURE__*/memo(function CodeBlock(_ref) {
46
50
  }, [providedLanguage]); // https://product-fabric.atlassian.net/browse/DST-2472
47
51
 
48
52
  var languageToUse = text ? language : 'text';
53
+ var renderer = codeBidiWarnings ? createBidiWarningRenderer(codeBidiWarningLabel) : undefined;
49
54
  return jsx(SyntaxHighlighter, {
50
55
  "data-testid": testId,
51
56
  "data-code-lang": language,
@@ -57,7 +62,8 @@ var CodeBlock = /*#__PURE__*/memo(function CodeBlock(_ref) {
57
62
  ,
58
63
  wrapLines: highlight.length > 0 || !!testId,
59
64
  lineProps: getLineProps,
60
- useInlineStyles: false
65
+ useInlineStyles: false,
66
+ renderer: renderer
61
67
  }, text);
62
68
  });
63
69
  CodeBlock.displayName = 'CodeBlock';
package/dist/esm/code.js CHANGED
@@ -2,9 +2,11 @@ import _extends from "@babel/runtime/helpers/extends";
2
2
  import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
3
 
4
4
  /** @jsx jsx */
5
- import { forwardRef, memo, useMemo } from 'react';
5
+ import React, { forwardRef, memo, useMemo } from 'react';
6
6
  import { jsx } from '@emotion/core';
7
7
  import { useGlobalTheme } from '@atlaskit/theme/components';
8
+ import CodeBidiWarning from './bidi-warning';
9
+ import codeBidiWarningDecorator from './bidi-warning/bidi-warning-decorator';
8
10
  import { getCodeStyles } from './internal/theme/styles';
9
11
  var Code = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Code(_ref, ref) {
10
12
  var testId = _ref.testId,
@@ -14,12 +16,58 @@ var Code = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Code(_ref, ref) {
14
16
  var styles = useMemo(function () {
15
17
  return getCodeStyles(theme);
16
18
  }, [theme]);
19
+
20
+ var children = props.children,
21
+ _props$codeBidiWarnin = props.codeBidiWarnings,
22
+ codeBidiWarnings = _props$codeBidiWarnin === void 0 ? true : _props$codeBidiWarnin,
23
+ codeBidiWarningLabel = props.codeBidiWarningLabel,
24
+ otherProps = _objectWithoutProperties(props, ["children", "codeBidiWarnings", "codeBidiWarningLabel"]);
25
+
26
+ var decoratedChildren = codeBidiWarnings ? jsx(RenderCodeChildrenWithBidiWarnings, {
27
+ codeBidiWarningLabel: codeBidiWarningLabel
28
+ }, children) : children;
17
29
  return jsx("code", _extends({
18
30
  ref: ref,
19
31
  "data-testid": testId,
20
32
  css: styles
21
- }, props));
33
+ }, otherProps), decoratedChildren);
22
34
  }));
35
+
36
+ function RenderCodeChildrenWithBidiWarnings(_ref2) {
37
+ var children = _ref2.children,
38
+ codeBidiWarningLabel = _ref2.codeBidiWarningLabel;
39
+ var replacedChildren = React.Children.map(children, function (childNode) {
40
+ if (typeof childNode === 'string') {
41
+ var decorated = codeBidiWarningDecorator(childNode, function (_ref3) {
42
+ var bidiCharacter = _ref3.bidiCharacter,
43
+ index = _ref3.index;
44
+ return jsx(CodeBidiWarning, {
45
+ bidiCharacter: bidiCharacter,
46
+ key: index,
47
+ label: codeBidiWarningLabel
48
+ });
49
+ });
50
+ return decorated;
51
+ }
52
+
53
+ if (isReactElement(childNode) && childNode.props.children) {
54
+ var newChildNode = /*#__PURE__*/React.cloneElement(childNode, {
55
+ children: jsx(RenderCodeChildrenWithBidiWarnings, {
56
+ codeBidiWarningLabel: codeBidiWarningLabel
57
+ }, childNode.props.children)
58
+ });
59
+ return newChildNode;
60
+ }
61
+
62
+ return childNode;
63
+ });
64
+ return jsx(React.Fragment, null, replacedChildren);
65
+ }
66
+
67
+ function isReactElement(child) {
68
+ return !!child.type;
69
+ }
70
+
23
71
  Code.displayName = 'Code';
24
72
  export { getCodeStyles };
25
73
  export default Code;