jjzjj

javascript - Internet Explorer XML 解析错误,无法重现示例

coder 2024-06-27 原文

我正在调试一个不是我写的更大系统的以下代码。有来自服务器的 XML 通过 AJAX 未正确解析。下面解释一下这个问题。

请注意连字符 - 如果 CDATA 部分中有连字符,Internet Explorer 会插入它们。

例如当我打印时

console.log(a.item(4).childNodes.item(0));

我明白了

`{"INCD":"30362790021","sycd":"BKJ2` 

为了调试问题,我尝试创建一个最小示例。

我已将变量 source(通过设置条件为 .ocmmand == 'List_detail' 的断点)复制到一个简单的 HTML Javascript 文件,如下所示:

<script>

    var source = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE primrose SYSTEM \"dtd/primrose.dtd\">\n<primrose><array type=\"string\"> .... </primrose>";

    var xml = new DOMParser().parseFromString(source, 'text/xml');
    var a = xml.documentElement.childNodes;
    console.log(a);
    console.log(a.item(4).childNodes.item(0));
</script>

全长示例位于 http://pastebin.com/GaHdjiWW

但这似乎工作得很好:

当我运行 console.log(a.item(4).childNodes.item(0)); 我得到

{"INCD":"30362790021","sycd":"BKJ2-2","type_code":"00000050555","type_name":"BKJ","series_code":"110302280810","web_product_id":"10302280810","series_name":"十字槽盘头螺钉","disp_brandName":"MISUMI","brd_code":"MSM1","ecal_brd_code":"MSM","main_photo":"MSM1/PHOTO/10302280810.jpg","catch_copy":null,"cad_2d":"1","cad_2d_type":"1","cad_3d":"1","cad_3d_type":"2","scene7_img_product":[ {"img_fileName":"110302280810_20149999_m_01_99999_jp","img_description":""}],"ary_displayLink":null,"ary_param_verify":"BKJ{2}\t{2}","ary_param_disp":"BKJ{2}\t{2}","param_conv_bef":null,"param_conv_bef_disp":null,"canOrder":"1"}

等等……

Internet Explorer 版本为 11.0.9600.18314

我知道IE10, 11 CDATA with hyphens parsed wrong它似乎是 IE 中的一个错误。但为什么我不能在同一个浏览器中重现它?

最佳答案

我将其追溯到将 MutationObserver.observe 与 childList 和子树选项一起使用:https://github.com/talee/mutationobserver-breaks-characterdata . 解决方法:在 IE <> 中使用已弃用的 MutationEvents。 https://www.npmjs.com/package/mutationobserver-polyfill为 IE 11 做这项工作。

破解代码:

var observer = new MutationObserver(function() {});
// Other mutation types/combos work fine
observer.observe(document, {
  childList: true,
  subtree: true
});

测试:

var parser = new DOMParser();
var expectedContent = 'hello-world';
// <div>hello-world</div> breaks too. Using CDATA as that's my current use
// case.
var xml = parser.parseFromString('<div><![CDATA[' + expectedContent + ']]></div>','text/xml');
var result = xml.firstChild.firstChild.data;

// Output test results for viewing
if (result != expectedContent) {
  statusOut.textContent = 'FAIL';
} else {
  statusOut.textContent = 'SUCCESS';
}
resultOut.textContent = 'Result: "' + result + '"';

我在 https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8589859/ 上为 IE 11 创建了一个错误.

然而基于https://connect.microsoft.com/IE/feedback/details/1398926/ie11-does-not-parse-cdata-containing-hyphens-correctly ,该问题不太可能得到解决,因为它与安全无关:

Posted by Brad [MSFT] on 2/17/2016 at 12:02 PM

I see. Thank you for the repro. I am able to repro using IE 11.29.10586.0 Unfortunately we are no longer working IE bugs unless they are security related. Sorry to be the bearer of bad news. I will leave the feedback in its current state since it does not apply to Edge.

关于javascript - Internet Explorer XML 解析错误,无法重现示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37655982/

有关javascript - Internet Explorer XML 解析错误,无法重现示例的更多相关文章

  1. Ruby 解析字符串 - 2

    我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  6. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  7. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  8. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  9. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  10. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

随机推荐