diff options
Diffstat (limited to 'sources/core/htmlparser/node.js')
-rw-r--r-- | sources/core/htmlparser/node.js | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/sources/core/htmlparser/node.js b/sources/core/htmlparser/node.js new file mode 100644 index 0000000..b38c8a8 --- /dev/null +++ b/sources/core/htmlparser/node.js | |||
@@ -0,0 +1,156 @@ | |||
1 | /** | ||
2 | * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. | ||
3 | * For licensing, see LICENSE.md or http://ckeditor.com/license | ||
4 | */ | ||
5 | |||
6 | 'use strict'; | ||
7 | |||
8 | ( function() { | ||
9 | /** | ||
10 | * A lightweight representation of HTML node. | ||
11 | * | ||
12 | * @since 4.1 | ||
13 | * @class | ||
14 | * @constructor Creates a node class instance. | ||
15 | */ | ||
16 | CKEDITOR.htmlParser.node = function() {}; | ||
17 | |||
18 | CKEDITOR.htmlParser.node.prototype = { | ||
19 | /** | ||
20 | * Remove this node from a tree. | ||
21 | * | ||
22 | * @since 4.1 | ||
23 | */ | ||
24 | remove: function() { | ||
25 | var children = this.parent.children, | ||
26 | index = CKEDITOR.tools.indexOf( children, this ), | ||
27 | previous = this.previous, | ||
28 | next = this.next; | ||
29 | |||
30 | previous && ( previous.next = next ); | ||
31 | next && ( next.previous = previous ); | ||
32 | children.splice( index, 1 ); | ||
33 | this.parent = null; | ||
34 | }, | ||
35 | |||
36 | /** | ||
37 | * Replace this node with given one. | ||
38 | * | ||
39 | * @since 4.1 | ||
40 | * @param {CKEDITOR.htmlParser.node} node The node that will replace this one. | ||
41 | */ | ||
42 | replaceWith: function( node ) { | ||
43 | var children = this.parent.children, | ||
44 | index = CKEDITOR.tools.indexOf( children, this ), | ||
45 | previous = node.previous = this.previous, | ||
46 | next = node.next = this.next; | ||
47 | |||
48 | previous && ( previous.next = node ); | ||
49 | next && ( next.previous = node ); | ||
50 | |||
51 | children[ index ] = node; | ||
52 | |||
53 | node.parent = this.parent; | ||
54 | this.parent = null; | ||
55 | }, | ||
56 | |||
57 | /** | ||
58 | * Insert this node after given one. | ||
59 | * | ||
60 | * @since 4.1 | ||
61 | * @param {CKEDITOR.htmlParser.node} node The node that will precede this element. | ||
62 | */ | ||
63 | insertAfter: function( node ) { | ||
64 | var children = node.parent.children, | ||
65 | index = CKEDITOR.tools.indexOf( children, node ), | ||
66 | next = node.next; | ||
67 | |||
68 | children.splice( index + 1, 0, this ); | ||
69 | |||
70 | this.next = node.next; | ||
71 | this.previous = node; | ||
72 | node.next = this; | ||
73 | next && ( next.previous = this ); | ||
74 | |||
75 | this.parent = node.parent; | ||
76 | }, | ||
77 | |||
78 | /** | ||
79 | * Insert this node before given one. | ||
80 | * | ||
81 | * @since 4.1 | ||
82 | * @param {CKEDITOR.htmlParser.node} node The node that will follow this element. | ||
83 | */ | ||
84 | insertBefore: function( node ) { | ||
85 | var children = node.parent.children, | ||
86 | index = CKEDITOR.tools.indexOf( children, node ); | ||
87 | |||
88 | children.splice( index, 0, this ); | ||
89 | |||
90 | this.next = node; | ||
91 | this.previous = node.previous; | ||
92 | node.previous && ( node.previous.next = this ); | ||
93 | node.previous = this; | ||
94 | |||
95 | this.parent = node.parent; | ||
96 | }, | ||
97 | |||
98 | /** | ||
99 | * Gets the closest ancestor element of this element which satisfies given condition | ||
100 | * | ||
101 | * @since 4.3 | ||
102 | * @param {String/Object/Function} condition Name of an ancestor, hash of names or validator function. | ||
103 | * @returns {CKEDITOR.htmlParser.element} The closest ancestor which satisfies given condition or `null`. | ||
104 | */ | ||
105 | getAscendant: function( condition ) { | ||
106 | var checkFn = | ||
107 | typeof condition == 'function' ? | ||
108 | condition : | ||
109 | typeof condition == 'string' ? | ||
110 | function( el ) { | ||
111 | return el.name == condition; | ||
112 | } : | ||
113 | function( el ) { | ||
114 | return el.name in condition; | ||
115 | }; | ||
116 | |||
117 | var parent = this.parent; | ||
118 | |||
119 | // Parent has to be an element - don't check doc fragment. | ||
120 | while ( parent && parent.type == CKEDITOR.NODE_ELEMENT ) { | ||
121 | if ( checkFn( parent ) ) | ||
122 | return parent; | ||
123 | parent = parent.parent; | ||
124 | } | ||
125 | |||
126 | return null; | ||
127 | }, | ||
128 | |||
129 | /** | ||
130 | * Wraps this element with given `wrapper`. | ||
131 | * | ||
132 | * @since 4.3 | ||
133 | * @param {CKEDITOR.htmlParser.element} wrapper The element which will be this element's new parent. | ||
134 | * @returns {CKEDITOR.htmlParser.element} Wrapper. | ||
135 | */ | ||
136 | wrapWith: function( wrapper ) { | ||
137 | this.replaceWith( wrapper ); | ||
138 | wrapper.add( this ); | ||
139 | return wrapper; | ||
140 | }, | ||
141 | |||
142 | /** | ||
143 | * Gets this node's index in its parent's children array. | ||
144 | * | ||
145 | * @since 4.3 | ||
146 | * @returns {Number} | ||
147 | */ | ||
148 | getIndex: function() { | ||
149 | return CKEDITOR.tools.indexOf( this.parent.children, this ); | ||
150 | }, | ||
151 | |||
152 | getFilterContext: function( context ) { | ||
153 | return context || {}; | ||
154 | } | ||
155 | }; | ||
156 | } )(); | ||