简体   繁体   English

Dom 解析在 IE11 中的 React App 上无法正常工作

[英]Dom parsing doesn't correctly work on React App in IE11

I am very tired with searсhing the hidden rat that causes my app to not correctly work in IE11.我对搜索导致我的应用程序无法在 IE11 中正常工作的隐藏老鼠感到非常厌倦。 My react App works properly in all browsers except for IE11.我的 react 应用程序在除 IE11 之外的所有浏览器中都能正常工作。 IE11 support is must-have. IE11 支持是必须的。 I obtain data from server and show it to the user in ModalBar.我从服务器获取数据并在 ModalBar 中显示给用户。 When I select the order from table of orders and click on printing the invoice the modalbar appears and all placeholders will be filled with obtained data.当我从订单表中选择订单并单击打印发票时,会出现模式栏,所有占位符都将填充获得的数据。

this is in chrome:这是在铬: 镀铬

and now this is in IE11:现在这是在 IE11 中: 在此处输入图片说明

the data obtained from server:从服务器获取的数据: 在此处输入图片说明

I figured out that placeholders which come not as object, and not as array of objects filled properly but "ops" and "details" placeholders not.我发现占位符不是作为对象出现的,也不是作为正确填充的对象数组,而是“操作”和“细节”占位符不是。 Ops and details come as array of objects and I use DOMParser to work with them.操作和细节以对象数组的形式出现,我使用 DOMParser 来处理它们。 Here is the code:这是代码:

prepareDocumentContent(document, documentData) {
let content = document.body;
console.log(88888888880, content);
if (isObject(documentData)) {
  const placeholders = document.placeholders.split(',').map(item => item.trim());
  let parsedArrays = [];
  for(let k = 0; k < placeholders.length; k++) {
    const placeholder = placeholders[k];
    const isObjectValue = placeholder.includes('.');
    if (isObjectValue) {
      const objectName = placeholder.split('.')[0];
      if (documentData[objectName] instanceof Array) {
        if (parsedArrays.indexOf(objectName) < 0) {
          parsedArrays = [
            ...parsedArrays,
            objectName
          ];
          const parser = new DOMParser();
          const doc = parser.parseFromString(content, 'text/html');
          const elements = doc.getElementsByClassName(objectName);
          for (let ei = 0; ei < elements.length; ei++) {
            const element = elements.item(ei);
            let replace = '';
            const arr = documentData[objectName];
            for (let i = 0; i < arr.length; i++) {
              let newElement = element.outerHTML;
              console.log('newElement', newElement);
              const item = arr[i];
              const names = Object.keys(item);
              for (let j = 0; j < names.length; j++) {
                const itemName = names[j];
                const regex = new RegExp(`{${objectName}` + '.' + `${itemName}}`, 'g');
                const replacing = typeof item[itemName] === 'undefined'
                  ? (!this.props.isIgnoreNotSpecified ? i18n.t('ModalWysiwygBar.not_specified') : '')
                  : item[itemName];
                console.log('replacing', replacing);
                newElement = newElement.replace(
                  regex,
                  replacing
                );
              }
              replace = replace + newElement;
              console.log('replace', replace);
            }
            content = content.replace(element.outerHTML, replace);
            console.log('contentObtained', content);
          }
        }
      } else {
        const regex = new RegExp(`{${placeholder}}`, 'g');
        const objectNotExists = typeof documentData[objectName] === 'undefined' || typeof documentData[objectName][placeholder.split('.')[1]] === 'undefined';
        const replacing = objectNotExists? (!this.props.isIgnoreNotSpecified ? i18n.t('ModalWysiwygBar.not_specified') : '' )
          : documentData[objectName][placeholder.split('.')[1]];
        content = content.replace(
          regex,
          replacing
        );
      }
    } else {
      const regex = new RegExp(`{${placeholder}}`, 'g');
      const replacing = !documentData[placeholder]
        ? (!this.props.isIgnoreNotSpecified ? i18n.t('ModalWysiwygBar.not_specified') : '')
        : documentData[placeholder];
      content = content.replace(
        regex,
        replacing
      );
    }
  }
} else {
  content = documentData.replace(/\n/g, '<br />');
}
const placeholders = document.placeholders.split(',');
placeholders.map(placeholder => {
  const regex = new RegExp(`{${placeholder}}`, 'g');
  const replacing = !this.props.isIgnoreNotSpecified ? i18n.t('ModalWysiwygBar.not_specified') : '';
  content = content.replace(
    regex,
    replacing
  );
});
return content;
}

