mirror of
https://github.com/google/blockly.git
synced 2026-01-14 20:37:10 +01:00
Add ignoring insertion markers in xml gen (#3883)
* Add ignoring insertion markers in xml gen
This commit is contained in:
39
core/xml.js
39
core/xml.js
@@ -77,10 +77,20 @@ Blockly.Xml.variablesToDom = function(variableList) {
|
||||
* @return {!Element} Tree of XML elements.
|
||||
*/
|
||||
Blockly.Xml.blockToDomWithXY = function(block, opt_noId) {
|
||||
if (block.isInsertionMarker()) { // Skip over insertion markers.
|
||||
block = block.getChildren(false)[0];
|
||||
if (!block) {
|
||||
// Disappears when appended. Cast to ANY b/c DocumentFragment -> Element
|
||||
// is invalid. We have to cast to ANY in between.
|
||||
return /** @type{?} */ (new DocumentFragment());
|
||||
}
|
||||
}
|
||||
|
||||
var width; // Not used in LTR.
|
||||
if (block.workspace.RTL) {
|
||||
width = block.workspace.getWidth();
|
||||
}
|
||||
|
||||
var element = Blockly.Xml.blockToDom(block, opt_noId);
|
||||
var xy = block.getRelativeToSurfaceXY();
|
||||
element.setAttribute('x',
|
||||
@@ -131,6 +141,19 @@ Blockly.Xml.allFieldsToDom_ = function(block, element) {
|
||||
* @return {!Element} Tree of XML elements.
|
||||
*/
|
||||
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. Cast to ANY b/c DocumentFragment -> Element
|
||||
// is invalid. We have to cast to ANY in between.
|
||||
return /** @type{?} */ (new DocumentFragment());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var element =
|
||||
Blockly.utils.xml.createElement(block.isShadow() ? 'shadow' : 'block');
|
||||
element.setAttribute('type', block.type);
|
||||
@@ -186,8 +209,11 @@ Blockly.Xml.blockToDom = function(block, opt_noId) {
|
||||
container.appendChild(Blockly.Xml.cloneShadow_(shadow, opt_noId));
|
||||
}
|
||||
if (childBlock) {
|
||||
container.appendChild(Blockly.Xml.blockToDom(childBlock, opt_noId));
|
||||
empty = false;
|
||||
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);
|
||||
@@ -217,9 +243,12 @@ Blockly.Xml.blockToDom = function(block, opt_noId) {
|
||||
|
||||
var nextBlock = block.getNextBlock();
|
||||
if (nextBlock) {
|
||||
var container = Blockly.utils.xml.createElement('next');
|
||||
container.appendChild(Blockly.Xml.blockToDom(nextBlock, opt_noId));
|
||||
element.appendChild(container);
|
||||
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())) {
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
suite('InsertionMarkers', function() {
|
||||
setup(function() {
|
||||
this.workspace = new Blockly.Workspace();
|
||||
this.workspace = Blockly.inject('blocklyDiv', {});
|
||||
Blockly.defineBlocksWithJsonArray([
|
||||
{
|
||||
"type": "stack_block",
|
||||
@@ -200,4 +200,187 @@ suite('InsertionMarkers', function() {
|
||||
this.assertGen(xml, 'stack[a];\n');
|
||||
});
|
||||
});
|
||||
suite('Serialization', function() {
|
||||
setup(function() {
|
||||
this.assertXml = function(xml, expectXml) {
|
||||
Blockly.Xml.domToWorkspace(xml, this.workspace);
|
||||
var block = this.workspace.getBlockById('insertion');
|
||||
block.setInsertionMarker(true);
|
||||
var xml = Blockly.Xml.workspaceToDom(this.workspace);
|
||||
Blockly.Xml.domToWorkspace(xml, this.workspace);
|
||||
xml = Blockly.Xml.domToText(xml);
|
||||
chai.assert.equal(xml, expectXml);
|
||||
};
|
||||
});
|
||||
teardown(function() {
|
||||
delete this.assertXml;
|
||||
});
|
||||
test('Marker Surrounds', function() {
|
||||
var xml = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
' <block type="statement_block" id="insertion" x="20" y="20">' +
|
||||
' <statement name="STATEMENT">' +
|
||||
' <block type="statement_block" id="a"/>' +
|
||||
' </statement>' +
|
||||
' </block>' +
|
||||
'</xml>');
|
||||
// Note how the x and y are not 20, they are slightly lower and end-er
|
||||
// because these are the coords of the wrapped block.
|
||||
this.assertXml(xml,
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
'<block type="statement_block" id="a" x="41" y="31"></block>' +
|
||||
'</xml>');
|
||||
});
|
||||
test('Marker Enclosed', function() {
|
||||
var xml = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
' <block type="statement_block" id="a" x="20" y="20">' +
|
||||
' <statement name="STATEMENT">' +
|
||||
' <block type="statement_block" id="insertion"/>' +
|
||||
' </statement>' +
|
||||
' </block>' +
|
||||
'</xml>');
|
||||
this.assertXml(xml,
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
'<block type="statement_block" id="a" x="20" y="20"></block>' +
|
||||
'</xml>');
|
||||
});
|
||||
test('Marker Enclosed and Surrounds', function() {
|
||||
var xml = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
' <block type="statement_block" id="a" x="20" y="20">' +
|
||||
' <statement name="STATEMENT">' +
|
||||
' <block type="statement_block" id="insertion">' +
|
||||
' <statement name="STATEMENT">' +
|
||||
' <block type="statement_block" id="b"/>' +
|
||||
' </statement>' +
|
||||
' </block>' +
|
||||
' </statement>' +
|
||||
' </block>' +
|
||||
'</xml>');
|
||||
this.assertXml(xml,
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
'<block type="statement_block" id="a" x="20" y="20">' +
|
||||
'<statement name="STATEMENT">' +
|
||||
'<block type="statement_block" id="b"></block>' +
|
||||
'</statement>' +
|
||||
'</block>' +
|
||||
'</xml>');
|
||||
});
|
||||
test('Marker Prev', function() {
|
||||
var xml = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
' <block type="stack_block" id="insertion" x="20" y="20">' +
|
||||
' <next>' +
|
||||
' <block type="stack_block" id="a"/>' +
|
||||
' </next>' +
|
||||
' </block>' +
|
||||
'</xml>');
|
||||
// Note how the y coord is not at 20, it is lower. This is because these
|
||||
// are the coords of the next block.
|
||||
this.assertXml(xml,
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
'<block type="stack_block" id="a" x="20" y="46"></block>' +
|
||||
'</xml>');
|
||||
});
|
||||
test('Marker Next', function() {
|
||||
var xml = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
' <block type="stack_block" id="a" x="20" y="20">' +
|
||||
' <next>' +
|
||||
' <block type="stack_block" id="insertion"/>' +
|
||||
' </next>' +
|
||||
' </block>' +
|
||||
'</xml>');
|
||||
this.assertXml(xml,
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
'<block type="stack_block" id="a" x="20" y="20"></block>' +
|
||||
'</xml>');
|
||||
});
|
||||
test('Marker Middle of Stack', function() {
|
||||
var xml = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
' <block type="stack_block" id="a" x="20" y="20">' +
|
||||
' <next>' +
|
||||
' <block type="stack_block" id="insertion">' +
|
||||
' <next>' +
|
||||
' <block type="stack_block" id="b"/>' +
|
||||
' </next>' +
|
||||
' </block>' +
|
||||
' </next>' +
|
||||
' </block>' +
|
||||
'</xml>');
|
||||
this.assertXml(xml,
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
'<block type="stack_block" id="a" x="20" y="20">' +
|
||||
'<next>' +
|
||||
'<block type="stack_block" id="b"></block>' +
|
||||
'</next>' +
|
||||
'</block>' +
|
||||
'</xml>');
|
||||
});
|
||||
test('Marker On Output', function() {
|
||||
var xml = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
' <block type="row_block" id="insertion" x="20" y="20">' +
|
||||
' <value name="INPUT">' +
|
||||
' <block type="row_block" id="a"/>' +
|
||||
' </value>' +
|
||||
' </block>' +
|
||||
'</xml>');
|
||||
// Note how the x value is not at 20. This is because these are the coords
|
||||
// of the wrapped block.
|
||||
this.assertXml(xml,
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
'<block type="row_block" id="a" x="41" y="20"></block>' +
|
||||
'</xml>');
|
||||
});
|
||||
test('Marker On Input', function() {
|
||||
var xml = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
' <block type="row_block" id="a" x="20" y="20">' +
|
||||
' <value name="INPUT">' +
|
||||
' <block type="row_block" id="insertion"/>' +
|
||||
' </value>' +
|
||||
' </block>' +
|
||||
'</xml>');
|
||||
this.assertXml(xml,
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
'<block type="row_block" id="a" x="20" y="20"></block>' +
|
||||
'</xml>');
|
||||
});
|
||||
test('Marker Middle of Row', function() {
|
||||
var xml = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
' <block type="row_block" id="a" x="20" y="20">' +
|
||||
' <value name="INPUT">' +
|
||||
' <block type="row_block" id="insertion">' +
|
||||
' <value name="INPUT">' +
|
||||
' <block type="row_block" id="b"/>' +
|
||||
' </value>' +
|
||||
' </block>' +
|
||||
' </value>' +
|
||||
' </block>' +
|
||||
'</xml>');
|
||||
this.assertXml(xml,
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
'<block type="row_block" id="a" x="20" y="20">' +
|
||||
'<value name="INPUT">' +
|
||||
'<block type="row_block" id="b"></block>' +
|
||||
'</value>' +
|
||||
'</block>' +
|
||||
'</xml>');
|
||||
});
|
||||
test('Marker Detatched', function() {
|
||||
var xml = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
' <block type="stack_block" id="insertion"/>' +
|
||||
' <block type="stack_block" id="a" x="20" y="20"/>' +
|
||||
'</xml>');
|
||||
this.assertXml(xml,
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
'<block type="stack_block" id="a" x="20" y="20"></block>' +
|
||||
'</xml>');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user