ssml-builder-js 1.0.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.
@@ -0,0 +1,851 @@
1
+ import {
2
+ __privateAdd,
3
+ __privateGet,
4
+ __privateSet
5
+ } from "./chunk-6S5ODO6A.mjs";
6
+
7
+ // packages/ssml-core/src/constants/ssml.ts
8
+ var SYNTHESIS_NAMESPACE = "http://www.w3.org/2001/10/synthesis";
9
+ var MSTTS_NAMESPACE = "http://www.w3.org/2001/mstts";
10
+ var DEFAULT_SSML_VERSION = "1.0";
11
+ var DEFAULT_SSML_LANGUAGE = "en-US";
12
+ var MAX_NESTING_DEPTH = 1e3;
13
+ var XML_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_.:-]*$/;
14
+ var MSTTS_TAG_PREFIX = "mstts:";
15
+ var SSML_TAGS = {
16
+ SPEAK: "speak",
17
+ VOICE: "voice",
18
+ PROSODY: "prosody",
19
+ BREAK: "break",
20
+ EXPRESS_AS: "express-as",
21
+ EXPRESS_AS_CAMEL: "expressAs",
22
+ MSTTS_EXPRESS_AS: "mstts:express-as",
23
+ SAY_AS: "say-as",
24
+ SAY_AS_CAMEL: "sayAs",
25
+ PHONEME: "phoneme",
26
+ EMPHASIS: "emphasis",
27
+ AUDIO: "audio",
28
+ SUB: "sub",
29
+ LANG: "lang",
30
+ MARK: "mark",
31
+ BOOKMARK: "bookmark",
32
+ LEXICON: "lexicon",
33
+ PARAGRAPH: "p",
34
+ SENTENCE: "s",
35
+ WORD: "w",
36
+ MSTTS_SILENCE: "mstts:silence",
37
+ SILENCE: "silence",
38
+ MSTTS_VISEME: "mstts:viseme",
39
+ VISEME: "viseme"
40
+ };
41
+ var SSML_ATTRS = {
42
+ VERSION: "version",
43
+ XMLNS: "xmlns",
44
+ XML_LANG: "xml:lang",
45
+ LANG: "lang",
46
+ MSTTS_XMLNS: "xmlns:mstts",
47
+ NAME: "name",
48
+ EFFECT: "effect",
49
+ RATE: "rate",
50
+ PITCH: "pitch",
51
+ VOLUME: "volume",
52
+ CONTOUR: "contour",
53
+ RANGE: "range",
54
+ TIME: "time",
55
+ STRENGTH: "strength",
56
+ STYLE: "style",
57
+ STYLE_DEGREE: "styledegree",
58
+ STYLE_DEGREE_CAMEL: "styleDegree",
59
+ ROLE: "role",
60
+ INTERPRET_AS: "interpret-as",
61
+ FORMAT: "format",
62
+ DETAIL: "detail",
63
+ ALPHABET: "alphabet",
64
+ PH: "ph",
65
+ LEVEL: "level",
66
+ SRC: "src",
67
+ DESC: "desc",
68
+ CLIP_BEGIN: "clipBegin",
69
+ CLIP_END: "clipEnd",
70
+ SPEED: "speed",
71
+ REPEAT_COUNT: "repeatCount",
72
+ REPEAT_DURATION: "repeatDuration",
73
+ SOUND_LEVEL: "soundLevel",
74
+ ALIAS: "alias",
75
+ MARK: "mark",
76
+ URI: "uri",
77
+ TYPE: "type",
78
+ VALUE: "value"
79
+ };
80
+
81
+ // packages/ssml-core/src/builder.ts
82
+ function escapeText(value) {
83
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
84
+ }
85
+ function escapeAttribute(value) {
86
+ return escapeText(value).replace(/"/g, "&quot;").replace(/'/g, "&apos;");
87
+ }
88
+ function addAttribute(attributes, name, value) {
89
+ if (value !== void 0) {
90
+ attributes[name] = value;
91
+ }
92
+ }
93
+ function getAttributes(element) {
94
+ const attributes = {
95
+ ...element.attributes ?? {}
96
+ };
97
+ switch (element.type) {
98
+ case SSML_TAGS.VOICE:
99
+ addAttribute(attributes, SSML_ATTRS.NAME, element.name);
100
+ addAttribute(attributes, SSML_ATTRS.EFFECT, element.effect);
101
+ break;
102
+ case SSML_TAGS.PROSODY:
103
+ addAttribute(attributes, SSML_ATTRS.RATE, element.rate);
104
+ addAttribute(attributes, SSML_ATTRS.PITCH, element.pitch);
105
+ addAttribute(attributes, SSML_ATTRS.VOLUME, element.volume);
106
+ addAttribute(attributes, SSML_ATTRS.CONTOUR, element.contour);
107
+ addAttribute(attributes, SSML_ATTRS.RANGE, element.range);
108
+ break;
109
+ case SSML_TAGS.BREAK:
110
+ addAttribute(attributes, SSML_ATTRS.TIME, element.time);
111
+ addAttribute(attributes, SSML_ATTRS.STRENGTH, element.strength);
112
+ break;
113
+ case SSML_TAGS.EXPRESS_AS:
114
+ case SSML_TAGS.EXPRESS_AS_CAMEL:
115
+ case SSML_TAGS.MSTTS_EXPRESS_AS:
116
+ addAttribute(attributes, SSML_ATTRS.STYLE, element.style);
117
+ addAttribute(attributes, SSML_ATTRS.STYLE_DEGREE, element.styleDegree);
118
+ addAttribute(attributes, SSML_ATTRS.ROLE, element.role);
119
+ break;
120
+ case SSML_TAGS.SAY_AS:
121
+ case SSML_TAGS.SAY_AS_CAMEL:
122
+ addAttribute(attributes, SSML_ATTRS.INTERPRET_AS, element.interpretAs);
123
+ addAttribute(attributes, SSML_ATTRS.FORMAT, element.format);
124
+ addAttribute(attributes, SSML_ATTRS.DETAIL, element.detail);
125
+ break;
126
+ case SSML_TAGS.PHONEME:
127
+ addAttribute(attributes, SSML_ATTRS.ALPHABET, element.alphabet);
128
+ addAttribute(attributes, SSML_ATTRS.PH, element.ph);
129
+ break;
130
+ case SSML_TAGS.EMPHASIS:
131
+ addAttribute(attributes, SSML_ATTRS.LEVEL, element.level);
132
+ break;
133
+ case SSML_TAGS.AUDIO:
134
+ addAttribute(attributes, SSML_ATTRS.SRC, element.src);
135
+ addAttribute(attributes, SSML_ATTRS.DESC, element.desc);
136
+ addAttribute(attributes, SSML_ATTRS.CLIP_BEGIN, element.clipBegin);
137
+ addAttribute(attributes, SSML_ATTRS.CLIP_END, element.clipEnd);
138
+ addAttribute(attributes, SSML_ATTRS.SPEED, element.speed);
139
+ addAttribute(attributes, SSML_ATTRS.REPEAT_COUNT, element.repeatCount);
140
+ addAttribute(attributes, SSML_ATTRS.REPEAT_DURATION, element.repeatDuration);
141
+ addAttribute(attributes, SSML_ATTRS.SOUND_LEVEL, element.soundLevel);
142
+ break;
143
+ case SSML_TAGS.SUB:
144
+ addAttribute(attributes, SSML_ATTRS.ALIAS, element.alias);
145
+ break;
146
+ case SSML_TAGS.LANG:
147
+ addAttribute(attributes, SSML_ATTRS.XML_LANG, element.lang);
148
+ break;
149
+ case SSML_TAGS.MARK:
150
+ addAttribute(attributes, SSML_ATTRS.NAME, element.name);
151
+ break;
152
+ case SSML_TAGS.BOOKMARK:
153
+ addAttribute(attributes, SSML_ATTRS.MARK, element.mark);
154
+ break;
155
+ case SSML_TAGS.LEXICON:
156
+ addAttribute(attributes, SSML_ATTRS.URI, element.uri);
157
+ break;
158
+ case SSML_TAGS.MSTTS_SILENCE:
159
+ case SSML_TAGS.SILENCE:
160
+ addAttribute(attributes, SSML_ATTRS.TYPE, element.typeValue ?? element.silenceType);
161
+ addAttribute(attributes, SSML_ATTRS.VALUE, element.value);
162
+ break;
163
+ case SSML_TAGS.MSTTS_VISEME:
164
+ case SSML_TAGS.VISEME:
165
+ addAttribute(attributes, SSML_ATTRS.TYPE, element.typeValue ?? element.visemeType);
166
+ break;
167
+ case SSML_TAGS.PARAGRAPH:
168
+ case SSML_TAGS.SENTENCE:
169
+ case SSML_TAGS.WORD:
170
+ case "element":
171
+ case "custom":
172
+ break;
173
+ }
174
+ return attributes;
175
+ }
176
+ function getTagName(element) {
177
+ switch (element.type) {
178
+ case SSML_TAGS.EXPRESS_AS:
179
+ case SSML_TAGS.EXPRESS_AS_CAMEL:
180
+ case SSML_TAGS.MSTTS_EXPRESS_AS:
181
+ return SSML_TAGS.MSTTS_EXPRESS_AS;
182
+ case SSML_TAGS.SAY_AS:
183
+ case SSML_TAGS.SAY_AS_CAMEL:
184
+ return SSML_TAGS.SAY_AS;
185
+ case SSML_TAGS.SILENCE:
186
+ case SSML_TAGS.MSTTS_SILENCE:
187
+ return SSML_TAGS.MSTTS_SILENCE;
188
+ case SSML_TAGS.VISEME:
189
+ case SSML_TAGS.MSTTS_VISEME:
190
+ return SSML_TAGS.MSTTS_VISEME;
191
+ case "element":
192
+ case "custom":
193
+ return element.name;
194
+ default:
195
+ return element.type;
196
+ }
197
+ }
198
+ function getChildren(element) {
199
+ return element.children ?? [];
200
+ }
201
+ function validateName(name, kind) {
202
+ if (!XML_NAME_PATTERN.test(name)) {
203
+ throw new Error(`Invalid XML ${kind} name: ${name}`);
204
+ }
205
+ }
206
+ function serializeAttributes(attributes) {
207
+ return Object.entries(attributes).map(([name, value]) => {
208
+ validateName(name, "attribute");
209
+ return ` ${name}="${escapeAttribute(String(value))}"`;
210
+ }).join("");
211
+ }
212
+ function serializeNode(node) {
213
+ if (typeof node === "string") {
214
+ return escapeText(node);
215
+ }
216
+ if (node.type === "text") {
217
+ return escapeText(node.value);
218
+ }
219
+ const tagName = getTagName(node);
220
+ validateName(tagName, "element");
221
+ const attributes = serializeAttributes(getAttributes(node));
222
+ const children = getChildren(node);
223
+ if (children.length === 0) {
224
+ return `<${tagName}${attributes}/>`;
225
+ }
226
+ return `<${tagName}${attributes}>${children.map(serializeNode).join("")}</${tagName}>`;
227
+ }
228
+ function usesMsttsNamespace(nodes) {
229
+ return nodes.some((node) => {
230
+ if (typeof node === "string" || node.type === "text") {
231
+ return false;
232
+ }
233
+ const tagName = getTagName(node);
234
+ return tagName.startsWith(MSTTS_TAG_PREFIX) || usesMsttsNamespace(getChildren(node));
235
+ });
236
+ }
237
+ function serializeDocument(document) {
238
+ const children = document.children ?? (document.content === void 0 ? [] : [document.content]);
239
+ const attributes = {
240
+ ...document.attributes ?? {},
241
+ [SSML_ATTRS.VERSION]: document.version,
242
+ [SSML_ATTRS.XMLNS]: SYNTHESIS_NAMESPACE,
243
+ [SSML_ATTRS.XML_LANG]: document.lang
244
+ };
245
+ if (usesMsttsNamespace(children) && attributes[SSML_ATTRS.MSTTS_XMLNS] === void 0) {
246
+ attributes[SSML_ATTRS.MSTTS_XMLNS] = MSTTS_NAMESPACE;
247
+ }
248
+ return `<${SSML_TAGS.SPEAK}${serializeAttributes(attributes)}>${children.map(serializeNode).join("")}</${SSML_TAGS.SPEAK}>`;
249
+ }
250
+ function buildSsml(documentOrContent, lang = DEFAULT_SSML_LANGUAGE) {
251
+ if (typeof documentOrContent === "string") {
252
+ return {
253
+ version: DEFAULT_SSML_VERSION,
254
+ lang,
255
+ content: documentOrContent
256
+ };
257
+ }
258
+ return serializeDocument(documentOrContent);
259
+ }
260
+
261
+ // packages/ssml-core/src/parser.ts
262
+ var XML_ENTITIES = {
263
+ amp: "&",
264
+ apos: "'",
265
+ gt: ">",
266
+ lt: "<",
267
+ quot: '"'
268
+ };
269
+ function hasOwn(object, property) {
270
+ return Object.getOwnPropertyDescriptor(object, property) !== void 0;
271
+ }
272
+ function setAttribute(attributes, name, value) {
273
+ Object.defineProperty(attributes, name, {
274
+ configurable: true,
275
+ enumerable: true,
276
+ value,
277
+ writable: true
278
+ });
279
+ }
280
+ function decodeEntity(entity) {
281
+ const namedValue = hasOwn(XML_ENTITIES, entity) ? XML_ENTITIES[entity] : void 0;
282
+ if (namedValue !== void 0) {
283
+ return namedValue;
284
+ }
285
+ const isHexadecimal = entity.startsWith("#x") || entity.startsWith("#X");
286
+ const isDecimal = entity.startsWith("#");
287
+ if (!isHexadecimal && !isDecimal) {
288
+ throw new Error(`Unknown XML entity: &${entity};`);
289
+ }
290
+ const digits = entity.slice(isHexadecimal ? 2 : 1);
291
+ const codePoint = Number.parseInt(digits, isHexadecimal ? 16 : 10);
292
+ if (!digits || !Number.isInteger(codePoint) || codePoint < 0 || codePoint > 1114111 || codePoint >= 55296 && codePoint <= 57343 || codePoint < 32 && ![9, 10, 13].includes(codePoint)) {
293
+ throw new Error(`Invalid XML character reference: &${entity};`);
294
+ }
295
+ return String.fromCodePoint(codePoint);
296
+ }
297
+ function decodeXmlEntities(value) {
298
+ let result = "";
299
+ let start = 0;
300
+ while (true) {
301
+ const ampersand = value.indexOf("&", start);
302
+ if (ampersand === -1) {
303
+ return result + value.slice(start);
304
+ }
305
+ result += value.slice(start, ampersand);
306
+ const semicolon = value.indexOf(";", ampersand + 1);
307
+ if (semicolon === -1) {
308
+ throw new Error("Unterminated XML entity reference");
309
+ }
310
+ result += decodeEntity(value.slice(ampersand + 1, semicolon));
311
+ start = semicolon + 1;
312
+ }
313
+ }
314
+ function isXmlNameStart(value) {
315
+ return value !== void 0 && /[A-Za-z_]/.test(value);
316
+ }
317
+ function isXmlNameCharacter(value) {
318
+ return value !== void 0 && /[A-Za-z0-9_.:-]/.test(value);
319
+ }
320
+ function isXmlWhitespace(value) {
321
+ return value === " " || value === " " || value === "\r" || value === "\n";
322
+ }
323
+ function removeStandardNamespaceAttributes(attributes) {
324
+ if (attributes[SSML_ATTRS.XMLNS] === SYNTHESIS_NAMESPACE) {
325
+ delete attributes[SSML_ATTRS.XMLNS];
326
+ }
327
+ if (attributes[SSML_ATTRS.MSTTS_XMLNS] === MSTTS_NAMESPACE) {
328
+ delete attributes[SSML_ATTRS.MSTTS_XMLNS];
329
+ }
330
+ }
331
+ var _index;
332
+ var XmlParser = class {
333
+ constructor(source) {
334
+ __privateAdd(this, _index, 0);
335
+ this.source = source;
336
+ }
337
+ parse() {
338
+ if (this.source.charCodeAt(0) === 65279) {
339
+ __privateSet(this, _index, __privateGet(this, _index) + 1);
340
+ }
341
+ this.skipMisc();
342
+ if (__privateGet(this, _index) >= this.source.length) {
343
+ this.fail("SSML input is empty");
344
+ }
345
+ if (this.source[__privateGet(this, _index)] !== "<") {
346
+ this.fail("SSML input must start with an XML element");
347
+ }
348
+ const root = this.parseElement(0);
349
+ this.skipMisc();
350
+ if (__privateGet(this, _index) !== this.source.length) {
351
+ this.fail("Unexpected content after the root XML element");
352
+ }
353
+ return root;
354
+ }
355
+ parseElement(depth) {
356
+ if (depth > MAX_NESTING_DEPTH) {
357
+ this.fail("XML nesting depth exceeds the supported limit");
358
+ }
359
+ this.expect("<");
360
+ if (this.source[__privateGet(this, _index)] === "/") {
361
+ this.fail("Unexpected closing XML element");
362
+ }
363
+ const name = this.parseName();
364
+ const { attributes, selfClosing } = this.parseStartTag();
365
+ if (selfClosing) {
366
+ return { name, attributes, children: [] };
367
+ }
368
+ const children = [];
369
+ while (__privateGet(this, _index) < this.source.length) {
370
+ if (this.source.startsWith("</", __privateGet(this, _index))) {
371
+ __privateSet(this, _index, __privateGet(this, _index) + 2);
372
+ const closingName = this.parseName();
373
+ this.skipWhitespace();
374
+ this.expect(">");
375
+ if (closingName !== name) {
376
+ this.fail(`Mismatched closing element: expected </${name}> but found </${closingName}>`);
377
+ }
378
+ return { name, attributes, children };
379
+ }
380
+ if (this.source.startsWith("<!--", __privateGet(this, _index))) {
381
+ this.skipComment();
382
+ continue;
383
+ }
384
+ if (this.source.startsWith("<![CDATA[", __privateGet(this, _index))) {
385
+ this.appendText(children, this.parseCdata());
386
+ continue;
387
+ }
388
+ if (this.source.startsWith("<?", __privateGet(this, _index))) {
389
+ this.skipProcessingInstruction();
390
+ continue;
391
+ }
392
+ if (this.source.startsWith("<!", __privateGet(this, _index))) {
393
+ this.fail("Unsupported XML declaration inside an element");
394
+ }
395
+ if (this.source[__privateGet(this, _index)] === "<") {
396
+ children.push(this.parseElement(depth + 1));
397
+ } else {
398
+ this.appendText(children, this.parseText());
399
+ }
400
+ }
401
+ this.fail(`Unclosed XML element: <${name}>`);
402
+ }
403
+ parseStartTag() {
404
+ const attributes = {};
405
+ while (__privateGet(this, _index) < this.source.length) {
406
+ this.skipWhitespace();
407
+ if (this.source.startsWith("/>", __privateGet(this, _index))) {
408
+ __privateSet(this, _index, __privateGet(this, _index) + 2);
409
+ return { attributes, selfClosing: true };
410
+ }
411
+ if (this.source[__privateGet(this, _index)] === ">") {
412
+ __privateSet(this, _index, __privateGet(this, _index) + 1);
413
+ return { attributes, selfClosing: false };
414
+ }
415
+ const name = this.parseName();
416
+ this.skipWhitespace();
417
+ this.expect("=");
418
+ this.skipWhitespace();
419
+ const quote = this.source[__privateGet(this, _index)];
420
+ if (quote !== '"' && quote !== "'") {
421
+ this.fail(`XML attribute ${name} must use a quoted value`);
422
+ }
423
+ __privateSet(this, _index, __privateGet(this, _index) + 1);
424
+ const valueStart = __privateGet(this, _index);
425
+ while (__privateGet(this, _index) < this.source.length && this.source[__privateGet(this, _index)] !== quote) {
426
+ if (this.source[__privateGet(this, _index)] === "<") {
427
+ this.fail(`Invalid "<" in XML attribute ${name}`);
428
+ }
429
+ __privateSet(this, _index, __privateGet(this, _index) + 1);
430
+ }
431
+ if (__privateGet(this, _index) >= this.source.length) {
432
+ this.fail(`Unclosed XML attribute ${name}`);
433
+ }
434
+ const value = decodeXmlEntities(this.source.slice(valueStart, __privateGet(this, _index)));
435
+ __privateSet(this, _index, __privateGet(this, _index) + 1);
436
+ if (hasOwn(attributes, name)) {
437
+ this.fail(`Duplicate XML attribute: ${name}`);
438
+ }
439
+ setAttribute(attributes, name, value);
440
+ }
441
+ this.fail("Unclosed XML start tag");
442
+ }
443
+ parseText() {
444
+ const start = __privateGet(this, _index);
445
+ while (__privateGet(this, _index) < this.source.length && this.source[__privateGet(this, _index)] !== "<") {
446
+ __privateSet(this, _index, __privateGet(this, _index) + 1);
447
+ }
448
+ const value = this.source.slice(start, __privateGet(this, _index));
449
+ if (value.includes("]]>")) {
450
+ this.fail("CDATA termination is not valid in ordinary XML text");
451
+ }
452
+ return decodeXmlEntities(value);
453
+ }
454
+ parseCdata() {
455
+ __privateSet(this, _index, __privateGet(this, _index) + "<![CDATA[".length);
456
+ const end = this.source.indexOf("]]>", __privateGet(this, _index));
457
+ if (end === -1) {
458
+ this.fail("Unclosed XML CDATA section");
459
+ }
460
+ const value = this.source.slice(__privateGet(this, _index), end);
461
+ __privateSet(this, _index, end + 3);
462
+ return value;
463
+ }
464
+ skipComment() {
465
+ __privateSet(this, _index, __privateGet(this, _index) + "<!--".length);
466
+ const end = this.source.indexOf("-->", __privateGet(this, _index));
467
+ if (end === -1) {
468
+ this.fail("Unclosed XML comment");
469
+ }
470
+ if (this.source.slice(__privateGet(this, _index), end).includes("--")) {
471
+ this.fail("XML comments cannot contain consecutive hyphens");
472
+ }
473
+ __privateSet(this, _index, end + 3);
474
+ }
475
+ skipProcessingInstruction() {
476
+ __privateSet(this, _index, __privateGet(this, _index) + "<?".length);
477
+ this.parseName();
478
+ const end = this.source.indexOf("?>", __privateGet(this, _index));
479
+ if (end === -1) {
480
+ this.fail("Unclosed XML processing instruction");
481
+ }
482
+ __privateSet(this, _index, end + 2);
483
+ }
484
+ skipMisc() {
485
+ while (__privateGet(this, _index) < this.source.length) {
486
+ this.skipWhitespace();
487
+ if (this.source.startsWith("<!--", __privateGet(this, _index))) {
488
+ this.skipComment();
489
+ continue;
490
+ }
491
+ if (this.source.startsWith("<?", __privateGet(this, _index))) {
492
+ this.skipProcessingInstruction();
493
+ continue;
494
+ }
495
+ if (this.source.startsWith("<!DOCTYPE", __privateGet(this, _index))) {
496
+ this.fail("DOCTYPE declarations are not supported");
497
+ }
498
+ break;
499
+ }
500
+ }
501
+ parseName() {
502
+ const first = this.source[__privateGet(this, _index)];
503
+ if (!isXmlNameStart(first)) {
504
+ this.fail("Invalid XML name");
505
+ }
506
+ const start = __privateGet(this, _index);
507
+ __privateSet(this, _index, __privateGet(this, _index) + 1);
508
+ while (isXmlNameCharacter(this.source[__privateGet(this, _index)])) {
509
+ __privateSet(this, _index, __privateGet(this, _index) + 1);
510
+ }
511
+ return this.source.slice(start, __privateGet(this, _index));
512
+ }
513
+ appendText(children, value) {
514
+ if (!value) {
515
+ return;
516
+ }
517
+ const previous = children[children.length - 1];
518
+ if (typeof previous === "string") {
519
+ children[children.length - 1] = previous + value;
520
+ } else {
521
+ children.push(value);
522
+ }
523
+ }
524
+ skipWhitespace() {
525
+ while (isXmlWhitespace(this.source[__privateGet(this, _index)])) {
526
+ __privateSet(this, _index, __privateGet(this, _index) + 1);
527
+ }
528
+ }
529
+ expect(value) {
530
+ if (!this.source.startsWith(value, __privateGet(this, _index))) {
531
+ this.fail(`Expected "${value}"`);
532
+ }
533
+ __privateSet(this, _index, __privateGet(this, _index) + value.length);
534
+ }
535
+ fail(message) {
536
+ throw new Error(`${message} at position ${__privateGet(this, _index)}`);
537
+ }
538
+ };
539
+ _index = new WeakMap();
540
+ function readAttribute(attributes, ...names) {
541
+ let found = false;
542
+ let value;
543
+ for (const name of names) {
544
+ if (hasOwn(attributes, name)) {
545
+ if (!found) {
546
+ value = String(attributes[name]);
547
+ found = true;
548
+ }
549
+ delete attributes[name];
550
+ }
551
+ }
552
+ return value;
553
+ }
554
+ function getElementAttributes(node) {
555
+ const attributes = { ...node.attributes };
556
+ removeStandardNamespaceAttributes(attributes);
557
+ return attributes;
558
+ }
559
+ function finishElement(element, node, attributes) {
560
+ if (node.children.length > 0) {
561
+ element.children = node.children.map(convertNode);
562
+ }
563
+ if (Object.keys(attributes).length > 0) {
564
+ element.attributes = attributes;
565
+ }
566
+ return element;
567
+ }
568
+ function convertElement(node) {
569
+ const attributes = getElementAttributes(node);
570
+ switch (node.name) {
571
+ case SSML_TAGS.VOICE: {
572
+ const element = { type: SSML_TAGS.VOICE };
573
+ const name = readAttribute(attributes, SSML_ATTRS.NAME);
574
+ const effect = readAttribute(attributes, SSML_ATTRS.EFFECT);
575
+ if (name !== void 0) element.name = name;
576
+ if (effect !== void 0) element.effect = effect;
577
+ return finishElement(element, node, attributes);
578
+ }
579
+ case SSML_TAGS.PROSODY: {
580
+ const element = { type: SSML_TAGS.PROSODY };
581
+ const rate = readAttribute(attributes, SSML_ATTRS.RATE);
582
+ const pitch = readAttribute(attributes, SSML_ATTRS.PITCH);
583
+ const volume = readAttribute(attributes, SSML_ATTRS.VOLUME);
584
+ const contour = readAttribute(attributes, SSML_ATTRS.CONTOUR);
585
+ const range = readAttribute(attributes, SSML_ATTRS.RANGE);
586
+ if (rate !== void 0) element.rate = rate;
587
+ if (pitch !== void 0) element.pitch = pitch;
588
+ if (volume !== void 0) element.volume = volume;
589
+ if (contour !== void 0) element.contour = contour;
590
+ if (range !== void 0) element.range = range;
591
+ return finishElement(element, node, attributes);
592
+ }
593
+ case SSML_TAGS.BREAK: {
594
+ const element = { type: SSML_TAGS.BREAK };
595
+ const time = readAttribute(attributes, SSML_ATTRS.TIME);
596
+ const strength = readAttribute(attributes, SSML_ATTRS.STRENGTH);
597
+ if (time !== void 0) element.time = time;
598
+ if (strength !== void 0) element.strength = strength;
599
+ return finishElement(element, node, attributes);
600
+ }
601
+ case SSML_TAGS.EXPRESS_AS:
602
+ case SSML_TAGS.EXPRESS_AS_CAMEL:
603
+ case SSML_TAGS.MSTTS_EXPRESS_AS: {
604
+ const element = { type: node.name };
605
+ const style = readAttribute(attributes, SSML_ATTRS.STYLE);
606
+ const styleDegree = readAttribute(attributes, SSML_ATTRS.STYLE_DEGREE, SSML_ATTRS.STYLE_DEGREE_CAMEL);
607
+ const role = readAttribute(attributes, SSML_ATTRS.ROLE);
608
+ if (style !== void 0) element.style = style;
609
+ if (styleDegree !== void 0) element.styleDegree = styleDegree;
610
+ if (role !== void 0) element.role = role;
611
+ return finishElement(element, node, attributes);
612
+ }
613
+ case SSML_TAGS.SAY_AS:
614
+ case SSML_TAGS.SAY_AS_CAMEL: {
615
+ const element = { type: node.name };
616
+ const interpretAs = readAttribute(attributes, SSML_ATTRS.INTERPRET_AS);
617
+ const format = readAttribute(attributes, SSML_ATTRS.FORMAT);
618
+ const detail = readAttribute(attributes, SSML_ATTRS.DETAIL);
619
+ if (interpretAs !== void 0) element.interpretAs = interpretAs;
620
+ if (format !== void 0) element.format = format;
621
+ if (detail !== void 0) element.detail = detail;
622
+ return finishElement(element, node, attributes);
623
+ }
624
+ case SSML_TAGS.PHONEME: {
625
+ const element = { type: SSML_TAGS.PHONEME };
626
+ const alphabet = readAttribute(attributes, SSML_ATTRS.ALPHABET);
627
+ const ph = readAttribute(attributes, SSML_ATTRS.PH);
628
+ if (alphabet !== void 0) element.alphabet = alphabet;
629
+ if (ph !== void 0) element.ph = ph;
630
+ return finishElement(element, node, attributes);
631
+ }
632
+ case SSML_TAGS.EMPHASIS: {
633
+ const element = { type: SSML_TAGS.EMPHASIS };
634
+ const level = readAttribute(attributes, SSML_ATTRS.LEVEL);
635
+ if (level !== void 0) element.level = level;
636
+ return finishElement(element, node, attributes);
637
+ }
638
+ case SSML_TAGS.AUDIO: {
639
+ const element = { type: SSML_TAGS.AUDIO };
640
+ const src = readAttribute(attributes, SSML_ATTRS.SRC);
641
+ const desc = readAttribute(attributes, SSML_ATTRS.DESC);
642
+ const clipBegin = readAttribute(attributes, SSML_ATTRS.CLIP_BEGIN);
643
+ const clipEnd = readAttribute(attributes, SSML_ATTRS.CLIP_END);
644
+ const speed = readAttribute(attributes, SSML_ATTRS.SPEED);
645
+ const repeatCount = readAttribute(attributes, SSML_ATTRS.REPEAT_COUNT);
646
+ const repeatDuration = readAttribute(attributes, SSML_ATTRS.REPEAT_DURATION);
647
+ const soundLevel = readAttribute(attributes, SSML_ATTRS.SOUND_LEVEL);
648
+ if (src !== void 0) element.src = src;
649
+ if (desc !== void 0) element.desc = desc;
650
+ if (clipBegin !== void 0) element.clipBegin = clipBegin;
651
+ if (clipEnd !== void 0) element.clipEnd = clipEnd;
652
+ if (speed !== void 0) element.speed = speed;
653
+ if (repeatCount !== void 0) element.repeatCount = repeatCount;
654
+ if (repeatDuration !== void 0) element.repeatDuration = repeatDuration;
655
+ if (soundLevel !== void 0) element.soundLevel = soundLevel;
656
+ return finishElement(element, node, attributes);
657
+ }
658
+ case SSML_TAGS.SUB: {
659
+ const element = { type: SSML_TAGS.SUB };
660
+ const alias = readAttribute(attributes, SSML_ATTRS.ALIAS);
661
+ if (alias !== void 0) element.alias = alias;
662
+ return finishElement(element, node, attributes);
663
+ }
664
+ case SSML_TAGS.LANG: {
665
+ const element = { type: SSML_TAGS.LANG };
666
+ const lang = readAttribute(attributes, SSML_ATTRS.XML_LANG, SSML_ATTRS.LANG);
667
+ if (lang !== void 0) element.lang = lang;
668
+ return finishElement(element, node, attributes);
669
+ }
670
+ case SSML_TAGS.MARK: {
671
+ const element = { type: SSML_TAGS.MARK };
672
+ const name = readAttribute(attributes, SSML_ATTRS.NAME);
673
+ if (name !== void 0) element.name = name;
674
+ return finishElement(element, node, attributes);
675
+ }
676
+ case SSML_TAGS.BOOKMARK: {
677
+ const element = { type: SSML_TAGS.BOOKMARK };
678
+ const mark = readAttribute(attributes, SSML_ATTRS.MARK);
679
+ if (mark !== void 0) element.mark = mark;
680
+ return finishElement(element, node, attributes);
681
+ }
682
+ case SSML_TAGS.LEXICON: {
683
+ const element = { type: SSML_TAGS.LEXICON };
684
+ const uri = readAttribute(attributes, SSML_ATTRS.URI);
685
+ if (uri !== void 0) element.uri = uri;
686
+ return finishElement(element, node, attributes);
687
+ }
688
+ case SSML_TAGS.PARAGRAPH: {
689
+ const element = { type: SSML_TAGS.PARAGRAPH };
690
+ return finishElement(element, node, attributes);
691
+ }
692
+ case SSML_TAGS.SENTENCE: {
693
+ const element = { type: SSML_TAGS.SENTENCE };
694
+ return finishElement(element, node, attributes);
695
+ }
696
+ case SSML_TAGS.WORD: {
697
+ const element = { type: SSML_TAGS.WORD };
698
+ return finishElement(element, node, attributes);
699
+ }
700
+ case SSML_TAGS.MSTTS_SILENCE:
701
+ case SSML_TAGS.SILENCE: {
702
+ const element = {
703
+ type: node.name === SSML_TAGS.MSTTS_SILENCE ? SSML_TAGS.MSTTS_SILENCE : SSML_TAGS.SILENCE
704
+ };
705
+ const typeValue = readAttribute(attributes, SSML_ATTRS.TYPE);
706
+ const value = readAttribute(attributes, SSML_ATTRS.VALUE);
707
+ if (typeValue !== void 0) element.typeValue = typeValue;
708
+ if (value !== void 0) element.value = value;
709
+ return finishElement(element, node, attributes);
710
+ }
711
+ case SSML_TAGS.MSTTS_VISEME:
712
+ case SSML_TAGS.VISEME: {
713
+ const element = {
714
+ type: node.name === SSML_TAGS.MSTTS_VISEME ? SSML_TAGS.MSTTS_VISEME : SSML_TAGS.VISEME
715
+ };
716
+ const typeValue = readAttribute(attributes, SSML_ATTRS.TYPE);
717
+ if (typeValue !== void 0) element.typeValue = typeValue;
718
+ return finishElement(element, node, attributes);
719
+ }
720
+ default: {
721
+ const element = {
722
+ name: node.name,
723
+ type: "custom"
724
+ };
725
+ return finishElement(element, node, attributes);
726
+ }
727
+ }
728
+ }
729
+ function convertNode(node) {
730
+ return typeof node === "string" ? node : convertElement(node);
731
+ }
732
+ function parseSsml(xmlString) {
733
+ if (typeof xmlString !== "string") {
734
+ throw new TypeError("SSML input must be a string");
735
+ }
736
+ const root = new XmlParser(xmlString).parse();
737
+ if (root.name !== SSML_TAGS.SPEAK) {
738
+ throw new Error(`SSML root element must be <${SSML_TAGS.SPEAK}>, found <${root.name}>`);
739
+ }
740
+ const attributes = { ...root.attributes };
741
+ const version = readAttribute(attributes, SSML_ATTRS.VERSION);
742
+ const lang = readAttribute(attributes, SSML_ATTRS.XML_LANG, SSML_ATTRS.LANG);
743
+ if (version === void 0) {
744
+ throw new Error(`SSML <${SSML_TAGS.SPEAK}> element is missing the "${SSML_ATTRS.VERSION}" attribute`);
745
+ }
746
+ if (lang === void 0) {
747
+ throw new Error(`SSML <${SSML_TAGS.SPEAK}> element is missing the "${SSML_ATTRS.XML_LANG}" attribute`);
748
+ }
749
+ removeStandardNamespaceAttributes(attributes);
750
+ const document = {
751
+ children: root.children.map(convertNode),
752
+ lang,
753
+ type: SSML_TAGS.SPEAK,
754
+ version
755
+ };
756
+ if (Object.keys(attributes).length > 0) {
757
+ document.attributes = attributes;
758
+ }
759
+ return document;
760
+ }
761
+
762
+ // packages/ssml-core/src/partial.ts
763
+ function escapeAttribute2(value) {
764
+ return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/'/g, "&apos;");
765
+ }
766
+ function getPartialTextNodes(text, version, lang) {
767
+ if (!text.includes("<")) {
768
+ return [text];
769
+ }
770
+ try {
771
+ const openingTag = `<${SSML_TAGS.SPEAK} ${SSML_ATTRS.VERSION}="${escapeAttribute2(version)}" ${SSML_ATTRS.XMLNS}="${SYNTHESIS_NAMESPACE}" ${SSML_ATTRS.XML_LANG}="${escapeAttribute2(lang)}">`;
772
+ return parseSsml(`${openingTag}${text}</${SSML_TAGS.SPEAK}>`).children ?? [];
773
+ } catch {
774
+ return [{ type: "text", value: text }];
775
+ }
776
+ }
777
+ function getVoiceContext(context) {
778
+ const voice = context.voice;
779
+ if (typeof voice === "object" && voice !== null) {
780
+ return voice;
781
+ }
782
+ if (context.voiceName === void 0 && context.voiceEffect === void 0 && typeof voice !== "string") {
783
+ return void 0;
784
+ }
785
+ return {
786
+ name: context.voiceName ?? voice,
787
+ effect: context.voiceEffect
788
+ };
789
+ }
790
+ function serializePartialSsml(text, context) {
791
+ const version = context.version ?? DEFAULT_SSML_VERSION;
792
+ const lang = context.lang ?? DEFAULT_SSML_LANGUAGE;
793
+ let children = getPartialTextNodes(text, version, lang);
794
+ if (context.prosody) {
795
+ children = [
796
+ {
797
+ type: SSML_TAGS.PROSODY,
798
+ ...context.prosody,
799
+ children
800
+ }
801
+ ];
802
+ }
803
+ const voice = getVoiceContext(context);
804
+ if (voice) {
805
+ children = [
806
+ {
807
+ type: SSML_TAGS.VOICE,
808
+ ...voice,
809
+ children
810
+ }
811
+ ];
812
+ }
813
+ const document = {
814
+ type: SSML_TAGS.SPEAK,
815
+ version,
816
+ lang,
817
+ attributes: context.attributes,
818
+ children
819
+ };
820
+ return buildSsml(document);
821
+ }
822
+ function buildPartialSsml(textOrOptions, context) {
823
+ if (typeof textOrOptions === "string") {
824
+ return serializePartialSsml(textOrOptions, context ?? {});
825
+ }
826
+ return serializePartialSsml(textOrOptions.text, textOrOptions);
827
+ }
828
+
829
+ // packages/ssml-core/src/validation.ts
830
+ var PARSER_POSITION_SUFFIX = / at position (\d+)$/;
831
+ function validateSsml(xmlString) {
832
+ try {
833
+ parseSsml(xmlString);
834
+ return null;
835
+ } catch (error) {
836
+ const rawMessage = error instanceof Error ? error.message : String(error);
837
+ const positionMatch = PARSER_POSITION_SUFFIX.exec(rawMessage);
838
+ return {
839
+ message: positionMatch ? rawMessage.slice(0, positionMatch.index) : rawMessage,
840
+ position: positionMatch ? Number.parseInt(positionMatch[1], 10) : 0
841
+ };
842
+ }
843
+ }
844
+
845
+ export {
846
+ buildSsml,
847
+ parseSsml,
848
+ buildPartialSsml,
849
+ validateSsml
850
+ };
851
+ //# sourceMappingURL=chunk-I3GP7OJU.mjs.map