Abstract
Protected
escapeProtected
Escapes special XML characters in text content to ensure valid XML output.
This method replaces XML special characters with their corresponding entity references to prevent XML parsing errors and potential security issues (XML injection). It should be used whenever inserting user-provided or dynamic text content into XML elements.
The following characters are escaped:
&
becomes &
(must be escaped first to avoid double-escaping)<
becomes <
(prevents opening of unintended tags)>
becomes >
(prevents closing of unintended tags)"
becomes "
(prevents breaking out of attribute values)'
becomes '
(prevents breaking out of attribute values)This method is marked as protected
so it's only accessible to classes that extend
SSMLElement, ensuring proper encapsulation while allowing all element implementations
to use this essential functionality.
The text content to escape
The text with all special XML characters properly escaped
// In a render method implementation
class TextElement extends SSMLElement {
private text: string = 'Hello & "world" <script>';
render(): string {
// Escapes to: Hello & "world" <script>
return `<text>${this.escapeXml(this.text)}</text>`;
}
}
// Edge cases handled correctly
this.escapeXml('5 < 10 & 10 > 5');
// Returns: '5 < 10 & 10 > 5'
this.escapeXml('She said "Hello"');
// Returns: 'She said "Hello"'
this.escapeXml("It's a test");
// Returns: 'It's a test'
// Prevents XML injection
this.escapeXml('</voice><voice name="evil">');
// Returns: '</voice><voice name="evil">'
Abstract
renderRenders the element as a valid SSML XML string.
This abstract method must be implemented by all concrete SSML element classes. The implementation should generate the appropriate XML representation of the element, including its opening and closing tags, attributes, and properly escaped content.
The rendered output must be valid according to the SSML 1.0 specification and compatible with Azure Speech Service requirements.
A string containing the XML representation of the SSML element
// Implementation in VoiceElement
class VoiceElement extends SSMLElement {
render(): string {
return `<voice name="en-US-AvaNeural">${content}</voice>`;
}
}
// Implementation in BreakElement
class BreakElement extends SSMLElement {
render(): string {
return `<break time="500ms"/>`;
}
}
// Implementation in EmphasisElement
class EmphasisElement extends SSMLElement {
render(): string {
return `<emphasis level="strong">${this.escapeXml(text)}</emphasis>`;
}
}
Abstract base class for all SSML elements in the library.
This class provides the foundation for all SSML elements (voice, paragraph, sentence, etc.) by defining the common interface and functionality that all elements must implement. It serves as the contract that ensures all elements can be rendered to valid SSML XML and handles the proper escaping of special XML characters.
All SSML element classes must extend this base class and implement the abstract
render()
method to generate their specific XML representation.Example
See