@tolgee/core 4.9.2 → 4.9.3-rc.d287ae9.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 (276) hide show
  1. package/dist/tolgee.cjs.js +1127 -7028
  2. package/dist/tolgee.cjs.js.map +1 -1
  3. package/dist/tolgee.cjs.min.js +1 -1
  4. package/dist/tolgee.cjs.min.js.map +1 -1
  5. package/dist/tolgee.esm.min.mjs +1 -1
  6. package/dist/tolgee.esm.min.mjs.map +1 -1
  7. package/dist/tolgee.esm.mjs +1125 -7023
  8. package/dist/tolgee.esm.mjs.map +1 -1
  9. package/dist/tolgee.umd.js +1127 -7028
  10. package/dist/tolgee.umd.js.map +1 -1
  11. package/dist/tolgee.umd.min.js +1 -1
  12. package/dist/tolgee.umd.min.js.map +1 -1
  13. package/lib/Controller/Cache/Cache.d.ts +22 -0
  14. package/lib/Controller/Cache/helpers.d.ts +4 -0
  15. package/lib/Controller/Controller.d.ts +146 -0
  16. package/lib/Controller/Events/EventEmitter.d.ts +6 -0
  17. package/lib/Controller/Events/EventEmitterSelective.d.ts +15 -0
  18. package/lib/Controller/Events/Events.d.ts +50 -0
  19. package/lib/Controller/Plugins/Plugins.d.ts +36 -0
  20. package/lib/Controller/State/State.d.ts +39 -0
  21. package/lib/Controller/State/helpers.d.ts +6 -0
  22. package/lib/Controller/State/initObserverOptions.d.ts +13 -0
  23. package/lib/Controller/State/initState.d.ts +47 -0
  24. package/lib/Controller/ValueObserver.d.ts +5 -0
  25. package/lib/Tolgee.d.ts +2 -68
  26. package/lib/TranslateParams.d.ts +2 -0
  27. package/lib/{Constants/Global.d.ts → constants.d.ts} +1 -2
  28. package/lib/helpers.d.ts +3 -0
  29. package/lib/index.d.ts +3 -8
  30. package/lib/types.d.ts +244 -84
  31. package/package.json +20 -29
  32. package/src/Controller/Cache/Cache.ts +293 -0
  33. package/src/Controller/Cache/helpers.ts +37 -0
  34. package/src/Controller/Controller.ts +310 -0
  35. package/src/Controller/Events/EventEmitter.ts +27 -0
  36. package/src/Controller/Events/EventEmitterSelective.test.ts +125 -0
  37. package/src/Controller/Events/EventEmitterSelective.ts +179 -0
  38. package/src/Controller/Events/Events.ts +66 -0
  39. package/src/Controller/Plugins/Plugins.ts +315 -0
  40. package/src/Controller/State/State.ts +175 -0
  41. package/src/Controller/State/helpers.ts +41 -0
  42. package/src/Controller/State/initObserverOptions.ts +38 -0
  43. package/src/Controller/State/initState.ts +81 -0
  44. package/src/Controller/ValueObserver.ts +23 -0
  45. package/src/Tolgee.ts +79 -330
  46. package/src/TranslateParams.test.ts +41 -0
  47. package/src/TranslateParams.ts +51 -0
  48. package/src/__test/backend.test.ts +48 -0
  49. package/src/__test/cache.test.ts +148 -0
  50. package/src/__test/client.test.ts +48 -0
  51. package/src/__test/events.test.ts +33 -0
  52. package/src/__test/initialization.test.ts +85 -0
  53. package/src/__test/jest-setup.ts +2 -0
  54. package/src/__test/languageDetection.test.ts +129 -0
  55. package/src/__test/languageStorage.test.ts +145 -0
  56. package/src/__test/languages.test.ts +112 -0
  57. package/src/__test/loading.test.ts +39 -0
  58. package/src/__test/namespaces.test.ts +99 -0
  59. package/src/__test/namespacesFallback.test.ts +74 -0
  60. package/src/__test/plugins.test.ts +136 -0
  61. package/src/__test/testTools.ts +7 -0
  62. package/src/{Constants/Global.ts → constants.ts} +1 -3
  63. package/src/helpers.ts +17 -0
  64. package/src/index.ts +9 -8
  65. package/src/types.ts +338 -90
  66. package/README.md +0 -45
  67. package/dist/Constants/Global.d.ts +0 -6
  68. package/dist/Constants/ModifierKey.d.ts +0 -6
  69. package/dist/Errors/ApiHttpError.d.ts +0 -5
  70. package/dist/Observer.d.ts +0 -14
  71. package/dist/Observer.test.d.ts +0 -2
  72. package/dist/Properties.d.ts +0 -17
  73. package/dist/Properties.test.d.ts +0 -1
  74. package/dist/Tolgee.d.ts +0 -68
  75. package/dist/Tolgee.test.d.ts +0 -1
  76. package/dist/TolgeeConfig.d.ts +0 -69
  77. package/dist/TolgeeConfig.test.d.ts +0 -1
  78. package/dist/__integration/FormatterIcu.test.d.ts +0 -1
  79. package/dist/__integration/FormatterMissing.d.ts +0 -1
  80. package/dist/__integration/Tolgee.test.d.ts +0 -1
  81. package/dist/__integration/TolgeeInvisible.test.d.ts +0 -1
  82. package/dist/__integration/mockTranslations.d.ts +0 -7
  83. package/dist/__integration/testConfig.d.ts +0 -9
  84. package/dist/__testFixtures/classMock.d.ts +0 -3
  85. package/dist/__testFixtures/createElement.d.ts +0 -2
  86. package/dist/__testFixtures/createTestDom.d.ts +0 -9
  87. package/dist/__testFixtures/mocked.d.ts +0 -20
  88. package/dist/__testFixtures/setupAfterEnv.d.ts +0 -8
  89. package/dist/helpers/NodeHelper.d.ts +0 -14
  90. package/dist/helpers/TextHelper.d.ts +0 -5
  91. package/dist/helpers/TextHelper.test.d.ts +0 -1
  92. package/dist/helpers/commonTypes.d.ts +0 -2
  93. package/dist/helpers/encoderPolyfill.d.ts +0 -8
  94. package/dist/helpers/secret.d.ts +0 -6
  95. package/dist/helpers/secret.test.d.ts +0 -1
  96. package/dist/helpers/sleep.d.ts +0 -1
  97. package/dist/highlighter/HighlightFunctionsInitializer.d.ts +0 -10
  98. package/dist/highlighter/HighlightFunctionsInitializer.test.d.ts +0 -1
  99. package/dist/highlighter/MouseEventHandler.d.ts +0 -29
  100. package/dist/highlighter/MouseEventHandler.test.d.ts +0 -1
  101. package/dist/highlighter/TranslationHighlighter.d.ts +0 -14
  102. package/dist/highlighter/TranslationHighlighter.test.d.ts +0 -1
  103. package/dist/index.d.ts +0 -10
  104. package/dist/internal.d.ts +0 -2
  105. package/dist/modules/IcuFormatter.d.ts +0 -2
  106. package/dist/modules/IcuFormatter.test.d.ts +0 -1
  107. package/dist/modules/index.d.ts +0 -1
  108. package/dist/services/ApiHttpService.d.ts +0 -15
  109. package/dist/services/CoreService.d.ts +0 -18
  110. package/dist/services/CoreService.test.d.ts +0 -1
  111. package/dist/services/DependencyService.d.ts +0 -39
  112. package/dist/services/DependencyService.test.d.ts +0 -1
  113. package/dist/services/ElementRegistrar.d.ts +0 -19
  114. package/dist/services/ElementRegistrar.test.d.ts +0 -1
  115. package/dist/services/EventEmitter.d.ts +0 -13
  116. package/dist/services/EventService.d.ts +0 -9
  117. package/dist/services/ModuleService.d.ts +0 -5
  118. package/dist/services/ScreenshotService.d.ts +0 -15
  119. package/dist/services/Subscription.d.ts +0 -5
  120. package/dist/services/TextService.d.ts +0 -14
  121. package/dist/services/TextService.test.d.ts +0 -1
  122. package/dist/services/TranslationService.d.ts +0 -75
  123. package/dist/services/TranslationService.test.d.ts +0 -1
  124. package/dist/services/__mocks__/CoreService.d.ts +0 -2
  125. package/dist/toolsManager/Messages.d.ts +0 -8
  126. package/dist/toolsManager/Messages.test.d.ts +0 -1
  127. package/dist/toolsManager/PluginManager.d.ts +0 -21
  128. package/dist/toolsManager/PluginManager.test.d.ts +0 -1
  129. package/dist/types/DTOs.d.ts +0 -20
  130. package/dist/types/apiSchema.generated.d.ts +0 -6185
  131. package/dist/types.d.ts +0 -123
  132. package/dist/wrappers/AbstractWrapper.d.ts +0 -8
  133. package/dist/wrappers/NodeHandler.d.ts +0 -18
  134. package/dist/wrappers/WrappedHandler.d.ts +0 -8
  135. package/dist/wrappers/invisible/AttributeHandler.d.ts +0 -8
  136. package/dist/wrappers/invisible/Coder.d.ts +0 -7
  137. package/dist/wrappers/invisible/ContentHandler.d.ts +0 -6
  138. package/dist/wrappers/invisible/CoreHandler.d.ts +0 -10
  139. package/dist/wrappers/invisible/InvisibleWrapper.d.ts +0 -18
  140. package/dist/wrappers/invisible/ValueMemory.d.ts +0 -5
  141. package/dist/wrappers/invisible/ValueMemory.test.d.ts +0 -1
  142. package/dist/wrappers/text/AttributeHandler.d.ts +0 -8
  143. package/dist/wrappers/text/AttributeHandler.test.d.ts +0 -1
  144. package/dist/wrappers/text/Coder.d.ts +0 -15
  145. package/dist/wrappers/text/Coder.test.d.ts +0 -1
  146. package/dist/wrappers/text/ContentHandler.d.ts +0 -8
  147. package/dist/wrappers/text/ContentHandler.test.d.ts +0 -1
  148. package/dist/wrappers/text/CoreHandler.d.ts +0 -17
  149. package/dist/wrappers/text/CoreHandler.test.d.ts +0 -1
  150. package/dist/wrappers/text/TextWrapper.d.ts +0 -20
  151. package/index.js +0 -7
  152. package/lib/Constants/ModifierKey.d.ts +0 -6
  153. package/lib/Errors/ApiHttpError.d.ts +0 -5
  154. package/lib/Observer.d.ts +0 -14
  155. package/lib/Properties.d.ts +0 -17
  156. package/lib/TolgeeConfig.d.ts +0 -69
  157. package/lib/helpers/NodeHelper.d.ts +0 -14
  158. package/lib/helpers/TextHelper.d.ts +0 -5
  159. package/lib/helpers/commonTypes.d.ts +0 -2
  160. package/lib/helpers/encoderPolyfill.d.ts +0 -8
  161. package/lib/helpers/secret.d.ts +0 -6
  162. package/lib/helpers/sleep.d.ts +0 -1
  163. package/lib/highlighter/HighlightFunctionsInitializer.d.ts +0 -10
  164. package/lib/highlighter/MouseEventHandler.d.ts +0 -29
  165. package/lib/highlighter/TranslationHighlighter.d.ts +0 -14
  166. package/lib/modules/IcuFormatter.d.ts +0 -2
  167. package/lib/modules/index.d.ts +0 -1
  168. package/lib/services/ApiHttpService.d.ts +0 -15
  169. package/lib/services/CoreService.d.ts +0 -18
  170. package/lib/services/DependencyService.d.ts +0 -39
  171. package/lib/services/ElementRegistrar.d.ts +0 -19
  172. package/lib/services/EventEmitter.d.ts +0 -13
  173. package/lib/services/EventService.d.ts +0 -9
  174. package/lib/services/ModuleService.d.ts +0 -5
  175. package/lib/services/ScreenshotService.d.ts +0 -15
  176. package/lib/services/Subscription.d.ts +0 -5
  177. package/lib/services/TextService.d.ts +0 -14
  178. package/lib/services/TranslationService.d.ts +0 -75
  179. package/lib/toolsManager/Messages.d.ts +0 -8
  180. package/lib/toolsManager/PluginManager.d.ts +0 -21
  181. package/lib/types/DTOs.d.ts +0 -20
  182. package/lib/types/apiSchema.generated.d.ts +0 -6185
  183. package/lib/wrappers/AbstractWrapper.d.ts +0 -8
  184. package/lib/wrappers/NodeHandler.d.ts +0 -18
  185. package/lib/wrappers/WrappedHandler.d.ts +0 -8
  186. package/lib/wrappers/invisible/AttributeHandler.d.ts +0 -8
  187. package/lib/wrappers/invisible/Coder.d.ts +0 -7
  188. package/lib/wrappers/invisible/ContentHandler.d.ts +0 -6
  189. package/lib/wrappers/invisible/CoreHandler.d.ts +0 -10
  190. package/lib/wrappers/invisible/InvisibleWrapper.d.ts +0 -18
  191. package/lib/wrappers/invisible/ValueMemory.d.ts +0 -5
  192. package/lib/wrappers/text/AttributeHandler.d.ts +0 -8
  193. package/lib/wrappers/text/Coder.d.ts +0 -15
  194. package/lib/wrappers/text/ContentHandler.d.ts +0 -8
  195. package/lib/wrappers/text/CoreHandler.d.ts +0 -17
  196. package/lib/wrappers/text/TextWrapper.d.ts +0 -20
  197. package/src/Constants/ModifierKey.ts +0 -6
  198. package/src/Errors/ApiHttpError.ts +0 -8
  199. package/src/Observer.test.ts +0 -119
  200. package/src/Observer.ts +0 -68
  201. package/src/Properties.test.ts +0 -150
  202. package/src/Properties.ts +0 -112
  203. package/src/Tolgee.test.ts +0 -473
  204. package/src/TolgeeConfig.test.ts +0 -21
  205. package/src/TolgeeConfig.ts +0 -134
  206. package/src/__integration/FormatterIcu.test.ts +0 -80
  207. package/src/__integration/FormatterMissing.ts +0 -54
  208. package/src/__integration/Tolgee.test.ts +0 -90
  209. package/src/__integration/TolgeeInvisible.test.ts +0 -145
  210. package/src/__integration/mockTranslations.ts +0 -6
  211. package/src/__integration/testConfig.ts +0 -16
  212. package/src/__testFixtures/classMock.ts +0 -11
  213. package/src/__testFixtures/createElement.ts +0 -43
  214. package/src/__testFixtures/createTestDom.ts +0 -26
  215. package/src/__testFixtures/mocked.ts +0 -25
  216. package/src/__testFixtures/setupAfterEnv.ts +0 -34
  217. package/src/helpers/NodeHelper.ts +0 -90
  218. package/src/helpers/TextHelper.test.ts +0 -62
  219. package/src/helpers/TextHelper.ts +0 -58
  220. package/src/helpers/commonTypes.ts +0 -8
  221. package/src/helpers/encoderPolyfill.ts +0 -96
  222. package/src/helpers/secret.test.ts +0 -61
  223. package/src/helpers/secret.ts +0 -68
  224. package/src/helpers/sleep.ts +0 -2
  225. package/src/highlighter/HighlightFunctionsInitializer.test.ts +0 -40
  226. package/src/highlighter/HighlightFunctionsInitializer.ts +0 -61
  227. package/src/highlighter/MouseEventHandler.test.ts +0 -151
  228. package/src/highlighter/MouseEventHandler.ts +0 -191
  229. package/src/highlighter/TranslationHighlighter.test.ts +0 -177
  230. package/src/highlighter/TranslationHighlighter.ts +0 -113
  231. package/src/internal.ts +0 -2
  232. package/src/modules/IcuFormatter.test.ts +0 -21
  233. package/src/modules/IcuFormatter.ts +0 -39
  234. package/src/modules/index.ts +0 -1
  235. package/src/services/ApiHttpService.ts +0 -85
  236. package/src/services/CoreService.test.ts +0 -141
  237. package/src/services/CoreService.ts +0 -76
  238. package/src/services/DependencyService.test.ts +0 -51
  239. package/src/services/DependencyService.ts +0 -116
  240. package/src/services/ElementRegistrar.test.ts +0 -131
  241. package/src/services/ElementRegistrar.ts +0 -108
  242. package/src/services/EventEmitter.ts +0 -52
  243. package/src/services/EventService.ts +0 -14
  244. package/src/services/ModuleService.ts +0 -14
  245. package/src/services/ScreenshotService.ts +0 -31
  246. package/src/services/Subscription.ts +0 -7
  247. package/src/services/TextService.test.ts +0 -88
  248. package/src/services/TextService.ts +0 -82
  249. package/src/services/TranslationService.test.ts +0 -358
  250. package/src/services/TranslationService.ts +0 -417
  251. package/src/services/__mocks__/CoreService.ts +0 -17
  252. package/src/toolsManager/Messages.test.ts +0 -79
  253. package/src/toolsManager/Messages.ts +0 -60
  254. package/src/toolsManager/PluginManager.test.ts +0 -108
  255. package/src/toolsManager/PluginManager.ts +0 -129
  256. package/src/types/DTOs.ts +0 -25
  257. package/src/types/apiSchema.generated.ts +0 -6208
  258. package/src/wrappers/AbstractWrapper.ts +0 -14
  259. package/src/wrappers/NodeHandler.ts +0 -143
  260. package/src/wrappers/WrappedHandler.ts +0 -28
  261. package/src/wrappers/invisible/AttributeHandler.ts +0 -23
  262. package/src/wrappers/invisible/Coder.ts +0 -65
  263. package/src/wrappers/invisible/ContentHandler.ts +0 -15
  264. package/src/wrappers/invisible/CoreHandler.ts +0 -17
  265. package/src/wrappers/invisible/InvisibleWrapper.ts +0 -59
  266. package/src/wrappers/invisible/ValueMemory.test.ts +0 -25
  267. package/src/wrappers/invisible/ValueMemory.ts +0 -16
  268. package/src/wrappers/text/AttributeHandler.test.ts +0 -118
  269. package/src/wrappers/text/AttributeHandler.ts +0 -25
  270. package/src/wrappers/text/Coder.test.ts +0 -298
  271. package/src/wrappers/text/Coder.ts +0 -202
  272. package/src/wrappers/text/ContentHandler.test.ts +0 -185
  273. package/src/wrappers/text/ContentHandler.ts +0 -21
  274. package/src/wrappers/text/CoreHandler.test.ts +0 -106
  275. package/src/wrappers/text/CoreHandler.ts +0 -45
  276. package/src/wrappers/text/TextWrapper.ts +0 -69
