MediaWiki:Script/Element.js : Différence entre versions

Ligne 7 : Ligne 7 :
 
   var sourceKeys = Object.keys(source);
 
   var sourceKeys = Object.keys(source);
  
   for (var i = 0; i < sourceKeys.length; i++) {
+
   for (var keyIndex = 0; keyIndex < sourceKeys.length; keyIndex++) {
     var prop = sourceKeys[i];
+
     var property = sourceKeys[keyIndex];
     var propValue = source[prop];
+
     var propValue = source[property];
  
     if (prop === 'viewBox') {
+
     if (property === 'viewBox') {
       target.setAttribute(prop, propValue);
+
       target.setAttribute(property, propValue);
  
     } else if (prop.indexOf("data") === 0 || allowedProps.indexOf(prop) !== -1) {
+
     } else if (property.indexOf("data") === 0 || allowedProps.indexOf(property) !== -1) {
       target.setAttribute(camelToKebab(prop), propValue);
+
       target.setAttribute(camelToKebab(property), propValue);
 
     }
 
     }
 
   }
 
   }
Ligne 21 : Ligne 21 :
  
  
 
+
function createElement(elementsToReplace, elementsNS, allowedProperties, elementDataset, elementType) {
function createLabelInput(element, allowedProperties, dataset) {
 
 
    
 
    
   var label = document.createElement("label");
+
   if (elementsNS.indexOf(elementType) !== -1) {
  var input = document.createElement("input");
+
    var newElement = document.createElementNS("http://www.w3.org/2000/svg", elementType);
     
+
   } else {
  copyProperties(dataset, input, allowedProperties);
+
    var newElement = document.createElement(elementType);
 
+
   }
  label.setAttribute("for", input.id);
 
  label.appendChild(input);
 
  label.appendChild(document.createTextNode(element.textContent));
 
 
 
  return label;
 
}
 
 
 
 
 
function createSvgPath(element, allowedProperties, dataset) {
 
 
 
  var svg = document.createElementNS(dataset["xmlns"], "svg");
 
   var path = document.createElementNS(dataset["xmlns"], "path");
 
  var title = document.createElement("title");
 
    
 
  var dAttribute = dataset["d"];
 
  var titleContent = dataset["title"];
 
 
 
  delete dataset["d"];
 
  delete dataset["title"];
 
 
 
  copyProperties(dataset, svg, allowedProperties);
 
 
 
  path.setAttribute("d", dAttribute);
 
  title.textContent = titleContent;
 
 
 
  svg.appendChild(title);
 
  svg.appendChild(path);
 
 
 
  return svg;
 
}
 
 
 
 
 
function createElement(element, allowedProperties, dataset, elementType) {
 
 
    
 
    
   var newElement = document.createElement(elementType);
+
   var childdNodes = elementsToReplace.childNodes;
  var children = element.childNodes;
 
 
        
 
        
   copyProperties(dataset, newElement, allowedProperties);
+
   copyProperties(elementDataset, newElement, allowedProperties);
 
        
 
        
   for (var i = 0; i < children.length; i++) {
+
   for (var childIndex = 0; childIndex < childdNodes.length; childIndex++) {
    var child = children[i];
+
     newElement.appendChild(childdNodes[childIndex].cloneNode(true));
     newElement.appendChild(child.cloneNode(true));
 
 
   }
 
   }
 
    
 
    
Ligne 77 : Ligne 41 :
  
  
function replaceElement(element, allowedElements, allowedProperties) {
+
function replaceElement(elementsToReplace, allowedElements, elementsNS, allowedProperties) {
 
    
 
    
   var dataset = element.dataset;
+
   var elementDataset = elementsToReplace.dataset;
   var elementType = dataset.element;
+
   var elementType = elementDataset.element;
 
    
 
    
 
   if (allowedElements.indexOf(elementType) !== -1) {
 
   if (allowedElements.indexOf(elementType) !== -1) {
      
+
     var newElement = createElement(elementsToReplace, elementsNS, allowedProperties, elementDataset, elementType);
    if (elementType === "label-input") {
+
 
      var newElement = createLabelInput(element, allowedProperties, dataset);
+
     elementsToReplace.parentNode.replaceChild(newElement, elementsToReplace);
     } else if (elementType === "svg-path") {
+
  } else {
      var newElement = createSvgPath(element, allowedProperties, dataset);
+
     elementsToReplace.remove();
    } else {
 
      var newElement = createElement(element, allowedProperties, dataset, elementType);
 
    }
 
   
 
     element.parentNode.replaceChild(newElement, element);
 
 
   }
 
   }
 
}
 
}
Ligne 99 : Ligne 58 :
 
(function(){
 
(function(){
 
    
 
    
  var elementsToReplace = document.querySelectorAll("div[data-element]");
+
   var allowedElements = ["input", "button", "select", "option", "label", "form", "svg", "path", "title"];
   var allowedElements = ["input", "button", "select", "option", "label", "form", "label-input", "svg-path"];
+
  var elementsNS = ["svg", "path", "title"];
   var allowedProperties = ["id", "class", "type", "name", "ariaHaspopup", "ariaExpanded", "ariaControls", "for", "min", "max", "value", "style", "pattern", "required", "selected", "xmlns", "height", "viewBox", "width", "fill", "d", "title"];
+
   var allowedProperties = ["id", "class", "type", "name", "ariaHaspopup", "ariaExpanded", "ariaControls", "for", "min", "max", "value", "style", "pattern", "required", "selected", "checked", "xmlns", "height", "viewBox", "width", "fill", "d", "title"];
 
+
 
   for (var i = elementsToReplace.length - 1; i >= 0; i--) {
+
   var elementsToReplace = document.querySelector("div[data-element]");
   
+
 
    var element = elementsToReplace[i];
+
  while (elementsToReplace) {
   
+
    replaceElement(elementsToReplace, allowedElements, elementsNS, allowedProperties);
    var children = element.children;
+
     elementsToReplace = document.querySelector("div[data-element]");
   
 
    for (var j = children.length - 1; j >= 0; j--) {
 
      var child = children[j];
 
      replaceElement(child, allowedElements, allowedProperties);
 
     }
 
   
 
    replaceElement(element, allowedElements, allowedProperties);
 
 
   }
 
   }
 
})();
 
})();

Version du 1 octobre 2023 à 18:32

function camelToKebab(str) {
  return str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
}


function copyProperties(source, target, allowedProps) {
  var sourceKeys = Object.keys(source);

  for (var keyIndex = 0; keyIndex < sourceKeys.length; keyIndex++) {
    var property = sourceKeys[keyIndex];
    var propValue = source[property];

    if (property === 'viewBox') {
      target.setAttribute(property, propValue);

    } else if (property.indexOf("data") === 0 || allowedProps.indexOf(property) !== -1) {
      target.setAttribute(camelToKebab(property), propValue);
    }
  }
}


function createElement(elementsToReplace, elementsNS, allowedProperties, elementDataset, elementType) {
  
  if (elementsNS.indexOf(elementType) !== -1) {
    var newElement = document.createElementNS("http://www.w3.org/2000/svg", elementType);
  } else {
    var newElement = document.createElement(elementType);
  }
  
  var childdNodes = elementsToReplace.childNodes;
      
  copyProperties(elementDataset, newElement, allowedProperties);
      
  for (var childIndex = 0; childIndex < childdNodes.length; childIndex++) {
    newElement.appendChild(childdNodes[childIndex].cloneNode(true));
  }
  
  return newElement;
}


function replaceElement(elementsToReplace, allowedElements, elementsNS, allowedProperties) {
  
  var elementDataset = elementsToReplace.dataset;
  var elementType = elementDataset.element;
  
  if (allowedElements.indexOf(elementType) !== -1) {
    var newElement = createElement(elementsToReplace, elementsNS, allowedProperties, elementDataset, elementType);

    elementsToReplace.parentNode.replaceChild(newElement, elementsToReplace);
  } else {
    elementsToReplace.remove();
  }
}


(function(){
  
  var allowedElements = ["input", "button", "select", "option", "label", "form", "svg", "path", "title"];
  var elementsNS = ["svg", "path", "title"];
  var allowedProperties = ["id", "class", "type", "name", "ariaHaspopup", "ariaExpanded", "ariaControls", "for", "min", "max", "value", "style", "pattern", "required", "selected", "checked", "xmlns", "height", "viewBox", "width", "fill", "d", "title"];
  
  var elementsToReplace = document.querySelector("div[data-element]");
  
  while (elementsToReplace) {
    replaceElement(elementsToReplace, allowedElements, elementsNS, allowedProperties);
    elementsToReplace = document.querySelector("div[data-element]");
  }
})();