To prevent Cross-Site Scripting (XSS) attacks, TCS SAPI provides isSafeHTML() which validates HTML content for dangerous patterns, and encodeForHTML() which encodes special characters to prevent script execution. The isSafeHTML method checks if HTML input is safe for a given context, while encodeForHTML converts characters like < and > to safe HTML entities. Using both provides defense-in-depth against XSS vulnerabilities.