@@ -1,298 +0,0 @@
1
- jest.dontMock('./Coder');
2
- jest.dontMock('../../services/DependencyService');
3
- jest.dontMock('../../helpers/TextHelper');
4
- jest.dontMock('../../services/TextService');
5
- jest.dontMock('../../services/ModuleService');
6
- jest.dontMock('../../modules/IcuFormatter');
7
-
8
- import { DependencyService } from '../../services/DependencyService';
9
- import { Coder } from './Coder';
10
- import { getMockedInstance } from '@testFixtures/mocked';
11
- import { TextService } from '../../services/TextService';
12
- import { Properties } from '../../Properties';
13
- import { TranslationService } from '../../services/TranslationService';
14
- import { IcuFormatter } from '../../modules/IcuFormatter';
15
-
16
- describe('Coder', () => {
17
- let mockedTranslationReturn = '';
18
- let textService: TextService;
19
- let coder: Coder;
20
-
21
- const getTranslationMock = jest.fn(async () => {
22
- return mockedTranslationReturn;
23
- });
24
-
25
- const getFromCacheOrCallbackMock = jest.fn(() => {
26
- return mockedTranslationReturn;
27
- });
28
-
29
- beforeEach(async () => {
30
- const depStore = new DependencyService();
31
- depStore.moduleService.addModule(IcuFormatter);
32
- depStore.run();
33
- textService = depStore.textService;
34
- mockedTranslationReturn = 'Dummy translated text {param1} {param2}';
35
-
36
- coder = new Coder(depStore.properties, textService);
37
-
38
- getMockedInstance(Properties).config = {
39
- inputPrefix: '{{',
40
- inputSuffix: '}}',
41
- restrictedElements: [],
42
- tagAttributes: {
43
- '*': ['aria-label'],
44
- },
45
- };
46
- getMockedInstance(TranslationService).getTranslation = getTranslationMock;
47
-
48
- getMockedInstance(TranslationService).getFromCacheOrFallback =
49
- getFromCacheOrCallbackMock;
50
- });
51
-
52
- afterEach(async () => {
53
- jest.clearAllMocks();
54
- });
55
-
56
- test('replace function replaces occurrences correctly', async () => {
57
- const text =
58
- 'This is text with params {{text:param1:hello,param2:hello 2 as well}}. Text continues';
59
- const replaced = await coder.unwrap(text);
60
- expect(replaced.text).toEqual(
61
- 'This is text with params Dummy translated text hello hello 2 as well. Text continues'
62
- );
63
- expect(replaced.keys[0].key).toEqual('text');
64
- expect(replaced.keys[0].params['param1']).toEqual('hello');
65
- expect(replaced.keys[0].params['param2']).toEqual('hello 2 as well');
66
- });
67
-
68
- test('replace function does not replace when prefix is escaped', async () => {
69
- const text = 'This is text: {{text:param1:aaaa,param2:aaaa}} \\{{text}}';
70
- const replaced = await coder.unwrap(text);
71
- expect(replaced.text).toEqual(
72
- 'This is text: Dummy translated text aaaa aaaa {{text}}'
73
- );
74
- });
75
-
76
- test("it doesn't affect not related backslashes", async () => {
77
- const text =
78
- '\\This is \\text: {{text:param1:aaaa,param2:aaaa}} \\{{text}}, see? \\';
79
- const replaced = await coder.unwrap(text);
80
- expect(replaced.text).toEqual(
81
- '\\This is \\text: Dummy translated text aaaa aaaa {{text}}, see? \\'
82
- );
83
- });
84
-
85
- test('correctly parses default value', async () => {
86
- const text =
87
- '\\This is \\text: {{text,This is my default value.\\:look!:param1:aaaa,param2:aaaa}} \\{{text}}, see? \\';
88
- await coder.unwrap(text);
89
- expect(getFromCacheOrCallbackMock).toHaveBeenCalledWith(
90
- 'text',
91
- undefined,
92
- 'This is my default value.:look!'
93
- );
94
- });
95
-
96
- test('replace function does not translate when params have escaped , or :', async () => {
97
- const text =
98
- 'This is text: {{text:param1:param 1 with\\,and \\:.,param2:hello \\:}}. Text continues';
99
- const replaced = await coder.unwrap(text);
100
- expect(replaced.text).toEqual(
101
- 'This is text: Dummy translated text param 1 with,and :. hello :. Text continues'
102
- );
103
- });
104
-
105
- test('replace function does not translate when params have escaped \\', async () => {
106
- const text =
107
- 'This is text: {{text:param1:param 1 with\\\\ and again \\\\,param2:hello \\\\}}. Text continues';
108
- const replaced = await coder.unwrap(text);
109
- expect(replaced.text).toEqual(
110
- 'This is text: Dummy translated text param 1 with\\ and again \\ hello \\. Text continues'
111
- );
112
- });
113
-
114
- test('replace function works with new lines in key', async () => {
115
- mockedTranslationReturn = 'yep';
116
- const text = 'This is text: {{text\nwith\nnew\nlines}}. Text continues';
117
- await coder.unwrap(text);
118
- expect(getFromCacheOrCallbackMock).toHaveBeenCalledWith(
119
- 'text\nwith\nnew\nlines',
120
- undefined,
121
- undefined
122
- );
123
- });
124
-
125
- test('works with escaped strings in params', async () => {
126
- const text = 'Text: {{text\nwith\nnew\nlines:hello:w\\,or\\:ld}}.';
127
- mockedTranslationReturn = 'translated {hello}';
128
- const result = await coder.unwrap(text);
129
- expect(result.keys[0].params['hello']).toEqual('w,or:ld');
130
- });
131
-
132
- describe('Different key occurrences', () => {
133
- beforeEach(() => {
134
- getMockedInstance(TranslationService).getFromCacheOrFallback = jest.fn(
135
- () => {
136
- return 'translated';
137
- }
138
- );
139
- });
140
-
141
- test('longer text will be handled correctly', async () => {
142
- const text =
143
- 'This is standard text to translate: {{text}}. This is another one: {{text}}.\n' +
144
- 'There is an text with slash before, but escaped: \\\\{{text}}\n' +
145
- 'This is one with escaped prefix, to it shall not translate this one: \\{{aaa}}\n' +
146
- 'There is another text: {{text}}\n' +
147
- 'There are two texts to translate next to each other: {{text}}{{text}}. And after it text continues: {{text}}.\n' +
148
- 'This is an end, finally.\n';
149
-
150
- const replaced = await coder.unwrap(text);
151
-
152
- expect(replaced.text).toEqual(
153
- 'This is standard text to translate: translated. This is another one: translated.\n' +
154
- 'There is an text with slash before, but escaped: \\translated\n' +
155
- 'This is one with escaped prefix, to it shall not translate this one: {{aaa}}\n' +
156
- 'There is another text: translated\n' +
157
- 'There are two texts to translate next to each other: translatedtranslated. And after it text continues: translated.\n' +
158
- 'This is an end, finally.\n'
159
- );
160
- });
161
-
162
- test('will translate correctly when the text begins with key', async () => {
163
- const text = '{{text}}, text continues';
164
- const replaced = await coder.unwrap(text);
165
- expect(replaced.text).toEqual('translated, text continues');
166
- });
167
-
168
- test('will not translate when there is nothing to translate', async () => {
169
- const text = '\\{{text}}, text continues';
170
- const replaced = await coder.unwrap(text);
171
- expect(replaced).toEqual(undefined);
172
- });
173
-
174
- test('will not translate when the text begins with escaped key', async () => {
175
- const text = '\\{{text}}, text continues {{other text}}';
176
- const replaced = await coder.unwrap(text);
177
- expect(replaced.text).toEqual('{{text}}, text continues translated');
178
- });
179
-
180
- test('will translate when the text begins with escaped escape character', async () => {
181
- const text = '\\\\{{text}}, text continues {{other text}}';
182
- const replaced = await coder.unwrap(text);
183
- expect(replaced.text).toEqual('\\translated, text continues translated');
184
- });
185
-
186
- test('will translate when the text begins with escaped escape character, what is escaped', async () => {
187
- const text = '\\\\\\{{text}}, text continues {{other text}}';
188
- const replaced = await coder.unwrap(text);
189
- expect(replaced.text).toEqual('\\\\{{text}}, text continues translated');
190
- });
191
- });
192
-
193
- describe('wrap function', () => {
194
- test('will correctly wrap text without params', () => {
195
- expect(coder.wrap('text')).toEqual('{{text}}');
196
- });
197
-
198
- test('will correctly wrap text with , and :', () => {
199
- expect(coder.wrap('text, other text: yes!')).toEqual(
200
- '{{text\\, other text\\: yes!}}'
201
- );
202
- });
203
-
204
- test('will correctly wrap text with ,, : and \\', () => {
205
- expect(coder.wrap('text, other text: \\ yes!')).toEqual(
206
- '{{text\\, other text\\: \\\\ yes!}}'
207
- );
208
- });
209
-
210
- test('will correctly wrap text with parameters', () => {
211
- expect(
212
- coder.wrap('text', {
213
- param1: 'the param value',
214
- param2: '2nd value',
215
- })
216
- ).toEqual('{{text:param1:the param value,param2:2nd value}}');
217
- });
218
-
219
- test('will correctly wrap text with parameters containing : an ,', () => {
220
- expect(
221
- coder.wrap('text', {
222
- param1: 'the param value: value, value2 or value3',
223
- param2: '2nd value',
224
- })
225
- ).toEqual(
226
- '{{text:param1:the param value\\: value\\, value2 or value3,param2:2nd value}}'
227
- );
228
- });
229
-
230
- test('it will correctly replace wrapped text', async () => {
231
- getMockedInstance(TranslationService).getFromCacheOrFallback = jest.fn(
232
- () => {
233
- return 'xxx {param1} {param2} xxx';
234
- }
235
- );
236
-
237
- const wrapped = coder.wrap(
238
- 'An longer key with some strange ,: ,: \\ characters',
239
- { param1: ',:,:', param2: '....\\...,' }
240
- );
241
-
242
- expect((await coder.unwrap(wrapped)).text).toEqual(
243
- 'xxx ,:,: ....\\..., xxx'
244
- );
245
- });
246
-
247
- test('will correctly convert param to string', () => {
248
- const wrapped = coder.wrap('key', { param1: 1 });
249
- expect(wrapped).toEqual('{{key:param1:1}}');
250
- });
251
-
252
- test('will correctly replace number parameters', async () => {
253
- getMockedInstance(TranslationService).getFromCacheOrFallback = jest.fn(
254
- () => {
255
- return 'xxx {param1, number} {param2, number} xxx';
256
- }
257
- );
258
-
259
- const wrapped = coder.wrap('key', { param1: 1, param2: 145.5 });
260
- expect((await coder.unwrap(wrapped)).text).toEqual('xxx 1 145.5 xxx');
261
- });
262
-
263
- test('correctly wraps default value', async () => {
264
- const wrapped = coder.wrap(
265
- 'key',
266
- { param1: 1, param2: 'Yes,yes,yes:yes' },
267
- 'Look: What a beautiful default\nvalue,' +
268
- ' translating will be such an experience.'
269
- );
270
- expect(wrapped).toEqual(
271
- '{{key,Look\\: What a beautiful default\n' +
272
- 'value\\, translating will be such an experience.' +
273
- ':param1:1,param2:Yes\\,yes\\,yes\\:yes}}'
274
- );
275
-
276
- await coder.unwrap(wrapped);
277
- expect(getFromCacheOrCallbackMock).toBeCalledWith(
278
- 'key',
279
- undefined,
280
- 'Look: What a beautiful default\nvalue, translating will be such an experience.'
281
- );
282
- });
283
-
284
- test('will correctly replace bigint parameter', async () => {
285
- getMockedInstance(TranslationService).getFromCacheOrFallback = jest.fn(
286
- () => {
287
- return 'xxx {param1, number} {param2, number} xxx';
288
- }
289
- );
290
-
291
- const wrapped = coder.wrap('key', {
292
- param1: 1,
293
- param2: BigInt(129374),
294
- });
295
- expect((await coder.unwrap(wrapped)).text).toEqual('xxx 1 129,374 xxx');
296
- });
297
- });
298
- });
@@ -1,202 +0,0 @@
1
- import { TextHelper } from '../../helpers/TextHelper';
2
- import { Properties } from '../../Properties';
3
- import { TextService } from '../../services/TextService';
4
- import {
5
- KeyAndParams,
6
- KeyAndParamsTags,
7
- TranslatedWithMetadataTags,
8
- TranslationParams,
9
- Unwrapped,
10
- } from '../../types';
11
-
12
- export class Coder {
13
- constructor(
14
- private properties: Properties,
15
- private textService: TextService
16
- ) {}
17
-
18
- private get rawUnWrapRegex(): string {
19
- const escapedPrefix = this.escapeForRegExp(
20
- this.properties.config.inputPrefix
21
- );
22
- const escapedSuffix = this.escapeForRegExp(
23
- this.properties.config.inputSuffix
24
- );
25
- return `(\\\\?)(${escapedPrefix}(.*?)${escapedSuffix})`;
26
- }
27
-
28
- private static parseUnwrapped(unwrappedString: string): KeyAndParams {
29
- let escaped = false;
30
- let actual = '';
31
- let paramName = '';
32
- let readingState: 'KEY' | 'DEFAULT_VALUE' | 'PARAM_NAME' | 'PARAM_VALUE' =
33
- 'KEY';
34
-
35
- const result = {
36
- key: '',
37
- params: {},
38
- defaultValue: undefined as string | undefined,
39
- } as KeyAndParams;
40
-
41
- for (const char of unwrappedString) {
42
- if (char === '\\' && !escaped) {
43
- escaped = true;
44
- continue;
45
- }
46
- if (escaped) {
47
- escaped = false;
48
- actual += char;
49
- continue;
50
- }
51
- if (readingState === 'KEY' && char === ',') {
52
- readingState = 'DEFAULT_VALUE';
53
- result.key = actual;
54
- actual = '';
55
- continue;
56
- }
57
-
58
- if (readingState === 'KEY' && char === ':') {
59
- readingState = 'PARAM_NAME';
60
- result.key = actual;
61
- actual = '';
62
- continue;
63
- }
64
-
65
- if (readingState === 'DEFAULT_VALUE' && char === ':') {
66
- readingState = 'PARAM_NAME';
67
- result.defaultValue = actual;
68
- actual = '';
69
- continue;
70
- }
71
-
72
- if (readingState === 'PARAM_NAME' && char === ':') {
73
- readingState = 'PARAM_VALUE';
74
- paramName = actual;
75
- actual = '';
76
- continue;
77
- }
78
-
79
- if (readingState === 'PARAM_VALUE' && char === ',') {
80
- readingState = 'PARAM_NAME';
81
- result.params[paramName] = actual;
82
- actual = '';
83
- continue;
84
- }
85
- actual += char;
86
- }
87
-
88
- if (readingState === 'KEY') {
89
- result.key = actual;
90
- }
91
-
92
- if (readingState === 'DEFAULT_VALUE') {
93
- result.defaultValue = actual;
94
- }
95
-
96
- if (readingState === 'PARAM_VALUE') {
97
- result.params[paramName] = actual;
98
- }
99
-
100
- return result;
101
- }
102
-
103
- unwrap(text: string): Unwrapped {
104
- const matchRegexp = new RegExp(this.rawUnWrapRegex, 'gs');
105
-
106
- const keysAndParams: KeyAndParamsTags<any>[] = [];
107
-
108
- let matched = false;
109
-
110
- let match;
111
- let start = 0;
112
- let result = '';
113
- while ((match = matchRegexp.exec(text)) !== null) {
114
- let pre = match[1] as string;
115
- const [fullMatch, _, wrapped, unwrapped] = match as [
116
- string,
117
- string,
118
- string,
119
- string
120
- ];
121
- const { index, input } = match;
122
- result += input.substr(start, index - start);
123
- start = index + fullMatch.length;
124
- if (pre === '\\') {
125
- if (!TextHelper.isCharEscaped(index, text)) {
126
- result += wrapped;
127
- continue;
128
- }
129
- pre = '';
130
- }
131
- const translated = this.getTranslatedWithMetadata(unwrapped);
132
- keysAndParams.push({
133
- key: translated.key,
134
- params: translated.params,
135
- defaultValue: translated.defaultValue,
136
- });
137
- matched = true;
138
- result += pre + translated.translated;
139
- }
140
-
141
- result += text.substring(start);
142
-
143
- if (matched) {
144
- return { text: result, keys: keysAndParams };
145
- }
146
-
147
- return undefined;
148
- }
149
-
150
- public wrap(
151
- key: string,
152
- params: TranslationParams = {},
153
- defaultValue: string | undefined = undefined
154
- ): string {
155
- let paramString = Object.entries(params)
156
- .map(
157
- ([name, value]) =>
158
- `${this.escapeParam(name)}:${this.escapeParam(value as string)}`
159
- )
160
- .join(',');
161
- paramString = paramString.length ? `:${paramString}` : '';
162
-
163
- const defaultString =
164
- defaultValue !== undefined ? `,${this.escapeParam(defaultValue)}` : '';
165
-
166
- return `${this.properties.config.inputPrefix}${this.escapeParam(
167
- key
168
- )}${defaultString}${paramString}${this.properties.config.inputSuffix}`;
169
- }
170
-
171
- private getTranslatedWithMetadata(
172
- text: string
173
- ): TranslatedWithMetadataTags<any> {
174
- const { key, params, defaultValue } = Coder.parseUnwrapped(text);
175
- const translated = this.textService.instant(
176
- key,
177
- params,
178
- undefined,
179
- false,
180
- defaultValue
181
- );
182
- return { translated, key, params, defaultValue };
183
- }
184
-
185
- private readonly escapeForRegExp = (string: string) => {
186
- return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
187
- };
188
-
189
- private readonly escapeParam = (param: any) => {
190
- if (typeof param === 'string') {
191
- return param.replace(/[,:\\]/gs, '\\$&');
192
- }
193
- if (typeof param === 'number' || typeof param === 'bigint') {
194
- return param.toString();
195
- }
196
- // eslint-disable-next-line no-console
197
- console.warn(
198
- `Parameters of type "${typeof param}" are not supported in "text" wrapper mode.`
199
- );
200
- return param;
201
- };
202
- }
@@ -1,185 +0,0 @@
1
- jest.dontMock('./ContentHandler');
2
- jest.dontMock('./TextWrapper');
3
- jest.dontMock('../NodeHandler');
4
- jest.dontMock('../../services/EventService');
5
- jest.dontMock('../../helpers/NodeHelper.ts');
6
- jest.dontMock('../../services/DependencyService');
7
-
8
- import { ContentHandler } from './ContentHandler';
9
- import '@testing-library/jest-dom/extend-expect';
10
- import { ElementWithMeta, NodeWithMeta, Unwrapped } from '../../types';
11
- import { NodeHelper } from '../../helpers/NodeHelper';
12
- import { createTestDom } from '@testFixtures/createTestDom';
13
- import { getMockedInstance } from '@testFixtures/mocked';
14
- import { ElementRegistrar } from '../../services/ElementRegistrar';
15
- import { Properties } from '../../Properties';
16
- import { DependencyService } from '../../services/DependencyService';
17
- import { Coder } from './Coder';
18
- import { TextWrapper } from './TextWrapper';
19
-
20
- describe('TextHandler', () => {
21
- let textHandler: ContentHandler;
22
-
23
- const mockedKeys = [
24
- {
25
- key: 'dummyKey',
26
- params: { dummyParam: 'dummyValue' },
27
- },
28
- ];
29
-
30
- const mockedTranslateInner = (text) => {
31
- return {
32
- text: text.replace(/{{(.*?)}}/gs, 'translated $1'),
33
- keys: mockedKeys,
34
- } as Unwrapped;
35
- };
36
-
37
- const gv = (key) => mockedTranslateInner(key).text;
38
- const mockedTranslate = jest.fn(mockedTranslateInner);
39
- let c: ReturnType<typeof createTestDom>;
40
- beforeEach(() => {
41
- const dependencyService = new DependencyService();
42
- dependencyService.init({});
43
-
44
- // @ts-ignore
45
- textHandler = (dependencyService.wrapper as TextWrapper).textHandler;
46
- getMockedInstance(Properties).config = {
47
- inputPrefix: '{{',
48
- inputSuffix: '}}',
49
- restrictedElements: [],
50
- tagAttributes: {
51
- '*': ['aria-label'],
52
- },
53
- passToParent: ['option'],
54
- };
55
- c = createTestDom(document);
56
- getMockedInstance(Coder).unwrap = (...args) => mockedTranslate(...args);
57
- });
58
-
59
- afterEach(async () => {
60
- jest.clearAllMocks();
61
- });
62
-
63
- describe('in production mode', () => {
64
- beforeEach(async () => {
65
- await textHandler.handle(document.body);
66
- });
67
-
68
- test('Can be created', () => {
69
- expect(textHandler).not.toBeUndefined();
70
- });
71
-
72
- test('will handle text in root', async () => {
73
- expect(`./text()[contains(., ${gv(c.keyInRoot)})]`).toBeFoundIn(
74
- document.body
75
- );
76
- });
77
-
78
- test('will handle text in div', async () => {
79
- expect(
80
- `./div/text()[contains(., 'Some trash... translated ${gv(
81
- c.keyInRootDiv
82
- )}')]`
83
- ).toBeFoundIn(document.body);
84
- });
85
-
86
- test('will handle text in div > div > span', async () => {
87
- const xpath = `./div/div/span/text()[contains(., 'translated ${gv(
88
- c.hereKey
89
- )} and translated ${gv(c.hereTooKey)}')]`;
90
- expect(xpath).toBeFoundIn(document.body);
91
- });
92
-
93
- describe("Node's _tolgee property", () => {
94
- let node: NodeWithMeta;
95
-
96
- beforeEach(() => {
97
- node = NodeHelper.evaluateToSingle(
98
- `./text()[contains(., ${gv(c.keyInRoot)})]`,
99
- document.body
100
- ) as NodeWithMeta;
101
- });
102
-
103
- test('will be defined', () => {
104
- expect(node._tolgee).toBeDefined();
105
- });
106
-
107
- test('will have proper oldTextContent', () => {
108
- expect(node._tolgee.oldTextContent).toContain(`{{${c.keyInRoot}}}`);
109
- });
110
-
111
- test('will have proper keys length', () => {
112
- expect(node._tolgee.keys).toHaveLength(1);
113
- });
114
-
115
- test('will have proper first key', () => {
116
- expect(node._tolgee.keys).toEqual(mockedKeys);
117
- });
118
- });
119
-
120
- describe("Parent element's _tolgee property and attribute", () => {
121
- let element: ElementWithMeta;
122
- let node: NodeWithMeta;
123
-
124
- beforeEach(() => {
125
- node = NodeHelper.evaluateToSingle(
126
- `./text()[contains(., ${gv(c.keyInRoot)})]`,
127
- document.body
128
- );
129
- element = node.parentElement as any as ElementWithMeta;
130
- });
131
-
132
- test('property will be defined', () => {
133
- expect(element._tolgee).toBeDefined();
134
- });
135
-
136
- test('will contain nodes array with correct node', () => {
137
- expect(element._tolgee.nodes).toEqual(new Set([node]));
138
- });
139
-
140
- test('attribute will be set', () => {
141
- expect(element.getAttribute('_tolgee')).toEqual('');
142
- });
143
- });
144
-
145
- test("will pass option's text node to select element", () => {
146
- const xPath = `//text()[contains(., 'translated option_key')]`;
147
- const node = NodeHelper.evaluateToSingle(xPath, document.body);
148
- expect(node.parentElement.parentElement).toHaveAttribute('_tolgee', '');
149
- expect(node.parentElement).not.toHaveAttribute('_tolgee');
150
- });
151
- });
152
-
153
- test('will pass recursively', async () => {
154
- getMockedInstance(Properties).config.passToParent = ['option', 'select'];
155
- await textHandler.handle(document.body);
156
- const xPath = `//text()[contains(., 'translated option_key')]`;
157
- const node = NodeHelper.evaluateToSingle(xPath, document.body);
158
- expect(node.parentElement.parentElement.parentElement).toHaveAttribute(
159
- '_tolgee',
160
- ''
161
- );
162
- expect(node.parentElement.parentElement).not.toHaveAttribute('_tolgee');
163
- });
164
-
165
- test('will pass with function', async () => {
166
- getMockedInstance(Properties).config.passToParent = (element) =>
167
- element.tagName === 'OPTION';
168
- await textHandler.handle(document.body);
169
- const xPath = `//text()[contains(., 'translated option_key')]`;
170
- const node = NodeHelper.evaluateToSingle(xPath, document.body);
171
- expect(node.parentElement.parentElement).toHaveAttribute('_tolgee', '');
172
- expect(node.parentElement).not.toHaveAttribute('_tolgee');
173
- });
174
-
175
- test('will register the node', async () => {
176
- await textHandler.handle(document.body);
177
- const node = NodeHelper.evaluateToSingle(
178
- `./text()[contains(., ${gv(c.keyInRoot)})]`,
179
- document.body
180
- );
181
- expect(getMockedInstance(ElementRegistrar).register).toBeCalledWith(
182
- node.parentElement
183
- );
184
- });
185
- });
@@ -1,21 +0,0 @@
1
- import { NodeHelper } from '../../helpers/NodeHelper';
2
- import { Properties } from '../../Properties';
3
- import { NodeHandler } from '../NodeHandler';
4
-
5
- export class ContentHandler {
6
- constructor(
7
- private properties: Properties,
8
- private nodeHandler: NodeHandler
9
- ) {}
10
-
11
- async handle(node: Node): Promise<void> {
12
- const inputPrefix = this.properties.config.inputPrefix;
13
- const inputSuffix = this.properties.config.inputSuffix;
14
-
15
- const xPath = `./descendant-or-self::text()[contains(., '${inputPrefix}') and contains(., '${inputSuffix}')]`;
16
- const nodes = NodeHelper.evaluate(xPath, node);
17
- const filtered: Text[] = this.nodeHandler.filterRestricted(nodes as Text[]);
18
-
19
- await this.nodeHandler.handleNodes(filtered);
20
- }
21
- }