mirror of
https://github.com/maierfelix/POGOserver.git
synced 2026-07-18 01:02:35 -05:00
177 lines
7.5 KiB
JavaScript
177 lines
7.5 KiB
JavaScript
// Copyright (c) Microsoft Open Technologies, Inc. Tous droits réservés.
|
||
// Sous licence Apache, version 2.0.
|
||
// Consultez http://www.apache.org/licenses/LICENSE-2.0.html.
|
||
// Shim de contenu dynamique JavaScript pour applications du Windows Store
|
||
(function () {
|
||
|
||
if (window.MSApp && MSApp.execUnsafeLocalFunction) {
|
||
|
||
// Certains nœuds ont une propriété "attributes" qui masque la propriété Node.prototype.attributes
|
||
// ce qui signifie que nous ne voyons pas réellement les attributs de Node (curieusement, la console de débogage VS
|
||
// semble affectée par le même problème).
|
||
//
|
||
var Element_setAttribute = Object.getOwnPropertyDescriptor(Element.prototype, "setAttribute").value;
|
||
var Element_removeAttribute = Object.getOwnPropertyDescriptor(Element.prototype, "removeAttribute").value;
|
||
var HTMLElement_insertAdjacentHTMLPropertyDescriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "insertAdjacentHTML");
|
||
var Node_get_attributes = Object.getOwnPropertyDescriptor(Node.prototype, "attributes").get;
|
||
var Node_get_childNodes = Object.getOwnPropertyDescriptor(Node.prototype, "childNodes").get;
|
||
var detectionDiv = document.createElement("div");
|
||
|
||
function getAttributes(element) {
|
||
return Node_get_attributes.call(element);
|
||
}
|
||
|
||
function setAttribute(element, attribute, value) {
|
||
try {
|
||
Element_setAttribute.call(element, attribute, value);
|
||
} catch (e) {
|
||
// ignorer
|
||
}
|
||
}
|
||
|
||
function removeAttribute(element, attribute) {
|
||
Element_removeAttribute.call(element, attribute);
|
||
}
|
||
|
||
function childNodes(element) {
|
||
return Node_get_childNodes.call(element);
|
||
}
|
||
|
||
function empty(element) {
|
||
while (element.childNodes.length) {
|
||
element.removeChild(element.lastChild);
|
||
}
|
||
}
|
||
|
||
function insertAdjacentHTML(element, position, html) {
|
||
HTMLElement_insertAdjacentHTMLPropertyDescriptor.value.call(element, position, html);
|
||
}
|
||
|
||
function inUnsafeMode() {
|
||
var isUnsafe = true;
|
||
try {
|
||
detectionDiv.innerHTML = "<test/>";
|
||
}
|
||
catch (ex) {
|
||
isUnsafe = false;
|
||
}
|
||
|
||
return isUnsafe;
|
||
}
|
||
|
||
function cleanse(html, targetElement) {
|
||
var cleaner = document.implementation.createHTMLDocument("cleaner");
|
||
empty(cleaner.documentElement);
|
||
MSApp.execUnsafeLocalFunction(function () {
|
||
insertAdjacentHTML(cleaner.documentElement, "afterbegin", html);
|
||
});
|
||
|
||
var scripts = cleaner.documentElement.querySelectorAll("script");
|
||
Array.prototype.forEach.call(scripts, function (script) {
|
||
switch (script.type.toLowerCase()) {
|
||
case "":
|
||
script.type = "text/inert";
|
||
break;
|
||
case "text/javascript":
|
||
case "text/ecmascript":
|
||
case "text/x-javascript":
|
||
case "text/jscript":
|
||
case "text/livescript":
|
||
case "text/javascript1.1":
|
||
case "text/javascript1.2":
|
||
case "text/javascript1.3":
|
||
script.type = "text/inert-" + script.type.slice("text/".length);
|
||
break;
|
||
case "application/javascript":
|
||
case "application/ecmascript":
|
||
case "application/x-javascript":
|
||
script.type = "application/inert-" + script.type.slice("application/".length);
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
});
|
||
|
||
function cleanseAttributes(element) {
|
||
var attributes = getAttributes(element);
|
||
if (attributes && attributes.length) {
|
||
// comme la collection d'attributs est dynamique, il est plus simple de mettre en file d'attente les changements de noms
|
||
var events;
|
||
for (var i = 0, len = attributes.length; i < len; i++) {
|
||
var attribute = attributes[i];
|
||
var name = attribute.name;
|
||
if ((name[0] === "o" || name[0] === "O") &&
|
||
(name[1] === "n" || name[1] === "N")) {
|
||
events = events || [];
|
||
events.push({ name: attribute.name, value: attribute.value });
|
||
}
|
||
}
|
||
if (events) {
|
||
for (var i = 0, len = events.length; i < len; i++) {
|
||
var attribute = events[i];
|
||
removeAttribute(element, attribute.name);
|
||
setAttribute(element, "x-" + attribute.name, attribute.value);
|
||
}
|
||
}
|
||
}
|
||
var children = childNodes(element);
|
||
for (var i = 0, len = children.length; i < len; i++) {
|
||
cleanseAttributes(children[i]);
|
||
}
|
||
}
|
||
cleanseAttributes(cleaner.documentElement);
|
||
|
||
var cleanedNodes = [];
|
||
|
||
if (targetElement.tagName === 'HTML') {
|
||
cleanedNodes = Array.prototype.slice.call(document.adoptNode(cleaner.documentElement).childNodes);
|
||
} else {
|
||
if (cleaner.head) {
|
||
cleanedNodes = cleanedNodes.concat(Array.prototype.slice.call(document.adoptNode(cleaner.head).childNodes));
|
||
}
|
||
if (cleaner.body) {
|
||
cleanedNodes = cleanedNodes.concat(Array.prototype.slice.call(document.adoptNode(cleaner.body).childNodes));
|
||
}
|
||
}
|
||
|
||
return cleanedNodes;
|
||
}
|
||
|
||
function cleansePropertySetter(property, setter) {
|
||
var propertyDescriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, property);
|
||
var originalSetter = propertyDescriptor.set;
|
||
Object.defineProperty(HTMLElement.prototype, property, {
|
||
get: propertyDescriptor.get,
|
||
set: function (value) {
|
||
if (window.WinJS && window.WinJS._execUnsafe && inUnsafeMode()) {
|
||
originalSetter.call(this, value);
|
||
} else {
|
||
var that = this;
|
||
var nodes = cleanse(value, that);
|
||
MSApp.execUnsafeLocalFunction(function () {
|
||
setter(propertyDescriptor, that, nodes);
|
||
});
|
||
}
|
||
},
|
||
enumerable: propertyDescriptor.enumerable,
|
||
configurable: propertyDescriptor.configurable,
|
||
});
|
||
}
|
||
cleansePropertySetter("innerHTML", function (propertyDescriptor, target, elements) {
|
||
empty(target);
|
||
for (var i = 0, len = elements.length; i < len; i++) {
|
||
target.appendChild(elements[i]);
|
||
}
|
||
});
|
||
cleansePropertySetter("outerHTML", function (propertyDescriptor, target, elements) {
|
||
for (var i = 0, len = elements.length; i < len; i++) {
|
||
target.insertAdjacentElement("afterend", elements[i]);
|
||
}
|
||
target.parentNode.removeChild(target);
|
||
});
|
||
|
||
}
|
||
|
||
}());
|