happy-dom
Version:
Happy DOM is a JavaScript implementation of a web browser without its graphical user interface. It includes many web standards from WHATWG DOM and HTML.
155 lines (141 loc) • 3.33 kB
text/typescript
/**
* Utility for encoding.
*/
export default class XMLEncodeUtility {
/**
* Encodes attribute value.
*
* @param value Value.
* @returns Escaped value.
*/
public static encodeXMLAttributeValue(value: string | null): string {
if (value === null) {
return '';
}
return value
.replace(/&/gu, '&')
.replace(/"/gu, '"')
.replace(/</gu, '<')
.replace(/>/gu, '>')
.replace(/\t/gu, '	')
.replace(/\n/gu, '
')
.replace(/\r/gu, '
');
}
/**
* Decodes attribute value.
*
* @param value Value.
* @returns Decoded value.
*/
public static decodeXMLAttributeValue(value: string | null): string {
if (value === null) {
return '';
}
return value
.replace(/"/gu, '"')
.replace(/</gu, '<')
.replace(/>/gu, '>')
.replace(/	/gu, '\t')
.replace(/
/gu, '\n')
.replace(/
/gu, '\r')
.replace(/&/gu, '&');
}
/**
* Encodes attribute value.
*
* @param value Value.
* @returns Escaped value.
*/
public static encodeHTMLAttributeValue(value: string | null): string {
if (value === null) {
return '';
}
return value.replace(/&/gu, '&').replace(/"/gu, '"');
}
/**
* Decodes attribute value.
*
* @param value Value.
* @returns Decoded value.
*/
public static decodeHTMLAttributeValue(value: string | null): string {
if (value === null) {
return '';
}
return value.replace(/"/gu, '"').replace(/&/gu, '&');
}
/**
* Encodes text content.
*
* @param text Value.
* @returns Escaped value.
*/
public static encodeTextContent(text: string | null): string {
if (text === null) {
return '';
}
return text
.replace(/&/gu, '&')
.replace(/\xA0/gu, ' ')
.replace(/</gu, '<')
.replace(/>/gu, '>');
}
/**
* Decodes text content.
*
* @param text Value.
* @returns Decoded value.
*/
public static decodeTextContent(text: string | null): string {
if (text === null) {
return '';
}
return text
.replace(/ /gu, String.fromCharCode(160))
.replace(/</gu, '<')
.replace(/>/gu, '>')
.replace(/&/gu, '&');
}
/**
* Decodes HTML entities.
*
* @param value Value.
* @returns Decoded value.
*/
public static decodeHTMLEntities(value: string): string {
if (value === null) {
return '';
}
return value
.replace(/</gu, '<')
.replace(/>/gu, '>')
.replace(/ /gu, String.fromCharCode(160))
.replace(/"/gu, '"')
.replace(/'/gu, "'")
.replace(/&#(\d+);/gu, (_match, dec) => String.fromCharCode(parseInt(dec, 10)))
.replace(/&#x([A-Fa-f\d]+);/gu, (_match, hex) => String.fromCharCode(parseInt(hex, 16)))
.replace(/&/gu, '&');
}
/**
* Decodes XML entities.
*
* @param value Value.
* @returns Decoded value.
*/
public static decodeXMLEntities(value: string): string {
if (value === null) {
return '';
}
return (
value
.replace(/</gu, '<')
.replace(/>/gu, '>')
// " " Should not be supported in XML.
.replace(/"/gu, '"')
.replace(/'/gu, "'")
.replace(/&#(\d+);/gu, (_match, dec) => String.fromCharCode(parseInt(dec, 10)))
.replace(/&#x([A-Fa-f\d]+);/gu, (_match, hex) => String.fromCharCode(parseInt(hex, 16)))
.replace(/&/gu, '&')
);
}
}