Dotclear


Ignore:
Timestamp:
09/18/18 20:22:10 (7 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
default
Message:

Use let and const rather than var (ES2015/ES6), use template string where is more efficient

File:
1 edited

Legend:

Unmodified
Added
Removed
  • plugins/dcLegacyEditor/js/jsToolBar/jsToolBar.wysiwyg.js

    r3736 r3880  
    6464jsToolBar.prototype.syncContents = function(from) { 
    6565  from = from || 'textarea'; 
    66   var This = this; 
     66  const This = this; 
    6767  if (from == 'textarea') { 
    6868    initContent(); 
    6969  } else { 
    7070    this.validBlockquote(); 
    71     var html = this.applyHtmlFilters(this.ibody.innerHTML); 
     71    let html = this.applyHtmlFilters(this.ibody.innerHTML); 
    7272    if (html == '<br />') { 
    7373      html = '<p></p>'; 
     
    8686      This.ibody.innerHTML = This.applyWysiwygFilters(This.textarea.value); 
    8787      if (This.ibody.createTextRange) { //cursor at the begin for IE 
    88         var IErange = This.ibody.createTextRange(); 
     88        const IErange = This.ibody.createTextRange(); 
    8989        IErange.execCommand("SelectAll"); 
    9090        IErange.collapse(); 
     
    9292      } 
    9393    } else { 
    94       var idoc = This.iwin.document; 
    95       var para = idoc.createElement('p'); 
     94      const idoc = This.iwin.document; 
     95      const para = idoc.createElement('p'); 
    9696      para.appendChild(idoc.createElement('br')); 
    9797      while (idoc.body.hasChildNodes()) { 
     
    108108}; 
    109109jsToolBar.prototype.applyHtmlFilters = function(str) { 
    110   for (var fn in this.htmlFilters) { 
     110  for (let fn in this.htmlFilters) { 
    111111    str = this.htmlFilters[fn].call(this, str); 
    112112  } 
     
    115115jsToolBar.prototype.wysiwygFilters = {}; 
    116116jsToolBar.prototype.applyWysiwygFilters = function(str) { 
    117   for (var fn in this.wysiwygFilters) { 
     117  for (let fn in this.wysiwygFilters) { 
    118118    str = this.wysiwygFilters[fn].call(this, str); 
    119119  } 
     
    143143 */ 
    144144jsToolBar.prototype.initWindow = function() { 
    145   var This = this; 
     145  const This = this; 
    146146 
    147147  this.iframe = document.createElement('iframe'); 
     
    160160 
    161161  function initIframe() { 
    162     var doc = This.iframe.contentWindow.document; 
     162    const doc = This.iframe.contentWindow.document; 
    163163    if (!doc) { 
    164164      setTimeout(initIframe, 1); 
     
    167167 
    168168    doc.open(); 
    169     var html = 
    170       '<html>\n' + 
    171       '<head>\n' + 
    172       '<link rel="stylesheet" href="style/default.css" type="text/css" media="screen" />' + 
    173       '<style type="text/css">' + This.iframe_css + '</style>\n' + 
    174       (This.base_url != '' ? '<base href="' + This.base_url + '" />' : '') + 
    175       '</head>\n' + 
    176       '<body>\n' + 
    177       '</body>\n' + 
    178       '</html>'; 
     169    const html = 
     170`<html> 
     171  <head> 
     172    <link rel="stylesheet" href="style/default.css" type="text/css" media="screen" /> 
     173    <style type="text/css">${This.iframe_css}</style> 
     174    ${This.base_url != '' ? `<base href="${This.base_url}" />` : ''} 
     175  </head> 
     176  <body></body> 
     177</html>`; 
    179178 
    180179    doc.write(html); 
     
    212211    } 
    213212 
    214     for (var evt in This.iwinEvents) { 
    215       var event = This.iwinEvents[evt]; 
     213    for (let evt in This.iwinEvents) { 
     214      const event = This.iwinEvents[evt]; 
    216215      This.addIwinEvent(This.iframe.contentWindow.document, event.type, event.fn, This); 
    217216    } 
     
    227226}; 
    228227jsToolBar.prototype.addIwinEvent = function(target, type, fn, scope) { 
    229   var myFn = function(e) { 
     228  const myFn = function(e) { 
    230229    fn.call(scope, e); 
    231230  }; 
     
    260259  } 
    261260 
    262   var This = this; 
     261  const This = this; 
    263262 
    264263  function setLink(title, link) { 
    265     var li = document.createElement('li'); 
    266     var a; 
     264    const li = document.createElement('li'); 
     265    let a; 
    267266    if (link) { 
    268267      a = document.createElement('a'); 
     
    307306      this.iwin.document.designMode = 'on'; 
    308307    } catch (e) {} // Firefox needs this 
    309     var This = this; 
     308    const This = this; 
    310309    setTimeout(function() { 
    311310      This.iframe.contentWindow.focus(); 
     
    326325}; 
    327326jsToolBar.prototype.resizeDragMove = function(event) { 
    328   var new_height = (this.dragStartH + event.clientY - this.dragStartY) + 'px'; 
     327  const new_height = `${this.dragStartH + event.clientY - this.dragStartY}px`; 
    329328  if (this.iframe != undefined) { 
    330329    this.iframe.style.height = new_height; 
     
    338337 */ 
    339338jsToolBar.prototype.insertNode = function(node) { 
    340   var range; 
     339  let range; 
    341340 
    342341  if (this.iwin.getSelection) { // Gecko 
    343     var sel = this.iwin.getSelection(); 
     342    const sel = this.iwin.getSelection(); 
    344343    range = sel.getRangeAt(0); 
    345344 
     
    366365  } else { // IE 
    367366    // lambda element 
    368     var p = this.iwin.document.createElement('div'); 
     367    const p = this.iwin.document.createElement('div'); 
    369368    p.appendChild(node); 
    370369    range = this.iwin.document.selection.createRange(); 
     
    381380 */ 
    382381jsToolBar.prototype.getSelectedNode = function() { 
    383   var sel; 
     382  let sel; 
    384383  var content; 
    385384  if (this.iwin.getSelection) { // Gecko 
    386385    sel = this.iwin.getSelection(); 
    387     var range = sel.getRangeAt(0); 
     386    const range = sel.getRangeAt(0); 
    388387    content = range.cloneContents(); 
    389388  } else { // IE 
    390389    sel = this.iwin.document.selection; 
    391     var d = this.iwin.document.createElement('div'); 
     390    const d = this.iwin.document.createElement('div'); 
    392391    d.innerHTML = sel.createRange().htmlText; 
    393392    content = this.iwin.document.createDocumentFragment(); 
    394     for (var i = 0; i < d.childNodes.length; i++) { 
     393    for (let i = 0; i < d.childNodes.length; i++) { 
    395394      content.appendChild(d.childNodes[i].cloneNode(true)); 
    396395    } 
     
    405404    return this.iwin.getSelection().toString(); 
    406405  } else { // IE 
    407     var range = this.iwin.document.selection.createRange(); 
     406    const range = this.iwin.document.selection.createRange(); 
    408407    return range.text; 
    409408  } 
     
    411410 
    412411jsToolBar.prototype.replaceNodeByContent = function(node) { 
    413   var content = this.iwin.document.createDocumentFragment(); 
    414   for (var i = 0; i < node.childNodes.length; i++) { 
     412  const content = this.iwin.document.createDocumentFragment(); 
     413  for (let i = 0; i < node.childNodes.length; i++) { 
    415414    content.appendChild(node.childNodes[i].cloneNode(true)); 
    416415  } 
     
    419418 
    420419jsToolBar.prototype.getBlockLevel = function() { 
    421   var blockElts = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']; 
    422  
    423   var range, commonAncestorContainer; 
     420  const blockElts = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']; 
     421 
     422  let range; 
     423  let commonAncestorContainer; 
    424424  if (this.iwin.getSelection) { //gecko 
    425     var selection = this.iwin.getSelection(); 
     425    const selection = this.iwin.getSelection(); 
    426426    range = selection.getRangeAt(0); 
    427427    commonAncestorContainer = range.commonAncestorContainer; 
     
    434434  } 
    435435 
    436   var ancestorTagName = commonAncestorContainer.tagName.toLowerCase(); 
     436  let ancestorTagName = commonAncestorContainer.tagName.toLowerCase(); 
    437437  while (arrayIndexOf(blockElts, ancestorTagName) == -1 && ancestorTagName != 'body') { 
    438438    commonAncestorContainer = commonAncestorContainer.parentNode; 
     
    443443}; 
    444444jsToolBar.prototype.adjustBlockLevelCombo = function() { 
    445   var blockLevel = this.getBlockLevel(); 
     445  const blockLevel = this.getBlockLevel(); 
    446446  if (blockLevel !== null) 
    447447    this.toolNodes.blocks.value = blockLevel.tagName.toLowerCase(); 
     
    471471 */ 
    472472jsToolBar.prototype.tagsoup2xhtml = function(html) { 
    473   for (var reg in this.simpleCleanRegex) { 
     473  for (let reg in this.simpleCleanRegex) { 
    474474    html = html.replace(this.simpleCleanRegex[reg][0], this.simpleCleanRegex[reg][1]); 
    475475  } 
     
    488488 
    489489  /* IE laisse souvent des attributs sans guillemets */ 
    490   var myRegexp = /<[^>]+((\s+\w+\s*=\s*)([^"'][\w~@+$,%\/:.#?=&;!*()-]*))[^>]*?>/; 
    491   var myQuoteFn = function(str, val1, val2, val3) { 
    492     var tamponRegex = new RegExp(regexpEscape(val1)); 
     490  const myRegexp = /<[^>]+((\s+\w+\s*=\s*)([^"'][\w~@+$,%\/:.#?=&;!*()-]*))[^>]*?>/; 
     491  const myQuoteFn = function(str, val1, val2, val3) { 
     492    const tamponRegex = new RegExp(regexpEscape(val1)); 
    493493    return str.replace(tamponRegex, val2 + '"' + val3 + '"'); 
    494494  }; 
     
    508508 
    509509  /* Trim only if there's no pre tag */ 
    510   var pattern_pre = /<pre>[\s\S]*<\/pre>/gi; 
     510  const pattern_pre = /<pre>[\s\S]*<\/pre>/gi; 
    511511  if (!pattern_pre.test(html)) { 
    512512    html = html.replace(/^\s+/gm, ''); 
     
    517517}; 
    518518jsToolBar.prototype.validBlockquote = function() { 
    519   var blockElts = ['address', 'blockquote', 'dl', 'div', 'fieldset', 'form', 'h1', 
     519  const blockElts = ['address', 'blockquote', 'dl', 'div', 'fieldset', 'form', 'h1', 
    520520    'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'ol', 'p', 'pre', 'table', 'ul' 
    521521  ]; 
    522   var BQs = this.iwin.document.getElementsByTagName('blockquote'); 
    523   var bqChilds; 
    524   var p; 
    525  
    526   for (var bq = 0; bq < BQs.length; bq++) { 
     522  const BQs = this.iwin.document.getElementsByTagName('blockquote'); 
     523  let bqChilds; 
     524  let p; 
     525 
     526  for (let bq = 0; bq < BQs.length; bq++) { 
    527527    bqChilds = BQs[bq].childNodes; 
    528     var frag = this.iwin.document.createDocumentFragment(); 
    529     for (var i = (bqChilds.length - 1); i >= 0; i--) { 
     528    let frag = this.iwin.document.createDocumentFragment(); 
     529    for (let i = (bqChilds.length - 1); i >= 0; i--) { 
    530530      if (bqChilds[i].nodeType == 1 && // Node.ELEMENT_NODE 
    531531        arrayIndexOf(blockElts, bqChilds[i].tagName.toLowerCase()) >= 0) { 
     
    561561 
    562562jsToolBar.prototype.removeTextFormating = function(html) { 
    563   for (var reg in this.removeFormatRegexp) { 
     563  for (let reg in this.removeFormatRegexp) { 
    564564    html = html.replace(this.removeFormatRegexp[reg][0], this.removeFormatRegexp[reg][1]); 
    565565  } 
     
    576576  fn: function(opt) { 
    577577    if (opt == 'none') { 
    578       var blockLevel = this.getBlockLevel(); 
     578      const blockLevel = this.getBlockLevel(); 
    579579      if (blockLevel !== null) { 
    580580        this.replaceNodeByContent(blockLevel); 
     
    611611 
    612612jsToolBar.prototype.elements.quote.fn.wysiwyg = function() { 
    613   var n = this.getSelectedNode(); 
    614   var q = this.iwin.document.createElement('q'); 
     613  const n = this.getSelectedNode(); 
     614  const q = this.iwin.document.createElement('q'); 
    615615  q.appendChild(n); 
    616616  this.insertNode(q); 
     
    618618 
    619619jsToolBar.prototype.elements.code.fn.wysiwyg = function() { 
    620   var n = this.getSelectedNode(); 
    621   var code = this.iwin.document.createElement('code'); 
     620  const n = this.getSelectedNode(); 
     621  const code = this.iwin.document.createElement('code'); 
    622622  code.appendChild(n); 
    623623  this.insertNode(code); 
     
    625625 
    626626jsToolBar.prototype.elements.mark.fn.wysiwyg = function() { 
    627   var n = this.getSelectedNode(); 
    628   var mark = this.iwin.document.createElement('mark'); 
     627  const n = this.getSelectedNode(); 
     628  const mark = this.iwin.document.createElement('mark'); 
    629629  mark.appendChild(n); 
    630630  this.insertNode(mark); 
     
    632632 
    633633jsToolBar.prototype.elements.br.fn.wysiwyg = function() { 
    634   var n = this.iwin.document.createElement('br'); 
     634  const n = this.iwin.document.createElement('br'); 
    635635  this.insertNode(n); 
    636636}; 
    637637 
    638638jsToolBar.prototype.elements.blockquote.fn.wysiwyg = function() { 
    639   var n = this.getSelectedNode(); 
    640   var q = this.iwin.document.createElement('blockquote'); 
     639  const n = this.getSelectedNode(); 
     640  const q = this.iwin.document.createElement('blockquote'); 
    641641  q.appendChild(n); 
    642642  this.insertNode(q); 
     
    659659 
    660660jsToolBar.prototype.elements.link.fn.wysiwyg = function() { 
    661   var href, hreflang; 
    662   var range, commonAncestorContainer; 
     661  let href; 
     662  let hreflang; 
     663  let range; 
     664  let commonAncestorContainer; 
    663665  if (this.iwin.getSelection) { //gecko 
    664     var selection = this.iwin.getSelection(); 
     666    const selection = this.iwin.getSelection(); 
    665667    range = selection.getRangeAt(0); 
    666668    commonAncestorContainer = range.commonAncestorContainer; 
     
    673675  } 
    674676 
    675   var ancestorTagName = commonAncestorContainer.tagName.toLowerCase(); 
     677  let ancestorTagName = commonAncestorContainer.tagName.toLowerCase(); 
    676678  while (ancestorTagName != 'a' && ancestorTagName != 'body') { 
    677679    commonAncestorContainer = commonAncestorContainer.parentNode; 
     
    707709 
    708710  // Create link 
    709   var n = this.getSelectedNode(); 
    710   var a = this.iwin.document.createElement('a'); 
     711  const n = this.getSelectedNode(); 
     712  const a = this.iwin.document.createElement('a'); 
    711713  a.href = href; 
    712714  if (hreflang) a.setAttribute('hreflang', hreflang); 
     
    723725jsToolBar.prototype.elements.removeFormat.disabled = !jsToolBar.prototype.can_wwg; 
    724726jsToolBar.prototype.elements.removeFormat.fn.xhtml = function() { 
    725   var html = this.textarea.value; 
     727  let html = this.textarea.value; 
    726728  html = this.removeTextFormating(html); 
    727729  this.textarea.value = html; 
    728730}; 
    729731jsToolBar.prototype.elements.removeFormat.fn.wysiwyg = function() { 
    730   var html = this.iwin.document.body.innerHTML; 
     732  let html = this.iwin.document.body.innerHTML; 
    731733  html = this.removeTextFormating(html); 
    732734  this.iwin.document.body.innerHTML = html; 
     
    738740    return aArray.indexOf(aValue); 
    739741  } else { 
    740     var index = -1; 
    741     var l = aArray.length; 
    742     for (var i = 0; i < l; i++) { 
     742    let index = -1; 
     743    const l = aArray.length; 
     744    for (let i = 0; i < l; i++) { 
    743745      if (aArray[i] === aValue) { 
    744746        index = i; 
     
    755757    return true; 
    756758  } else if (obj.attachEvent) { 
    757     var r = obj.attachEvent("on" + evType, fn); 
     759    const r = obj.attachEvent("on" + evType, fn); 
    758760    return r; 
    759761  } else { 
     
    767769    return true; 
    768770  } else if (obj.detachEvent) { 
    769     var r = obj.detachEvent("on" + evType, fn); 
     771    const r = obj.detachEvent("on" + evType, fn); 
    770772    return r; 
    771773  } else { 
Note: See TracChangeset for help on using the changeset viewer.

Sites map