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   

Travelled to 10 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

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: 459 / 1782
Version history: 6 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1001263 & http://1001263.tinybrain.de