1

usIにはonSubmitを含むhtmlがあります:

<form id="login_box" onSubmit="alert('{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}'); this.submit(); this.reset(); return false;">

問題は、私が何をしても、ネストされた引用符をエスケープしないことです。私はもう試した:

<form id="login_box" onSubmit="alert('{{=T(\'Thank you for contacting us! We have received your email and will contact you shortly.\')}}'); this.submit(); this.reset(); return false;">

<form id="login_box" onSubmit="alert(\"{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}\"); this.submit(); this.reset(); return false;">

<form id="login_box" onSubmit=\"alert(\\"{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}\\"); this.submit(); this.reset(); return false;\">

そして、それが機能しないことを知っていても、私が考えることができる他のコンボ。私が試みることは、ネストされた引用符をエスケープすることではありません。

4

4 に答える 4

2

Pythonで小枝または同様のテンプレートシステムを使用していると思います。

翻訳したい場合は{{=T('message to translate')}}、その文字列をカスタム属性に入れるだけです。

例:

<form id="login_box" msg="{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}" onSubmit="alert(this.getAttribute('msg')); this.submit(); this.reset(); return false;">

:)

于 2012-06-14T18:57:48.333 に答える
1

これがweb2pyテンプレートにあると仮定すると、元のコードは次のようになります。

onSubmit="alert('{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}'); this.submit(); this.reset(); return false;"

次のHTMLを生成します。

onSubmit="alert('Thank you for contacting us! We have received your email and will contact you shortly.'); this.submit(); this.reset(); return false;"

私の知る限り、これはブラウザで正常に機能します。ただし、アラート内のこれらの一重引用符をエスケープしたい場合は、次のようにすることができます。

onSubmit="alert({{="'%s'" % T('Thank you for contacting us! We have received your email and will contact you shortly.')}}); this.submit(); this.reset(); return false;"

これにより、次のHTMLが生成されます。

onSubmit="alert(&#x27;Thank you for contacting us! We have received your email and will contact you shortly.&#x27;); this.submit(); this.reset(); return false;"

これはブラウザでも機能するはずです。

区切り文字内のすべて{{ }}はPythonコードであり、サーバー上で実行され、応答に書き込む前にエスケープされます。区切り文字の外側はすべて{{ }}そのまま応答に含まれます(つまり、web2pyによるエスケープは行われません)。元のコードでは、アラートの単一引用符はweb2pyテンプレートの区切り文字の外側にあるため、web2pyによってエスケープされず、そのまま配信されます。これについては、web2pyブックのこのセクションで詳しく説明しています。

于 2012-06-15T19:01:38.590 に答える
0

返された値を HTML エンコードし、JSON としてエンコードします。

于 2012-06-14T19:24:01.953 に答える
0

HTML 属性では、一部の文字 ("および&) を HTML エンティティとしてエンコードする必要があります。JavaScript 文字列では、文字列区切り文字 (例: ') をバックスラッシュを使用してエスケープする必要があります。これは、最初のものが機能することを意味します。

<form id="login_box" onSubmit="alert('{{=T(\'Thank you for contacting us! We have received your email and will contact you shortly.\')}}'); this.submit(); this.reset(); return false;">

クリックするとアラート{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}が表示されます。これがあなたが望むものだと仮定しますか?

編集:

{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}

文字列を返すので、

<form id="login_box" onSubmit="alert('{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}'); this.submit(); this.reset(); return false;">

となります

<form id="login_box" onSubmit="alert('Thank you for contacting us! We have received your email and will contact you shortly.'); this.submit(); this.reset(); return false;">

レンダリングされた HTML で。正解です。ただし、バックスラッシュをエスケープする必要があり、html エンティティは T 関数の出力を (この順序で) エンコードします。

于 2012-06-14T18:36:59.573 に答える