tizen-tv-dev-cli
Version:
command cli from tizen devtools sdk
42 lines (36 loc) • 1.53 kB
JavaScript
var xml_special_to_encoded_attribute = {
'&': '&',
'<': '<',
'"': '"',
'\r': '
',
'\n': '
',
'\t': '	'
}
var xml_special_to_encoded_text = {
'&': '&',
'<': '<',
'>': '>',
'\r': '
'
}
function encodeSpecialCharactersInAttribute(attributeValue){
return attributeValue
.replace(/[\r\n\t ]+/g, ' ') // White space normalization (Note: this should normally be done by the xml parser) See: https://www.w3.org/TR/xml/#AVNormalize
.replace(/([&<"\r\n\t])/g, function(str, item){
// Special character normalization. See:
// - https://www.w3.org/TR/xml-c14n#ProcessingModel (Attribute Nodes)
// - https://www.w3.org/TR/xml-c14n#Example-Chars
return xml_special_to_encoded_attribute[item]
});
}
function encodeSpecialCharactersInText(text){
return text
.replace(/\r\n?/g, '\n') // Line ending normalization (Note: this should normally be done by the xml parser). See: https://www.w3.org/TR/xml/#sec-line-ends
.replace(/([&<>\r])/g, function(str, item){
// Special character normalization. See:
// - https://www.w3.org/TR/xml-c14n#ProcessingModel (Text Nodes)
// - https://www.w3.org/TR/xml-c14n#Example-Chars
return xml_special_to_encoded_text[item]
});
}
exports.encodeSpecialCharactersInAttribute = encodeSpecialCharactersInAttribute;
exports.encodeSpecialCharactersInText = encodeSpecialCharactersInText;