@samitouri / QOS-React / commits / 51d16473b0

Create ms-unsafe functions in one place, simplifies code.

jim committed Feb 17, 2016 at 21:10 UTC 51d16473b0d583ec5a5ab0e5ec5da2b032cf4029
4 files changed +50 -38
src/renderers/dom/client/utils/DOMChildrenOperations.js
+7 -13
@@ -9,8 +9,6 @@
9 * @providesModule DOMChildrenOperations
10 */
11
12 -/* globals MSApp */
13 -
12 'use strict';
13
14 var DOMLazyTree = require('DOMLazyTree');
@@ -18,6 +16,7 @@ var Danger = require('Danger');
16 var ReactMultiChildUpdateTypes = require('ReactMultiChildUpdateTypes');
17 var ReactPerf = require('ReactPerf');
18
19 +var createMicrosoftUnsafeLocalFunction = require('createMicrosoftUnsafeLocalFunction');
20 var setInnerHTML = require('setInnerHTML');
21 var setTextContent = require('setTextContent');
22
@@ -33,19 +32,14 @@ function getNodeAfter(parentNode, node) {
32 * @param {number} index Index at which to insert the child.
33 * @internal
34 */
36 -function insertChildAt(parentNode, childNode, referenceNode) {
37 - // We rely exclusively on `insertBefore(node, null)` instead of also using
38 - // `appendChild(node)`. (Using `undefined` is not allowed by all browsers so
39 - // we are careful to use `null`.)
40 -
41 - if (typeof MSApp !== 'undefined' && MSApp.execUnsafeLocalFunction) {
42 - MSApp.execUnsafeLocalFunction(function() {
43 - parentNode.insertBefore(childNode, referenceNode);
44 - });
45 - } else {
35 +var insertChildAt = createMicrosoftUnsafeLocalFunction(
36 + function(parentNode, childNode, referenceNode) {
37 + // We rely exclusively on `insertBefore(node, null)` instead of also using
38 + // `appendChild(node)`. (Using `undefined` is not allowed by all browsers so
39 + // we are careful to use `null`.)
40 parentNode.insertBefore(childNode, referenceNode);
41 }
48 -}
42 +);
43
44 function insertLazyTreeChildAt(parentNode, childTree, referenceNode) {
45 DOMLazyTree.insertTreeBefore(parentNode, childTree, referenceNode);
src/renderers/dom/client/utils/DOMLazyTree.js
+5 -11
@@ -9,10 +9,9 @@
9 * @providesModule DOMLazyTree
10 */
11
12 -/* globals MSApp */
13 -
12 'use strict';
13
14 +var createMicrosoftUnsafeLocalFunction = require('createMicrosoftUnsafeLocalFunction');
15 var setTextContent = require('setTextContent');
16
17 /**
@@ -52,17 +51,12 @@ function insertTreeChildren(tree) {
51 }
52 }
53
55 -function insertTreeBefore(parentNode, tree, referenceNode) {
56 - if (typeof MSApp !== 'undefined' && MSApp.execUnsafeLocalFunction) {
57 - MSApp.execUnsafeLocalFunction(function() {
58 - parentNode.insertBefore(tree.node, referenceNode);
59 - });
60 - } else {
54 +var insertTreeBefore = createMicrosoftUnsafeLocalFunction(
55 + function(parentNode, tree, referenceNode) {
56 parentNode.insertBefore(tree.node, referenceNode);
57 + insertTreeChildren(tree);
58 }
63 -
64 - insertTreeChildren(tree);
65 -}
59 +);
60
61 function replaceChildWithTree(oldNode, newTree) {
62 oldNode.parentNode.replaceChild(newTree.node, oldNode);
src/renderers/dom/client/utils/createMicrosoftUnsafeLocalFunction.js new
+31
@@ -0,0 +1,31 @@
1 +/**
2 + * Copyright 2013-present, Facebook, Inc.
3 + * All rights reserved.
4 + *
5 + * This source code is licensed under the BSD-style license found in the
6 + * LICENSE file in the root directory of this source tree. An additional grant
7 + * of patent rights can be found in the PATENTS file in the same directory.
8 + *
9 + * @providesModule createMicrosoftUnsafeLocalFunction
10 + */
11 +
12 +/* globals MSApp */
13 +
14 +'use strict';
15 +
16 +/**
17 + * Create a function which has 'unsafe' privileges (required by windows8 apps)
18 + */
19 +var createMicrosoftUnsafeLocalFunction = function(func) {
20 + if (typeof MSApp !== 'undefined' && MSApp.execUnsafeLocalFunction) {
21 + return function(arg0, arg1, arg2, arg3) {
22 + MSApp.execUnsafeLocalFunction(function() {
23 + return func(arg0, arg1, arg2, arg3);
24 + });
25 + };
26 + } else {
27 + return func;
28 + }
29 +};
30 +
31 +module.exports = createMicrosoftUnsafeLocalFunction;
src/renderers/dom/client/utils/setInnerHTML.js
+7 -14
@@ -9,8 +9,6 @@
9 * @providesModule setInnerHTML
10 */
11
12 -/* globals MSApp */
13 -
12 'use strict';
13
14 var ExecutionEnvironment = require('ExecutionEnvironment');
@@ -18,6 +16,8 @@ var ExecutionEnvironment = require('ExecutionEnvironment');
16 var WHITESPACE_TEST = /^[ \r\n\t\f]/;
17 var NONVISIBLE_TEST = /<(!--|link|noscript|meta|script|style)[ \r\n\t\f\/>]/;
18
19 +var createMicrosoftUnsafeLocalFunction = require('createMicrosoftUnsafeLocalFunction');
20 +
21 /**
22 * Set the innerHTML property of a node, ensuring that whitespace is preserved
23 * even in IE8.
@@ -26,18 +26,11 @@ var NONVISIBLE_TEST = /<(!--|link|noscript|meta|script|style)[ \r\n\t\f\/>]/;
26 * @param {string} html
27 * @internal
28 */
29 -var setInnerHTML = function(node, html) {
30 - node.innerHTML = html;
31 -};
32 -
33 -// Win8 apps: Allow all html to be inserted
34 -if (typeof MSApp !== 'undefined' && MSApp.execUnsafeLocalFunction) {
35 - setInnerHTML = function(node, html) {
36 - MSApp.execUnsafeLocalFunction(function() {
37 - node.innerHTML = html;
38 - });
39 - };
40 -}
29 +var setInnerHTML = createMicrosoftUnsafeLocalFunction(
30 + function(node, html) {
31 + node.innerHTML = html;
32 + }
33 +);
34
35 if (ExecutionEnvironment.canUseDOM) {
36 // IE8: When updating a just created node with innerHTML only leading