@transferwise/components 46.155.1 → 46.157.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 (264) hide show
  1. package/build/Calendar/Calendar.constants.js +10 -0
  2. package/build/Calendar/Calendar.constants.js.map +1 -0
  3. package/build/Calendar/Calendar.constants.mjs +8 -0
  4. package/build/Calendar/Calendar.constants.mjs.map +1 -0
  5. package/build/Calendar/Calendar.js +121 -17
  6. package/build/Calendar/Calendar.js.map +1 -1
  7. package/build/Calendar/Calendar.messages.js +12 -0
  8. package/build/Calendar/Calendar.messages.js.map +1 -1
  9. package/build/Calendar/Calendar.messages.mjs +12 -0
  10. package/build/Calendar/Calendar.messages.mjs.map +1 -1
  11. package/build/Calendar/Calendar.mjs +122 -18
  12. package/build/Calendar/Calendar.mjs.map +1 -1
  13. package/build/Calendar/DateHeader/DateHeader.js +5 -1
  14. package/build/Calendar/DateHeader/DateHeader.js.map +1 -1
  15. package/build/Calendar/DateHeader/DateHeader.mjs +5 -1
  16. package/build/Calendar/DateHeader/DateHeader.mjs.map +1 -1
  17. package/build/Calendar/DayCalendar/DayCalendar.js +20 -2
  18. package/build/Calendar/DayCalendar/DayCalendar.js.map +1 -1
  19. package/build/Calendar/DayCalendar/DayCalendar.mjs +20 -2
  20. package/build/Calendar/DayCalendar/DayCalendar.mjs.map +1 -1
  21. package/build/Calendar/DayCalendar/Table/DayCalendarTable.js +60 -19
  22. package/build/Calendar/DayCalendar/Table/DayCalendarTable.js.map +1 -1
  23. package/build/Calendar/DayCalendar/Table/DayCalendarTable.mjs +60 -19
  24. package/build/Calendar/DayCalendar/Table/DayCalendarTable.mjs.map +1 -1
  25. package/build/Calendar/MonthCalendar/MonthCalendar.js +6 -0
  26. package/build/Calendar/MonthCalendar/MonthCalendar.js.map +1 -1
  27. package/build/Calendar/MonthCalendar/MonthCalendar.mjs +6 -0
  28. package/build/Calendar/MonthCalendar/MonthCalendar.mjs.map +1 -1
  29. package/build/Calendar/MonthCalendar/Table/MonthCalendarTable.js +8 -3
  30. package/build/Calendar/MonthCalendar/Table/MonthCalendarTable.js.map +1 -1
  31. package/build/Calendar/MonthCalendar/Table/MonthCalendarTable.mjs +8 -3
  32. package/build/Calendar/MonthCalendar/Table/MonthCalendarTable.mjs.map +1 -1
  33. package/build/Calendar/TableLink/TableLink.js +95 -0
  34. package/build/Calendar/TableLink/TableLink.js.map +1 -0
  35. package/build/Calendar/TableLink/TableLink.mjs +91 -0
  36. package/build/Calendar/TableLink/TableLink.mjs.map +1 -0
  37. package/build/Calendar/YearCalendar/Table/YearCalendarTable.js +8 -3
  38. package/build/Calendar/YearCalendar/Table/YearCalendarTable.js.map +1 -1
  39. package/build/Calendar/YearCalendar/Table/YearCalendarTable.mjs +8 -3
  40. package/build/Calendar/YearCalendar/Table/YearCalendarTable.mjs.map +1 -1
  41. package/build/Calendar/YearCalendar/YearCalendar.js +7 -0
  42. package/build/Calendar/YearCalendar/YearCalendar.js.map +1 -1
  43. package/build/Calendar/YearCalendar/YearCalendar.mjs +7 -0
  44. package/build/Calendar/YearCalendar/YearCalendar.mjs.map +1 -1
  45. package/build/Calendar/_helpers/clampToDayViewBoundary/clampToDayViewBoundary.js +24 -0
  46. package/build/Calendar/_helpers/clampToDayViewBoundary/clampToDayViewBoundary.js.map +1 -0
  47. package/build/Calendar/_helpers/clampToDayViewBoundary/clampToDayViewBoundary.mjs +22 -0
  48. package/build/Calendar/_helpers/clampToDayViewBoundary/clampToDayViewBoundary.mjs.map +1 -0
  49. package/build/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.js +33 -0
  50. package/build/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.js.map +1 -0
  51. package/build/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.mjs +31 -0
  52. package/build/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.mjs.map +1 -0
  53. package/build/Calendar/_helpers/isDateDisabled/isDateDisabled.js +40 -0
  54. package/build/Calendar/_helpers/isDateDisabled/isDateDisabled.js.map +1 -0
  55. package/build/Calendar/_helpers/isDateDisabled/isDateDisabled.mjs +38 -0
  56. package/build/Calendar/_helpers/isDateDisabled/isDateDisabled.mjs.map +1 -0
  57. package/build/DateLookup/DateLookup.js +20 -9
  58. package/build/DateLookup/DateLookup.js.map +1 -1
  59. package/build/DateLookup/DateLookup.mjs +20 -9
  60. package/build/DateLookup/DateLookup.mjs.map +1 -1
  61. package/build/ExpressiveMoneyInput/CurrencySelector/CurrencySelector.js +3 -5
  62. package/build/ExpressiveMoneyInput/CurrencySelector/CurrencySelector.js.map +1 -1
  63. package/build/ExpressiveMoneyInput/CurrencySelector/CurrencySelector.mjs +3 -5
  64. package/build/ExpressiveMoneyInput/CurrencySelector/CurrencySelector.mjs.map +1 -1
  65. package/build/Inputs/SelectInput/TriggerButton/SelectInputTriggerButton.js.map +1 -1
  66. package/build/Inputs/SelectInput/TriggerButton/SelectInputTriggerButton.mjs.map +1 -1
  67. package/build/Markup/Markup.js +138 -0
  68. package/build/Markup/Markup.js.map +1 -0
  69. package/build/Markup/Markup.mjs +133 -0
  70. package/build/Markup/Markup.mjs.map +1 -0
  71. package/build/Markup/constants.js +21 -0
  72. package/build/Markup/constants.js.map +1 -0
  73. package/build/Markup/constants.mjs +15 -0
  74. package/build/Markup/constants.mjs.map +1 -0
  75. package/build/Markup/utils/parseMarkup.js +263 -0
  76. package/build/Markup/utils/parseMarkup.js.map +1 -0
  77. package/build/Markup/utils/parseMarkup.mjs +258 -0
  78. package/build/Markup/utils/parseMarkup.mjs.map +1 -0
  79. package/build/Markup/utils/sanitise.js +111 -0
  80. package/build/Markup/utils/sanitise.js.map +1 -0
  81. package/build/Markup/utils/sanitise.mjs +108 -0
  82. package/build/Markup/utils/sanitise.mjs.map +1 -0
  83. package/build/Typeahead/Typeahead.js +53 -27
  84. package/build/Typeahead/Typeahead.js.map +1 -1
  85. package/build/Typeahead/Typeahead.messages.js +3 -0
  86. package/build/Typeahead/Typeahead.messages.js.map +1 -1
  87. package/build/Typeahead/Typeahead.messages.mjs +3 -0
  88. package/build/Typeahead/Typeahead.messages.mjs.map +1 -1
  89. package/build/Typeahead/Typeahead.mjs +53 -27
  90. package/build/Typeahead/Typeahead.mjs.map +1 -1
  91. package/build/Typeahead/TypeaheadInput/TypeaheadInput.js +2 -2
  92. package/build/Typeahead/TypeaheadInput/TypeaheadInput.js.map +1 -1
  93. package/build/Typeahead/TypeaheadInput/TypeaheadInput.mjs +2 -2
  94. package/build/Typeahead/TypeaheadInput/TypeaheadInput.mjs.map +1 -1
  95. package/build/Typeahead/TypeaheadOption/TypeaheadOption.js +3 -1
  96. package/build/Typeahead/TypeaheadOption/TypeaheadOption.js.map +1 -1
  97. package/build/Typeahead/TypeaheadOption/TypeaheadOption.mjs +3 -1
  98. package/build/Typeahead/TypeaheadOption/TypeaheadOption.mjs.map +1 -1
  99. package/build/i18n/en.json +5 -0
  100. package/build/i18n/en.json.js +5 -0
  101. package/build/i18n/en.json.js.map +1 -1
  102. package/build/i18n/en.json.mjs +5 -0
  103. package/build/i18n/en.json.mjs.map +1 -1
  104. package/build/i18n/hu.json +1 -1
  105. package/build/i18n/hu.json.js +1 -1
  106. package/build/i18n/hu.json.mjs +1 -1
  107. package/build/index.js +4 -2
  108. package/build/index.js.map +1 -1
  109. package/build/index.mjs +2 -1
  110. package/build/index.mjs.map +1 -1
  111. package/build/main.css +168 -43
  112. package/build/styles/Calendar/Calendar.css +112 -37
  113. package/build/styles/Calendar/TableLink/TableLink.css +115 -0
  114. package/build/styles/Markup/Markup.css +28 -0
  115. package/build/styles/Markup/_stories/Vulnerability/Vulnerability.css +69 -0
  116. package/build/styles/Typeahead/Typeahead.css +4 -0
  117. package/build/styles/Typeahead/TypeaheadOption/TypeaheadOption.css +5 -0
  118. package/build/styles/main.css +168 -43
  119. package/build/types/Calendar/Calendar.constants.d.ts +2 -0
  120. package/build/types/Calendar/Calendar.constants.d.ts.map +1 -0
  121. package/build/types/Calendar/Calendar.d.ts +16 -3
  122. package/build/types/Calendar/Calendar.d.ts.map +1 -1
  123. package/build/types/Calendar/Calendar.messages.d.ts +20 -0
  124. package/build/types/Calendar/Calendar.messages.d.ts.map +1 -1
  125. package/build/types/Calendar/Calendar.types.d.ts +32 -0
  126. package/build/types/Calendar/Calendar.types.d.ts.map +1 -0
  127. package/build/types/Calendar/DateHeader/DateHeader.d.ts +2 -0
  128. package/build/types/Calendar/DateHeader/DateHeader.d.ts.map +1 -1
  129. package/build/types/Calendar/DayCalendar/DayCalendar.d.ts +7 -1
  130. package/build/types/Calendar/DayCalendar/DayCalendar.d.ts.map +1 -1
  131. package/build/types/Calendar/DayCalendar/Table/DayCalendarTable.d.ts +6 -0
  132. package/build/types/Calendar/DayCalendar/Table/DayCalendarTable.d.ts.map +1 -1
  133. package/build/types/Calendar/MonthCalendar/MonthCalendar.d.ts +1 -0
  134. package/build/types/Calendar/MonthCalendar/MonthCalendar.d.ts.map +1 -1
  135. package/build/types/Calendar/MonthCalendar/Table/MonthCalendarTable.d.ts +2 -1
  136. package/build/types/Calendar/MonthCalendar/Table/MonthCalendarTable.d.ts.map +1 -1
  137. package/build/types/Calendar/{tableLink → TableLink}/TableLink.d.ts +5 -1
  138. package/build/types/Calendar/TableLink/TableLink.d.ts.map +1 -0
  139. package/build/types/Calendar/TableLink/index.d.ts.map +1 -0
  140. package/build/types/Calendar/YearCalendar/Table/YearCalendarTable.d.ts +2 -1
  141. package/build/types/Calendar/YearCalendar/Table/YearCalendarTable.d.ts.map +1 -1
  142. package/build/types/Calendar/YearCalendar/YearCalendar.d.ts +1 -0
  143. package/build/types/Calendar/YearCalendar/YearCalendar.d.ts.map +1 -1
  144. package/build/types/Calendar/_helpers/clampToDayViewBoundary/clampToDayViewBoundary.d.ts +8 -0
  145. package/build/types/Calendar/_helpers/clampToDayViewBoundary/clampToDayViewBoundary.d.ts.map +1 -0
  146. package/build/types/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.d.ts +3 -0
  147. package/build/types/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.d.ts.map +1 -0
  148. package/build/types/Calendar/_helpers/index.d.ts +4 -0
  149. package/build/types/Calendar/_helpers/index.d.ts.map +1 -0
  150. package/build/types/Calendar/_helpers/isDateDisabled/isDateDisabled.d.ts +3 -0
  151. package/build/types/Calendar/_helpers/isDateDisabled/isDateDisabled.d.ts.map +1 -0
  152. package/build/types/Calendar/index.d.ts +1 -0
  153. package/build/types/Calendar/index.d.ts.map +1 -1
  154. package/build/types/DateLookup/DateLookup.d.ts +24 -5
  155. package/build/types/DateLookup/DateLookup.d.ts.map +1 -1
  156. package/build/types/DateLookup/index.d.ts +1 -0
  157. package/build/types/DateLookup/index.d.ts.map +1 -1
  158. package/build/types/ExpressiveMoneyInput/CurrencySelector/CurrencySelector.d.ts +6 -4
  159. package/build/types/ExpressiveMoneyInput/CurrencySelector/CurrencySelector.d.ts.map +1 -1
  160. package/build/types/Inputs/SelectInput/TriggerButton/SelectInputTriggerButton.d.ts +1 -1
  161. package/build/types/Inputs/SelectInput/TriggerButton/SelectInputTriggerButton.d.ts.map +1 -1
  162. package/build/types/Markup/Markup.d.ts +18 -0
  163. package/build/types/Markup/Markup.d.ts.map +1 -0
  164. package/build/types/Markup/Markup.types.d.ts +77 -0
  165. package/build/types/Markup/Markup.types.d.ts.map +1 -0
  166. package/build/types/Markup/_stories/Vulnerability/Vulnerability.d.ts +12 -0
  167. package/build/types/Markup/_stories/Vulnerability/Vulnerability.d.ts.map +1 -0
  168. package/build/types/Markup/constants.d.ts +6 -0
  169. package/build/types/Markup/constants.d.ts.map +1 -0
  170. package/build/types/Markup/index.d.ts +3 -0
  171. package/build/types/Markup/index.d.ts.map +1 -0
  172. package/build/types/Markup/utils/parseMarkup.d.ts +8 -0
  173. package/build/types/Markup/utils/parseMarkup.d.ts.map +1 -0
  174. package/build/types/Markup/utils/sanitise.d.ts +4 -0
  175. package/build/types/Markup/utils/sanitise.d.ts.map +1 -0
  176. package/build/types/Typeahead/Typeahead.d.ts.map +1 -1
  177. package/build/types/Typeahead/Typeahead.messages.d.ts +5 -0
  178. package/build/types/Typeahead/Typeahead.messages.d.ts.map +1 -1
  179. package/build/types/Typeahead/TypeaheadInput/TypeaheadInput.d.ts.map +1 -1
  180. package/build/types/Typeahead/TypeaheadOption/TypeaheadOption.d.ts +1 -0
  181. package/build/types/Typeahead/TypeaheadOption/TypeaheadOption.d.ts.map +1 -1
  182. package/build/types/index.d.ts +5 -2
  183. package/build/types/index.d.ts.map +1 -1
  184. package/build/types/test-utils/index.d.ts +10 -0
  185. package/build/types/test-utils/index.d.ts.map +1 -1
  186. package/package.json +1 -1
  187. package/src/Calendar/Calendar.constants.ts +5 -0
  188. package/src/Calendar/Calendar.css +112 -37
  189. package/src/Calendar/Calendar.disabledDates.test.tsx +610 -0
  190. package/src/Calendar/Calendar.less +5 -35
  191. package/src/Calendar/Calendar.messages.ts +21 -0
  192. package/src/Calendar/Calendar.test.tsx +342 -32
  193. package/src/Calendar/Calendar.tsx +169 -19
  194. package/src/Calendar/Calendar.types.ts +35 -0
  195. package/src/Calendar/DateHeader/DateHeader.tsx +6 -0
  196. package/src/Calendar/DayCalendar/DayCalendar.tsx +39 -1
  197. package/src/Calendar/DayCalendar/Table/DayCalendarTable.tsx +67 -27
  198. package/src/Calendar/MonthCalendar/MonthCalendar.tsx +8 -0
  199. package/src/Calendar/MonthCalendar/Table/MonthCalendarTable.tsx +15 -3
  200. package/src/Calendar/TableLink/TableLink.css +115 -0
  201. package/src/Calendar/TableLink/TableLink.less +68 -0
  202. package/src/Calendar/TableLink/TableLink.tsx +117 -0
  203. package/src/Calendar/YearCalendar/Table/YearCalendarTable.tsx +15 -3
  204. package/src/Calendar/YearCalendar/YearCalendar.tsx +9 -0
  205. package/src/Calendar/_helpers/clampToDayViewBoundary/clampToDayViewBoundary.test.ts +90 -0
  206. package/src/Calendar/_helpers/clampToDayViewBoundary/clampToDayViewBoundary.ts +25 -0
  207. package/src/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.test.ts +98 -0
  208. package/src/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.ts +45 -0
  209. package/src/Calendar/_helpers/index.ts +3 -0
  210. package/src/Calendar/_helpers/isDateDisabled/isDateDisabled.test.ts +169 -0
  211. package/src/Calendar/_helpers/isDateDisabled/isDateDisabled.ts +59 -0
  212. package/src/Calendar/_stories/Calendar.accessibility.docs.mdx +118 -0
  213. package/src/Calendar/_stories/Calendar.story.tsx +144 -4
  214. package/src/Calendar/_stories/Calendar.test.story.tsx +114 -0
  215. package/src/Calendar/index.ts +7 -0
  216. package/src/DateLookup/DateLookup.disabledDates.test.tsx +312 -0
  217. package/src/DateLookup/DateLookup.test.tsx +34 -37
  218. package/src/DateLookup/DateLookup.tsx +37 -13
  219. package/src/DateLookup/_stories/DateLookup.accessibility.docs.mdx +108 -0
  220. package/src/DateLookup/_stories/DateLookup.story.tsx +126 -2
  221. package/src/DateLookup/index.ts +6 -0
  222. package/src/ExpressiveMoneyInput/CurrencySelector/CurrencySelector.tsx +12 -13
  223. package/src/Inputs/SelectInput/SelectInput.test.tsx +37 -0
  224. package/src/Inputs/SelectInput/TriggerButton/SelectInputTriggerButton.tsx +1 -1
  225. package/src/Markup/Markup.css +28 -0
  226. package/src/Markup/Markup.injection.test.tsx +163 -0
  227. package/src/Markup/Markup.less +29 -0
  228. package/src/Markup/Markup.test.tsx +502 -0
  229. package/src/Markup/Markup.tsx +175 -0
  230. package/src/Markup/Markup.types.ts +62 -0
  231. package/src/Markup/_stories/Markup.docs.mdx +75 -0
  232. package/src/Markup/_stories/Markup.security.docs.mdx +343 -0
  233. package/src/Markup/_stories/Markup.story.tsx +266 -0
  234. package/src/Markup/_stories/Vulnerability/Vulnerability.css +69 -0
  235. package/src/Markup/_stories/Vulnerability/Vulnerability.tsx +30 -0
  236. package/src/Markup/constants.ts +23 -0
  237. package/src/Markup/index.ts +2 -0
  238. package/src/Markup/utils/parseMarkup.test.ts +706 -0
  239. package/src/Markup/utils/parseMarkup.ts +265 -0
  240. package/src/Markup/utils/sanitise.test.ts +499 -0
  241. package/src/Markup/utils/sanitise.ts +129 -0
  242. package/src/Typeahead/Typeahead.css +4 -0
  243. package/src/Typeahead/Typeahead.less +2 -0
  244. package/src/Typeahead/Typeahead.messages.ts +5 -0
  245. package/src/Typeahead/Typeahead.test.tsx +80 -3
  246. package/src/Typeahead/Typeahead.tsx +50 -27
  247. package/src/Typeahead/TypeaheadInput/TypeaheadInput.tsx +2 -6
  248. package/src/Typeahead/TypeaheadOption/TypeaheadOption.css +5 -0
  249. package/src/Typeahead/TypeaheadOption/TypeaheadOption.less +9 -0
  250. package/src/Typeahead/TypeaheadOption/TypeaheadOption.tsx +4 -2
  251. package/src/i18n/en.json +4 -0
  252. package/src/i18n/hu.json +1 -1
  253. package/src/index.ts +5 -2
  254. package/src/main.css +168 -43
  255. package/src/main.less +1 -0
  256. package/build/Calendar/tableLink/TableLink.js +0 -61
  257. package/build/Calendar/tableLink/TableLink.js.map +0 -1
  258. package/build/Calendar/tableLink/TableLink.mjs +0 -57
  259. package/build/Calendar/tableLink/TableLink.mjs.map +0 -1
  260. package/build/types/Calendar/tableLink/TableLink.d.ts.map +0 -1
  261. package/build/types/Calendar/tableLink/index.d.ts.map +0 -1
  262. package/src/Calendar/tableLink/TableLink.tsx +0 -77
  263. /package/build/types/Calendar/{tableLink → TableLink}/index.d.ts +0 -0
  264. /package/src/Calendar/{tableLink → TableLink}/index.ts +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parseMarkup.js","sources":["../../../src/Markup/utils/parseMarkup.ts"],"sourcesContent":["import { ALLOWED_TAGS, ALLOWED_TARGETS, MAX_NESTING_DEPTH, TAG_ALIASES } from '../constants';\nimport { MarkupNode } from '../Markup.types';\n\nexport { MAX_NESTING_DEPTH };\nexport const MAX_INPUT_LENGTH = 10_000;\nexport const MAX_ACCESSIBILITY_LABEL_LENGTH = 250;\n\nconst OBJECT_PROTOTYPE_KEYS = new Set(Object.getOwnPropertyNames(Object.prototype));\n\n// eslint-disable-next-line regexp/no-control-character\nconst CONTROL_CHARS = /[\\x00-\\x1f\\x7f-\\x9f]/g;\nconst HTML_METACHAR = /[<>\"']/g;\n\n/**\n * Matches opening and closing tags from the ALLOWED_TAGS allow-list.\n *\n * Capture groups:\n * 1. `/` if this is a closing tag, undefined otherwise\n * 2. Tag name\n * 3. Raw attribute string between the tag name and `>` (empty for closing tags)\n *\n * The lookahead `(?=[\\s>/])` after the tag name ensures we don't match partial\n * words — e.g. `<stronger>` won't be matched as `<strong>`.\n *\n * Global flag (`g`) allows iterative matching via `exec()`.\n */\nconst TAG_REGEX = new RegExp(\n `<(\\\\/)?(${ALLOWED_TAGS.join('|')})(?=[\\\\s>/])((?:[^>\"']*(?:\"[^\"]*\"|'[^']*'))*[^>\"']*)>`,\n 'g',\n);\n\ntype Token =\n | { type: 'text'; content: string }\n | { type: 'newline' }\n | { type: 'open'; tag: string; attrs: string }\n | { type: 'close'; tag: string };\n\n/**\n * Splits the input string into a flat list of tokens by scanning for known tags.\n * Text between tags is emitted as text/newline tokens. Unrecognised tags are never\n * tokenised — they remain part of the surrounding text content.\n */\nfunction tokenise(input: string): Token[] {\n const tokens: Token[] = [];\n let lastIndex = 0;\n\n TAG_REGEX.lastIndex = 0;\n let match = TAG_REGEX.exec(input);\n\n while (match !== null) {\n if (match.index > lastIndex) {\n pushTextWithNewlines(tokens, input.slice(lastIndex, match.index));\n }\n\n const isClose = match[1] === '/';\n const tag = match[2];\n const attrs = match[3] || '';\n\n if (isClose) {\n tokens.push({ type: 'close', tag });\n } else {\n tokens.push({ type: 'open', tag, attrs });\n }\n\n lastIndex = TAG_REGEX.lastIndex;\n match = TAG_REGEX.exec(input);\n }\n\n if (lastIndex < input.length) {\n pushTextWithNewlines(tokens, input.slice(lastIndex));\n }\n\n return tokens;\n}\n\n/** Splits a text fragment on newline characters, interleaving newline tokens between parts. */\nfunction pushTextWithNewlines(tokens: Token[], text: string): void {\n const parts = text.split('\\n');\n for (let index = 0; index < parts.length; index += 1) {\n if (index > 0) {\n tokens.push({ type: 'newline' });\n }\n if (parts[index]) {\n tokens.push({ type: 'text', content: parts[index] });\n }\n }\n}\n\ntype ParseResult = { closed: true; nodes: MarkupNode[] } | { closed: false; nodes: MarkupNode[] };\n\n/**\n * Recursive descent parser that consumes tokens and builds a MarkupNode tree.\n *\n * When `untilTag` is provided, the function collects nodes until it finds a matching\n * close token — returning `{ closed: true }`. If it exhausts all tokens without finding\n * the close, it returns `{ closed: false }` so the caller can fall back to plain text\n * (graceful degradation for malformed input).\n *\n * The shared `position` object acts as a cursor across recursive calls, avoiding the\n * need to slice or copy the token array.\n */\nfunction parse(\n tokens: Token[],\n position: { index: number },\n untilTag?: string,\n depth = 0,\n): ParseResult {\n const nodes: MarkupNode[] = [];\n\n while (position.index < tokens.length) {\n const token = tokens[position.index];\n\n if (token.type === 'text') {\n nodes.push({ type: 'text', content: token.content });\n position.index += 1;\n } else if (token.type === 'newline') {\n nodes.push({ type: 'newline' });\n position.index += 1;\n } else if (token.type === 'close') {\n if (token.tag === untilTag) {\n position.index += 1;\n return { closed: true, nodes };\n }\n // Mismatched close tag — render as literal text\n nodes.push({ type: 'text', content: `</${token.tag}>` });\n position.index += 1;\n } else if (token.type === 'open') {\n if (depth >= MAX_NESTING_DEPTH) {\n nodes.push({ type: 'text', content: `<${token.tag}${token.attrs}>` });\n position.index += 1;\n } else {\n position.index += 1;\n const result = parse(tokens, position, token.tag, depth + 1);\n\n if (result.closed) {\n const node = buildNode(token.tag, token.attrs, result.nodes);\n nodes.push(node);\n } else {\n nodes.push({ type: 'text', content: `<${token.tag}${token.attrs}>` });\n nodes.push(...result.nodes);\n }\n }\n }\n }\n\n if (untilTag) {\n return { closed: false, nodes };\n }\n\n return { closed: true, nodes };\n}\n\n/**\n * Maps a raw tag name to a typed MarkupNode. Resolves aliases first (p→paragraph, a→link),\n * then builds the appropriate node shape. Link nodes carry full attributes; other element\n * nodes extract only accessibilityLabel.\n */\nfunction buildNode(rawTag: string, attrs: string, children: MarkupNode[]): MarkupNode {\n const tag = TAG_ALIASES[rawTag] || rawTag;\n\n if (tag === 'link') {\n return { type: 'link', children, ...parseAttributes(attrs) };\n }\n\n if (tag === 'paragraph') {\n return { type: 'paragraph', children };\n }\n\n const { accessibilityLabel } = parseAttributes(attrs);\n\n return {\n type: tag as MarkupNode['type'],\n children,\n ...(accessibilityLabel && { accessibilityLabel }),\n } as MarkupNode;\n}\n\n/**\n * Matches allow-listed attributes from the raw attribute string captured by TAG_REGEX.\n * Requires whitespace before each attribute name to prevent matching inside quoted values.\n *\n * Example: for input ` href=\"https://wise.com\" target=\"_blank\"`\n * - Match 1: groups = ['href', 'https://wise.com', undefined]\n * - Match 2: groups = ['target', '_blank', undefined]\n *\n * Capture groups:\n * 1. Attribute name (href, action, target, or accessibilityLabel)\n * 2. Double-quoted value (if present)\n * 3. Single-quoted value (if present)\n */\nconst ATTR_REGEX = /(?<=\\s)(href|action|target|accessibilityLabel)=(?:\"([^\"]*)\"|'([^']*)')/g;\n\ntype ParsedAttributes = {\n href?: string;\n action?: string;\n target?: string;\n accessibilityLabel?: string;\n};\n\n/**\n * Extracts allowed attributes from a raw attribute string.\n * Supports both double- and single-quoted values. Any attribute not in the\n * allow-list (href, action, target, accessibilityLabel) is silently discarded.\n * First occurrence wins — duplicates are ignored.\n */\nfunction parseAttributes(attrs: string): ParsedAttributes {\n const result: ParsedAttributes = {};\n ATTR_REGEX.lastIndex = 0;\n let match = ATTR_REGEX.exec(attrs);\n\n while (match !== null) {\n const key = match[1] as 'href' | 'action' | 'target' | 'accessibilityLabel';\n const value = match[2] ?? match[3];\n\n if (!(key in result)) {\n const sanitized = sanitizeAttributeValue(key, value);\n if (sanitized !== undefined) {\n result[key] = sanitized;\n }\n }\n\n match = ATTR_REGEX.exec(attrs);\n }\n\n return result;\n}\n\nfunction sanitizeAttributeValue(\n key: 'href' | 'action' | 'target' | 'accessibilityLabel',\n value: string,\n): string | undefined {\n switch (key) {\n case 'href':\n return value.replace(CONTROL_CHARS, '');\n case 'action':\n if (OBJECT_PROTOTYPE_KEYS.has(value)) {\n return undefined;\n }\n return value;\n case 'target':\n if (!ALLOWED_TARGETS.includes(value)) {\n return undefined;\n }\n return value;\n case 'accessibilityLabel':\n return value.replace(HTML_METACHAR, '').slice(0, MAX_ACCESSIBILITY_LABEL_LENGTH);\n default:\n return undefined;\n }\n}\n\n/** Entry point: tokenises the input string, then parses the token stream into a MarkupNode tree. */\nexport function parseMarkup(input: string): MarkupNode[] {\n if (!input) {\n return [];\n }\n\n if (input.length > MAX_INPUT_LENGTH) {\n return [{ type: 'text', content: input }];\n }\n\n const tokens = tokenise(input);\n const position = { index: 0 };\n return parse(tokens, position).nodes;\n}\n"],"names":["MAX_INPUT_LENGTH","MAX_ACCESSIBILITY_LABEL_LENGTH","OBJECT_PROTOTYPE_KEYS","Set","Object","getOwnPropertyNames","prototype","CONTROL_CHARS","HTML_METACHAR","TAG_REGEX","RegExp","ALLOWED_TAGS","join","tokenise","input","tokens","lastIndex","match","exec","index","pushTextWithNewlines","slice","isClose","tag","attrs","push","type","length","text","parts","split","content","parse","position","untilTag","depth","nodes","token","closed","MAX_NESTING_DEPTH","result","node","buildNode","rawTag","children","TAG_ALIASES","parseAttributes","accessibilityLabel","ATTR_REGEX","key","value","sanitized","sanitizeAttributeValue","undefined","replace","has","ALLOWED_TARGETS","includes","parseMarkup"],"mappings":";;;;AAIO,MAAMA,gBAAgB,GAAG;AACzB,MAAMC,8BAA8B,GAAG;AAE9C,MAAMC,qBAAqB,GAAG,IAAIC,GAAG,CAACC,MAAM,CAACC,mBAAmB,CAACD,MAAM,CAACE,SAAS,CAAC,CAAC;AAEnF;AACA,MAAMC,aAAa,GAAG,uBAAuB;AAC7C,MAAMC,aAAa,GAAG,SAAS;AAE/B;;;;;;;;;;;;AAYG;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AACvB,MAAA,OAAOA,KAAK,CAACI,OAAO,CAAC9C,aAAa,EAAE,EAAE,CAAC,CAACa,KAAK,CAAC,CAAC,EAAEpB,8BAA8B,CAAC;AAClF,IAAA;AACE,MAAA,OAAOoD,SAAS;AACpB;AACF;AAEA;AACM,SAAUK,WAAWA,CAAC5C,KAAa,EAAA;EACvC,IAAI,CAACA,KAAK,EAAE;AACV,IAAA,OAAO,EAAE;AACX,EAAA;AAEA,EAAA,IAAIA,KAAK,CAACa,MAAM,GAAG3B,gBAAgB,EAAE;AACnC,IAAA,OAAO,CAAC;AAAE0B,MAAAA,IAAI,EAAE,MAAM;AAAEK,MAAAA,OAAO,EAAEjB;AAAK,KAAE,CAAC;AAC3C,EAAA;AAEA,EAAA,MAAMC,MAAM,GAAGF,QAAQ,CAACC,KAAK,CAAC;AAC9B,EAAA,MAAMmB,QAAQ,GAAG;AAAEd,IAAAA,KAAK,EAAE;GAAG;AAC7B,EAAA,OAAOa,KAAK,CAACjB,MAAM,EAAEkB,QAAQ,CAAC,CAACG,KAAK;AACtC;;;;;;;"}
