xml_test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @license
  3. * Blockly Tests
  4. *
  5. * Copyright 2014 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. 'use strict';
  21. var XML_TEXT = ['<xml xmlns="http://www.w3.org/1999/xhtml">',
  22. ' <block type="controls_repeat_ext" inline="true" x="21" y="23">',
  23. ' <value name="TIMES">',
  24. ' <block type="math_number">',
  25. ' <field name="NUM">10</field>',
  26. ' </block>',
  27. ' </value>',
  28. ' <statement name="DO">',
  29. ' <block type="variables_set" inline="true">',
  30. ' <field name="VAR">item</field>',
  31. ' <value name="VALUE">',
  32. ' <block type="lists_create_empty"></block>',
  33. ' </value>',
  34. ' <next>',
  35. ' <block type="text_print" inline="false">',
  36. ' <value name="TEXT">',
  37. ' <block type="text">',
  38. ' <field name="TEXT">Hello</field>',
  39. ' </block>',
  40. ' </value>',
  41. ' </block>',
  42. ' </next>',
  43. ' </block>',
  44. ' </statement>',
  45. ' </block>',
  46. '</xml>'].join('\n');
  47. function test_textToDom() {
  48. var dom = Blockly.Xml.textToDom(XML_TEXT);
  49. assertEquals('XML tag', 'xml', dom.nodeName);
  50. assertEquals('Block tags', 6, dom.getElementsByTagName('block').length);
  51. }
  52. function test_domToText() {
  53. var dom = Blockly.Xml.textToDom(XML_TEXT);
  54. var text = Blockly.Xml.domToText(dom);
  55. assertEquals('Round trip', XML_TEXT.replace(/\s+/g, ''),
  56. text.replace(/\s+/g, ''));
  57. }
  58. function test_domToWorkspace() {
  59. Blockly.Blocks.test_block = {
  60. init: function() {
  61. this.jsonInit({
  62. message0: 'test',
  63. });
  64. }
  65. };
  66. try {
  67. var dom = Blockly.Xml.textToDom(
  68. '<xml xmlns="http://www.w3.org/1999/xhtml">' +
  69. ' <block type="test_block" inline="true" x="21" y="23">' +
  70. ' </block>' +
  71. '</xml>');
  72. var workspace = new Blockly.Workspace();
  73. Blockly.Xml.domToWorkspace(dom, workspace);
  74. assertEquals('Block count', 1, workspace.getAllBlocks().length);
  75. } finally {
  76. delete Blockly.Blocks.test_block;
  77. }
  78. }
  79. function test_domToPrettyText() {
  80. var dom = Blockly.Xml.textToDom(XML_TEXT);
  81. var text = Blockly.Xml.domToPrettyText(dom);
  82. assertEquals('Round trip', XML_TEXT.replace(/\s+/g, ''),
  83. text.replace(/\s+/g, ''));
  84. }