I think the problem is with this code section:我认为问题出在这个代码部分:

if (documentData[objectName] instanceof Array) {
        if (parsedArrays.indexOf(objectName) < 0) {
          parsedArrays = [
            ...parsedArrays,
            objectName
          ];
          const parser = new DOMParser();
          const doc = parser.parseFromString(content, 'text/html');
          const elements = doc.getElementsByClassName(objectName);
          for (let ei = 0; ei < elements.length; ei++) {
            const element = elements.item(ei);
            let replace = '';
            const arr = documentData[objectName];
            for (let i = 0; i < arr.length; i++) {
              let newElement = element.outerHTML;
              console.log('newElement', newElement);
              const item = arr[i];
              const names = Object.keys(item);
              for (let j = 0; j < names.length; j++) {
                const itemName = names[j];
                const regex = new RegExp(`{${objectName}` + '.' + `${itemName}}`, 'g');
                const replacing = typeof item[itemName] === 'undefined'
                  ? (!this.props.isIgnoreNotSpecified ? i18n.t('ModalWysiwygBar.not_specified') : '')
                  : item[itemName];
                console.log('replacing', replacing);
                newElement = newElement.replace(
                  regex,
                  replacing
                );
              }
              replace = replace + newElement;
              console.log('replace', replace);
            }
            content = content.replace(element.outerHTML, replace);
            console.log('contentObtained', content);
          }
        }
      }

other sections of code in this function do own work rightly.此函数中的其他代码部分正确地执行自己的工作。 I examined the console logs in IE11 and it seems that the function begins to work correctly but from the output I get cutted shortened html string.我检查了 IE11 中的控制台日志,似乎该功能开始正常工作,但从输出中我得到了缩短的 html 字符串。 And explorer doesnt show is the string shortened or not on mouse hover.并且资源管理器不显示鼠标悬停时字符串是否缩短。 Nothing understandable.没什么好理解的。 I attached screens of chrome and IE.我附加了 chrome 和 IE 的屏幕。

Chrome, everything right: Chrome,一切正常: 在此处输入图片说明

IE11, simple shortened string: IE11,简单的缩短字符串: 在此处输入图片说明

在此处输入图片说明

Why IE11 doesn't show full string as other browsers?为什么 IE11 不像其他浏览器那样显示完整的字符串? And in another point of view, if IE11 does not form html string correctly then html will be broken and no data will be shown in modalBar.从另一个角度来看,如果 IE11 没有正确形成 html 字符串,那么 html 将被破坏,并且 modalBar 中不会显示任何数据。 But there are all tables all stricture shown except for some unfilled placeholders.但是除了一些未填充的占位符外,所有表格都显示了所有限制。 I think it means that IE11 form html string rightly, but just does not show full string in its console.我认为这意味着 IE11 正确地形成了 html 字符串,但只是没有在其控制台中显示完整的字符串。 But why?但为什么? and IE11 begin to fill ops and details correctly, but again, in the end, it shows shortened string and lamentable result in the modal window.和 IE11 开始正确填充操作和细节,但最后,它再次在模式窗口中显示缩短的字符串和可悲的结果。

HOW TO Overcome the problem in IE11 ?如何克服 IE11 中的问题? where is the rat?老鼠在哪里? Thanks in advance for your help在此先感谢您的帮助

This might be because you are using some es6 functions/styles of javascript in your react app and its not working in IE11.这可能是因为您在 React 应用程序中使用了一些 es6 函数/样式的 javascript 并且它在 IE11 中不起作用。 Please make sure your babel configured properly to work with ie11.请确保您的 babel 配置正确以与 ie11 一起使用。 it should be something like this:-它应该是这样的:-

{
  "targets": {
    "chrome": "58",
    "ie": "11"
  }
}

Please refer this for more bable documentation请参阅此以获取更多bable 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM