简体   繁体   English

如何使用Javascript将带有特殊字符的多行字符串分配给HTML textarea

[英]How to assign multiline string with special characters to HTML textarea with Javascript

In Flask I render homepage.html and post back to HTML textarea textid the same text on homepage.html 在Flask中,我渲染homepage.html,并将与homepage.html上相同的文本发回到HTML textarea textid

@app.route('/',methods = ['GET', 'POST'])
def result():
    if request.method == 'POST':
        userdata = request.form['Name']
        return render_template("homepage.html", userdata= userdata)

In HTML homepage.html 在HTML homepage.html中

<script>
    var fillinuserinput = function(){
        document.getElementById("textid").value = "{{ userdata }}"
   }
</script>

When userdata(html textarea value) is a single line string with no special characters it works. 当userdata(html textarea值)是单行字符串且没有特殊字符时,它将起作用。 But when its multiline with special characters it fails. 但是,当它的多行带有特殊字符时,它将失败。

Below is the before text(the text is python code) in userdata(html textarea value): 以下是userdata(html textarea值)中的before文本(文本是python代码):

#!/usr/bin/env python #!/ usr / bin / env python

import string 导入字符串

shift = 3 平移= 3

choice = raw_input("would you like to encode or decode?") choice = raw_input(“您想编码还是解码?”)

Below is the userdata coming back from Flask: 以下是从Flask返回的用户数据:

document.getElementById("textid").value = " #!/usr/bin/env python document.getElementById(“ textid”)。value =“#!/ usr / bin / env python

import string 导入字符串

shift = 3 平移= 3

choice = raw_input(& # 3 4;would you like to encode or decode? & # 3 4;) 选择= raw_input(&#3 4;您要编码还是解码?&#3 4;)

The data coming back I get invalid or unexpected token plus I get the &#34(i do put utf-8 on the client and server side, not sure if this matters) 返回的数据我得到了无效或意外的令牌,再加上&#34(我确实将utf-8放在客户端和服务器端,不确定这是否重要)

Any help would be greatly appreciated. 任何帮助将不胜感激。

I'm not too familiar with Flask, but most template languages will treat interpolated input as unsafe, and will escape any text you give it. 我对Flask不太熟悉,但是大多数模板语言会将插值输入视为不安全,并且会转义您输入的所有文本。 If you want to avoid escaping your input, check out http://flask.pocoo.org/docs/0.12/templating/ 如果您想避免转义输入,请查看http://flask.pocoo.org/docs/0.12/templating/

According to that page, there are a few options: 根据该页面,有一些选项:

  1. In the Python code, wrap the HTML string in a Markup object before passing it to the template. 在Python代码中,将HTML字符串包装到Markup对象中,然后再将其传递给模板。 This is in general the recommended way. 通常,这是推荐的方式。
  2. Inside the template, use the |safe filter to explicitly mark a string as safe HTML ({{ myvariable|safe }}) 在模板内部,使用| safe过滤器将字符串显式标记为安全HTML({{myvariable | safe}})。
  3. Temporarily disable the autoescape system altogether. 暂时完全禁用自动转义系统。

To disable the autoescape system in templates, you can use the {% autoescape %} block: 要在模板中禁用自动转义系统,可以使用{%autoescape%}块:

{% autoescape false %}
    <p>autoescaping is disabled here
    <p>{{ will_not_be_escaped }}
{% endautoescape %}

The other related issue is that I assume when you say you have multil-ine input, it is using newlines. 另一个相关的问题是,我假设当您说您有多行输入时,它正在使用换行符。 Those have no meaning in HTML, so you won't see linebreaks in your output. 这些在HTML中没有意义,因此您不会在输出中看到换行符。 You would need to replace newlines with <br> tags to actually get them. 您可能需要用<br>标记替换换行符才能真正获得它们。 That, in combination with marking the strings as "safe" should get you the results you're looking for. 结合将字符串标记为“安全”,可以为您提供所需的结果。 Keep in mind that you will want to sanitize the input ahead of time, then replace the newlines with <br> tags. 请记住,您将需要提前清理输入,然后用<br>标记替换换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM