资讯专栏INFORMATION COLUMN

【速记】React解决IE浏览器svg标签不支持innerHTML操作的问题及相关拓展知识

smallStone / 1539人阅读

摘要:代码资料文件文件文件关于系列的的网页的操作需要权限的相关文档关于浏览器无法一些元素无法设置属性的解决方案和原因

react代码资料:

文件:packages/react-dom/src/client/setInnerHTML.js

/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow
 */

import {Namespaces} from "../shared/DOMNamespaces";
import createMicrosoftUnsafeLocalFunction from "../shared/createMicrosoftUnsafeLocalFunction";

// SVG temp container for IE lacking innerHTML
let reusableSVGContainer;

/**
 * Set the innerHTML property of a node
 *
 * @param {DOMElement} node
 * @param {string} html
 * @internal
 */
const setInnerHTML = createMicrosoftUnsafeLocalFunction(function(
  node: Element,
  html: string,
): void {
  // IE does not have innerHTML for SVG nodes, so instead we inject the
  // new markup in a temp node and then move the child nodes across into
  // the target node

  if (node.namespaceURI === Namespaces.svg && !("innerHTML" in node)) {
    reusableSVGContainer =
      reusableSVGContainer || document.createElement("div");
    reusableSVGContainer.innerHTML = "" + html + "";
    const svgNode = reusableSVGContainer.firstChild;
    while (node.firstChild) {
      node.removeChild(node.firstChild);
    }
    while (svgNode.firstChild) {
      node.appendChild(svgNode.firstChild);
    }
  } else {
    node.innerHTML = html;
  }
});

export default setInnerHTML;

文件:packages/react-dom/src/shared/createMicrosoftUnsafeLocalFunction.js

/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

/* globals MSApp */

/**
 * Create a function which has "unsafe" privileges (required by windows8 apps)
 */
const createMicrosoftUnsafeLocalFunction = function(func) {
  if (typeof MSApp !== "undefined" && MSApp.execUnsafeLocalFunction) {
    return function(arg0, arg1, arg2, arg3) {
      MSApp.execUnsafeLocalFunction(function() {
        return func(arg0, arg1, arg2, arg3);
      });
    };
  } else {
    return func;
  }
};

export default createMicrosoftUnsafeLocalFunction;

文件:packages/react-dom/src/shared/DOMNamespaces.js

/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

const HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
const MATH_NAMESPACE = "http://www.w3.org/1998/Math/MathML";
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";

export const Namespaces = {
  html: HTML_NAMESPACE,
  mathml: MATH_NAMESPACE,
  svg: SVG_NAMESPACE,
};
关于Windows8系列的APP的网页的innerHTML操作需要权限的相关文档:

execUnsafeLocalFunction method : https://msdn.microsoft.com/zh...

The new Windows 10 security model for HTML/Javascript apps. : https://github.com/MicrosoftE...

关于IE浏览器无法一些元素无法设置innerHTML属性的解决方案和原因:

https://stackoverflow.com/que...

The property is read/write for all objects except the following, for which it is read-only: COL, COLGROUP, FRAMESET, HEAD, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR.
function setTBodyInnerHTML(tbody, html) {
  var temp = tbody.ownerDocument.createElement("div");
  temp.innerHTML = "" + html + "
"; tbody.parentNode.replaceChild(temp.firstChild.firstChild, tbody); }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/99391.html

相关文章

  • 《JavaScript高级程序设计》(第3版)读书笔记 第11章 DOM拓展

    摘要:对的两个主要拓展是选择和。以下插入标记的拓展已经纳入了规范。在写模式下,会根据指定的字符串创建新的子树,然后用这个子树完全替换调用元素。在删除带有时间处理程序或引用了其他对象子树时,就有可能导致内存占用问题。 尽管DOM作为API已经非常完善了,但为了实现更多功能,仍然会有一些标准或专有的拓展。2008年之前,浏览器中几乎所有的拓展都是专有的,此后W3C着手将一些已经成为事实标准的专...

    luck 评论0 收藏0
  • js速记

    摘要:相关最大的特性就在于直接操纵网页上的节点,从而实现网页的局部刷新而非全局刷新。该回调函数会在送回响应的时候被调用。当然了,如果浏览器不支持对象,会返回,在这时需要进行额外的处理。 前言 马上就要参加一个团队项目进行React的前端开发了。最近正在着手熟练React语法,然后发现本质上还是建立在对javascript的深刻理解上。市面上在js基础上封装出了非常多优秀的车轮,其中最被新手广...

    MageekChiu 评论0 收藏0
  • js基础知识笔记

    摘要:常见内存泄漏情形全局变量被忘记的或者闭包引用闭包概念有权访问另一个函数作用域的变量的函数。会话存储刷新页面依旧存在,与在持久上不同外,其余一致。请求向指定的资源提交被处理的数据,数据量和类型没限制,不主动缓存,页面刷新数据会被重新提交。 defer 脚本延迟执行,适用于外部脚本文件async 立即下载,不保证顺序(建议不修改DOM,避免重绘) CDN加速 (Content De...

    李文鹏 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<