Attribute values need to be encoded. If I'm building a jQuery object like so:
$('<div data-value="' + value + '">');
Really, value must be attribute encoded like so:
$('<div data-value="' + HtmlAttributeEncode(value) + '">');
I cannot find such a native function. Some suggest it's simply a matter of replacing double quotes with ", but Microsoft's HttpEncoder.HtmlAttributeEncode method encodes these four characters & < " '. I've seem implementations such as quoteattr here: https://stackoverflow.com/a/9756789/88409, but that is horribly inefficient, calling replace to iterate over the string multiple times. Likewise, I need a native function for encoding a javascript string (e.g. $('<div onclick="var s =\'' + HtmlAttributeEncode(JavaScriptStringEncode(value)) + '\';alert(s);"></div>).appendTo(body); << contrived example for illustration only)
Is there a native equivalent of this functionality?
Note: Please don't mention escape (which is now deprecated in favor of encodeURI and encodeURIComponent) all of which have nothing to do with attribute encoding.