utils.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module engine/dev-utils/utils
  7. */
  8. /**
  9. * Helper function, converts a map to the 'key1="value1" key2="value1"' format.
  10. *
  11. * @private
  12. * @param {Map} map Map to convert.
  13. * @returns {String} Converted map.
  14. */
  15. export function convertMapToTags( map ) {
  16. let string = '';
  17. for ( const entry of map ) {
  18. string += ` ${ entry[ 0 ] }=${ JSON.stringify( entry[ 1 ] ) }`;
  19. }
  20. return string;
  21. }
  22. /**
  23. * Helper function, converts a map to the '{"key1":"value1","key2":"value2"}' format.
  24. *
  25. * @private
  26. * @param {Map} map Map to convert.
  27. * @returns {String} Converted map.
  28. */
  29. export function convertMapToStringifiedObject( map ) {
  30. const obj = {};
  31. for ( const entry of map ) {
  32. obj[ entry[ 0 ] ] = entry[ 1 ];
  33. }
  34. return JSON.stringify( obj );
  35. }
  36. /**
  37. * @private
  38. */
  39. export const treeDump = Symbol( '_treeDump' );
  40. const maxTreeDumpLength = 20;
  41. /**
  42. * Helper function, stores the `document` state for a given `version` as a string in a private property.
  43. *
  44. * @private
  45. * @param {*} document
  46. * @param {*} version
  47. */
  48. export function dumpTrees( document, version ) {
  49. console.log( document, version );
  50. let string = '';
  51. for ( const root of document.roots ) {
  52. string += root.printTree() + '\n';
  53. }
  54. document[ treeDump ][ version ] = string.substr( 0, string.length - 1 ); // Remove the last "\n".
  55. const overflow = document[ treeDump ].length - maxTreeDumpLength;
  56. if ( overflow > 0 ) {
  57. document[ treeDump ][ overflow - 1 ] = null;
  58. }
  59. }
  60. export function initDocumentDumping( document ) {
  61. document[ treeDump ] = [];
  62. }
  63. /**
  64. * Helper function that dumps document for the given version.
  65. *
  66. * @private
  67. * @param {*} document
  68. * @param {*} version
  69. */
  70. export function logDocument( document, version ) {
  71. console.log( '--------------------' );
  72. if ( document[ treeDump ][ version ] ) {
  73. console.log( document[ treeDump ][ version ] );
  74. } else {
  75. console.log( 'Tree log unavailable for given version: ' + version );
  76. }
  77. }