Conditions | 29 |
Paths | 1348 |
Total Lines | 141 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like tinymce.PluginManager.add(ꞌautolinkꞌ) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** |
||
68 | function parseCurrentLine(editor, end_offset, delimiter) { |
||
69 | var rng, end, start, endContainer, bookmark, text, matches, prev, len, rngText; |
||
70 | |||
71 | function scopeIndex(container, index) { |
||
72 | if (index < 0) { |
||
73 | index = 0; |
||
74 | } |
||
75 | |||
76 | if (container.nodeType == 3) { |
||
77 | var len = container.data.length; |
||
78 | |||
79 | if (index > len) { |
||
80 | index = len; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | return index; |
||
85 | } |
||
86 | |||
87 | function setStart(container, offset) { |
||
88 | if (container.nodeType != 1 || container.hasChildNodes()) { |
||
89 | rng.setStart(container, scopeIndex(container, offset)); |
||
90 | } else { |
||
91 | rng.setStartBefore(container); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | function setEnd(container, offset) { |
||
96 | if (container.nodeType != 1 || container.hasChildNodes()) { |
||
97 | rng.setEnd(container, scopeIndex(container, offset)); |
||
98 | } else { |
||
99 | rng.setEndAfter(container); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | // Never create a link when we are inside a link |
||
104 | if (editor.selection.getNode().tagName == 'A') { |
||
105 | return; |
||
106 | } |
||
107 | |||
108 | // We need at least five characters to form a URL, |
||
109 | // hence, at minimum, five characters from the beginning of the line. |
||
110 | rng = editor.selection.getRng(true).cloneRange(); |
||
111 | if (rng.startOffset < 5) { |
||
112 | // During testing, the caret is placed between two text nodes. |
||
113 | // The previous text node contains the URL. |
||
114 | prev = rng.endContainer.previousSibling; |
||
115 | if (!prev) { |
||
116 | if (!rng.endContainer.firstChild || !rng.endContainer.firstChild.nextSibling) { |
||
117 | return; |
||
118 | } |
||
119 | |||
120 | prev = rng.endContainer.firstChild.nextSibling; |
||
121 | } |
||
122 | |||
123 | len = prev.length; |
||
124 | setStart(prev, len); |
||
125 | setEnd(prev, len); |
||
126 | |||
127 | if (rng.endOffset < 5) { |
||
128 | return; |
||
129 | } |
||
130 | |||
131 | end = rng.endOffset; |
||
132 | endContainer = prev; |
||
133 | } else { |
||
134 | endContainer = rng.endContainer; |
||
135 | |||
136 | // Get a text node |
||
137 | if (endContainer.nodeType != 3 && endContainer.firstChild) { |
||
138 | while (endContainer.nodeType != 3 && endContainer.firstChild) { |
||
139 | endContainer = endContainer.firstChild; |
||
140 | } |
||
141 | |||
142 | // Move range to text node |
||
143 | if (endContainer.nodeType == 3) { |
||
144 | setStart(endContainer, 0); |
||
145 | setEnd(endContainer, endContainer.nodeValue.length); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | if (rng.endOffset == 1) { |
||
150 | end = 2; |
||
151 | } else { |
||
152 | end = rng.endOffset - 1 - end_offset; |
||
153 | } |
||
154 | } |
||
155 | |||
156 | start = end; |
||
157 | |||
158 | do { |
||
159 | // Move the selection one character backwards. |
||
160 | setStart(endContainer, end >= 2 ? end - 2 : 0); |
||
161 | setEnd(endContainer, end >= 1 ? end - 1 : 0); |
||
162 | end -= 1; |
||
163 | rngText = rng.toString(); |
||
164 | |||
165 | // Loop until one of the following is found: a blank space, , delimiter, (end-2) >= 0 |
||
166 | } while (rngText != ' ' && rngText !== '' && rngText.charCodeAt(0) != 160 && (end - 2) >= 0 && rngText != delimiter); |
||
167 | |||
168 | if (rng.toString() == delimiter || rng.toString().charCodeAt(0) == 160) { |
||
169 | setStart(endContainer, end); |
||
170 | setEnd(endContainer, start); |
||
171 | end += 1; |
||
172 | } else if (rng.startOffset === 0) { |
||
173 | setStart(endContainer, 0); |
||
174 | setEnd(endContainer, start); |
||
175 | } else { |
||
176 | setStart(endContainer, end); |
||
177 | setEnd(endContainer, start); |
||
178 | } |
||
179 | |||
180 | // Exclude last . from word like "www.site.com." |
||
181 | text = rng.toString(); |
||
182 | if (text.charAt(text.length - 1) == '.') { |
||
183 | setEnd(endContainer, start - 1); |
||
184 | } |
||
185 | |||
186 | text = rng.toString(); |
||
187 | matches = text.match(AutoLinkPattern); |
||
188 | |||
189 | if (matches) { |
||
190 | if (matches[1] == 'www.') { |
||
191 | matches[1] = 'http://www.'; |
||
192 | } else if (/@$/.test(matches[1]) && !/^mailto:/.test(matches[1])) { |
||
193 | matches[1] = 'mailto:' + matches[1]; |
||
194 | } |
||
195 | |||
196 | bookmark = editor.selection.getBookmark(); |
||
197 | |||
198 | editor.selection.setRng(rng); |
||
199 | editor.execCommand('createlink', false, matches[1] + matches[2]); |
||
200 | |||
201 | if (editor.settings.default_link_target) { |
||
202 | editor.dom.setAttrib(editor.selection.getNode(), 'target', editor.settings.default_link_target); |
||
203 | } |
||
204 | |||
205 | editor.selection.moveToBookmark(bookmark); |
||
206 | editor.nodeChanged(); |
||
207 | } |
||
208 | } |
||
209 | }); |
||
210 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.