Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

20
LINES

< > BotCompany Repo | #1001263 // htmlencode

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (57L/1K).

static String htmlencode(Object o) {
  ret htmlencode(str(o));
}

static String htmlencode(String s) {
  if (s == null) ret "";
  StringBuilder out = new StringBuilder(Math.max(16, s.length()));
  for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&') {
      int cp = s.codePointAt(i);
      out.append("&#x");
      out.append(intToHex_flexLength(cp));
      out.append(';');
      i += Character.charCount(cp)-1;
    } else
      out.append(c);
  }
  ret out.toString();
}

Author comment

http://stackoverflow.com/questions/1265282/recommended-method-for-escaping-html-in-java

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001263
Snippet name: htmlencode
Eternal ID of this version: #1001263/7
Text MD5: 2fbcc218224eea94068930c91d4f1a29
Transpilation MD5: d1fba18eab5f7930490101e19da1f0fd
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-12 23:24:35
Source code size: 569 bytes / 20 lines
Pitched / IR pitched: No / No
Views / Downloads: 788 / 2323
Version history: 6 change(s)
Referenced in: [show references]