xml_test.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_domToPrettyText() {
  59. var dom = Blockly.Xml.textToDom(XML_TEXT);
  60. var text = Blockly.Xml.domToPrettyText(dom);
  61. assertEquals('Round trip', XML_TEXT.replace(/\s+/g, ''),
  62. text.replace(/\s+/g, ''));
  63. }