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

Expose a public method to escape characters within an attribute #1278

Closed
ghost opened this issue Nov 13, 2019 · 1 comment
Closed

Expose a public method to escape characters within an attribute #1278

ghost opened this issue Nov 13, 2019 · 1 comment
Assignees
Milestone

Comments

@ghost
Copy link

ghost commented Nov 13, 2019

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.

@jhy jhy closed this as completed in 9ba6dc7 Jul 8, 2024
@jhy jhy self-assigned this Jul 8, 2024
@jhy jhy added this to the 1.18.1 milestone Jul 8, 2024
@jhy
Copy link
Owner

jhy commented Jul 8, 2024

Thanks for the suggestion - I have updated Entities.escape(string) so that it's output is suitable for both text data and attributes. It's more maximal in what gets escaped, so that the string can be used in either context. The output from Element.html() etc is still more conservative, and uses the context of if in an attribute or in text to decide what to escape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant