Blockly.Xml.blockToDom = function()

in core/xml.js [152:274]


Blockly.Xml.blockToDom = function(block, opt_noId) {
  // Skip over insertion markers.
  if (block.isInsertionMarker()) {
    var child = block.getChildren(false)[0];
    if (child) {
      return Blockly.Xml.blockToDom(child);
    } else {
      // Disappears when appended.
      return new DocumentFragment();
    }
  }


  var element =
      Blockly.utils.xml.createElement(block.isShadow() ? 'shadow' : 'block');
  element.setAttribute('type', block.type);
  if (!opt_noId) {
    // It's important to use setAttribute here otherwise IE11 won't serialize
    // the block's ID when domToText is called.
    element.setAttribute('id', block.id);
  }
  if (block.mutationToDom) {
    // Custom data for an advanced block.
    var mutation = block.mutationToDom();
    if (mutation && (mutation.hasChildNodes() || mutation.hasAttributes())) {
      element.appendChild(mutation);
    }
  }

  Blockly.Xml.allFieldsToDom_(block, element);

  if (block.isBreakpointSet()) {
    var breakpointElement =  Blockly.utils.xml.createElement('breakpoint');
    element.appendChild(breakpointElement);
  }

  var commentText = block.getCommentText();
  if (commentText) {
    var size = block.commentModel.size;
    var pinned = block.commentModel.pinned;
    var position = block.getCommentIcon && block.getCommentIcon()
      ? block.getCommentIcon().getRelativePosition() : null;

    var commentElement = Blockly.utils.xml.createElement('comment');
    commentElement.appendChild(Blockly.utils.xml.createTextNode(commentText));
    commentElement.setAttribute('pinned', pinned);
    commentElement.setAttribute('h', size.height);
    commentElement.setAttribute('w', size.width);

    element.appendChild(commentElement);
  }

  if (block.data) {
    var dataElement = Blockly.utils.xml.createElement('data');
    dataElement.appendChild(Blockly.utils.xml.createTextNode(block.data));
    element.appendChild(dataElement);
  }

  for (var i = 0, input; (input = block.inputList[i]); i++) {
    var container;
    var empty = true;
    if (input.type == Blockly.inputTypes.DUMMY) {
      continue;
    } else {
      var childBlock = input.connection.targetBlock();
      if (input.type == Blockly.inputTypes.VALUE) {
        container = Blockly.utils.xml.createElement('value');
      } else if (input.type == Blockly.inputTypes.STATEMENT) {
        container = Blockly.utils.xml.createElement('statement');
      }
      var shadow = input.connection.getShadowDom();
      if (shadow && (!childBlock || !childBlock.isShadow())) {
        container.appendChild(Blockly.Xml.cloneShadow_(shadow, opt_noId));
      }
      if (childBlock) {
        var elem = Blockly.Xml.blockToDom(childBlock, opt_noId);
        if (elem.nodeType == Blockly.utils.dom.NodeType.ELEMENT_NODE) {
          container.appendChild(elem);
          empty = false;
        }
      }
    }
    container.setAttribute('name', input.name);
    if (!empty) {
      element.appendChild(container);
    }
  }
  if (block.inputsInline != undefined &&
      block.inputsInline != block.inputsInlineDefault) {
    element.setAttribute('inline', block.inputsInline);
  }
  if (block.isCollapsed()) {
    element.setAttribute('collapsed', true);
  }
  if (!block.isEnabled()) {
    element.setAttribute('disabled', true);
  }
  if (!block.isDeletable() && !block.isShadow()) {
    element.setAttribute('deletable', false);
  }
  if (!block.isMovablePersisted() && !block.isShadow()) {
    element.setAttribute('movable', false);
  }
  if (!block.isEditablePersisted()) {
    element.setAttribute('editable', false);
  }

  var nextBlock = block.getNextBlock();
  if (nextBlock) {
    var elem = Blockly.Xml.blockToDom(nextBlock, opt_noId);
    if (elem.nodeType == Blockly.utils.dom.NodeType.ELEMENT_NODE) {
      var container = Blockly.utils.xml.createElement('next');
      container.appendChild(elem);
      element.appendChild(container);
    }
  }
  var shadow = block.nextConnection && block.nextConnection.getShadowDom();
  if (shadow && (!nextBlock || !nextBlock.isShadow())) {
    container.appendChild(Blockly.Xml.cloneShadow_(shadow, opt_noId));
  }

  return element;
};