1

更新 1

現在、テキストを WT プロジェクトにロードしている方法を次に示します。

wApp->require("ace.js");

//orignal XML, reads in incorrectly on one line
//std::string data = ReadFile("Q:\\settings.xml");

//XML after being formatted in notepad to look like xml, reads in correctly
//std::string data = ReadFile("Q:\\settings.txt");

//changed extension back to XML, edited in notepad++ to XML format, reads in correctly
std::string data = ReadFile("Q:\\settings_from_text.xml");

//test xml tag, reads in correctly
//std::string data = "<tag_1>some tag content</tag_1>";

//test xml tag with newline, reads in incorrectly on one line, doesnt read newline
//std::string data = "<tag_1>some tag content</tag_1>\n<tag_1>some tag content</tag_1>";

_ace_editor = new WText(data, Wt::PlainText);

//_ace_editor->setText(data);
_ace_editor->setInline(false);

// A WContainerWidget is rendered as a div
_ace_editor->resize(1000, 500);

std::string editor_ref = _ace_editor->jsRef(); // is a text string that will be the element when executed in JS

std::string command =
  editor_ref + "._ace_editor = ace.edit(" + editor_ref + ");" +
  editor_ref + "._ace_editor.setTheme(\"ace/theme/chrome\");" +
  editor_ref + "._ace_editor.getSession().setMode(\"ace/mode/xml\");";// +
  //editor_ref + "._ace_editor.setValue(\"" + data + "\");";

_ace_editor->doJavaScript(command);

また、ここに ReadFile 関数があります

std::ifstream in(path, std::ios::in | std::ios::binary);
if(in)
{
  std::string contents;
  in.seekg(0, std::ios::end);
  contents.resize(in.tellg());
  in.seekg(0, std::ios::beg);
  in.read(&contents[0], contents.size());
  in.close();
  return(contents);
}
throw(errno);

元の投稿

いくつかの XML ファイルを WT ( http://www.webtoolkit.eu/wt?wtd =rqBfShGlNupXgK3M1sWOxUk1Loz3BsW0 ) ページ。問題は、何らかの理由で XML ファイルのすべてのタグがロードから省略されていることです。例: 次の内容の XML ファイル

<?xml version="1.0"?>
<settings>
    <tag_1>some tag content</tag_1>
    <tag_2/>
</settings>

としてロードされます

some tag content

タグの内容だけでなく、XML ファイル全体が必要です。

少し調査を行った後、さまざまなフォーラムで同じことを尋ねる他のかなりの数の人々を見つけましたが、これまでに試したことはすべてうまくいきませんでした。

これには、Ace モードを XML に設定する、ace ウィンドウに設定する前に別のコンテナーにテキストを読み込もうとする、配色を変更する、別の方法でファイルを解析するなどが含まれます。

私はVisual Studio 2010を使用しています。デバッグから、ファイルがすべてのタグを含む文字列に完全に読み込まれることがわかりますが、Aceウィンドウに設定された後、それらは省略されます。

4

2 に答える 2

4

WT ページに配置するかどうかに関係なく、最終的にこれは JavaScript に関する質問です。ACE エディターとは JavaScript ツールです。xml コンテンツをどのようにロードしているかについては何も示されていないので、xml ファイルのコンテンツをページの出力ソースに書き込んでいるに違いないと推測できますか?? ソースを表示すると、タグが表示されますか? もしそうなら、あなたはそれについて間違っています。以下の完全に機能する例で示すように、xml ファイルは javascript/ajax を介してロードする必要があります (サーバー上の xml ファイルの場所への $.ajax 呼び出しの「url」を編集します)。タグとすべてのコンテンツが表示されます。 xml ファイルの。ajax リクエスト コードを単純化するためだけに、jQuery ライブラリを追加しました。楽しみ!

<!DOCTYPE html>
<html lang="en">
<head>
<title>ACE in Action</title>
<style type="text/css" media="screen">
    #editor { 
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    }
</style>
</head>
<body>

<div id="editor"></div>

<script src="http://rawgithub.com/ajaxorg/ace-builds/master/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
    var callback = function (data, status, xhr) {
        //data will be the xml returned from the server
        if (status == 'success') {
            var editor = ace.edit("editor");
            //apparently, only modes supported are 'html', 'javascript' & 'text'
            editor.getSession().setMode("ace/mode/html");
            editor.setValue(data);
        }
    };
    //using jQuery to fire off an ajax request to load the xml,
    //using our callback as the success function
    $.ajax(
        {
            url : '/testing/cd_catalog.xml',
            dataType : 'text', //explicitly requesting the xml as text, rather than an xml document
            success : callback
        }
    );

</script>
</body>
</html>

実際、「javascript/ajax を介してロードする必要がある」と述べたことの一部を取り消します。コンテンツを事前にエディタ div に入れるという ACE の例に従っているだけであることがわかりました。html または xml コンテンツでこれを行う場合、エディター div の innerHTML をコピーしてからエディターをインスタンス化し、その値を以前に保存した innerHTML に設定しない限り、タグはブラウザーによって評価され、表示されません。例えば:

<div id="editor"><?xml version="1.0" encoding="ISO-8859-1">
<books>
<text>some text content</text>
<book/>
</books></div>

<script src="http://rawgithub.com/ajaxorg/ace-builds/master/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
    var txt = document.getElementById('editor').innerHTML;
    var editor = ace.edit("editor");
    //editor.setTheme("ace/theme/monokai");
    editor.getSession().setMode("ace/mode/html");
    editor.setValue(txt);
</script>
于 2013-04-22T13:08:46.680 に答える
1

XML の XML フラグメント... 適切にエスケープしない限り、ブラウザがそれらを解釈することを何とか期待できます。これを試して:

txt = new WText("<bla>something</bla>", Wt::PlainText);

これにより、テキスト内のすべての XML っぽい文字がエスケープされます。

Wt のデフォルト (XHTMLText) は、入力を XML として解析しようとし、成功すると、XML から可能な XSS ベクターをフィルタリングしてから、XML としてブラウザーに送信します。テキストを XML として解析できない場合は、XML っぽい文字をエスケープして、リベラルなパーサーを備えたブラウザーが意図せずに攻撃ベクトルを実行するのを回避します。

3 番目のオプション (XHTMLUnsafeText) は XSS フィルタリングをバイパスします - 危険なので、テキストが安全であり、ユーザーが直接的または間接的に影響を与えることができないことがわかっている場合にのみ使用してください。

于 2013-04-24T09:09:15.380 に答える