Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EncodeAndDecodeXML.js #11

Open
jnbdz opened this issue Nov 9, 2015 · 0 comments
Open

EncodeAndDecodeXML.js #11

jnbdz opened this issue Nov 9, 2015 · 0 comments
Labels

Comments

@jnbdz
Copy link
Member

jnbdz commented Nov 9, 2015

/*

---
description: Two useful methods for stings to encode and decode html/xml elements

authors:
  - Jean-Nicolas Boulay Desjardins (http://jean-nicolas.name)

license:
  - MIT-style license

requires:
 - core/1.3:   '*'

provides:
  - encodeXML
  - decodeXML
...
*/

String.implement({
encodeXML: function(){  
    var xml_to_escaped = {
        '&': '&',
        '"': '"',
        '<': '&lt;',
        '>': '&gt;'
    };
    return String(this).replace(/([\&"<>])/g, function(str, item) {
        return xml_to_escaped[item];
    });
},
decodeXML: function(){
    var escaped_to_xml = {
        '&amp;': '&',
        '&quot;': '"',
        '&lt;': '<',
        '&gt;': '>'
    };        
    return String(this).replace(/(&quot;|&lt;|&gt;|&amp;)/g, function(str, item) {
        return escaped_to_xml[item];
    });
}
});
@jnbdz jnbdz added the Proposal label Nov 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant