utils.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * Note: This package is used only internally for debugging purposes and should not be used
  7. * in other environments. It uses a few special methods not existing in the default
  8. * building process. That is also why there are no tests for this file.
  9. *
  10. * @module engine/dev-utils/utils
  11. */
  12. /* globals console */
  13. /**
  14. * Helper function, converts a map to the 'key1="value1" key2="value1"' format.
  15. *
  16. * @private
  17. * @param {Map} map Map to convert.
  18. * @returns {String} Converted map.
  19. */
  20. export function convertMapToTags( map ) {
  21. let string = '';
  22. for ( const entry of map ) {
  23. string += ` ${ entry[ 0 ] }=${ JSON.stringify( entry[ 1 ] ) }`;
  24. }
  25. return string;
  26. }
  27. /**
  28. * Helper function, converts a map to the '{"key1":"value1","key2":"value2"}' format.
  29. *
  30. * @private
  31. * @param {Map} map Map to convert.
  32. * @returns {String} Converted map.
  33. */
  34. export function convertMapToStringifiedObject( map ) {
  35. const obj = {};
  36. for ( const entry of map ) {
  37. obj[ entry[ 0 ] ] = entry[ 1 ];
  38. }
  39. return JSON.stringify( obj );
  40. }
  41. const treeDump = Symbol( '_treeDump' );
  42. const maxTreeDumpLength = 20;
  43. /**
  44. * Helper function that stores the `document` state for a given `version`.
  45. *
  46. * @private
  47. * @param {*} document
  48. * @param {*} version
  49. */
  50. export function dumpTrees( document, version ) {
  51. console.log( document, version );
  52. let string = '';
  53. for ( const root of document.roots ) {
  54. string += root.printTree() + '\n';
  55. }
  56. document[ treeDump ][ version ] = string.substr( 0, string.length - 1 ); // Remove the last "\n".
  57. const overflow = document[ treeDump ].length - maxTreeDumpLength;
  58. if ( overflow > 0 ) {
  59. document[ treeDump ][ overflow - 1 ] = null;
  60. }
  61. }
  62. /**
  63. * Helper function that initializes document dumping.
  64. *
  65. * @private
  66. * @param {*} document
  67. */
  68. export function initDocumentDumping( document ) {
  69. document[ treeDump ] = [];
  70. }
  71. /**
  72. * Helper function that logs document for the given version.
  73. *
  74. * @private
  75. * @param {*} document
  76. * @param {*} version
  77. */
  78. export function logDocument( document, version ) {
  79. console.log( '--------------------' );
  80. if ( document[ treeDump ][ version ] ) {
  81. console.log( document[ treeDump ][ version ] );
  82. } else {
  83. console.log( 'Tree log unavailable for given version: ' + version );
  84. }
  85. }