]> git.sesse.net Git - remoteglot/commitdiff
Build pieces through the DOM, not innerHTML.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 24 Dec 2022 23:05:49 +0000 (00:05 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 24 Dec 2022 23:05:49 +0000 (00:05 +0100)
www/js/chessboard-0.3.0.js

index 451f1a9f4d17cf73ed93c0b52788ccfb2cda1577..b5a4b8795013ee5310ba21ce8555e7ea9c99bc02 100644 (file)
@@ -641,23 +641,20 @@ function buildPieceImgSrc(piece) {
  * @param {!string=} id
  */
 function buildPiece(piece, hidden, id) {
-  var html = '<img src="' + buildPieceImgSrc(piece) + '" ';
+  let img = document.createElement('img');
+  img.src = buildPieceImgSrc(piece);
   if (id && typeof id === 'string') {
-    html += 'id="' + id + '" ';
+    img.setAttribute('id', id);
   }
-  html += 'alt="" ' +
-  'class="' + CSS.piece + '" ' +
-  'data-piece="' + piece + '" ' +
-  'style="width: ' + SQUARE_SIZE + 'px;' +
-  'height: ' + SQUARE_SIZE + 'px;';
+  img.setAttribute('alt', '');
+  img.classList.add(CSS.piece);
+  img.setAttribute('data-piece', piece);
+  img.style.width = SQUARE_SIZE + 'px';
+  img.style.height = SQUARE_SIZE + 'px';
   if (hidden === true) {
-    html += 'display:none;';
+    img.style.display = 'none';
   }
-  html += '" />';
-
-  let elem = document.createElement('template');
-  elem.innerHTML = html;
-  return elem.content;
+  return img;
 }
 
 function buildSparePieces(color) {