ssml-builder-js 2.3.0 → 2.5.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.
- package/README.md +158 -0
- package/dist/{chunk-PSN6XAFF.mjs → chunk-4RHHW33A.mjs} +551 -2
- package/dist/chunk-4RHHW33A.mjs.map +1 -0
- package/dist/{chunk-NPZ44CHU.mjs → chunk-ALFYHSCR.mjs} +1 -1
- package/dist/chunk-ALFYHSCR.mjs.map +1 -0
- package/dist/core.d.mts +36 -1
- package/dist/core.d.ts +36 -1
- package/dist/core.js +552 -0
- package/dist/core.js.map +1 -1
- package/dist/core.mjs +7 -1
- package/dist/elements.js.map +1 -1
- package/dist/elements.mjs +1 -1
- package/dist/index.d.mts +14 -3
- package/dist/index.d.ts +14 -3
- package/dist/index.js +584 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +39 -6
- package/dist/index.mjs.map +1 -1
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +1 -1
- package/package.json +12 -3
- package/dist/chunk-NPZ44CHU.mjs.map +0 -1
- package/dist/chunk-PSN6XAFF.mjs.map +0 -1
package/dist/core.d.mts
CHANGED
|
@@ -156,4 +156,39 @@ interface SsmlValidationError {
|
|
|
156
156
|
}
|
|
157
157
|
declare function validateSsml(xmlString: string): SsmlValidationError | null;
|
|
158
158
|
|
|
159
|
-
|
|
159
|
+
interface SsmlTextNodeContext {
|
|
160
|
+
parentTag: string;
|
|
161
|
+
parentAttributes: Record<string, string>;
|
|
162
|
+
ancestorTags: string[];
|
|
163
|
+
path: string[];
|
|
164
|
+
}
|
|
165
|
+
interface MapSsmlTextNodesOptions {
|
|
166
|
+
/** Element names whose text should not be passed to the transform. */
|
|
167
|
+
skipTags?: readonly string[];
|
|
168
|
+
/** Decides whether an individual text node should be passed to the transform. */
|
|
169
|
+
filter?: (context: SsmlTextNodeContext) => boolean;
|
|
170
|
+
}
|
|
171
|
+
declare function extractSsmlText(ssml: string): string[];
|
|
172
|
+
declare function mapSsmlTextNodes(ssml: string, transform: (text: string, context: SsmlTextNodeContext) => string | Promise<string>, options?: MapSsmlTextNodesOptions): Promise<string>;
|
|
173
|
+
|
|
174
|
+
type SsmlDiagnosticSeverity = "error" | "warning";
|
|
175
|
+
interface SsmlDiagnostic {
|
|
176
|
+
line: number;
|
|
177
|
+
column: number;
|
|
178
|
+
message: string;
|
|
179
|
+
severity: SsmlDiagnosticSeverity;
|
|
180
|
+
}
|
|
181
|
+
interface AzureValidationOptions {
|
|
182
|
+
allowedAudioOrigins?: readonly string[];
|
|
183
|
+
allowExternalAudio?: boolean;
|
|
184
|
+
allowHttpAudio?: boolean;
|
|
185
|
+
customVoiceStyleMap?: Record<string, readonly string[]>;
|
|
186
|
+
maxLength?: number;
|
|
187
|
+
unknownVoicePolicy?: "error" | "warn" | "ignore";
|
|
188
|
+
validateNestedVoices?: boolean;
|
|
189
|
+
}
|
|
190
|
+
/** @deprecated Use AzureValidationOptions instead. */
|
|
191
|
+
type AzureSsmlValidationOptions = AzureValidationOptions;
|
|
192
|
+
declare function validateAzureSsml(ssml: string, options?: AzureValidationOptions): SsmlDiagnostic[];
|
|
193
|
+
|
|
194
|
+
export { type AudioElement, type AzureSsmlValidationOptions, type AzureValidationOptions, type BookmarkElement, type BreakElement, type BuildPartialSsmlOptions, type CustomElement, type EmphasisElement, type ExpressAsElement, type LangElement, type LexiconElement, type MapSsmlTextNodesOptions, type MarkElement, type MsttsSilenceElement, type MsttsVisemeElement, type NamedElement, type ParagraphElement, type PhonemeElement, type ProsodyElement, type SayAsElement, type SentenceElement, type SsmlAttributeValue, type SsmlAttributes, type SsmlBreakElement, type SsmlDiagnostic, type SsmlDiagnosticSeverity, type SsmlDocument, type SsmlElement, type SsmlElementBase, type SsmlExpressAsElement, type SsmlNode, type SsmlPartialContext, type SsmlPartialProsody, type SsmlPartialVoice, type SsmlPhonemeElement, type SsmlProsodyElement, type SsmlSayAsElement, type SsmlText, type SsmlTextNodeContext, type SsmlValidationError, type SsmlVoiceElement, type SubElement, type VoiceElement, type WordElement, buildPartialSsml, buildSsml, extractSsmlText, mapSsmlTextNodes, parseSsml, validateAzureSsml, validateSsml };
|
package/dist/core.d.ts
CHANGED
|
@@ -156,4 +156,39 @@ interface SsmlValidationError {
|
|
|
156
156
|
}
|
|
157
157
|
declare function validateSsml(xmlString: string): SsmlValidationError | null;
|
|
158
158
|
|
|
159
|
-
|
|
159
|
+
interface SsmlTextNodeContext {
|
|
160
|
+
parentTag: string;
|
|
161
|
+
parentAttributes: Record<string, string>;
|
|
162
|
+
ancestorTags: string[];
|
|
163
|
+
path: string[];
|
|
164
|
+
}
|
|
165
|
+
interface MapSsmlTextNodesOptions {
|
|
166
|
+
/** Element names whose text should not be passed to the transform. */
|
|
167
|
+
skipTags?: readonly string[];
|
|
168
|
+
/** Decides whether an individual text node should be passed to the transform. */
|
|
169
|
+
filter?: (context: SsmlTextNodeContext) => boolean;
|
|
170
|
+
}
|
|
171
|
+
declare function extractSsmlText(ssml: string): string[];
|
|
172
|
+
declare function mapSsmlTextNodes(ssml: string, transform: (text: string, context: SsmlTextNodeContext) => string | Promise<string>, options?: MapSsmlTextNodesOptions): Promise<string>;
|
|
173
|
+
|
|
174
|
+
type SsmlDiagnosticSeverity = "error" | "warning";
|
|
175
|
+
interface SsmlDiagnostic {
|
|
176
|
+
line: number;
|
|
177
|
+
column: number;
|
|
178
|
+
message: string;
|
|
179
|
+
severity: SsmlDiagnosticSeverity;
|
|
180
|
+
}
|
|
181
|
+
interface AzureValidationOptions {
|
|
182
|
+
allowedAudioOrigins?: readonly string[];
|
|
183
|
+
allowExternalAudio?: boolean;
|
|
184
|
+
allowHttpAudio?: boolean;
|
|
185
|
+
customVoiceStyleMap?: Record<string, readonly string[]>;
|
|
186
|
+
maxLength?: number;
|
|
187
|
+
unknownVoicePolicy?: "error" | "warn" | "ignore";
|
|
188
|
+
validateNestedVoices?: boolean;
|
|
189
|
+
}
|
|
190
|
+
/** @deprecated Use AzureValidationOptions instead. */
|
|
191
|
+
type AzureSsmlValidationOptions = AzureValidationOptions;
|
|
192
|
+
declare function validateAzureSsml(ssml: string, options?: AzureValidationOptions): SsmlDiagnostic[];
|
|
193
|
+
|
|
194
|
+
export { type AudioElement, type AzureSsmlValidationOptions, type AzureValidationOptions, type BookmarkElement, type BreakElement, type BuildPartialSsmlOptions, type CustomElement, type EmphasisElement, type ExpressAsElement, type LangElement, type LexiconElement, type MapSsmlTextNodesOptions, type MarkElement, type MsttsSilenceElement, type MsttsVisemeElement, type NamedElement, type ParagraphElement, type PhonemeElement, type ProsodyElement, type SayAsElement, type SentenceElement, type SsmlAttributeValue, type SsmlAttributes, type SsmlBreakElement, type SsmlDiagnostic, type SsmlDiagnosticSeverity, type SsmlDocument, type SsmlElement, type SsmlElementBase, type SsmlExpressAsElement, type SsmlNode, type SsmlPartialContext, type SsmlPartialProsody, type SsmlPartialVoice, type SsmlPhonemeElement, type SsmlProsodyElement, type SsmlSayAsElement, type SsmlText, type SsmlTextNodeContext, type SsmlValidationError, type SsmlVoiceElement, type SubElement, type VoiceElement, type WordElement, buildPartialSsml, buildSsml, extractSsmlText, mapSsmlTextNodes, parseSsml, validateAzureSsml, validateSsml };
|
package/dist/core.js
CHANGED
|
@@ -29,7 +29,10 @@ var core_exports = {};
|
|
|
29
29
|
__export(core_exports, {
|
|
30
30
|
buildPartialSsml: () => buildPartialSsml,
|
|
31
31
|
buildSsml: () => buildSsml,
|
|
32
|
+
extractSsmlText: () => extractSsmlText,
|
|
33
|
+
mapSsmlTextNodes: () => mapSsmlTextNodes,
|
|
32
34
|
parseSsml: () => parseSsml,
|
|
35
|
+
validateAzureSsml: () => validateAzureSsml,
|
|
33
36
|
validateSsml: () => validateSsml
|
|
34
37
|
});
|
|
35
38
|
module.exports = __toCommonJS(core_exports);
|
|
@@ -877,11 +880,560 @@ function validateSsml(xmlString) {
|
|
|
877
880
|
};
|
|
878
881
|
}
|
|
879
882
|
}
|
|
883
|
+
|
|
884
|
+
// packages/ssml-core/src/textNodes.ts
|
|
885
|
+
function decodeXmlText(value) {
|
|
886
|
+
return value.replace(/&(?:amp|apos|gt|lt|quot);|&#(?:x[\da-f]+|\d+);/gi, (entity) => {
|
|
887
|
+
if (entity === "&") return "&";
|
|
888
|
+
if (entity === "'") return "'";
|
|
889
|
+
if (entity === ">") return ">";
|
|
890
|
+
if (entity === "<") return "<";
|
|
891
|
+
if (entity === """) return '"';
|
|
892
|
+
const hexadecimal = entity.toLowerCase().startsWith("&#x");
|
|
893
|
+
const digits = entity.slice(hexadecimal ? 3 : 2, -1);
|
|
894
|
+
return String.fromCodePoint(Number.parseInt(digits, hexadecimal ? 16 : 10));
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
function encodeXmlText(value) {
|
|
898
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
899
|
+
}
|
|
900
|
+
function decodeXmlAttribute(value) {
|
|
901
|
+
return decodeXmlText(value);
|
|
902
|
+
}
|
|
903
|
+
function findTagEnd(source, start) {
|
|
904
|
+
let quote = "";
|
|
905
|
+
for (let index = start; index < source.length; index += 1) {
|
|
906
|
+
const character = source[index];
|
|
907
|
+
if (quote) {
|
|
908
|
+
if (character === quote) quote = "";
|
|
909
|
+
} else if (character === '"' || character === "'") {
|
|
910
|
+
quote = character;
|
|
911
|
+
} else if (character === ">") {
|
|
912
|
+
return index;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
return source.length - 1;
|
|
916
|
+
}
|
|
917
|
+
function readTagName(tag) {
|
|
918
|
+
const match = /^<\s*([A-Za-z_][A-Za-z0-9_.:-]*)/.exec(tag);
|
|
919
|
+
return match?.[1];
|
|
920
|
+
}
|
|
921
|
+
function readTagAttributes(tag, name) {
|
|
922
|
+
const attributes = {};
|
|
923
|
+
const nameStart = tag.indexOf(name);
|
|
924
|
+
const attributeSource = tag.slice(nameStart + name.length, tag.length - 1).replace(/\/\s*$/, "");
|
|
925
|
+
const attributePattern = /([A-Za-z_][A-Za-z0-9_.:-]*)\s*=\s*(["'])([\s\S]*?)\2/g;
|
|
926
|
+
for (const match of attributeSource.matchAll(attributePattern)) {
|
|
927
|
+
attributes[match[1].toLowerCase()] = decodeXmlAttribute(match[3]);
|
|
928
|
+
}
|
|
929
|
+
return attributes;
|
|
930
|
+
}
|
|
931
|
+
function collectTextNodes(source) {
|
|
932
|
+
const nodes = [];
|
|
933
|
+
const elements = [];
|
|
934
|
+
let index = 0;
|
|
935
|
+
const addText = (start, end, rawText, sourceStart = start, sourceEnd = end) => {
|
|
936
|
+
if (!rawText) return;
|
|
937
|
+
const path = elements.map((element) => element.name);
|
|
938
|
+
const parent = elements[elements.length - 1];
|
|
939
|
+
nodes.push({
|
|
940
|
+
context: {
|
|
941
|
+
ancestorTags: path.slice(0, -1),
|
|
942
|
+
parentAttributes: { ...parent?.attributes ?? {} },
|
|
943
|
+
parentTag: parent?.name ?? "",
|
|
944
|
+
path
|
|
945
|
+
},
|
|
946
|
+
decodedText: decodeXmlText(rawText),
|
|
947
|
+
end,
|
|
948
|
+
sourceEnd,
|
|
949
|
+
sourceStart,
|
|
950
|
+
start
|
|
951
|
+
});
|
|
952
|
+
};
|
|
953
|
+
while (index < source.length) {
|
|
954
|
+
if (source[index] !== "<") {
|
|
955
|
+
const nextTag = source.indexOf("<", index);
|
|
956
|
+
const end2 = nextTag === -1 ? source.length : nextTag;
|
|
957
|
+
addText(index, end2, source.slice(index, end2));
|
|
958
|
+
index = end2;
|
|
959
|
+
continue;
|
|
960
|
+
}
|
|
961
|
+
if (source.startsWith("<!--", index)) {
|
|
962
|
+
const end2 = source.indexOf("-->", index + 4);
|
|
963
|
+
index = end2 === -1 ? source.length : end2 + 3;
|
|
964
|
+
continue;
|
|
965
|
+
}
|
|
966
|
+
if (source.startsWith("<![CDATA[", index)) {
|
|
967
|
+
const contentStart = index + 9;
|
|
968
|
+
const end2 = source.indexOf("]]>", contentStart);
|
|
969
|
+
const contentEnd = end2 === -1 ? source.length : end2;
|
|
970
|
+
addText(
|
|
971
|
+
contentStart,
|
|
972
|
+
contentEnd,
|
|
973
|
+
source.slice(contentStart, contentEnd),
|
|
974
|
+
index,
|
|
975
|
+
end2 === -1 ? source.length : end2 + 3
|
|
976
|
+
);
|
|
977
|
+
index = end2 === -1 ? source.length : end2 + 3;
|
|
978
|
+
continue;
|
|
979
|
+
}
|
|
980
|
+
if (source.startsWith("<?", index)) {
|
|
981
|
+
const end2 = source.indexOf("?>", index + 2);
|
|
982
|
+
index = end2 === -1 ? source.length : end2 + 2;
|
|
983
|
+
continue;
|
|
984
|
+
}
|
|
985
|
+
if (source.startsWith("</", index)) {
|
|
986
|
+
const end2 = findTagEnd(source, index + 2);
|
|
987
|
+
elements.pop();
|
|
988
|
+
index = end2 + 1;
|
|
989
|
+
continue;
|
|
990
|
+
}
|
|
991
|
+
const end = findTagEnd(source, index + 1);
|
|
992
|
+
const tag = source.slice(index, end + 1);
|
|
993
|
+
const name = readTagName(tag);
|
|
994
|
+
if (name && !/\/\s*>$/.test(tag)) elements.push({ attributes: readTagAttributes(tag, name), name });
|
|
995
|
+
index = end + 1;
|
|
996
|
+
}
|
|
997
|
+
return nodes;
|
|
998
|
+
}
|
|
999
|
+
function extractSsmlText(ssml) {
|
|
1000
|
+
parseSsml(ssml);
|
|
1001
|
+
return collectTextNodes(ssml).map((node) => node.decodedText);
|
|
1002
|
+
}
|
|
1003
|
+
async function mapSsmlTextNodes(ssml, transform, options = {}) {
|
|
1004
|
+
parseSsml(ssml);
|
|
1005
|
+
const nodes = collectTextNodes(ssml);
|
|
1006
|
+
const skipTags = new Set((options.skipTags ?? ["phoneme", "say-as", "sub"]).map((tag) => tag.toLowerCase()));
|
|
1007
|
+
const replacements = await Promise.all(
|
|
1008
|
+
nodes.map(async (node) => {
|
|
1009
|
+
const context = {
|
|
1010
|
+
ancestorTags: [...node.context.ancestorTags],
|
|
1011
|
+
parentAttributes: { ...node.context.parentAttributes },
|
|
1012
|
+
parentTag: node.context.parentTag,
|
|
1013
|
+
path: [...node.context.path]
|
|
1014
|
+
};
|
|
1015
|
+
const shouldTransform = !skipTags.has(context.parentTag.toLowerCase()) && (options.filter?.(context) ?? true);
|
|
1016
|
+
if (!shouldTransform) return ssml.slice(node.sourceStart, node.sourceEnd);
|
|
1017
|
+
const transformed = await transform(node.decodedText, context);
|
|
1018
|
+
if (typeof transformed !== "string") {
|
|
1019
|
+
throw new TypeError("SSML text node transform must return a string");
|
|
1020
|
+
}
|
|
1021
|
+
return transformed === node.decodedText ? ssml.slice(node.sourceStart, node.sourceEnd) : encodeXmlText(transformed);
|
|
1022
|
+
})
|
|
1023
|
+
);
|
|
1024
|
+
let result = "";
|
|
1025
|
+
let cursor = 0;
|
|
1026
|
+
nodes.forEach((node, nodeIndex) => {
|
|
1027
|
+
result += ssml.slice(cursor, node.sourceStart) + replacements[nodeIndex];
|
|
1028
|
+
cursor = node.sourceEnd;
|
|
1029
|
+
});
|
|
1030
|
+
return result + ssml.slice(cursor);
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
// packages/ssml-core/src/azureValidation.ts
|
|
1034
|
+
var EXPRESS_AS_STYLES = {
|
|
1035
|
+
"en-us-jennyneural": [
|
|
1036
|
+
"assistant",
|
|
1037
|
+
"chat",
|
|
1038
|
+
"customerservice",
|
|
1039
|
+
"newscast",
|
|
1040
|
+
"cheerful",
|
|
1041
|
+
"empathetic",
|
|
1042
|
+
"excited",
|
|
1043
|
+
"friendly",
|
|
1044
|
+
"hopeful",
|
|
1045
|
+
"sad",
|
|
1046
|
+
"shouting",
|
|
1047
|
+
"terrified",
|
|
1048
|
+
"unfriendly",
|
|
1049
|
+
"whispering"
|
|
1050
|
+
],
|
|
1051
|
+
"en-us-guyneural": [
|
|
1052
|
+
"angry",
|
|
1053
|
+
"cheerful",
|
|
1054
|
+
"excited",
|
|
1055
|
+
"friendly",
|
|
1056
|
+
"hopeful",
|
|
1057
|
+
"newscast",
|
|
1058
|
+
"sad",
|
|
1059
|
+
"shouting",
|
|
1060
|
+
"terrified",
|
|
1061
|
+
"unfriendly",
|
|
1062
|
+
"whispering"
|
|
1063
|
+
],
|
|
1064
|
+
"en-us-jennymultilingualneural": [
|
|
1065
|
+
"cheerful",
|
|
1066
|
+
"empathetic",
|
|
1067
|
+
"excited",
|
|
1068
|
+
"friendly",
|
|
1069
|
+
"hopeful",
|
|
1070
|
+
"sad",
|
|
1071
|
+
"shouting",
|
|
1072
|
+
"terrified",
|
|
1073
|
+
"unfriendly",
|
|
1074
|
+
"whispering"
|
|
1075
|
+
],
|
|
1076
|
+
"en-us-andrewneural": ["empathetic", "relieved"],
|
|
1077
|
+
"ja-jp-mayuneural": ["calm", "cheerful", "sad"],
|
|
1078
|
+
"ja-jp-nanamineural": ["chat", "customerservice", "cheerful", "whispering", "sad"],
|
|
1079
|
+
"ja-jp-keitaneural": ["chat"],
|
|
1080
|
+
"ko-kr-sunhineural": ["cheerful", "sad"],
|
|
1081
|
+
"zh-cn-yunxineural": [
|
|
1082
|
+
"narration-relaxed",
|
|
1083
|
+
"embarrassed",
|
|
1084
|
+
"fearful",
|
|
1085
|
+
"sad",
|
|
1086
|
+
"disgruntled",
|
|
1087
|
+
"serious",
|
|
1088
|
+
"angry",
|
|
1089
|
+
"depressed",
|
|
1090
|
+
"chat",
|
|
1091
|
+
"cheerful",
|
|
1092
|
+
"assistant"
|
|
1093
|
+
],
|
|
1094
|
+
"zh-cn-xiaoxiaoneural": [
|
|
1095
|
+
"assistant",
|
|
1096
|
+
"chat",
|
|
1097
|
+
"customerservice",
|
|
1098
|
+
"newscast",
|
|
1099
|
+
"cheerful",
|
|
1100
|
+
"empathetic",
|
|
1101
|
+
"excited",
|
|
1102
|
+
"friendly",
|
|
1103
|
+
"hopeful",
|
|
1104
|
+
"sad",
|
|
1105
|
+
"terrified",
|
|
1106
|
+
"whispering",
|
|
1107
|
+
"poetry-reading",
|
|
1108
|
+
"sports_commentary",
|
|
1109
|
+
"sports_commentary_excited",
|
|
1110
|
+
"story"
|
|
1111
|
+
],
|
|
1112
|
+
"fr-fr-deniseneural": ["cheerful", "sad"],
|
|
1113
|
+
"fr-fr-henrineural": ["cheerful", "sad"],
|
|
1114
|
+
"pt-br-franciscaneural": ["calm"],
|
|
1115
|
+
"it-it-elsaneural": ["cheerful", "sad"],
|
|
1116
|
+
"de-de-katjaneural": ["cheerful", "sad"],
|
|
1117
|
+
"de-de-conradneural": ["cheerful", "sad"],
|
|
1118
|
+
"ru-ru-svetlananeural": ["cheerful", "sad", "angry", "disgruntled", "embarrassed", "fearful"]
|
|
1119
|
+
};
|
|
1120
|
+
var ALLOWED_BREAK_STRENGTHS = /* @__PURE__ */ new Set(["none", "x-weak", "weak", "medium", "strong", "x-strong"]);
|
|
1121
|
+
var ALLOWED_SAY_AS = /* @__PURE__ */ new Set([
|
|
1122
|
+
"characters",
|
|
1123
|
+
"spell-out",
|
|
1124
|
+
"cardinal",
|
|
1125
|
+
"ordinal",
|
|
1126
|
+
"number",
|
|
1127
|
+
"date",
|
|
1128
|
+
"time",
|
|
1129
|
+
"telephone",
|
|
1130
|
+
"fraction",
|
|
1131
|
+
"address",
|
|
1132
|
+
"name",
|
|
1133
|
+
"currency"
|
|
1134
|
+
]);
|
|
1135
|
+
var ALLOWED_ROLES = /* @__PURE__ */ new Set([
|
|
1136
|
+
"Girl",
|
|
1137
|
+
"Boy",
|
|
1138
|
+
"YoungAdultFemale",
|
|
1139
|
+
"YoungAdultMale",
|
|
1140
|
+
"OlderAdultFemale",
|
|
1141
|
+
"OlderAdultMale",
|
|
1142
|
+
"SeniorFemale",
|
|
1143
|
+
"SeniorMale"
|
|
1144
|
+
]);
|
|
1145
|
+
var ALLOWED_EMPHASIS_LEVELS = /* @__PURE__ */ new Set(["strong", "moderate", "reduced", "none"]);
|
|
1146
|
+
var ALLOWED_SILENCE_TYPES = /* @__PURE__ */ new Set([
|
|
1147
|
+
"Leading",
|
|
1148
|
+
"Tailing",
|
|
1149
|
+
"Sentenceboundary",
|
|
1150
|
+
"Comma",
|
|
1151
|
+
"Semicolon",
|
|
1152
|
+
"Enumerationcomma"
|
|
1153
|
+
]);
|
|
1154
|
+
var ALLOWED_VISEME_TYPES = /* @__PURE__ */ new Set(["redlips_front", "FacialExpression"]);
|
|
1155
|
+
function decodeAttribute(value) {
|
|
1156
|
+
return value.replace(
|
|
1157
|
+
/&(?:amp|apos|gt|lt|quot);/gi,
|
|
1158
|
+
(entity) => ({ "&": "&", "'": "'", ">": ">", "<": "<", """: '"' })[entity.toLowerCase()] ?? entity
|
|
1159
|
+
);
|
|
1160
|
+
}
|
|
1161
|
+
function findTagEnd2(source, start) {
|
|
1162
|
+
let quote = "";
|
|
1163
|
+
for (let index = start; index < source.length; index += 1) {
|
|
1164
|
+
const character = source[index];
|
|
1165
|
+
if (quote) {
|
|
1166
|
+
if (character === quote) quote = "";
|
|
1167
|
+
} else if (character === '"' || character === "'") quote = character;
|
|
1168
|
+
else if (character === ">") return index;
|
|
1169
|
+
}
|
|
1170
|
+
return source.length - 1;
|
|
1171
|
+
}
|
|
1172
|
+
function tokenizeElements(source) {
|
|
1173
|
+
const tokens = [];
|
|
1174
|
+
const openElements = [];
|
|
1175
|
+
let index = 0;
|
|
1176
|
+
while (index < source.length) {
|
|
1177
|
+
const start = source.indexOf("<", index);
|
|
1178
|
+
if (start === -1) break;
|
|
1179
|
+
if (source.startsWith("<!--", start)) {
|
|
1180
|
+
const end2 = source.indexOf("-->", start + 4);
|
|
1181
|
+
index = end2 === -1 ? source.length : end2 + 3;
|
|
1182
|
+
continue;
|
|
1183
|
+
}
|
|
1184
|
+
if (source.startsWith("<![CDATA[", start)) {
|
|
1185
|
+
const end2 = source.indexOf("]]>", start + 9);
|
|
1186
|
+
index = end2 === -1 ? source.length : end2 + 3;
|
|
1187
|
+
continue;
|
|
1188
|
+
}
|
|
1189
|
+
if (source.startsWith("<?", start)) {
|
|
1190
|
+
const end2 = source.indexOf("?>", start + 2);
|
|
1191
|
+
index = end2 === -1 ? source.length : end2 + 2;
|
|
1192
|
+
continue;
|
|
1193
|
+
}
|
|
1194
|
+
const end = findTagEnd2(source, start + 1);
|
|
1195
|
+
const raw = source.slice(start, end + 1);
|
|
1196
|
+
if (raw.startsWith("</")) {
|
|
1197
|
+
openElements.pop();
|
|
1198
|
+
index = end + 1;
|
|
1199
|
+
continue;
|
|
1200
|
+
}
|
|
1201
|
+
const nameMatch = /^<\s*([A-Za-z_][A-Za-z0-9_.:-]*)/.exec(raw);
|
|
1202
|
+
if (!nameMatch?.[1]) {
|
|
1203
|
+
index = end + 1;
|
|
1204
|
+
continue;
|
|
1205
|
+
}
|
|
1206
|
+
const attributes = /* @__PURE__ */ new Map();
|
|
1207
|
+
const attributeSource = raw.slice(nameMatch[0].length, raw.length - 1).replace(/\/\s*$/, "");
|
|
1208
|
+
const attributePattern = /([A-Za-z_][A-Za-z0-9_.:-]*)\s*=\s*(["'])([\s\S]*?)\2/g;
|
|
1209
|
+
for (const match of attributeSource.matchAll(attributePattern)) {
|
|
1210
|
+
attributes.set(match[1].toLowerCase(), decodeAttribute(match[3]));
|
|
1211
|
+
}
|
|
1212
|
+
const selfClosing = /\/\s*>$/.test(raw);
|
|
1213
|
+
const parentVoiceName = [...openElements].reverse().find((element) => element.voiceName)?.voiceName;
|
|
1214
|
+
tokens.push({ attributes, end, name: nameMatch[1], parentVoiceName, selfClosing, start });
|
|
1215
|
+
if (!selfClosing) {
|
|
1216
|
+
openElements.push({
|
|
1217
|
+
name: nameMatch[1],
|
|
1218
|
+
voiceName: nameMatch[1].toLowerCase() === "voice" ? attributes.get("name") : parentVoiceName
|
|
1219
|
+
});
|
|
1220
|
+
}
|
|
1221
|
+
index = end + 1;
|
|
1222
|
+
}
|
|
1223
|
+
return tokens;
|
|
1224
|
+
}
|
|
1225
|
+
function location(source, offset) {
|
|
1226
|
+
const before = source.slice(0, Math.max(0, offset));
|
|
1227
|
+
const line = before.split("\n").length;
|
|
1228
|
+
return { line, column: before.length - (before.lastIndexOf("\n") + 1) + 1 };
|
|
1229
|
+
}
|
|
1230
|
+
function addDiagnostic(diagnostics, source, offset, message, severity = "error") {
|
|
1231
|
+
diagnostics.push({ ...location(source, offset), message, severity });
|
|
1232
|
+
}
|
|
1233
|
+
function attr(token, name) {
|
|
1234
|
+
return token.attributes.get(name.toLowerCase());
|
|
1235
|
+
}
|
|
1236
|
+
function normalizeVoiceStyleMap(customVoiceStyleMap) {
|
|
1237
|
+
const map = new Map(
|
|
1238
|
+
Object.entries(EXPRESS_AS_STYLES).map(([voiceName, styles]) => [voiceName.toLowerCase(), styles])
|
|
1239
|
+
);
|
|
1240
|
+
for (const [voiceName, styles] of Object.entries(customVoiceStyleMap ?? {})) {
|
|
1241
|
+
map.set(
|
|
1242
|
+
voiceName.toLowerCase(),
|
|
1243
|
+
styles.map((style) => style.toLowerCase())
|
|
1244
|
+
);
|
|
1245
|
+
}
|
|
1246
|
+
return map;
|
|
1247
|
+
}
|
|
1248
|
+
function diagnosticSeverity(policy) {
|
|
1249
|
+
if (policy === "ignore") return void 0;
|
|
1250
|
+
return policy === "error" ? "error" : "warning";
|
|
1251
|
+
}
|
|
1252
|
+
function validateElement(token, source, diagnostics, voiceName, options, voiceStyleMap) {
|
|
1253
|
+
const name = token.name.toLowerCase();
|
|
1254
|
+
if (name === "voice" && !attr(token, "name")?.trim())
|
|
1255
|
+
addDiagnostic(diagnostics, source, token.start, '<voice> requires a non-empty "name" attribute.');
|
|
1256
|
+
if (name === "break") {
|
|
1257
|
+
const time = attr(token, "time");
|
|
1258
|
+
const strength = attr(token, "strength");
|
|
1259
|
+
if (!time && !strength)
|
|
1260
|
+
addDiagnostic(diagnostics, source, token.start, '<break> requires either "time" or "strength".');
|
|
1261
|
+
if (time && strength)
|
|
1262
|
+
addDiagnostic(diagnostics, source, token.start, '<break> must not specify both "time" and "strength".');
|
|
1263
|
+
if (time && !/^\d+(?:\.\d+)?(?:ms|s)$/.test(time.trim()))
|
|
1264
|
+
addDiagnostic(diagnostics, source, token.start, '<break time> must use a numeric value followed by "ms" or "s".');
|
|
1265
|
+
if (strength && !ALLOWED_BREAK_STRENGTHS.has(strength))
|
|
1266
|
+
addDiagnostic(diagnostics, source, token.start, `Unsupported <break strength> value "${strength}".`);
|
|
1267
|
+
}
|
|
1268
|
+
if (name === "prosody") {
|
|
1269
|
+
const rate = attr(token, "rate");
|
|
1270
|
+
const pitch = attr(token, "pitch");
|
|
1271
|
+
const volume = attr(token, "volume");
|
|
1272
|
+
if (rate && !/^(x-slow|slow|medium|fast|x-fast|[+-]?\d+(?:\.\d+)?%)$/.test(rate.trim()))
|
|
1273
|
+
addDiagnostic(diagnostics, source, token.start, `Unsupported <prosody rate> value "${rate}".`);
|
|
1274
|
+
if (pitch && !/^(x-low|low|medium|high|x-high|[+-]?\d+(?:\.\d+)?(?:st|Hz|%)?)$/.test(pitch.trim()))
|
|
1275
|
+
addDiagnostic(diagnostics, source, token.start, `Unsupported <prosody pitch> value "${pitch}".`);
|
|
1276
|
+
if (volume && !/^(silent|x-soft|soft|medium|loud|x-loud|[+-]?\d+(?:\.\d+)?(?:dB|%)?)$/.test(volume.trim()))
|
|
1277
|
+
addDiagnostic(diagnostics, source, token.start, `Unsupported <prosody volume> value "${volume}".`);
|
|
1278
|
+
}
|
|
1279
|
+
if (name === "mstts:express-as" || name === "express-as" || name === "expressas") {
|
|
1280
|
+
const style = attr(token, "style");
|
|
1281
|
+
if (!style?.trim())
|
|
1282
|
+
addDiagnostic(diagnostics, source, token.start, '<mstts:express-as> requires a non-empty "style" attribute.');
|
|
1283
|
+
const degree = attr(token, "styledegree") ?? attr(token, "style-degree");
|
|
1284
|
+
if (degree && (!/^\d+(?:\.\d+)?$/.test(degree) || Number(degree) < 0.01 || Number(degree) > 2))
|
|
1285
|
+
addDiagnostic(
|
|
1286
|
+
diagnostics,
|
|
1287
|
+
source,
|
|
1288
|
+
token.start,
|
|
1289
|
+
"<mstts:express-as styledegree> must be a number between 0.01 and 2."
|
|
1290
|
+
);
|
|
1291
|
+
const role = attr(token, "role");
|
|
1292
|
+
if (role && !ALLOWED_ROLES.has(role))
|
|
1293
|
+
addDiagnostic(diagnostics, source, token.start, `Unsupported <mstts:express-as role> value "${role}".`);
|
|
1294
|
+
const supportedStyles = voiceName ? voiceStyleMap.get(voiceName.toLowerCase()) : void 0;
|
|
1295
|
+
const severity = diagnosticSeverity(options.unknownVoicePolicy ?? "warn");
|
|
1296
|
+
if (style && supportedStyles && !supportedStyles.includes(style.toLowerCase()) && severity)
|
|
1297
|
+
addDiagnostic(
|
|
1298
|
+
diagnostics,
|
|
1299
|
+
source,
|
|
1300
|
+
token.start,
|
|
1301
|
+
`Unknown style "${style}" is not supported by voice "${voiceName}" according to the configured voice style map.`,
|
|
1302
|
+
severity
|
|
1303
|
+
);
|
|
1304
|
+
if (style && voiceName && !supportedStyles && severity)
|
|
1305
|
+
addDiagnostic(
|
|
1306
|
+
diagnostics,
|
|
1307
|
+
source,
|
|
1308
|
+
token.start,
|
|
1309
|
+
`Unknown style "${style}" cannot be verified because voice "${voiceName}" is not registered in the voice style map.`,
|
|
1310
|
+
severity
|
|
1311
|
+
);
|
|
1312
|
+
}
|
|
1313
|
+
if (name === "say-as" || name === "sayas") {
|
|
1314
|
+
const interpretAs = attr(token, "interpret-as");
|
|
1315
|
+
if (!interpretAs || !ALLOWED_SAY_AS.has(interpretAs))
|
|
1316
|
+
addDiagnostic(diagnostics, source, token.start, `<say-as> requires a supported "interpret-as" value.`);
|
|
1317
|
+
}
|
|
1318
|
+
if (name === "phoneme" && (!attr(token, "alphabet") || !attr(token, "ph")))
|
|
1319
|
+
addDiagnostic(diagnostics, source, token.start, '<phoneme> requires both "alphabet" and "ph" attributes.');
|
|
1320
|
+
if (name === "emphasis" && attr(token, "level") && !ALLOWED_EMPHASIS_LEVELS.has(attr(token, "level") ?? ""))
|
|
1321
|
+
addDiagnostic(diagnostics, source, token.start, `Unsupported <emphasis level> value "${attr(token, "level")}".`);
|
|
1322
|
+
if (name === "sub" && !attr(token, "alias")?.trim())
|
|
1323
|
+
addDiagnostic(diagnostics, source, token.start, '<sub> requires a non-empty "alias" attribute.');
|
|
1324
|
+
if (name === "lang" && !attr(token, "xml:lang")?.trim() && !attr(token, "lang")?.trim())
|
|
1325
|
+
addDiagnostic(diagnostics, source, token.start, '<lang> requires an "xml:lang" attribute.');
|
|
1326
|
+
if (name === "mark" && !attr(token, "name")?.trim())
|
|
1327
|
+
addDiagnostic(diagnostics, source, token.start, '<mark> requires a non-empty "name" attribute.');
|
|
1328
|
+
if (name === "bookmark" && !attr(token, "mark")?.trim())
|
|
1329
|
+
addDiagnostic(diagnostics, source, token.start, '<bookmark> requires a non-empty "mark" attribute.');
|
|
1330
|
+
if (name === "lexicon") {
|
|
1331
|
+
const uri = attr(token, "uri");
|
|
1332
|
+
if (!uri) addDiagnostic(diagnostics, source, token.start, '<lexicon> requires a "uri" attribute.');
|
|
1333
|
+
else {
|
|
1334
|
+
try {
|
|
1335
|
+
const parsed = new URL(uri);
|
|
1336
|
+
if (parsed.protocol !== "https:")
|
|
1337
|
+
addDiagnostic(diagnostics, source, token.start, "<lexicon uri> must use HTTPS.");
|
|
1338
|
+
} catch {
|
|
1339
|
+
addDiagnostic(diagnostics, source, token.start, "<lexicon uri> must be an absolute HTTPS URL.");
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
if (name === "mstts:silence") {
|
|
1344
|
+
const type = attr(token, "type");
|
|
1345
|
+
const value = attr(token, "value");
|
|
1346
|
+
if (!type || !ALLOWED_SILENCE_TYPES.has(type))
|
|
1347
|
+
addDiagnostic(diagnostics, source, token.start, '<mstts:silence> requires a supported "type" attribute.');
|
|
1348
|
+
if (!value || !/^\d+(?:\.\d+)?(?:ms|s)$/.test(value.trim()))
|
|
1349
|
+
addDiagnostic(diagnostics, source, token.start, '<mstts:silence> requires a time-valued "value" attribute.');
|
|
1350
|
+
}
|
|
1351
|
+
if (name === "mstts:viseme") {
|
|
1352
|
+
const type = attr(token, "type");
|
|
1353
|
+
if (!type || !ALLOWED_VISEME_TYPES.has(type))
|
|
1354
|
+
addDiagnostic(diagnostics, source, token.start, '<mstts:viseme> requires a supported "type" attribute.');
|
|
1355
|
+
}
|
|
1356
|
+
if (name === "audio") {
|
|
1357
|
+
const src = attr(token, "src");
|
|
1358
|
+
if (!src) addDiagnostic(diagnostics, source, token.start, '<audio> requires a "src" attribute.');
|
|
1359
|
+
else {
|
|
1360
|
+
let parsed;
|
|
1361
|
+
try {
|
|
1362
|
+
parsed = new URL(src);
|
|
1363
|
+
} catch {
|
|
1364
|
+
addDiagnostic(diagnostics, source, token.start, "<audio src> must be an absolute HTTP(S) URL.");
|
|
1365
|
+
return;
|
|
1366
|
+
}
|
|
1367
|
+
if (parsed.protocol !== "https:" && !(options.allowHttpAudio && parsed.protocol === "http:"))
|
|
1368
|
+
addDiagnostic(diagnostics, source, token.start, "<audio src> must use HTTPS.");
|
|
1369
|
+
if (options.allowedAudioOrigins && !options.allowedAudioOrigins.includes(parsed.origin))
|
|
1370
|
+
addDiagnostic(diagnostics, source, token.start, `<audio src> origin "${parsed.origin}" is not allowed.`);
|
|
1371
|
+
else if (!options.allowExternalAudio)
|
|
1372
|
+
addDiagnostic(
|
|
1373
|
+
diagnostics,
|
|
1374
|
+
source,
|
|
1375
|
+
token.start,
|
|
1376
|
+
`<audio src> external origin "${parsed.origin}" is blocked by default; set allowExternalAudio to true or provide allowedAudioOrigins.`,
|
|
1377
|
+
"error"
|
|
1378
|
+
);
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
function validateAzureSsml(ssml, options = {}) {
|
|
1383
|
+
const diagnostics = [];
|
|
1384
|
+
if (typeof ssml !== "string") {
|
|
1385
|
+
return [{ line: 1, column: 1, message: "SSML input must be a string", severity: "error" }];
|
|
1386
|
+
}
|
|
1387
|
+
const maxLength = options.maxLength ?? 1e4;
|
|
1388
|
+
if (ssml.length > maxLength)
|
|
1389
|
+
addDiagnostic(diagnostics, ssml, maxLength, `SSML exceeds the maximum length of ${maxLength} characters.`);
|
|
1390
|
+
try {
|
|
1391
|
+
parseSsml(ssml);
|
|
1392
|
+
} catch (error) {
|
|
1393
|
+
const message = error instanceof Error ? error.message.replace(/ at position \d+$/, "") : String(error);
|
|
1394
|
+
const match = / at position (\d+)$/.exec(error instanceof Error ? error.message : "");
|
|
1395
|
+
addDiagnostic(diagnostics, ssml, match ? Number(match[1]) : 0, message);
|
|
1396
|
+
return diagnostics;
|
|
1397
|
+
}
|
|
1398
|
+
const tokens = tokenizeElements(ssml);
|
|
1399
|
+
const speak = tokens.find((token) => token.name.toLowerCase() === "speak");
|
|
1400
|
+
const voices = tokens.filter((token) => token.name.toLowerCase() === "voice");
|
|
1401
|
+
if (!speak || voices.length === 0)
|
|
1402
|
+
addDiagnostic(
|
|
1403
|
+
diagnostics,
|
|
1404
|
+
ssml,
|
|
1405
|
+
speak?.start ?? 0,
|
|
1406
|
+
"Azure SSML requires at least one <voice> element under <speak>."
|
|
1407
|
+
);
|
|
1408
|
+
const voiceName = voices[0] ? attr(voices[0], "name") : void 0;
|
|
1409
|
+
const voiceStyleMap = normalizeVoiceStyleMap(options.customVoiceStyleMap);
|
|
1410
|
+
const policySeverity = diagnosticSeverity(options.unknownVoicePolicy ?? "warn");
|
|
1411
|
+
const voicesToValidate = options.validateNestedVoices === false ? voices.slice(0, 1) : voices;
|
|
1412
|
+
for (const token of voicesToValidate) {
|
|
1413
|
+
const name = attr(token, "name")?.trim();
|
|
1414
|
+
if (name && !voiceStyleMap.has(name.toLowerCase()) && policySeverity)
|
|
1415
|
+
addDiagnostic(
|
|
1416
|
+
diagnostics,
|
|
1417
|
+
ssml,
|
|
1418
|
+
token.start,
|
|
1419
|
+
`Unknown voice "${name}" is not registered in the voice style map.`,
|
|
1420
|
+
policySeverity
|
|
1421
|
+
);
|
|
1422
|
+
}
|
|
1423
|
+
for (const token of tokens) {
|
|
1424
|
+
const tokenVoiceName = options.validateNestedVoices === false ? voiceName : token.parentVoiceName;
|
|
1425
|
+
validateElement(token, ssml, diagnostics, tokenVoiceName, options, voiceStyleMap);
|
|
1426
|
+
}
|
|
1427
|
+
return diagnostics;
|
|
1428
|
+
}
|
|
880
1429
|
// Annotate the CommonJS export names for ESM import in node:
|
|
881
1430
|
0 && (module.exports = {
|
|
882
1431
|
buildPartialSsml,
|
|
883
1432
|
buildSsml,
|
|
1433
|
+
extractSsmlText,
|
|
1434
|
+
mapSsmlTextNodes,
|
|
884
1435
|
parseSsml,
|
|
1436
|
+
validateAzureSsml,
|
|
885
1437
|
validateSsml
|
|
886
1438
|
});
|
|
887
1439
|
//# sourceMappingURL=core.js.map
|