Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ve.debug.js

raw symbols SCIP
1 /*!
2 * VisualEditor debugging methods.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /* eslint-disable no-console */
8
9 /**
10 * @property {boolean} debug
11 * @memberof ve
12 */
13 ve.debug = true;
14
15 /* Methods */
16
17 /**
18 * Logs data to the console.
19 *
20 * @param {...any} [data] Data to log
21 */
22 ve.log = console.log;
23
24 /**
25 * Logs error to the console.
26 *
27 * @param {...any} [data] Data to log
28 */
29 ve.error = console.error;
30
31 /**
32 * Logs an object to the console.
33 *
34 * @param {Object} obj Object to log
35 */
36 ve.dir = console.dir;
37
38 /**
39 * Like outerHTML serialization, but wraps each text node in a fake tag. This
40 * makes it obvious whether there are split text nodes present.
41 *
42 * @param {Node} domNode The node to serialize
43 * @return {string} Serialization of the node and its contents
44 */
45 ve.serializeNodeDebug = function ( domNode ) {
46 const html = [];
47 function add( node ) {
48 if ( node.nodeType === Node.TEXT_NODE ) {
49 html.push( '<#text>', ve.escapeHtml( node.textContent ), '</#text>' );
50 return;
51 } else if ( node.nodeType !== Node.ELEMENT_NODE ) {
52 html.push( '<#unknown type=\'' + node.nodeType + '\'/>' );
53 return;
54 }
55 // else node.nodeType === Node.ELEMENT_NODE
56
57 html.push( '<', ve.escapeHtml( node.nodeName.toLowerCase() ) );
58 for ( let i = 0, len = node.attributes.length; i < len; i++ ) {
59 const attr = node.attributes[ i ];
60 html.push(
61 ' ',
62 ve.escapeHtml( attr.name ),
63 // Single quotes are less annoying in JSON escaping
64 '=\'',
65 ve.escapeHtml( attr.value ),
66 '\''
67 );
68 }
69 html.push( '>' );
70 for ( let i = 0, len = node.childNodes.length; i < len; i++ ) {
71 add( node.childNodes[ i ] );
72 }
73 html.push( '</', ve.escapeHtml( node.nodeName.toLowerCase() ), '>' );
74 }
75 add( domNode );
76 return html.join( '' );
77 };
78
79 /**
80 * Get a human-readable summary of a transaction
81 *
82 * @param {ve.dm.Transaction} tx A transaction
83 * @return {string} Human-readable summary
84 */
85 ve.summarizeTransaction = function ( tx ) {
86 function summarizeItems( items ) {
87 return '\'' + items.map( ( item ) => {
88 if ( item.type ) {
89 return '<' + item.type + '>';
90 } else if ( Array.isArray( item ) ) {
91 return item[ 0 ];
92 } else if ( typeof item === 'string' ) {
93 return item;
94 } else {
95 throw new Error( 'Unknown item type: ' + item );
96 }
97 } ).join( '' ) + '\'';
98 }
99 let annotations = 0;
100 return '(' + ( tx.authorId ? ( tx.authorId + ' ' ) : '' ) + tx.operations.map( ( op ) => {
101 if ( op.type === 'retain' ) {
102 return ( annotations ? 'annotate ' : 'retain ' ) + op.length;
103 } else if ( op.type === 'replace' ) {
104 if ( op.remove.length === 0 ) {
105 return 'insert ' + summarizeItems( op.insert );
106 } else if ( op.insert.length === 0 ) {
107 return 'remove ' + summarizeItems( op.remove );
108 } else {
109 return 'replace ' + summarizeItems( op.remove ) +
110 ' -> ' + summarizeItems( op.insert );
111 }
112 } else if ( op.type === 'attribute' ) {
113 return 'attribute';
114 } else if ( op.type === 'annotate' ) {
115 annotations += op.bias === 'start' ? 1 : -1;
116 return 'annotate';
117 } else if ( op.type.slice( -8 ) === 'Metadata' ) {
118 // We don't care much because we're deprecating metadata ops
119 return 'metadata';
120 } else {
121 throw new Error( 'Unknown op type: ' + op.type );
122 }
123 } ).join( ', ' ) + ')';
124 };
125
126 /**
127 * Initialize ve.filibuster
128 *
129 * ve.filibuster will monitor calls in ve.{dm,ce,ui} and DM / DOM changes
130 */
131 ve.initFilibuster = function () {
132 if ( ve.filibuster ) {
133 ve.filibuster.clearLogs();
134 return;
135 }
136
137 const surface = ve.init.target.surface;
138 ve.filibuster = new ve.Filibuster()
139 .wrapClass( ve.EventSequencer )
140 .wrapNamespace( ve.dm, 've.dm', [
141 // nowrapList
142 ve.dm.LinearSelection.prototype.getDescription,
143 ve.dm.TableSelection.prototype.getDescription,
144 ve.dm.NullSelection.prototype.getDescription
145 ] )
146 .wrapNamespace( ve.ce, 've.ce' )
147 .wrapNamespace( ve.ui, 've.ui', [
148 // nowrapList
149 ve.ui.Surface.prototype.startFilibuster,
150 ve.ui.Surface.prototype.stopFilibuster
151 ] )
152 // Cannot use wrapped methods here
153 .setObserver( 'dm doc', () => JSON.stringify( ve.Filibuster.static.clonePlain(
154 surface.model.documentModel.data.data
155 ) ) )
156 .setObserver( 'dm selection', () => {
157 // Cannot use wrapped methods here
158 const selection = surface.model.selection;
159 if ( !selection ) {
160 return 'null';
161 }
162 return selection.getDescription();
163 } )
164 // Cannot use wrapped methods here
165 .setObserver( 'DOM doc', () => ve.serializeNodeDebug( surface.view.$element[ 0 ] ) )
166 .setObserver( 'DOM selection', () => {
167 // Cannot use wrapped methods here
168 const nativeSelection = surface.view.nativeSelection;
169 if ( nativeSelection.focusNode === null ) {
170 return 'null';
171 }
172 return JSON.stringify( {
173 anchorNode: ve.serializeNodeDebug( nativeSelection.anchorNode ),
174 anchorOffset: nativeSelection.anchorOffset,
175 focusNode: (
176 nativeSelection.focusNode === nativeSelection.anchorNode ?
177 '(=anchorNode)' :
178 ve.serializeNodeDebug( nativeSelection.focusNode )
179 ),
180 focusOffset: nativeSelection.focusOffset
181 } );
182 } );
183 };