Xhtml validation in TypeScript Rich text editor control
18 Nov 20182 minutes to read
The editor includes an enableXhtml property that allows for continuous validation of the Rich Text Editor’s source content against the XHTML standard. When content is entered or modified in the editor, this feature ensures ongoing compliance by automatically removing invalid elements and attributes.
The editor checks the following settings on validation:
Validating attributes
- Case Sensitivity: All attributes must be in lowercase.
- Quotation Marks: Proper use of quotation marks around attribute values is enforced.
- Validity: Only valid attributes for corresponding HTML elements are allowed.
- Required Attributes: All required attributes for HTML elements must be included.
Validating HTML elements
- Case Sensitivity: All HTML tags must be in lowercase.
- Proper Closing: All opening tags must have corresponding closing tags.
- Element Validity: Only valid HTML elements are permitted.
- Nesting: Elements must be properly nested to maintain structure.
- Root Element: The content must have a single root element.
- Element Hierarchy: Inline elements cannot contain block elements.
Cross-Site scripting (XSS)
The Rich Text Editor allows users to edit the content with security by preventing cross-site scripting (XSS). By default, it provides built-in support to remove elements from editor content that cause XSS attacks. The editor removes the elements based on the attributes if it is possible to execute a script.
Enable XSS validation
The enableHtmlSanitize property determines whether XSS prevention is active. It’s set to true by default.
In the following sample, we removed the script tag and onmouseover attribute from the content of the Rich Text Editor.
The XSS prevention feature is only applicable when the editorMode is set to HTML.
Custom cross-site scripting prevention
For more precise control over XSS prevention, you can implement custom filtering logic using the beforeSanitizeHtml event.
Implementing custom cross-site scripting and filtering in Rich Text Editor
- Use the beforeSanitizeHtml event to define custom filtering rules.
- Utilize the
helperfunction from the event argument to apply your custom filters. - Set the
cancelargument totrueif you want to override the built-in XSS prevention entirely.
The following sample demonstrates how to filter the script tag by value.
You can also filter out the e.selectors.tags and e.selector.attributes in the beforeSanitizeHtml event to control which HTML tags and attributes are allowed to appear.
For instance, if you want to display <iframe>, By manipulating the e.selectors.tags property in this event, you can selectively remove tags like <iframe>. This approach ensures that your application can safely display iframe while preventing potential security risks associated with XSS vulnerabilities.
The following sample demonstrates how to filter the iframe tag.