本文介绍了有没有办法让 webkit 的 javascript 正确处理空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

事实证明,在处理 XSLT 时,webkit 对空格的表现似乎不是特别好.它似乎平等地对待换行符、回车符、制表符和空格,甚至到 substring-after('test string','
') 将返回 string.

It turns out webkit doesn't appear to behave particularly well with whitespace while processing an XSLT. It seems to treat newline, carriage returns, tabs and spaces equally, even to the point where substring-after('test string','
') will return string.

有没有办法让它正常运行,或者这是 webkit 中没有解决方法的错误?

Is there any way of getting it to behave properly, or is this a bug in webkit that doesn't have a workaround?

这是一些演示错误的 javascript:

Here's some javascript that demonstrates the error:

var parser = new DOMParser();
var xsltText = "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"version=\"1.0\"><xsl:template match=\"test\"><output><xsl:value-of select=\"string-length(substring-after(., '&#10;'))\"/></output></xsl:template></xsl:stylesheet>";
var xslt = parser.parseFromString(xsltText, "text/xml");
var xmlText = "<test>test string</test>";
var xml = parser.parseFromString(xmlText, "text/xml");
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslt);
var processed = xsltProcessor.transformToDocument(xml);
var result = processed.firstChild.textContent;

result 变量显然应该包含0";在 Safari 4 和 Chrome 8 中,它返回6".

The result variable should obviously contain '0'; in Safari 4 and Chrome 8 it returns '6'.

推荐答案

我无法在 Safari 5.0.3 中重现该问题.测试用例(在浏览器窗口中加载的 XML 文档):

I can't reproduce that problem with Safari 5.0.3.Test case (XML document loaded in browser window):

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test2011012801Xsl.xml"?>
<test>foo bar</test>

样式表在哪里

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:template match="/">
    <html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="test">
    <p><xsl:value-of select="string-length(substring-after(., '&#10;'))"/></p>
  </xsl:template>

</xsl:stylesheet>

Firefox (3.6) 和 Windows 上的 Safari 中的输出均为0".因此,至少在该测试中,没有证据表明文本节点中的空格字符被视为换行符.

Output in both Firefox (3.6) and Safari on Windows is "0". So at least with that test there is no evidence that a space character in a text node is treated as a newline character.

这篇关于有没有办法让 webkit 的 javascript 正确处理空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:53