/
areconnectedthroughproperties.js
80 lines (61 loc) · 2.06 KB
/
areconnectedthroughproperties.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module watchdog/utils/areconnectedthroughproperties
*/
/* globals console */
import getSubNodes from './getsubnodes';
/**
* Traverses both structures to find out whether there is a reference that is shared between both structures.
*
* @param {Object|Array} target1
* @param {Object|Array} target2
*/
export default function areConnectedThroughProperties( target1, target2, excludedNodes = new Set() ) {
if ( target1 === target2 && isObject( target1 ) ) {
return true;
}
// @if CK_DEBUG_WATCHDOG // return checkConnectionBetweenProps( target1, target2, excludedNodes );
const subNodes1 = getSubNodes( target1, excludedNodes );
const subNodes2 = getSubNodes( target2, excludedNodes );
for ( const node of subNodes1 ) {
if ( subNodes2.has( node ) ) {
return true;
}
}
return false;
}
/* istanbul ignore next */
// eslint-disable-next-line
function checkConnectionBetweenProps( target1, target2, excludedNodes ) {
const { subNodes: subNodes1, prevNodeMap: prevNodeMap1 } = getSubNodes( target1, excludedNodes.subNodes );
const { subNodes: subNodes2, prevNodeMap: prevNodeMap2 } = getSubNodes( target2, excludedNodes.subNodes );
for ( const sharedNode of subNodes1 ) {
if ( subNodes2.has( sharedNode ) ) {
const connection = [];
connection.push( sharedNode );
let node = prevNodeMap1.get( sharedNode );
while ( node && node !== target1 ) {
connection.push( node );
node = prevNodeMap1.get( node );
}
node = prevNodeMap2.get( sharedNode );
while ( node && node !== target2 ) {
connection.unshift( node );
node = prevNodeMap2.get( node );
}
console.log( '--------' );
console.log( { target1 } );
console.log( { sharedNode } );
console.log( { target2 } );
console.log( { connection } );
return true;
}
}
return false;
}
function isObject( structure ) {
return typeof structure === 'object' && structure !== null;
}