@tolgee/core 4.9.2 → 4.9.3-rc.128edeb.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 (288) hide show
  1. package/dist/tolgee.cjs.js +1275 -7024
  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.js +1404 -0
  6. package/dist/tolgee.esm.js.map +1 -0
  7. package/dist/tolgee.esm.min.mjs +1 -1
  8. package/dist/tolgee.esm.min.mjs.map +1 -1
  9. package/dist/tolgee.umd.js +1275 -7024
  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 +110 -0
  16. package/lib/Controller/Events/EventEmitter.d.ts +6 -0
  17. package/lib/Controller/Events/EventEmitterSelective.d.ts +7 -0
  18. package/lib/Controller/Events/Events.d.ts +14 -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/FormatSimple/FormatError.d.ts +7 -0
  26. package/lib/FormatSimple/FormatSimple.d.ts +3 -0
  27. package/lib/FormatSimple/formatParser.d.ts +1 -0
  28. package/lib/FormatSimple/formatter.d.ts +2 -0
  29. package/lib/Tolgee.d.ts +2 -68
  30. package/lib/TranslateParams.d.ts +2 -0
  31. package/lib/{Constants/Global.d.ts → constants.d.ts} +1 -3
  32. package/lib/helpers.d.ts +3 -0
  33. package/lib/index.d.ts +4 -8
  34. package/lib/types.d.ts +244 -84
  35. package/package.json +21 -29
  36. package/src/Controller/Cache/Cache.ts +293 -0
  37. package/src/Controller/Cache/helpers.ts +37 -0
  38. package/src/Controller/Controller.ts +310 -0
  39. package/src/Controller/Events/EventEmitter.ts +30 -0
  40. package/src/Controller/Events/EventEmitterSelective.test.ts +125 -0
  41. package/src/Controller/Events/EventEmitterSelective.ts +188 -0
  42. package/src/Controller/Events/Events.ts +66 -0
  43. package/src/Controller/Plugins/Plugins.ts +315 -0
  44. package/src/Controller/State/State.ts +175 -0
  45. package/src/Controller/State/helpers.ts +41 -0
  46. package/src/Controller/State/initObserverOptions.ts +38 -0
  47. package/src/Controller/State/initState.ts +81 -0
  48. package/src/Controller/ValueObserver.ts +26 -0
  49. package/src/FormatSimple/FormatError.ts +26 -0
  50. package/src/FormatSimple/FormatSimple.ts +13 -0
  51. package/src/FormatSimple/formatParser.ts +133 -0
  52. package/src/FormatSimple/formatter.test.ts +190 -0
  53. package/src/FormatSimple/formatter.ts +19 -0
  54. package/src/Tolgee.ts +79 -330
  55. package/src/TranslateParams.test.ts +41 -0
  56. package/src/TranslateParams.ts +51 -0
  57. package/src/__test/backend.test.ts +48 -0
  58. package/src/__test/cache.test.ts +148 -0
  59. package/src/__test/client.test.ts +48 -0
  60. package/src/__test/events.test.ts +33 -0
  61. package/src/__test/format.simple.test.ts +26 -0
  62. package/src/__test/initialization.test.ts +85 -0
  63. package/src/__test/jest-setup.ts +2 -0
  64. package/src/__test/languageDetection.test.ts +129 -0
  65. package/src/__test/languageStorage.test.ts +145 -0
  66. package/src/__test/languages.test.ts +112 -0
  67. package/src/__test/loading.test.ts +39 -0
  68. package/src/__test/namespaces.test.ts +99 -0
  69. package/src/__test/namespacesFallback.test.ts +74 -0
  70. package/src/__test/plugins.test.ts +136 -0
  71. package/src/__test/testTools.ts +7 -0
  72. package/src/{Constants/Global.ts → constants.ts} +1 -6
  73. package/src/helpers.ts +17 -0
  74. package/src/index.ts +9 -8
  75. package/src/types.ts +338 -90
  76. package/README.md +0 -45
  77. package/dist/Constants/Global.d.ts +0 -6
  78. package/dist/Constants/ModifierKey.d.ts +0 -6
  79. package/dist/Errors/ApiHttpError.d.ts +0 -5
  80. package/dist/Observer.d.ts +0 -14
  81. package/dist/Observer.test.d.ts +0 -2
  82. package/dist/Properties.d.ts +0 -17
  83. package/dist/Properties.test.d.ts +0 -1
  84. package/dist/Tolgee.d.ts +0 -68
  85. package/dist/Tolgee.test.d.ts +0 -1
  86. package/dist/TolgeeConfig.d.ts +0 -69
  87. package/dist/TolgeeConfig.test.d.ts +0 -1
  88. package/dist/__integration/FormatterIcu.test.d.ts +0 -1
  89. package/dist/__integration/FormatterMissing.d.ts +0 -1
  90. package/dist/__integration/Tolgee.test.d.ts +0 -1
  91. package/dist/__integration/TolgeeInvisible.test.d.ts +0 -1
  92. package/dist/__integration/mockTranslations.d.ts +0 -7
  93. package/dist/__integration/testConfig.d.ts +0 -9
  94. package/dist/__testFixtures/classMock.d.ts +0 -3
  95. package/dist/__testFixtures/createElement.d.ts +0 -2
  96. package/dist/__testFixtures/createTestDom.d.ts +0 -9
  97. package/dist/__testFixtures/mocked.d.ts +0 -20
  98. package/dist/__testFixtures/setupAfterEnv.d.ts +0 -8
  99. package/dist/helpers/NodeHelper.d.ts +0 -14
  100. package/dist/helpers/TextHelper.d.ts +0 -5
  101. package/dist/helpers/TextHelper.test.d.ts +0 -1
  102. package/dist/helpers/commonTypes.d.ts +0 -2
  103. package/dist/helpers/encoderPolyfill.d.ts +0 -8
  104. package/dist/helpers/secret.d.ts +0 -6
  105. package/dist/helpers/secret.test.d.ts +0 -1
  106. package/dist/helpers/sleep.d.ts +0 -1
  107. package/dist/highlighter/HighlightFunctionsInitializer.d.ts +0 -10
  108. package/dist/highlighter/HighlightFunctionsInitializer.test.d.ts +0 -1
  109. package/dist/highlighter/MouseEventHandler.d.ts +0 -29
  110. package/dist/highlighter/MouseEventHandler.test.d.ts +0 -1
  111. package/dist/highlighter/TranslationHighlighter.d.ts +0 -14
  112. package/dist/highlighter/TranslationHighlighter.test.d.ts +0 -1
  113. package/dist/index.d.ts +0 -10
  114. package/dist/internal.d.ts +0 -2
  115. package/dist/modules/IcuFormatter.d.ts +0 -2
  116. package/dist/modules/IcuFormatter.test.d.ts +0 -1
  117. package/dist/modules/index.d.ts +0 -1
  118. package/dist/services/ApiHttpService.d.ts +0 -15
  119. package/dist/services/CoreService.d.ts +0 -18
  120. package/dist/services/CoreService.test.d.ts +0 -1
  121. package/dist/services/DependencyService.d.ts +0 -39
  122. package/dist/services/DependencyService.test.d.ts +0 -1
  123. package/dist/services/ElementRegistrar.d.ts +0 -19
  124. package/dist/services/ElementRegistrar.test.d.ts +0 -1
  125. package/dist/services/EventEmitter.d.ts +0 -13
  126. package/dist/services/EventService.d.ts +0 -9
  127. package/dist/services/ModuleService.d.ts +0 -5
  128. package/dist/services/ScreenshotService.d.ts +0 -15
  129. package/dist/services/Subscription.d.ts +0 -5
  130. package/dist/services/TextService.d.ts +0 -14
  131. package/dist/services/TextService.test.d.ts +0 -1
  132. package/dist/services/TranslationService.d.ts +0 -75
  133. package/dist/services/TranslationService.test.d.ts +0 -1
  134. package/dist/services/__mocks__/CoreService.d.ts +0 -2
  135. package/dist/tolgee.esm.mjs +0 -7150
  136. package/dist/tolgee.esm.mjs.map +0 -1
  137. package/dist/toolsManager/Messages.d.ts +0 -8
  138. package/dist/toolsManager/Messages.test.d.ts +0 -1
  139. package/dist/toolsManager/PluginManager.d.ts +0 -21
  140. package/dist/toolsManager/PluginManager.test.d.ts +0 -1
  141. package/dist/types/DTOs.d.ts +0 -20
  142. package/dist/types/apiSchema.generated.d.ts +0 -6185
  143. package/dist/types.d.ts +0 -123
  144. package/dist/wrappers/AbstractWrapper.d.ts +0 -8
  145. package/dist/wrappers/NodeHandler.d.ts +0 -18
  146. package/dist/wrappers/WrappedHandler.d.ts +0 -8
  147. package/dist/wrappers/invisible/AttributeHandler.d.ts +0 -8
  148. package/dist/wrappers/invisible/Coder.d.ts +0 -7
  149. package/dist/wrappers/invisible/ContentHandler.d.ts +0 -6
  150. package/dist/wrappers/invisible/CoreHandler.d.ts +0 -10
  151. package/dist/wrappers/invisible/InvisibleWrapper.d.ts +0 -18
  152. package/dist/wrappers/invisible/ValueMemory.d.ts +0 -5
  153. package/dist/wrappers/invisible/ValueMemory.test.d.ts +0 -1
  154. package/dist/wrappers/text/AttributeHandler.d.ts +0 -8
  155. package/dist/wrappers/text/AttributeHandler.test.d.ts +0 -1
  156. package/dist/wrappers/text/Coder.d.ts +0 -15
  157. package/dist/wrappers/text/Coder.test.d.ts +0 -1
  158. package/dist/wrappers/text/ContentHandler.d.ts +0 -8
  159. package/dist/wrappers/text/ContentHandler.test.d.ts +0 -1
  160. package/dist/wrappers/text/CoreHandler.d.ts +0 -17
  161. package/dist/wrappers/text/CoreHandler.test.d.ts +0 -1
  162. package/dist/wrappers/text/TextWrapper.d.ts +0 -20
  163. package/index.js +0 -7
  164. package/lib/Constants/ModifierKey.d.ts +0 -6
  165. package/lib/Errors/ApiHttpError.d.ts +0 -5
  166. package/lib/Observer.d.ts +0 -14
  167. package/lib/Properties.d.ts +0 -17
  168. package/lib/TolgeeConfig.d.ts +0 -69
  169. package/lib/helpers/NodeHelper.d.ts +0 -14
  170. package/lib/helpers/TextHelper.d.ts +0 -5
  171. package/lib/helpers/commonTypes.d.ts +0 -2
  172. package/lib/helpers/encoderPolyfill.d.ts +0 -8
  173. package/lib/helpers/secret.d.ts +0 -6
  174. package/lib/helpers/sleep.d.ts +0 -1
  175. package/lib/highlighter/HighlightFunctionsInitializer.d.ts +0 -10
  176. package/lib/highlighter/MouseEventHandler.d.ts +0 -29
  177. package/lib/highlighter/TranslationHighlighter.d.ts +0 -14
  178. package/lib/modules/IcuFormatter.d.ts +0 -2
  179. package/lib/modules/index.d.ts +0 -1
  180. package/lib/services/ApiHttpService.d.ts +0 -15
  181. package/lib/services/CoreService.d.ts +0 -18
  182. package/lib/services/DependencyService.d.ts +0 -39
  183. package/lib/services/ElementRegistrar.d.ts +0 -19
  184. package/lib/services/EventEmitter.d.ts +0 -13
  185. package/lib/services/EventService.d.ts +0 -9
  186. package/lib/services/ModuleService.d.ts +0 -5
  187. package/lib/services/ScreenshotService.d.ts +0 -15
  188. package/lib/services/Subscription.d.ts +0 -5
  189. package/lib/services/TextService.d.ts +0 -14
  190. package/lib/services/TranslationService.d.ts +0 -75
  191. package/lib/toolsManager/Messages.d.ts +0 -8
  192. package/lib/toolsManager/PluginManager.d.ts +0 -21
  193. package/lib/types/DTOs.d.ts +0 -20
  194. package/lib/types/apiSchema.generated.d.ts +0 -6185
  195. package/lib/wrappers/AbstractWrapper.d.ts +0 -8
  196. package/lib/wrappers/NodeHandler.d.ts +0 -18
  197. package/lib/wrappers/WrappedHandler.d.ts +0 -8
  198. package/lib/wrappers/invisible/AttributeHandler.d.ts +0 -8
  199. package/lib/wrappers/invisible/Coder.d.ts +0 -7
  200. package/lib/wrappers/invisible/ContentHandler.d.ts +0 -6
  201. package/lib/wrappers/invisible/CoreHandler.d.ts +0 -10
  202. package/lib/wrappers/invisible/InvisibleWrapper.d.ts +0 -18
  203. package/lib/wrappers/invisible/ValueMemory.d.ts +0 -5
  204. package/lib/wrappers/text/AttributeHandler.d.ts +0 -8
  205. package/lib/wrappers/text/Coder.d.ts +0 -15
  206. package/lib/wrappers/text/ContentHandler.d.ts +0 -8
  207. package/lib/wrappers/text/CoreHandler.d.ts +0 -17
  208. package/lib/wrappers/text/TextWrapper.d.ts +0 -20
  209. package/src/Constants/ModifierKey.ts +0 -6
  210. package/src/Errors/ApiHttpError.ts +0 -8
  211. package/src/Observer.test.ts +0 -119
  212. package/src/Observer.ts +0 -68
  213. package/src/Properties.test.ts +0 -150
  214. package/src/Properties.ts +0 -112
  215. package/src/Tolgee.test.ts +0 -473
  216. package/src/TolgeeConfig.test.ts +0 -21
  217. package/src/TolgeeConfig.ts +0 -134
  218. package/src/__integration/FormatterIcu.test.ts +0 -80
  219. package/src/__integration/FormatterMissing.ts +0 -54
  220. package/src/__integration/Tolgee.test.ts +0 -90
  221. package/src/__integration/TolgeeInvisible.test.ts +0 -145
  222. package/src/__integration/mockTranslations.ts +0 -6
  223. package/src/__integration/testConfig.ts +0 -16
  224. package/src/__testFixtures/classMock.ts +0 -11
  225. package/src/__testFixtures/createElement.ts +0 -43
  226. package/src/__testFixtures/createTestDom.ts +0 -26
  227. package/src/__testFixtures/mocked.ts +0 -25
  228. package/src/__testFixtures/setupAfterEnv.ts +0 -34
  229. package/src/helpers/NodeHelper.ts +0 -90
  230. package/src/helpers/TextHelper.test.ts +0 -62
  231. package/src/helpers/TextHelper.ts +0 -58
  232. package/src/helpers/commonTypes.ts +0 -8
  233. package/src/helpers/encoderPolyfill.ts +0 -96
  234. package/src/helpers/secret.test.ts +0 -61
  235. package/src/helpers/secret.ts +0 -68
  236. package/src/helpers/sleep.ts +0 -2
  237. package/src/highlighter/HighlightFunctionsInitializer.test.ts +0 -40
  238. package/src/highlighter/HighlightFunctionsInitializer.ts +0 -61
  239. package/src/highlighter/MouseEventHandler.test.ts +0 -151
  240. package/src/highlighter/MouseEventHandler.ts +0 -191
  241. package/src/highlighter/TranslationHighlighter.test.ts +0 -177
  242. package/src/highlighter/TranslationHighlighter.ts +0 -113
  243. package/src/internal.ts +0 -2
  244. package/src/modules/IcuFormatter.test.ts +0 -21
  245. package/src/modules/IcuFormatter.ts +0 -39
  246. package/src/modules/index.ts +0 -1
  247. package/src/services/ApiHttpService.ts +0 -85
  248. package/src/services/CoreService.test.ts +0 -141
  249. package/src/services/CoreService.ts +0 -76
  250. package/src/services/DependencyService.test.ts +0 -51
  251. package/src/services/DependencyService.ts +0 -116
  252. package/src/services/ElementRegistrar.test.ts +0 -131
  253. package/src/services/ElementRegistrar.ts +0 -108
  254. package/src/services/EventEmitter.ts +0 -52
  255. package/src/services/EventService.ts +0 -14
  256. package/src/services/ModuleService.ts +0 -14
  257. package/src/services/ScreenshotService.ts +0 -31
  258. package/src/services/Subscription.ts +0 -7
  259. package/src/services/TextService.test.ts +0 -88
  260. package/src/services/TextService.ts +0 -82
  261. package/src/services/TranslationService.test.ts +0 -358
  262. package/src/services/TranslationService.ts +0 -417
  263. package/src/services/__mocks__/CoreService.ts +0 -17
  264. package/src/toolsManager/Messages.test.ts +0 -79
  265. package/src/toolsManager/Messages.ts +0 -60
  266. package/src/toolsManager/PluginManager.test.ts +0 -108
  267. package/src/toolsManager/PluginManager.ts +0 -129
  268. package/src/types/DTOs.ts +0 -25
  269. package/src/types/apiSchema.generated.ts +0 -6208
  270. package/src/wrappers/AbstractWrapper.ts +0 -14
  271. package/src/wrappers/NodeHandler.ts +0 -143
  272. package/src/wrappers/WrappedHandler.ts +0 -28
  273. package/src/wrappers/invisible/AttributeHandler.ts +0 -23
  274. package/src/wrappers/invisible/Coder.ts +0 -65
  275. package/src/wrappers/invisible/ContentHandler.ts +0 -15
  276. package/src/wrappers/invisible/CoreHandler.ts +0 -17
  277. package/src/wrappers/invisible/InvisibleWrapper.ts +0 -59
  278. package/src/wrappers/invisible/ValueMemory.test.ts +0 -25
  279. package/src/wrappers/invisible/ValueMemory.ts +0 -16
  280. package/src/wrappers/text/AttributeHandler.test.ts +0 -118
  281. package/src/wrappers/text/AttributeHandler.ts +0 -25
  282. package/src/wrappers/text/Coder.test.ts +0 -298
  283. package/src/wrappers/text/Coder.ts +0 -202
  284. package/src/wrappers/text/ContentHandler.test.ts +0 -185
  285. package/src/wrappers/text/ContentHandler.ts +0 -21
  286. package/src/wrappers/text/CoreHandler.test.ts +0 -106
  287. package/src/wrappers/text/CoreHandler.ts +0 -45
  288. 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
- }