Expose a public method to escape characters within an attribute · Issue #1278 · jhy/jsoup (original) (raw)

Hello

I need to escape the characters within an attribute in a program that converts an HTML file into a single Java class with a single public static method that converts the Java source code into HTML:
https://github.com/gouessej/HtmlFlow/blob/patch-1/src/main/java/htmlflow/flowifier/AbstractHtmlToJavaHtmlFlowNodeVisitor.java#L319

The package protected static method named "escape" does the correct job by passing inAttribute at true but I can't call it in the source code above because it's not public. Please can you provide a public method that allows to pass inAttribute at true? It would be cleaner than my dirty kludge that doesn't take into account some other cases.

I suggest the addition of the following method:

/**
  * HTML escape an input string within an attribute.
  *
  * @param string the un-escaped string within an attribute to escape
  * @param out the output settings to use
  * @return the escaped string
  */
 public static String escapeInAttribute(String string, Document.OutputSettings out) {
     if (string == null)
         return "";
     StringBuilder accum = StringUtil.borrowBuilder();
     try {
         escape(accum, string, out, true, false, false);
     } catch (IOException e) {
         throw new SerializationException(e); // doesn't happen
     }
     return StringUtil.releaseBuilder(accum);
 }

Best regards and keep up the good work.