Dan*_*don 1 javascript ajax html5 whitespace ace-editor
我写了以下Javascript:
(function () {
var computationModule = (function foo1(stdlib, foreign, heap) {
"use asm";
var sqrt = stdlib.Math.sqrt,
heapArray = new stdlib.Int32Array(heap),
outR = 0.0,
outI = 0.0;
function computeRow(canvasWidth, canvasHeight, limit, max, rowNumber, minR, maxR, minI, maxI) {
canvasWidth = +canvasWidth;
canvasHeight = +canvasHeight;
limit = +limit;
max = max | 0;
rowNumber = +rowNumber;
minR = +minR;
maxR = +maxR;
minI = +minI;
maxI = +maxI;
var columnNumber = 0.0,
zReal = 0.0,
zImaginary = 0.0,
numberToEscape = 0;
var columnNumberInt = 0;
// Compute the imaginary part of the numbers that correspond to pixels in this row.
zImaginary = +(((maxI - minI) * +rowNumber) / +canvasHeight + minI);
// Iterate over the pixels in this row.
// Compute the number of iterations to escape for each pixel that will determine its color.
for (columnNumber = +0; + columnNumber < +canvasWidth; columnNumber = +(+columnNumber + 1.0)) {
// Compute the real part of the number for this pixel.
zReal = +(((maxR - minR) * +columnNumber) / +canvasWidth + minR);
numberToEscape = howManyToEscape(zReal, zImaginary, max, limit) | 0;
columnNumberInt = columnNumberInt + 1 | 0;
heapArray[(columnNumberInt * 4) >> 2] = numberToEscape | 0;
}
}
// Function to determine how many iterations for a point to escape.
function howManyToEscape(r, i, max, limit) {
r = +r;
i = +i;
max = max | 0;
limit = +limit;
var j = 0,
ar = 0.0,
ai = 0.0;
ar = +r;
ai = +i;
for (j = 0;
(j | 0) < (max | 0); j = (j + 1) | 0) {
iteratingFunction(ar, ai, r, i)
ar = outR;
ai = outI;
if (+(ar * ar + ai * ai) >= +(limit * limit))
return j | 0;
}
return j | 0;
}
// The iterating function defining the fractal to draw
// r and i are the real and imaginary parts of the value from the previous iteration
// r0 and i0 are the starting points
function iteratingFunction(r, i, r0, i0) {
r = +r;
i = +i;
r0 = +r0;
i0 = +i0;
computePower(r, i, 2);
// Set the output from this function to t
outR = +(r0 + outR);
outI = +(i0 + outI);
}
// Compute the result of [r,i] raised to the power n.
// Place the resulting real part in outR and the imaginary part in outI.
function computePower(r, i, n) {
// Tell asm.js that r, i are floating point and n is an integer.
r = +r;
i = +i;
n = n | 0;
// Declare and initialize variables to be numbers.
var rResult = 0.0;
var iResult = 0.0;
var j = 0;
var tr = 0.0;
var ti = 0.0;
// Declare and initialize variables that will be used only in the
// event we need to compute the reciprocal.
var abs = 0.0;
var recr = 0.0;
var reci = 0.0;
if ((n | 0) < (0 | 0)) {
// For n less than 0, compute the reciprocal and then raise it to the opposite power.
abs = +sqrt(r * r + i * i);
recr = r / abs;
reci = -i / abs;
r = recr;
i = reci;
n = -n | 0;
}
rResult = r;
iResult = i;
for (j = 1;
(j | 0) < (n | 0); j = (j + 1) | 0) {
tr = rResult * r - iResult * i;
ti = rResult * i + iResult * r;
rResult = tr;
iResult = ti;
}
outR = rResult;
outI = iResult;
} // end computePower
return {
computeRow: computeRow
};
})(self, foreign, heap);
// Return computationModule that we just defined.
return computationModule;
})();
Run Code Online (Sandbox Code Playgroud)
除了我想让我的Web应用程序在ACE文本编辑器(http://ace.c9.io/)中显示Javascript 以便用户可以在运行时修改代码之外,这个Javascript没有什么特别不寻常的.
我使用jQuery AJAX加载Javascript,然后将ACE编辑器的内容设置为Javascript代码.用户修改代码后,可以单击按钮运行代码.(这个使用eval)
我遇到的问题是ACE显示奇怪的字符而不是空格.
奇怪的是,如果我尝试从ACE编辑器复制文本,奇怪的字符就会消失,空格也很正常.此外,即使显示这些奇怪的字符,代码也能正常运行.
我还注意到使用Firefox时没有出现问题,但它出现在Chrome和IE 11中.
最后,问题只发生在我将代码放在真实的Web服务器上时.它不会在我的开发环境中重现.
再看一下,我可以看到它不只是我通过AJAX加载的文本.即使我键入新空格,也会出现更多文本字符!
可能出现什么问题,以至于文本显示不正确?
这是应用程序的链接:http://danielsadventure.info/html5fractal/
charset="utf-8"在包含ace的脚本标记中使用.
<script src="path/to/ace.js" charset="utf-8">
这可能与此有关:
当发送方未提供显式字符集参数时,"文本"类型的媒体子类型被定义为在通过HTTP接收时具有默认字符集值"ISO-8859-1".除"ISO-8859-1"或其子集之外的字符集中的数据必须用适当的字符集值标记.有关兼容性问题,请参见第3.4.1节.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1
因此,传递给脚本的字符串的编码与ACE(或一般JS)所期望的编码不同,即UTF-8.