@@ -0,0 +1,258 @@
1
+ import { MAX_NESTING_DEPTH, ALLOWED_TAGS, TAG_ALIASES, ALLOWED_TARGETS } from '../constants.mjs';
2
+
3
+ const MAX_INPUT_LENGTH = 10_000;
4
+ const MAX_ACCESSIBILITY_LABEL_LENGTH = 250;
5
+ const OBJECT_PROTOTYPE_KEYS = new Set(Object.getOwnPropertyNames(Object.prototype));
6
+ // eslint-disable-next-line regexp/no-control-character
7
+ const CONTROL_CHARS = /[\x00-\x1f\x7f-\x9f]/g;
8
+ const HTML_METACHAR = /[<>"']/g;
9
+ /**
10
+ * Matches opening and closing tags from the ALLOWED_TAGS allow-list.
11
+ *
12
+ * Capture groups:
13
+ * 1. `/` if this is a closing tag, undefined otherwise
14
+ * 2. Tag name
15
+ * 3. Raw attribute string between the tag name and `>` (empty for closing tags)
16
+ *
17
+ * The lookahead `(?=[\s>/])` after the tag name ensures we don't match partial
18
+ * words — e.g. `<stronger>` won't be matched as `<strong>`.
19
+ *
20
+ * Global flag (`g`) allows iterative matching via `exec()`.
21
+ */
22
+ const TAG_REGEX = new RegExp(`<(\\/)?(${ALLOWED_TAGS.join('|')})(?=[\\s>/])((?:[^>"']*(?:"[^"]*"|'[^']*'))*[^>"']*)>`, 'g');
23
+ /**
24
+ * Splits the input string into a flat list of tokens by scanning for known tags.
25
+ * Text between tags is emitted as text/newline tokens. Unrecognised tags are never
26
+ * tokenised — they remain part of the surrounding text content.
27
+ */
28
+ function tokenise(input) {
29
+ const tokens = [];
30
+ let lastIndex = 0;
31
+ TAG_REGEX.lastIndex = 0;
32
+ let match = TAG_REGEX.exec(input);
33
+ while (match !== null) {
34
+ if (match.index > lastIndex) {
35
+ pushTextWithNewlines(tokens, input.slice(lastIndex, match.index));
36
+ }
37
+ const isClose = match[1] === '/';
38
+ const tag = match[2];
39
+ const attrs = match[3] || '';
40
+ if (isClose) {
41
+ tokens.push({
42
+ type: 'close',
43
+ tag
44
+ });
45
+ } else {
46
+ tokens.push({
47
+ type: 'open',
48
+ tag,
49
+ attrs
50
+ });
51
+ }
52
+ lastIndex = TAG_REGEX.lastIndex;
53
+ match = TAG_REGEX.exec(input);
54
+ }
55
+ if (lastIndex < input.length) {
56
+ pushTextWithNewlines(tokens, input.slice(lastIndex));
57
+ }
58
+ return tokens;
59
+ }
60
+ /** Splits a text fragment on newline characters, interleaving newline tokens between parts. */
61
+ function pushTextWithNewlines(tokens, text) {
62
+ const parts = text.split('\n');
63
+ for (let index = 0; index < parts.length; index += 1) {
64
+ if (index > 0) {
65
+ tokens.push({
66
+ type: 'newline'
67
+ });
68
+ }
69
+ if (parts[index]) {
70
+ tokens.push({
71
+ type: 'text',
72
+ content: parts[index]
73
+ });
74
+ }
75
+ }
76
+ }
77
+ /**
78
+ * Recursive descent parser that consumes tokens and builds a MarkupNode tree.
79
+ *
80
+ * When `untilTag` is provided, the function collects nodes until it finds a matching
81
+ * close token — returning `{ closed: true }`. If it exhausts all tokens without finding
82
+ * the close, it returns `{ closed: false }` so the caller can fall back to plain text
83
+ * (graceful degradation for malformed input).
84
+ *
85
+ * The shared `position` object acts as a cursor across recursive calls, avoiding the
86
+ * need to slice or copy the token array.
87
+ */
88
+ function parse(tokens, position, untilTag, depth = 0) {
89
+ const nodes = [];
90
+ while (position.index < tokens.length) {
91
+ const token = tokens[position.index];
92
+ if (token.type === 'text') {
93
+ nodes.push({
94
+ type: 'text',
95
+ content: token.content
96
+ });
97
+ position.index += 1;
98
+ } else if (token.type === 'newline') {
99
+ nodes.push({
100
+ type: 'newline'
101
+ });
102
+ position.index += 1;
103
+ } else if (token.type === 'close') {
104
+ if (token.tag === untilTag) {
105
+ position.index += 1;
106
+ return {
107
+ closed: true,
108
+ nodes
109
+ };
110
+ }
111
+ // Mismatched close tag — render as literal text
112
+ nodes.push({
113
+ type: 'text',
114
+ content: `</${token.tag}>`
115
+ });
116
+ position.index += 1;
117
+ } else if (token.type === 'open') {
118
+ if (depth >= MAX_NESTING_DEPTH) {
119
+ nodes.push({
120
+ type: 'text',
121
+ content: `<${token.tag}${token.attrs}>`
122
+ });
123
+ position.index += 1;
124
+ } else {
125
+ position.index += 1;
126
+ const result = parse(tokens, position, token.tag, depth + 1);
127
+ if (result.closed) {
128
+ const node = buildNode(token.tag, token.attrs, result.nodes);
129
+ nodes.push(node);
130
+ } else {
131
+ nodes.push({
132
+ type: 'text',
133
+ content: `<${token.tag}${token.attrs}>`
134
+ });
135
+ nodes.push(...result.nodes);
136
+ }
137
+ }
138
+ }
139
+ }
140
+ if (untilTag) {
141
+ return {
142
+ closed: false,
143
+ nodes
144
+ };
145
+ }
146
+ return {
147
+ closed: true,
148
+ nodes
149
+ };
150
+ }
151
+ /**
152
+ * Maps a raw tag name to a typed MarkupNode. Resolves aliases first (p→paragraph, a→link),
153
+ * then builds the appropriate node shape. Link nodes carry full attributes; other element
154
+ * nodes extract only accessibilityLabel.
155
+ */
156
+ function buildNode(rawTag, attrs, children) {
157
+ const tag = TAG_ALIASES[rawTag] || rawTag;
158
+ if (tag === 'link') {
159
+ return {
160
+ type: 'link',
161
+ children,
162
+ ...parseAttributes(attrs)
163
+ };
164
+ }
165
+ if (tag === 'paragraph') {
166
+ return {
167
+ type: 'paragraph',
168
+ children
169
+ };
170
+ }
171
+ const {
172
+ accessibilityLabel
173
+ } = parseAttributes(attrs);
174
+ return {
175
+ type: tag,
176
+ children,
177
+ ...(accessibilityLabel && {
178
+ accessibilityLabel
179
+ })
180
+ };
181
+ }
182
+ /**
183
+ * Matches allow-listed attributes from the raw attribute string captured by TAG_REGEX.
184
+ * Requires whitespace before each attribute name to prevent matching inside quoted values.
185
+ *
186
+ * Example: for input ` href="https://wise.com" target="_blank"`
187
+ * - Match 1: groups = ['href', 'https://wise.com', undefined]
188
+ * - Match 2: groups = ['target', '_blank', undefined]
189
+ *
190
+ * Capture groups:
191
+ * 1. Attribute name (href, action, target, or accessibilityLabel)
192
+ * 2. Double-quoted value (if present)
193
+ * 3. Single-quoted value (if present)
194
+ */
195
+ const ATTR_REGEX = /(?<=\s)(href|action|target|accessibilityLabel)=(?:"([^"]*)"|'([^']*)')/g;
196
+ /**
197
+ * Extracts allowed attributes from a raw attribute string.
198
+ * Supports both double- and single-quoted values. Any attribute not in the
199
+ * allow-list (href, action, target, accessibilityLabel) is silently discarded.
200
+ * First occurrence wins — duplicates are ignored.
201
+ */
202
+ function parseAttributes(attrs) {
203
+ const result = {};
204
+ ATTR_REGEX.lastIndex = 0;
205
+ let match = ATTR_REGEX.exec(attrs);
206
+ while (match !== null) {
207
+ const key = match[1];
208
+ const value = match[2] ?? match[3];
209
+ if (!(key in result)) {
210
+ const sanitized = sanitizeAttributeValue(key, value);
211
+ if (sanitized !== undefined) {
212
+ result[key] = sanitized;
213
+ }
214
+ }
215
+ match = ATTR_REGEX.exec(attrs);
216
+ }
217
+ return result;
218
+ }
219
+ function sanitizeAttributeValue(key, value) {
220
+ switch (key) {
221
+ case 'href':
222
+ return value.replace(CONTROL_CHARS, '');
223
+ case 'action':
224
+ if (OBJECT_PROTOTYPE_KEYS.has(value)) {
225
+ return undefined;
226
+ }
227
+ return value;
228
+ case 'target':
229
+ if (!ALLOWED_TARGETS.includes(value)) {
230
+ return undefined;
231
+ }
232
+ return value;
233
+ case 'accessibilityLabel':
234
+ return value.replace(HTML_METACHAR, '').slice(0, MAX_ACCESSIBILITY_LABEL_LENGTH);
235
+ default:
236
+ return undefined;
237
+ }
238
+ }
239
+ /** Entry point: tokenises the input string, then parses the token stream into a MarkupNode tree. */
240
+ function parseMarkup(input) {
241
+ if (!input) {
242
+ return [];
243
+ }
244
+ if (input.length > MAX_INPUT_LENGTH) {
245
+ return [{
246
+ type: 'text',
247
+ content: input
248
+ }];
249
+ }
250
+ const tokens = tokenise(input);
251
+ const position = {
252
+ index: 0
253
+ };
254
+ return parse(tokens, position).nodes;
255
+ }
256
+
257
+ export { MAX_ACCESSIBILITY_LABEL_LENGTH, MAX_INPUT_LENGTH, MAX_NESTING_DEPTH, parseMarkup };
258
+ //# sourceMappingURL=parseMarkup.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parseMarkup.mjs","sources":["../../../src/Markup/utils/parseMarkup.ts"],"sourcesContent":["import { ALLOWED_TAGS, ALLOWED_TARGETS, MAX_NESTING_DEPTH, TAG_ALIASES } from '../constants';\nimport { MarkupNode } from '../Markup.types';\n\nexport { MAX_NESTING_DEPTH };\nexport const MAX_INPUT_LENGTH = 10_000;\nexport const MAX_ACCESSIBILITY_LABEL_LENGTH = 250;\n\nconst OBJECT_PROTOTYPE_KEYS = new Set(Object.getOwnPropertyNames(Object.prototype));\n\n// eslint-disable-next-line regexp/no-control-character\nconst CONTROL_CHARS = /[\\x00-\\x1f\\x7f-\\x9f]/g;\nconst HTML_METACHAR = /[<>\"']/g;\n\n/**\n * Matches opening and closing tags from the ALLOWED_TAGS allow-list.\n *\n * Capture groups:\n * 1. `/` if this is a closing tag, undefined otherwise\n * 2. Tag name\n * 3. Raw attribute string between the tag name and `>` (empty for closing tags)\n *\n * The lookahead `(?=[\\s>/])` after the tag name ensures we don't match partial\n * words — e.g. `<stronger>` won't be matched as `<strong>`.\n *\n * Global flag (`g`) allows iterative matching via `exec()`.\n */\nconst TAG_REGEX = new RegExp(\n `<(\\\\/)?(${ALLOWED_TAGS.join('|')})(?=[\\\\s>/])((?:[^>\"']*(?:\"[^\"]*\"|'[^']*'))*[^>\"']*)>`,\n 'g',\n);\n\ntype Token =\n | { type: 'text'; content: string }\n | { type: 'newline' }\n | { type: 'open'; tag: string; attrs: string }\n | { type: 'close'; tag: string };\n\n/**\n * Splits the input string into a flat list of tokens by scanning for known tags.\n * Text between tags is emitted as text/newline tokens. Unrecognised tags are never\n * tokenised — they remain part of the surrounding text content.\n */\nfunction tokenise(input: string): Token[] {\n const tokens: Token[] = [];\n let lastIndex = 0;\n\n TAG_REGEX.lastIndex = 0;\n let match = TAG_REGEX.exec(input);\n\n while (match !== null) {\n if (match.index > lastIndex) {\n pushTextWithNewlines(tokens, input.slice(lastIndex, match.index));\n }\n\n const isClose = match[1] === '/';\n const tag = match[2];\n const attrs = match[3] || '';\n\n if (isClose) {\n tokens.push({ type: 'close', tag });\n } else {\n tokens.push({ type: 'open', tag, attrs });\n }\n\n lastIndex = TAG_REGEX.lastIndex;\n match = TAG_REGEX.exec(input);\n }\n\n if (lastIndex < input.length) {\n pushTextWithNewlines(tokens, input.slice(lastIndex));\n }\n\n return tokens;\n}\n\n/** Splits a text fragment on newline characters, interleaving newline tokens between parts. */\nfunction pushTextWithNewlines(tokens: Token[], text: string): void {\n const parts = text.split('\\n');\n for (let index = 0; index < parts.length; index += 1) {\n if (index > 0) {\n tokens.push({ type: 'newline' });\n }\n if (parts[index]) {\n tokens.push({ type: 'text', content: parts[index] });\n }\n }\n}\n\ntype ParseResult = { closed: true; nodes: MarkupNode[] } | { closed: false; nodes: MarkupNode[] };\n\n/**\n * Recursive descent parser that consumes tokens and builds a MarkupNode tree.\n *\n * When `untilTag` is provided, the function collects nodes until it finds a matching\n * close token — returning `{ closed: true }`. If it exhausts all tokens without finding\n * the close, it returns `{ closed: false }` so the caller can fall back to plain text\n * (graceful degradation for malformed input).\n *\n * The shared `position` object acts as a cursor across recursive calls, avoiding the\n * need to slice or copy the token array.\n */\nfunction parse(\n tokens: Token[],\n position: { index: number },\n untilTag?: string,\n depth = 0,\n): ParseResult {\n const nodes: MarkupNode[] = [];\n\n while (position.index < tokens.length) {\n const token = tokens[position.index];\n\n if (token.type === 'text') {\n nodes.push({ type: 'text', content: token.content });\n position.index += 1;\n } else if (token.type === 'newline') {\n nodes.push({ type: 'newline' });\n position.index += 1;\n } else if (token.type === 'close') {\n if (token.tag === untilTag) {\n position.index += 1;\n return { closed: true, nodes };\n }\n // Mismatched close tag — render as literal text\n nodes.push({ type: 'text', content: `</${token.tag}>` });\n position.index += 1;\n } else if (token.type === 'open') {\n if (depth >= MAX_NESTING_DEPTH) {\n nodes.push({ type: 'text', content: `<${token.tag}${token.attrs}>` });\n position.index += 1;\n } else {\n position.index += 1;\n const result = parse(tokens, position, token.tag, depth + 1);\n\n if (result.closed) {\n const node = buildNode(token.tag, token.attrs, result.nodes);\n nodes.push(node);\n } else {\n nodes.push({ type: 'text', content: `<${token.tag}${token.attrs}>` });\n nodes.push(...result.nodes);\n }\n }\n }\n }\n\n if (untilTag) {\n return { closed: false, nodes };\n }\n\n return { closed: true, nodes };\n}\n\n/**\n * Maps a raw tag name to a typed MarkupNode. Resolves aliases first (p→paragraph, a→link),\n * then builds the appropriate node shape. Link nodes carry full attributes; other element\n * nodes extract only accessibilityLabel.\n */\nfunction buildNode(rawTag: string, attrs: string, children: MarkupNode[]): MarkupNode {\n const tag = TAG_ALIASES[rawTag] || rawTag;\n\n if (tag === 'link') {\n return { type: 'link', children, ...parseAttributes(attrs) };\n }\n\n if (tag === 'paragraph') {\n return { type: 'paragraph', children };\n }\n\n const { accessibilityLabel } = parseAttributes(attrs);\n\n return {\n type: tag as MarkupNode['type'],\n children,\n ...(accessibilityLabel && { accessibilityLabel }),\n } as MarkupNode;\n}\n\n/**\n * Matches allow-listed attributes from the raw attribute string captured by TAG_REGEX.\n * Requires whitespace before each attribute name to prevent matching inside quoted values.\n *\n * Example: for input ` href=\"https://wise.com\" target=\"_blank\"`\n * - Match 1: groups = ['href', 'https://wise.com', undefined]\n * - Match 2: groups = ['target', '_blank', undefined]\n *\n * Capture groups:\n * 1. Attribute name (href, action, target, or accessibilityLabel)\n * 2. Double-quoted value (if present)\n * 3. Single-quoted value (if present)\n */\nconst ATTR_REGEX = /(?<=\\s)(href|action|target|accessibilityLabel)=(?:\"([^\"]*)\"|'([^']*)')/g;\n\ntype ParsedAttributes = {\n href?: string;\n action?: string;\n target?: string;\n accessibilityLabel?: string;\n};\n\n/**\n * Extracts allowed attributes from a raw attribute string.\n * Supports both double- and single-quoted values. Any attribute not in the\n * allow-list (href, action, target, accessibilityLabel) is silently discarded.\n * First occurrence wins — duplicates are ignored.\n */\nfunction parseAttributes(attrs: string): ParsedAttributes {\n const result: ParsedAttributes = {};\n ATTR_REGEX.lastIndex = 0;\n let match = ATTR_REGEX.exec(attrs);\n\n while (match !== null) {\n const key = match[1] as 'href' | 'action' | 'target' | 'accessibilityLabel';\n const value = match[2] ?? match[3];\n\n if (!(key in result)) {\n const sanitized = sanitizeAttributeValue(key, value);\n if (sanitized !== undefined) {\n result[key] = sanitized;\n }\n }\n\n match = ATTR_REGEX.exec(attrs);\n }\n\n return result;\n}\n\nfunction sanitizeAttributeValue(\n key: 'href' | 'action' | 'target' | 'accessibilityLabel',\n value: string,\n): string | undefined {\n switch (key) {\n case 'href':\n return value.replace(CONTROL_CHARS, '');\n case 'action':\n if (OBJECT_PROTOTYPE_KEYS.has(value)) {\n return undefined;\n }\n return value;\n case 'target':\n if (!ALLOWED_TARGETS.includes(value)) {\n return undefined;\n }\n return value;\n case 'accessibilityLabel':\n return value.replace(HTML_METACHAR, '').slice(0, MAX_ACCESSIBILITY_LABEL_LENGTH);\n default:\n return undefined;\n }\n}\n\n/** Entry point: tokenises the input string, then parses the token stream into a MarkupNode tree. */\nexport function parseMarkup(input: string): MarkupNode[] {\n if (!input) {\n return [];\n }\n\n if (input.length > MAX_INPUT_LENGTH) {\n return [{ type: 'text', content: input }];\n }\n\n const tokens = tokenise(input);\n const position = { index: 0 };\n return parse(tokens, position).nodes;\n}\n"],"names":["MAX_INPUT_LENGTH","MAX_ACCESSIBILITY_LABEL_LENGTH","OBJECT_PROTOTYPE_KEYS","Set","Object","getOwnPropertyNames","prototype","CONTROL_CHARS","HTML_METACHAR","TAG_REGEX","RegExp","ALLOWED_TAGS","join","tokenise","input","tokens","lastIndex","match","exec","index","pushTextWithNewlines","slice","isClose","tag","attrs","push","type","length","text","parts","split","content","parse","position","untilTag","depth","nodes","token","closed","MAX_NESTING_DEPTH","result","node","buildNode","rawTag","children","TAG_ALIASES","parseAttributes","accessibilityLabel","ATTR_REGEX","key","value","sanitized","sanitizeAttributeValue","undefined","replace","has","ALLOWED_TARGETS","includes","parseMarkup"],"mappings":";;AAIO,MAAMA,gBAAgB,GAAG;AACzB,MAAMC,8BAA8B,GAAG;AAE9C,MAAMC,qBAAqB,GAAG,IAAIC,GAAG,CAACC,MAAM,CAACC,mBAAmB,CAACD,MAAM,CAACE,SAAS,CAAC,CAAC;AAEnF;AACA,MAAMC,aAAa,GAAG,uBAAuB;AAC7C,MAAMC,aAAa,GAAG,SAAS;AAE/B;;;;;;;;;;;;AAYG;AACH,MAAMC,SAAS,GAAG,IAAIC,MAAM,CAC1B,CAAA,QAAA,EAAWC,YAAY,CAACC,IAAI,CAAC,GAAG,CAAC,CAAA,qDAAA,CAAuD,EACxF,GAAG,CACJ;AAQD;;;;AAIG;AACH,SAASC,QAAQA,CAACC,KAAa,EAAA;EAC7B,MAAMC,MAAM,GAAY,EAAE;EAC1B,IAAIC,SAAS,GAAG,CAAC;EAEjBP,SAAS,CAACO,SAAS,GAAG,CAAC;AACvB,EAAA,IAAIC,KAAK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BAAgB,EAAE;AACnC,IAAA,OAAO,CAAC;AAAE0B,MAAAA,IAAI,EAAE,MAAM;AAAEK,MAAAA,OAAO,EAAEjB;AAAK,KAAE,CAAC;AAC3C,EAAA;AAEA,EAAA,MAAMC,MAAM,GAAGF,QAAQ,CAACC,KAAK,CAAC;AAC9B,EAAA,MAAMmB,QAAQ,GAAG;AAAEd,IAAAA,KAAK,EAAE;GAAG;AAC7B,EAAA,OAAOa,KAAK,CAACjB,MAAM,EAAEkB,QAAQ,CAAC,CAACG,KAAK;AACtC;;;;"}
@@ -0,0 +1,111 @@
1
+ 'use strict';
2
+
3
+ var constants = require('../constants.js');
4
+
5
+ const INVISIBLE_CHARS = /[\u200b\u200c\u200d\ufeff\u00ad\u2060\u180e]/g;
6
+ // eslint-disable-next-line regexp/no-control-character
7
+ const CONTROL_CHARS = /[\u0000-\u001f\u007f-\u009f]/;
8
+ const DANGEROUS_SCHEME = /^(?:javascript|vbscript|data|jscript)\s*:/i;
9
+ // eslint-disable-next-line regexp/no-useless-non-capturing-group
10
+ const ENCODED_COLON_SCHEME = /^(?:javascript|vbscript|data|jscript)(?:%3a)/i;
11
+ const IPV6_BRACKETS = /^\[|\]$/g;
12
+ const NUMERIC_IP = /^[\d.]+$/;
13
+ const HEX_IP = /^0x[0-9a-f]+$/i;
14
+ function sanitiseHref(href) {
15
+ const cleaned = href.replace(INVISIBLE_CHARS, '');
16
+ const trimmed = cleaned.trim();
17
+ if (!trimmed) {
18
+ return undefined;
19
+ }
20
+ if (CONTROL_CHARS.test(trimmed)) {
21
+ return undefined;
22
+ }
23
+ if (DANGEROUS_SCHEME.test(trimmed)) {
24
+ return undefined;
25
+ }
26
+ if (ENCODED_COLON_SCHEME.test(trimmed)) {
27
+ return undefined;
28
+ }
29
+ if (trimmed.startsWith('//')) {
30
+ return undefined;
31
+ }
32
+ if (trimmed.startsWith('\\')) {
33
+ return undefined;
34
+ }
35
+ if (trimmed.startsWith('/')) {
36
+ return trimmed[1] !== '\\' ? trimmed : undefined;
37
+ }
38
+ if (trimmed.startsWith('#')) {
39
+ return trimmed;
40
+ }
41
+ if (trimmed.startsWith('./') || trimmed.startsWith('../')) {
42
+ return undefined;
43
+ }
44
+ try {
45
+ const url = new URL(trimmed);
46
+ if (!constants.ALLOWED_PROTOCOLS.includes(url.protocol)) {
47
+ return undefined;
48
+ }
49
+ if (url.protocol === 'https:') {
50
+ if (!trimmed.startsWith('https://')) {
51
+ return undefined;
52
+ }
53
+ if (!url.hostname) {
54
+ return undefined;
55
+ }
56
+ if (url.username || url.password) {
57
+ return undefined;
58
+ }
59
+ if (isInternalHost(url.hostname)) {
60
+ return undefined;
61
+ }
62
+ }
63
+ if (url.protocol === 'mailto:') {
64
+ if (url.search) {
65
+ return undefined;
66
+ }
67
+ if (!url.pathname) {
68
+ return undefined;
69
+ }
70
+ }
71
+ return trimmed;
72
+ } catch {
73
+ return undefined;
74
+ }
75
+ }
76
+ // Synthetic origin used to resolve relative paths via the WHATWG URL parser.
77
+ // If the resolved origin still equals this sentinel, the path stays on-origin.
78
+ const SSR_SENTINEL_ORIGIN = 'https://internal.invalid';
79
+ function isHrefAllowedByMode(href, mode) {
80
+ switch (mode) {
81
+ case 'none':
82
+ return false;
83
+ case 'all':
84
+ return true;
85
+ case 'internal':
86
+ try {
87
+ if (!href.startsWith('/')) {
88
+ const parsed = new URL(href);
89
+ return parsed.protocol === 'https:' && parsed.hostname === 'wise.com';
90
+ }
91
+ const parsed = new URL(href, SSR_SENTINEL_ORIGIN);
92
+ return parsed.origin === SSR_SENTINEL_ORIGIN;
93
+ } catch {
94
+ return false;
95
+ }
96
+ }
97
+ }
98
+ function isInternalHost(hostname) {
99
+ const stripped = hostname.replace(IPV6_BRACKETS, '');
100
+ const host = stripped.endsWith('.') ? stripped.slice(0, -1) : stripped;
101
+ if (NUMERIC_IP.test(host)) return true;
102
+ if (HEX_IP.test(host)) return true;
103
+ if (host === '::1' || host === '0:0:0:0:0:0:0:1') return true;
104
+ if (host === 'localhost') return true;
105
+ if (host.endsWith('.internal')) return true;
106
+ return false;
107
+ }
108
+
109
+ exports.isHrefAllowedByMode = isHrefAllowedByMode;
110
+ exports.sanitiseHref = sanitiseHref;
111
+ //# sourceMappingURL=sanitise.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitise.js","sources":["../../../src/Markup/utils/sanitise.ts"],"sourcesContent":["import { ALLOWED_PROTOCOLS } from '../constants';\n\nconst INVISIBLE_CHARS = /[\\u200b\\u200c\\u200d\\ufeff\\u00ad\\u2060\\u180e]/g;\n// eslint-disable-next-line regexp/no-control-character\nconst CONTROL_CHARS = /[\\u0000-\\u001f\\u007f-\\u009f]/;\nconst DANGEROUS_SCHEME = /^(?:javascript|vbscript|data|jscript)\\s*:/i;\n// eslint-disable-next-line regexp/no-useless-non-capturing-group\nconst ENCODED_COLON_SCHEME = /^(?:javascript|vbscript|data|jscript)(?:%3a)/i;\nconst IPV6_BRACKETS = /^\\[|\\]$/g;\nconst NUMERIC_IP = /^[\\d.]+$/;\nconst HEX_IP = /^0x[0-9a-f]+$/i;\n\nexport function sanitiseHref(href: string): string | undefined {\n const cleaned = href.replace(INVISIBLE_CHARS, '');\n const trimmed = cleaned.trim();\n\n if (!trimmed) {\n return undefined;\n }\n\n if (CONTROL_CHARS.test(trimmed)) {\n return undefined;\n }\n\n if (DANGEROUS_SCHEME.test(trimmed)) {\n return undefined;\n }\n\n if (ENCODED_COLON_SCHEME.test(trimmed)) {\n return undefined;\n }\n\n if (trimmed.startsWith('//')) {\n return undefined;\n }\n\n if (trimmed.startsWith('\\\\')) {\n return undefined;\n }\n\n if (trimmed.startsWith('/')) {\n return trimmed[1] !== '\\\\' ? trimmed : undefined;\n }\n\n if (trimmed.startsWith('#')) {\n return trimmed;\n }\n\n if (trimmed.startsWith('./') || trimmed.startsWith('../')) {\n return undefined;\n }\n\n try {\n const url = new URL(trimmed);\n\n if (!ALLOWED_PROTOCOLS.includes(url.protocol)) {\n return undefined;\n }\n\n if (url.protocol === 'https:') {\n if (!trimmed.startsWith('https://')) {\n return undefined;\n }\n\n if (!url.hostname) {\n return undefined;\n }\n\n if (url.username || url.password) {\n return undefined;\n }\n\n if (isInternalHost(url.hostname)) {\n return undefined;\n }\n }\n\n if (url.protocol === 'mailto:') {\n if (url.search) {\n return undefined;\n }\n\n if (!url.pathname) {\n return undefined;\n }\n }\n\n return trimmed;\n } catch {\n return undefined;\n }\n}\n\nexport type AllowLinksMode = 'internal' | 'all' | 'none';\n\n// Synthetic origin used to resolve relative paths via the WHATWG URL parser.\n// If the resolved origin still equals this sentinel, the path stays on-origin.\nconst SSR_SENTINEL_ORIGIN = 'https://internal.invalid';\n\nexport function isHrefAllowedByMode(href: string, mode: AllowLinksMode): boolean {\n switch (mode) {\n case 'none':\n return false;\n case 'all':\n return true;\n case 'internal':\n try {\n if (!href.startsWith('/')) {\n const parsed = new URL(href);\n return parsed.protocol === 'https:' && parsed.hostname === 'wise.com';\n }\n const parsed = new URL(href, SSR_SENTINEL_ORIGIN);\n return parsed.origin === SSR_SENTINEL_ORIGIN;\n } catch {\n return false;\n }\n }\n}\n\nfunction isInternalHost(hostname: string): boolean {\n const stripped = hostname.replace(IPV6_BRACKETS, '');\n const host = stripped.endsWith('.') ? stripped.slice(0, -1) : stripped;\n if (NUMERIC_IP.test(host)) return true;\n if (HEX_IP.test(host)) return true;\n if (host === '::1' || host === '0:0:0:0:0:0:0:1') return true;\n if (host === 'localhost') return true;\n if (host.endsWith('.internal')) return true;\n return false;\n}\n"],"names":["INVISIBLE_CHARS","CONTROL_CHARS","DANGEROUS_SCHEME","ENCODED_COLON_SCHEME","IPV6_BRACKETS","NUMERIC_IP","HEX_IP","sanitiseHref","href","cleaned","replace","trimmed","trim","undefined","test","startsWith","url","URL","ALLOWED_PROTOCOLS","includes","protocol","hostname","username","password","isInternalHost","search","pathname","SSR_SENTINEL_ORIGIN","isHrefAllowedByMode","mode","parsed","origin","stripped","host","endsWith","slice"],"mappings":";;;;AAEA,MAAMA,eA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@@
1
+ import { ALLOWED_PROTOCOLS } from '../constants.mjs';
2
+
3
+ const INVISIBLE_CHARS = /[\u200b\u200c\u200d\ufeff\u00ad\u2060\u180e]/g;
4
+ // eslint-disable-next-line regexp/no-control-character
5
+ const CONTROL_CHARS = /[\u0000-\u001f\u007f-\u009f]/;
6
+ const DANGEROUS_SCHEME = /^(?:javascript|vbscript|data|jscript)\s*:/i;
7
+ // eslint-disable-next-line regexp/no-useless-non-capturing-group
8
+ const ENCODED_COLON_SCHEME = /^(?:javascript|vbscript|data|jscript)(?:%3a)/i;
9
+ const IPV6_BRACKETS = /^\[|\]$/g;
10
+ const NUMERIC_IP = /^[\d.]+$/;
11
+ const HEX_IP = /^0x[0-9a-f]+$/i;
12
+ function sanitiseHref(href) {
13
+ const cleaned = href.replace(INVISIBLE_CHARS, '');
14
+ const trimmed = cleaned.trim();
15
+ if (!trimmed) {
16
+ return undefined;
17
+ }
18
+ if (CONTROL_CHARS.test(trimmed)) {
19
+ return undefined;
20
+ }
21
+ if (DANGEROUS_SCHEME.test(trimmed)) {
22
+ return undefined;
23
+ }
24
+ if (ENCODED_COLON_SCHEME.test(trimmed)) {
25
+ return undefined;
26
+ }
27
+ if (trimmed.startsWith('//')) {
28
+ return undefined;
29
+ }
30
+ if (trimmed.startsWith('\\')) {
31
+ return undefined;
32
+ }
33
+ if (trimmed.startsWith('/')) {
34
+ return trimmed[1] !== '\\' ? trimmed : undefined;
35
+ }
36
+ if (trimmed.startsWith('#')) {
37
+ return trimmed;
38
+ }
39
+ if (trimmed.startsWith('./') || trimmed.startsWith('../')) {
40
+ return undefined;
41
+ }
42
+ try {
43
+ const url = new URL(trimmed);
44
+ if (!ALLOWED_PROTOCOLS.includes(url.protocol)) {
45
+ return undefined;
46
+ }
47
+ if (url.protocol === 'https:') {
48
+ if (!trimmed.startsWith('https://')) {
49
+ return undefined;
50
+ }
51
+ if (!url.hostname) {
52
+ return undefined;
53
+ }
54
+ if (url.username || url.password) {
55
+ return undefined;
56
+ }
57
+ if (isInternalHost(url.hostname)) {
58
+ return undefined;
59
+ }
60
+ }
61
+ if (url.protocol === 'mailto:') {
62
+ if (url.search) {
63
+ return undefined;
64
+ }
65
+ if (!url.pathname) {
66
+ return undefined;
67
+ }
68
+ }
69
+ return trimmed;
70
+ } catch {
71
+ return undefined;
72
+ }
73
+ }
74
+ // Synthetic origin used to resolve relative paths via the WHATWG URL parser.
75
+ // If the resolved origin still equals this sentinel, the path stays on-origin.
76
+ const SSR_SENTINEL_ORIGIN = 'https://internal.invalid';
77
+ function isHrefAllowedByMode(href, mode) {
78
+ switch (mode) {
79
+ case 'none':
80
+ return false;
81
+ case 'all':
82
+ return true;
83
+ case 'internal':
84
+ try {
85
+ if (!href.startsWith('/')) {
86
+ const parsed = new URL(href);
87
+ return parsed.protocol === 'https:' && parsed.hostname === 'wise.com';
88
+ }
89
+ const parsed = new URL(href, SSR_SENTINEL_ORIGIN);
90
+ return parsed.origin === SSR_SENTINEL_ORIGIN;
91
+ } catch {
92
+ return false;
93
+ }
94
+ }
95
+ }
96
+ function isInternalHost(hostname) {
97
+ const stripped = hostname.replace(IPV6_BRACKETS, '');
98
+ const host = stripped.endsWith('.') ? stripped.slice(0, -1) : stripped;
99
+ if (NUMERIC_IP.test(host)) return true;
100
+ if (HEX_IP.test(host)) return true;
101
+ if (host === '::1' || host === '0:0:0:0:0:0:0:1') return true;
102
+ if (host === 'localhost') return true;
103
+ if (host.endsWith('.internal')) return true;
104
+ return false;
105
+ }
106
+
107
+ export { isHrefAllowedByMode, sanitiseHref };
108
+ //# sourceMappingURL=sanitise.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitise.mjs","sources":["../../../src/Markup/utils/sanitise.ts"],"sourcesContent":["import { ALLOWED_PROTOCOLS } from '../constants';\n\nconst INVISIBLE_CHARS = /[\\u200b\\u200c\\u200d\\ufeff\\u00ad\\u2060\\u180e]/g;\n// eslint-disable-next-line regexp/no-control-character\nconst CONTROL_CHARS = /[\\u0000-\\u001f\\u007f-\\u009f]/;\nconst DANGEROUS_SCHEME = /^(?:javascript|vbscript|data|jscript)\\s*:/i;\n// eslint-disable-next-line regexp/no-useless-non-capturing-group\nconst ENCODED_COLON_SCHEME = /^(?:javascript|vbscript|data|jscript)(?:%3a)/i;\nconst IPV6_BRACKETS = /^\\[|\\]$/g;\nconst NUMERIC_IP = /^[\\d.]+$/;\nconst HEX_IP = /^0x[0-9a-f]+$/i;\n\nexport function sanitiseHref(href: string): string | undefined {\n const cleaned = href.replace(INVISIBLE_CHARS, '');\n const trimmed = cleaned.trim();\n\n if (!trimmed) {\n return undefined;\n }\n\n if (CONTROL_CHARS.test(trimmed)) {\n return undefined;\n }\n\n if (DANGEROUS_SCHEME.test(trimmed)) {\n return undefined;\n }\n\n if (ENCODED_COLON_SCHEME.test(trimmed)) {\n return undefined;\n }\n\n if (trimmed.startsWith('//')) {\n return undefined;\n }\n\n if (trimmed.startsWith('\\\\')) {\n return undefined;\n }\n\n if (trimmed.startsWith('/')) {\n return trimmed[1] !== '\\\\' ? trimmed : undefined;\n }\n\n if (trimmed.startsWith('#')) {\n return trimmed;\n }\n\n if (trimmed.startsWith('./') || trimmed.startsWith('../')) {\n return undefined;\n }\n\n try {\n const url = new URL(trimmed);\n\n if (!ALLOWED_PROTOCOLS.includes(url.protocol)) {\n return undefined;\n }\n\n if (url.protocol === 'https:') {\n if (!trimmed.startsWith('https://')) {\n return undefined;\n }\n\n if (!url.hostname) {\n return undefined;\n }\n\n if (url.username || url.password) {\n return undefined;\n }\n\n if (isInternalHost(url.hostname)) {\n return undefined;\n }\n }\n\n if (url.protocol === 'mailto:') {\n if (url.search) {\n return undefined;\n }\n\n if (!url.pathname) {\n return undefined;\n }\n }\n\n return trimmed;\n } catch {\n return undefined;\n }\n}\n\nexport type AllowLinksMode = 'internal' | 'all' | 'none';\n\n// Synthetic origin used to resolve relative paths via the WHATWG URL parser.\n// If the resolved origin still equals this sentinel, the path stays on-origin.\nconst SSR_SENTINEL_ORIGIN = 'https://internal.invalid';\n\nexport function isHrefAllowedByMode(href: string, mode: AllowLinksMode): boolean {\n switch (mode) {\n case 'none':\n return false;\n case 'all':\n return true;\n case 'internal':\n try {\n if (!href.startsWith('/')) {\n const parsed = new URL(href);\n return parsed.protocol === 'https:' && parsed.hostname === 'wise.com';\n }\n const parsed = new URL(href, SSR_SENTINEL_ORIGIN);\n return parsed.origin === SSR_SENTINEL_ORIGIN;\n } catch {\n return false;\n }\n }\n}\n\nfunction isInternalHost(hostname: string): boolean {\n const stripped = hostname.replace(IPV6_BRACKETS, '');\n const host = stripped.endsWith('.') ? stripped.slice(0, -1) : stripped;\n if (NUMERIC_IP.test(host)) return true;\n if (HEX_IP.test(host)) return true;\n if (host === '::1' || host === '0:0:0:0:0:0:0:1') return true;\n if (host === 'localhost') return true;\n if (host.endsWith('.internal')) return true;\n return false;\n}\n"],"names":["INVISIBLE_CHARS","CONTROL_CHARS","DANGEROUS_SCHEME","ENCODED_COLON_SCHEME","IPV6_BRACKETS","NUMERIC_IP","HEX_IP","sanitiseHref","href","cleaned","replace","trimmed","trim","undefined","test","startsWith","url","URL","ALLOWED_PROTOCOLS","includes","protocol","hostname","username","password","isInternalHost","search","pathname","SSR_SENTINEL_ORIGIN","isHrefAllowedByMode","mode","parsed","origin","stripped","host","endsWith","slice"],"mappings":";;AAEA,MAAMA,eAAe,GAAG,+CAA+C;AACvE;AACA,MAAMC,aAAa,GAAG,8BAA8B;AACpD,MAAMC,gBAAgB,GAAG,4CAA4C;AACrE;AACA,MAAMC,oBAAoB,GAAG,+CAA+C;AAC5E,MAAMC,aAAa,GAAG,UAAU;AAChC,MAAMC,UAAU,GAAG,UAAU;AAC7B,MAAMC,MAAM,GAAG,gBAAgB;AAEzB,SAAUC,YAAYA,CAACC,IAAY,EAAA;EACvC,MAAMC,OAAO,GAAGD,IAAI,CAACE,OAAO,CAACV,eAAe,EAAE,EAAE,CAAC;AACjD,EAAA,MAAMW,OAAO,GAAGF,OAAO,CAACG,IA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