1 /** |
|
2 * ElementUtils.js |
|
3 * |
|
4 * Copyright, Moxiecode Systems AB |
|
5 * Released under LGPL License. |
|
6 * |
|
7 * License: http://www.tinymce.com/license |
|
8 * Contributing: http://www.tinymce.com/contributing |
|
9 */ |
|
10 |
|
11 /** |
|
12 * Utility class for various element specific functions. |
|
13 * |
|
14 * @private |
|
15 */ |
|
16 define("tinymce/dom/ElementUtils", [ |
|
17 "tinymce/dom/BookmarkManager", |
|
18 "tinymce/util/Tools" |
|
19 ], function(BookmarkManager, Tools) { |
|
20 var each = Tools.each; |
|
21 |
|
22 function ElementUtils(dom) { |
|
23 /** |
|
24 * Compares two nodes and checks if it's attributes and styles matches. |
|
25 * This doesn't compare classes as items since their order is significant. |
|
26 * |
|
27 * @method compare |
|
28 * @param {Node} node1 First node to compare with. |
|
29 * @param {Node} node2 Second node to compare with. |
|
30 * @return {boolean} True/false if the nodes are the same or not. |
|
31 */ |
|
32 this.compare = function(node1, node2) { |
|
33 // Not the same name |
|
34 if (node1.nodeName != node2.nodeName) { |
|
35 return false; |
|
36 } |
|
37 |
|
38 /** |
|
39 * Returns all the nodes attributes excluding internal ones, styles and classes. |
|
40 * |
|
41 * @private |
|
42 * @param {Node} node Node to get attributes from. |
|
43 * @return {Object} Name/value object with attributes and attribute values. |
|
44 */ |
|
45 function getAttribs(node) { |
|
46 var attribs = {}; |
|
47 |
|
48 each(dom.getAttribs(node), function(attr) { |
|
49 var name = attr.nodeName.toLowerCase(); |
|
50 |
|
51 // Don't compare internal attributes or style |
|
52 if (name.indexOf('_') !== 0 && name !== 'style' && name !== 'data-mce-style') { |
|
53 attribs[name] = dom.getAttrib(node, name); |
|
54 } |
|
55 }); |
|
56 |
|
57 return attribs; |
|
58 } |
|
59 |
|
60 /** |
|
61 * Compares two objects checks if it's key + value exists in the other one. |
|
62 * |
|
63 * @private |
|
64 * @param {Object} obj1 First object to compare. |
|
65 * @param {Object} obj2 Second object to compare. |
|
66 * @return {boolean} True/false if the objects matches or not. |
|
67 */ |
|
68 function compareObjects(obj1, obj2) { |
|
69 var value, name; |
|
70 |
|
71 for (name in obj1) { |
|
72 // Obj1 has item obj2 doesn't have |
|
73 if (obj1.hasOwnProperty(name)) { |
|
74 value = obj2[name]; |
|
75 |
|
76 // Obj2 doesn't have obj1 item |
|
77 if (typeof value == "undefined") { |
|
78 return false; |
|
79 } |
|
80 |
|
81 // Obj2 item has a different value |
|
82 if (obj1[name] != value) { |
|
83 return false; |
|
84 } |
|
85 |
|
86 // Delete similar value |
|
87 delete obj2[name]; |
|
88 } |
|
89 } |
|
90 |
|
91 // Check if obj 2 has something obj 1 doesn't have |
|
92 for (name in obj2) { |
|
93 // Obj2 has item obj1 doesn't have |
|
94 if (obj2.hasOwnProperty(name)) { |
|
95 return false; |
|
96 } |
|
97 } |
|
98 |
|
99 return true; |
|
100 } |
|
101 |
|
102 // Attribs are not the same |
|
103 if (!compareObjects(getAttribs(node1), getAttribs(node2))) { |
|
104 return false; |
|
105 } |
|
106 |
|
107 // Styles are not the same |
|
108 if (!compareObjects(dom.parseStyle(dom.getAttrib(node1, 'style')), dom.parseStyle(dom.getAttrib(node2, 'style')))) { |
|
109 return false; |
|
110 } |
|
111 |
|
112 return !BookmarkManager.isBookmarkNode(node1) && !BookmarkManager.isBookmarkNode(node2); |
|
113 }; |
|
114 } |
|
115 |
|
116 return ElementUtils; |
|
117 }); |
|