防止Jsoup丢弃额外的空格 [英] Prevent Jsoup from discarding extra whitespace

查看:506
本文介绍了防止Jsoup丢弃额外的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jsoup来清理表单中的用户输入。有问题的表单包含一个< textarea> ,它需要纯文本。提交表单时,我用 Jsoup.clean(textareaContents)清理输入;但是,由于html忽略了额外的空格, Jsoup.clean()将从输入中删除有价值的空白字符。

I'm using Jsoup for sanitizing user input from a form. The form in question contains a <textarea> that expects plain text. When the form is submitted, I clean the input with Jsoup.clean(textareaContents); however, since html ignores extra whitespace, Jsoup.clean() will remove valuable whitespace characters from the input.

例如,如果有人在 textarea 中输入了一些文本行:

For example, if someone entered some lines of text in the textarea:

hello

test

Jsoup.clean之后(),您将拥有:

hello test

如何使 Jsoup.clean()保留空格?我知道它是为解析html而设计的,这不是html,所以有更好的选择吗?

How can you make Jsoup.clean() preserve whitespace? I know it's designed for parsing html and this isn't html, so is there a better alternative?

推荐答案

如果你的textarea只是期待纯文本,然后我认为你最好只用HTML转义纯文本。即将用户的输入< > 标记转换为& lt; & gt; 。输入我们的输出(输入可能更安全,所以你只需要考虑一次)。

If your textarea just expects plain text, then I think you'd be better off just HTML escaping the plain text. I.e. convert user's input < and > tags to &lt; and &gt; respectively. Either on input our output (input might be safer so you only need to think about it once).

jsoup HTML cleaner 旨在解析不受信任的输入HTML和outuput trusted HTML,其中使用元素进行格式化。

The jsoup HTML cleaner is, as you say, designed to parse untrusted input HTML and outuput trusted HTML, where formatting is done with elements.

这篇关于防止Jsoup丢弃额外的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