Skip to content

Commit

Permalink
HtmlUtils.isAllowedXmlCharacter allow emojis in xml response
Browse files Browse the repository at this point in the history
Signed-off-by: pizzi80 <[email protected]>
  • Loading branch information
pizzi80 committed Nov 13, 2024
1 parent 5e87226 commit d550ec4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
21 changes: 18 additions & 3 deletions impl/src/main/java/com/sun/faces/util/HtmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.sun.faces.util;

import static java.lang.Character.isHighSurrogate;
import static java.lang.Character.isLowSurrogate;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
Expand Down Expand Up @@ -324,13 +327,25 @@ public static void writeAttribute(Writer out, boolean escapeUnicode, boolean esc

private static boolean isPrintableControlChar(int ch, boolean forXml) {

return (ch == 0x09 || ch == 0x0A || (ch == 0x0C && !forXml) || ch == 0x0D);

boolean isPrintable = ch == 0x09 || ch == 0x0A || (ch == 0x0C && !forXml) || ch == 0x0D;
// if (!isPrintable) System.out.println(">>>>>>>>>>>" + ch + " xml? " + forXml + " is not printable: ");
return isPrintable;
}

public static boolean isAllowedXmlCharacter(int ch) {

// if it's a special char se è un emoji non è un carattere "singolo" ma una coppia high/low surrogate
if ( isEmoji((char)ch) ) return true;

// See https://www.w3.org/TR/xml/#charsets Character Range
return ch < 0x20 ? isPrintableControlChar(ch, true) : ch <= 0xD7FF || ch >= 0xE000 && ch <= 0xFFFD;
boolean isAllowedXmlChar = ch < 0x20 ? isPrintableControlChar(ch, true) : ch <= 0xD7FF || ch >= 0xE000 && ch <= 0xFFFD;
// if (!isAllowedXmlChar) System.out.println(">>>>>>>>>>>" + ch + " is not allowed");
return isAllowedXmlChar;
}

/** @return true if it's an emoji char. an emoji is a char with high/low surrogate pair */
public static boolean isEmoji( char c ) {
return isHighSurrogate(c) || isLowSurrogate(c);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions impl/src/test/java/com/sun/faces/util/HtmlUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ void testAllowedXmlCharacter() {
assertTrue(isAllowedXmlCharacter(c));
}

for (int c = 0xD800; c <= 0xDFFF; c++) {
assertFalse(isAllowedXmlCharacter(c));
}
// problematic test
// for (int c = 0xD800; c <= 0xDFFF; c++) {
// assertFalse(isAllowedXmlCharacter(c));
// }

for (int c = 0xE000; c <= 0xFFFD; c++) {
assertTrue(isAllowedXmlCharacter(c));
Expand Down

0 comments on commit d550ec4

Please sign in to comment.