w2tags 0.9.55 → 0.9.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. data/Manifest.txt +31 -0
  2. data/lib/w2tags/parser.rb +2 -5
  3. data/lib/w2tags/try/public/css/csscolors.css +47 -0
  4. data/lib/w2tags/try/public/css/jquery.cluetip.css +130 -0
  5. data/lib/w2tags/try/public/css/jscolors.css +55 -0
  6. data/lib/w2tags/try/public/css/rubycolors.css +82 -0
  7. data/lib/w2tags/try/public/css/xmlcolors.css +51 -0
  8. data/lib/w2tags/try/public/img/loading.gif +0 -0
  9. data/lib/w2tags/try/public/js/codemirror.js +308 -0
  10. data/lib/w2tags/try/public/js/editor.js +1340 -0
  11. data/lib/w2tags/try/public/js/highlight.js +68 -0
  12. data/lib/w2tags/try/public/js/jquery.cluetip.js +470 -0
  13. data/lib/w2tags/try/public/js/jquery.js +4376 -0
  14. data/lib/w2tags/try/public/js/mirrorframe.js +81 -0
  15. data/lib/w2tags/try/public/js/parsecss.js +155 -0
  16. data/lib/w2tags/try/public/js/parsehtmlmixed.js +74 -0
  17. data/lib/w2tags/try/public/js/parsejavascript.js +350 -0
  18. data/lib/w2tags/try/public/js/parsew2tags.js +157 -0
  19. data/lib/w2tags/try/public/js/parsexml.js +292 -0
  20. data/lib/w2tags/try/public/js/select.js +607 -0
  21. data/lib/w2tags/try/public/js/stringstream.js +140 -0
  22. data/lib/w2tags/try/public/js/tokenize.js +57 -0
  23. data/lib/w2tags/try/public/js/tokenizejavascript.js +175 -0
  24. data/lib/w2tags/try/public/js/undo.js +404 -0
  25. data/lib/w2tags/try/public/js/util.js +134 -0
  26. data/lib/w2tags/try/public/w2/basic.w2erb +8 -2
  27. data/lib/w2tags/try/public/w2/erb_base.hot.html +167 -0
  28. data/lib/w2tags/try/public/w2/erb_rails.hot.html +59 -0
  29. data/lib/w2tags/try/public/w2/html.hot.html +1 -0
  30. data/lib/w2tags/try/public/w2/rails.hot.html +37 -0
  31. data/lib/w2tags/try/public/w2/try.rb.hot.html +50 -0
  32. data/lib/w2tags/try/try.rb +3 -2
  33. data/lib/w2tags/try/views/index.erb +85 -15
  34. data/lib/w2tags/try/views/layout.erb +4 -5
  35. data/lib/w2tags/try/views/parse.erb +1 -0
  36. data/tasks/setup.rb +1 -1
  37. metadata +58 -2
@@ -0,0 +1,157 @@
1
+ /* Simple parser for CSS */
2
+
3
+ var CSSParser = Editor.Parser = (function() {
4
+ var tokenizeCSS = (function() {
5
+ function normal(source, setState) {
6
+ var ch = source.next();
7
+ if (ch == "-" && source.equals("#")) { source.nextWhileMatches(/./); return "rb-comment"; } else
8
+ if (ch == ":") { source.nextWhileMatches(/[\w-]/); return "rb-method-parameter"; } else
9
+ if (ch == "#") { source.nextWhileMatches(/[\w-]/); return "rb-constant"; } else
10
+ if (ch == ".") { source.nextWhileMatches(/[\w-]/); return "rb-method"; } else
11
+ if (ch == "-") { source.nextWhileMatches(/\w/); return "rb-symbol"; } else
12
+ if (ch == "^") { source.nextWhileMatches(/\w/); return "rb-symbol"; } else
13
+ if (ch == "%") { source.nextWhileMatches(/\w/); return "rb-keyword"; } else
14
+ if (ch == "=") { source.nextWhileMatches(/\w/); return "rb-operator"; } else
15
+ if (ch == "@") { source.nextWhileMatches(/\w/); return "rb-keyword"; } else
16
+ if (ch == "$") { source.nextWhileMatches(/[\d*]/); return "rb-regexp"; } else
17
+ if (ch == "!" && source.equals("H")) { source.nextWhileMatches(/./); return "rb-regexp"; } else
18
+ if (ch == "/" && source.equals("*")) {
19
+ setState(inCComment);
20
+ return null;
21
+ }
22
+ else if (ch == "<" && source.equals("!")) {
23
+ setState(inSGMLComment);
24
+ return null;
25
+ }
26
+ else if (ch == "=") {
27
+ return "css-compare";
28
+ }
29
+ else if (source.equals("=") && (ch == "~" || ch == "|")) {
30
+ source.next();
31
+ return "css-compare";
32
+ }
33
+ else if (ch == "\"" || ch == "'") {
34
+ setState(inString(ch));
35
+ return null;
36
+ }
37
+ else if (ch == "#") {
38
+ source.nextWhileMatches(/\w/);
39
+ return "css-hash";
40
+ }
41
+ else if (/\d/.test(ch)) {
42
+ source.nextWhileMatches(/[\w.%]/);
43
+ return "css-unit";
44
+ }
45
+ else if (/[,.+>*\/]/.test(ch)) {
46
+ return "css-select-op";
47
+ }
48
+ else if (/[;{}:\[\]]/.test(ch)) {
49
+ return "rb-heredoc";
50
+ }
51
+ else {
52
+ source.nextWhileMatches(/[\w\\\-_]/);
53
+ return "css-identifier";
54
+ }
55
+ }
56
+
57
+ function inCComment(source, setState) {
58
+ var maybeEnd = false;
59
+ while (!source.endOfLine()) {
60
+ var ch = source.next();
61
+ if (maybeEnd && ch == "/") {
62
+ setState(normal);
63
+ break;
64
+ }
65
+ maybeEnd = (ch == "*");
66
+ }
67
+ return "css-comment";
68
+ }
69
+
70
+ function inSGMLComment(source, setState) {
71
+ var dashes = 0;
72
+ while (!source.endOfLine()) {
73
+ var ch = source.next();
74
+ if (dashes >= 2 && ch == ">") {
75
+ setState(normal);
76
+ break;
77
+ }
78
+ dashes = (ch == "-") ? dashes + 1 : 0;
79
+ }
80
+ return "css-comment";
81
+ }
82
+
83
+ function inString(quote) {
84
+ return function(source, setState) {
85
+ var escaped = false;
86
+ while (!source.endOfLine()) {
87
+ var ch = source.next();
88
+ if (ch == quote && !escaped)
89
+ break;
90
+ escaped = !escaped && ch == "\\";
91
+ }
92
+ if (!escaped)
93
+ setState(normal);
94
+ return "rb-string";
95
+ };
96
+ }
97
+
98
+ return function(source, startState) {
99
+ return tokenizer(source, startState || normal);
100
+ };
101
+ })();
102
+
103
+ function indentCSS(inBraces, inRule, base) {
104
+ return function(nextChars) {
105
+ if (!inBraces || /^\}/.test(nextChars)) return base;
106
+ else if (inRule) return base + indentUnit * 2;
107
+ else return base + indentUnit;
108
+ };
109
+ }
110
+
111
+ // This is a very simplistic parser -- since CSS does not really
112
+ // nest, it works acceptably well, but some nicer colouroing could
113
+ // be provided with a more complicated parser.
114
+ function parseCSS(source, basecolumn) {
115
+ basecolumn = basecolumn || 0;
116
+ var tokens = tokenizeCSS(source);
117
+ var inBraces = false, inRule = false;
118
+
119
+ var iter = {
120
+ next: function() {
121
+ var token = tokens.next(), style = token.style, content = token.content;
122
+
123
+ if (style == "css-identifier" && inRule)
124
+ token.style = "css-value";
125
+ if (style == "css-hash")
126
+ token.style = inRule ? "css-colorcode" : "css-identifier";
127
+
128
+ if (content == "\n")
129
+ token.indentation = indentCSS(inBraces, inRule, basecolumn);
130
+
131
+ if (content == "{")
132
+ inBraces = true;
133
+ else if (content == "}")
134
+ inBraces = inRule = false;
135
+ else if (inBraces && content == ";")
136
+ inRule = false;
137
+ else if (inBraces && style != "css-comment" && style != "whitespace")
138
+ inRule = true;
139
+
140
+ return token;
141
+ },
142
+
143
+ copy: function() {
144
+ var _inBraces = inBraces, _inRule = inRule, _tokenState = tokens.state;
145
+ return function(source) {
146
+ tokens = tokenizeCSS(source, _tokenState);
147
+ inBraces = _inBraces;
148
+ inRule = _inRule;
149
+ return iter;
150
+ };
151
+ }
152
+ };
153
+ return iter;
154
+ }
155
+
156
+ return {make: parseCSS, electricChars: "}"};
157
+ })();
@@ -0,0 +1,292 @@
1
+ /* This file defines an XML parser, with a few kludges to make it
2
+ * useable for HTML. autoSelfClosers defines a set of tag names that
3
+ * are expected to not have a closing tag, and doNotIndent specifies
4
+ * the tags inside of which no indentation should happen (see Config
5
+ * object). These can be disabled by passing the editor an object like
6
+ * {useHTMLKludges: false} as parserConfig option.
7
+ */
8
+
9
+ var XMLParser = Editor.Parser = (function() {
10
+ var Kludges = {
11
+ autoSelfClosers: {"br": true, "img": true, "hr": true, "link": true, "input": true,
12
+ "meta": true, "col": true, "frame": true, "base": true, "area": true},
13
+ doNotIndent: {"pre": true, "!cdata": true}
14
+ };
15
+ var NoKludges = {autoSelfClosers: {}, doNotIndent: {"!cdata": true}};
16
+ var UseKludges = Kludges;
17
+ var alignCDATA = false;
18
+
19
+ // Simple stateful tokenizer for XML documents. Returns a
20
+ // MochiKit-style iterator, with a state property that contains a
21
+ // function encapsulating the current state. See tokenize.js.
22
+ var tokenizeXML = (function() {
23
+ function inText(source, setState) {
24
+ var ch = source.next();
25
+ if (ch == "<") {
26
+ if (source.equals("!")) {
27
+ source.next();
28
+ if (source.equals("[")) {
29
+ if (source.lookAhead("[CDATA[", true)) {
30
+ setState(inBlock("xml-cdata", "]]>"));
31
+ return null;
32
+ }
33
+ else {
34
+ return "xml-text";
35
+ }
36
+ }
37
+ else if (source.lookAhead("--", true)) {
38
+ setState(inBlock("xml-comment", "-->"));
39
+ return null;
40
+ }
41
+ else {
42
+ return "xml-text";
43
+ }
44
+ }
45
+ else if (source.equals("?")) {
46
+ source.next();
47
+ source.nextWhileMatches(/[\w\._\-]/);
48
+ setState(inBlock("xml-processing", "?>"));
49
+ return "xml-processing";
50
+ }
51
+ else {
52
+ if (source.equals("/")) source.next();
53
+ setState(inTag);
54
+ return "xml-punctuation";
55
+ }
56
+ }
57
+ else if (ch == "&") {
58
+ while (!source.endOfLine()) {
59
+ if (source.next() == ";")
60
+ break;
61
+ }
62
+ return "xml-entity";
63
+ }
64
+ else {
65
+ source.nextWhileMatches(/[^&<\n]/);
66
+ return "xml-text";
67
+ }
68
+ }
69
+
70
+ function inTag(source, setState) {
71
+ var ch = source.next();
72
+ if (ch == ">") {
73
+ setState(inText);
74
+ return "xml-punctuation";
75
+ }
76
+ else if (/[?\/]/.test(ch) && source.equals(">")) {
77
+ source.next();
78
+ setState(inText);
79
+ return "xml-punctuation";
80
+ }
81
+ else if (ch == "=") {
82
+ return "xml-punctuation";
83
+ }
84
+ else if (/[\'\"]/.test(ch)) {
85
+ setState(inAttribute(ch));
86
+ return null;
87
+ }
88
+ else {
89
+ source.nextWhileMatches(/[^\s\u00a0=<>\"\'\/?]/);
90
+ return "xml-name";
91
+ }
92
+ }
93
+
94
+ function inAttribute(quote) {
95
+ return function(source, setState) {
96
+ while (!source.endOfLine()) {
97
+ if (source.next() == quote) {
98
+ setState(inTag);
99
+ break;
100
+ }
101
+ }
102
+ return "xml-attribute";
103
+ };
104
+ }
105
+
106
+ function inBlock(style, terminator) {
107
+ return function(source, setState) {
108
+ while (!source.endOfLine()) {
109
+ if (source.lookAhead(terminator, true)) {
110
+ setState(inText);
111
+ break;
112
+ }
113
+ source.next();
114
+ }
115
+ return style;
116
+ };
117
+ }
118
+
119
+ return function(source, startState) {
120
+ return tokenizer(source, startState || inText);
121
+ };
122
+ })();
123
+
124
+ // The parser. The structure of this function largely follows that of
125
+ // parseJavaScript in parsejavascript.js (there is actually a bit more
126
+ // shared code than I'd like), but it is quite a bit simpler.
127
+ function parseXML(source) {
128
+ var tokens = tokenizeXML(source);
129
+ var cc = [base];
130
+ var tokenNr = 0, indented = 0;
131
+ var currentTag = null, context = null;
132
+ var consume, marked;
133
+
134
+ function push(fs) {
135
+ for (var i = fs.length - 1; i >= 0; i--)
136
+ cc.push(fs[i]);
137
+ }
138
+ function cont() {
139
+ push(arguments);
140
+ consume = true;
141
+ }
142
+ function pass() {
143
+ push(arguments);
144
+ consume = false;
145
+ }
146
+
147
+ function mark(style) {
148
+ marked = style;
149
+ }
150
+ function expect(text) {
151
+ return function(style, content) {
152
+ if (content == text) cont();
153
+ else mark("xml-error") || cont(arguments.callee);
154
+ };
155
+ }
156
+
157
+ function pushContext(tagname, startOfLine) {
158
+ var noIndent = UseKludges.doNotIndent.hasOwnProperty(tagname) || (context && context.noIndent);
159
+ context = {prev: context, name: tagname, indent: indented, startOfLine: startOfLine, noIndent: noIndent};
160
+ }
161
+ function popContext() {
162
+ context = context.prev;
163
+ }
164
+ function computeIndentation(baseContext) {
165
+ return function(nextChars, current) {
166
+ var context = baseContext;
167
+ if (context && context.noIndent)
168
+ return current;
169
+ if (alignCDATA && /<!\[CDATA\[/.test(nextChars))
170
+ return 0;
171
+ if (context && /^<\//.test(nextChars))
172
+ context = context.prev;
173
+ while (context && !context.startOfLine)
174
+ context = context.prev;
175
+ if (context)
176
+ return context.indent + indentUnit;
177
+ else
178
+ return 0;
179
+ };
180
+ }
181
+
182
+ function base() {
183
+ return pass(element, base);
184
+ }
185
+ var harmlessTokens = {"xml-text": true, "xml-entity": true, "xml-comment": true, "xml-processing": true};
186
+ function element(style, content) {
187
+ if (content == "<") cont(tagname, attributes, endtag(tokenNr == 1));
188
+ else if (content == "</") cont(closetagname, expect(">"));
189
+ else if (style == "xml-cdata") {
190
+ if (!context || context.name != "!cdata") pushContext("!cdata");
191
+ if (/\]\]>$/.test(content)) popContext();
192
+ cont();
193
+ }
194
+ else if (harmlessTokens.hasOwnProperty(style)) cont();
195
+ else mark("xml-error") || cont();
196
+ }
197
+ function tagname(style, content) {
198
+ if (style == "xml-name") {
199
+ currentTag = content.toLowerCase();
200
+ mark("xml-tagname");
201
+ cont();
202
+ }
203
+ else {
204
+ currentTag = null;
205
+ pass();
206
+ }
207
+ }
208
+ function closetagname(style, content) {
209
+ if (style == "xml-name" && context && content.toLowerCase() == context.name) {
210
+ popContext();
211
+ mark("xml-tagname");
212
+ }
213
+ else {
214
+ mark("xml-error");
215
+ }
216
+ cont();
217
+ }
218
+ function endtag(startOfLine) {
219
+ return function(style, content) {
220
+ if (content == "/>" || (content == ">" && UseKludges.autoSelfClosers.hasOwnProperty(currentTag))) cont();
221
+ else if (content == ">") pushContext(currentTag, startOfLine) || cont();
222
+ else mark("xml-error") || cont(arguments.callee);
223
+ };
224
+ }
225
+ function attributes(style) {
226
+ if (style == "xml-name") mark("xml-attname") || cont(attribute, attributes);
227
+ else pass();
228
+ }
229
+ function attribute(style, content) {
230
+ if (content == "=") cont(value);
231
+ else if (content == ">" || content == "/>") pass(endtag);
232
+ else pass();
233
+ }
234
+ function value(style) {
235
+ if (style == "xml-attribute") cont(value);
236
+ else pass();
237
+ }
238
+
239
+ return {
240
+ indentation: function() {return indented;},
241
+
242
+ next: function(){
243
+ var token = tokens.next();
244
+ if (token.style == "whitespace" && tokenNr == 0)
245
+ indented = token.value.length;
246
+ else
247
+ tokenNr++;
248
+ if (token.content == "\n") {
249
+ indented = tokenNr = 0;
250
+ token.indentation = computeIndentation(context);
251
+ }
252
+
253
+ if (token.style == "whitespace" || token.type == "xml-comment")
254
+ return token;
255
+
256
+ while(true){
257
+ consume = marked = false;
258
+ cc.pop()(token.style, token.content);
259
+ if (consume){
260
+ if (marked)
261
+ token.style = marked;
262
+ return token;
263
+ }
264
+ }
265
+ },
266
+
267
+ copy: function(){
268
+ var _cc = cc.concat([]), _tokenState = tokens.state, _context = context;
269
+ var parser = this;
270
+
271
+ return function(input){
272
+ cc = _cc.concat([]);
273
+ tokenNr = indented = 0;
274
+ context = _context;
275
+ tokens = tokenizeXML(input, _tokenState);
276
+ return parser;
277
+ };
278
+ }
279
+ };
280
+ }
281
+
282
+ return {
283
+ make: parseXML,
284
+ electricChars: "/",
285
+ configure: function(config) {
286
+ if (config.useHTMLKludges != null)
287
+ UseKludges = config.useHTMLKludges ? Kludges : NoKludges;
288
+ if (config.alignCDATA)
289
+ alignCDATA = config.alignCDATA;
290
+ }
291
+ };
292
+ })();
@@ -0,0 +1,607 @@
1
+ /* Functionality for finding, storing, and restoring selections
2
+ *
3
+ * This does not provide a generic API, just the minimal functionality
4
+ * required by the CodeMirror system.
5
+ */
6
+
7
+ // Namespace object.
8
+ var select = {};
9
+
10
+ (function() {
11
+ select.ie_selection = document.selection && document.selection.createRangeCollection;
12
+
13
+ // Find the 'top-level' (defined as 'a direct child of the node
14
+ // passed as the top argument') node that the given node is
15
+ // contained in. Return null if the given node is not inside the top
16
+ // node.
17
+ function topLevelNodeAt(node, top) {
18
+ while (node && node.parentNode != top)
19
+ node = node.parentNode;
20
+ return node;
21
+ }
22
+
23
+ // Find the top-level node that contains the node before this one.
24
+ function topLevelNodeBefore(node, top) {
25
+ while (!node.previousSibling && node.parentNode != top)
26
+ node = node.parentNode;
27
+ return topLevelNodeAt(node.previousSibling, top);
28
+ }
29
+
30
+ var fourSpaces = "\u00a0\u00a0\u00a0\u00a0";
31
+
32
+ select.scrollToNode = function(element) {
33
+ if (!element) return;
34
+ var doc = element.ownerDocument, body = doc.body,
35
+ win = (doc.defaultView || doc.parentWindow),
36
+ html = doc.documentElement,
37
+ atEnd = !element.nextSibling || !element.nextSibling.nextSibling
38
+ || !element.nextSibling.nextSibling.nextSibling;
39
+ // In Opera (and recent Webkit versions), BR elements *always*
40
+ // have a offsetTop property of zero.
41
+ var compensateHack = 0;
42
+ while (element && !element.offsetTop) {
43
+ compensateHack++;
44
+ element = element.previousSibling;
45
+ }
46
+ // atEnd is another kludge for these browsers -- if the cursor is
47
+ // at the end of the document, and the node doesn't have an
48
+ // offset, just scroll to the end.
49
+ if (compensateHack == 0) atEnd = false;
50
+
51
+ var y = compensateHack * (element ? element.offsetHeight : 0), x = 0, pos = element;
52
+ while (pos && pos.offsetParent) {
53
+ y += pos.offsetTop;
54
+ // Don't count X offset for <br> nodes
55
+ if (!isBR(pos))
56
+ x += pos.offsetLeft;
57
+ pos = pos.offsetParent;
58
+ }
59
+
60
+ var scroll_x = body.scrollLeft || html.scrollLeft || 0,
61
+ scroll_y = body.scrollTop || html.scrollTop || 0,
62
+ screen_x = x - scroll_x, screen_y = y - scroll_y, scroll = false;
63
+
64
+ if (screen_x < 0 || screen_x > (win.innerWidth || html.clientWidth || 0)) {
65
+ scroll_x = x;
66
+ scroll = true;
67
+ }
68
+ if (screen_y < 0 || atEnd || screen_y > (win.innerHeight || html.clientHeight || 0) - 50) {
69
+ scroll_y = atEnd ? 1e10 : y;
70
+ scroll = true;
71
+ }
72
+ if (scroll) win.scrollTo(scroll_x, scroll_y);
73
+ };
74
+
75
+ select.scrollToCursor = function(container) {
76
+ select.scrollToNode(select.selectionTopNode(container, true) || container.firstChild);
77
+ };
78
+
79
+ // Used to prevent restoring a selection when we do not need to.
80
+ var currentSelection = null;
81
+
82
+ select.snapshotChanged = function() {
83
+ if (currentSelection) currentSelection.changed = true;
84
+ };
85
+
86
+ // This is called by the code in editor.js whenever it is replacing
87
+ // a text node. The function sees whether the given oldNode is part
88
+ // of the current selection, and updates this selection if it is.
89
+ // Because nodes are often only partially replaced, the length of
90
+ // the part that gets replaced has to be taken into account -- the
91
+ // selection might stay in the oldNode if the newNode is smaller
92
+ // than the selection's offset. The offset argument is needed in
93
+ // case the selection does move to the new object, and the given
94
+ // length is not the whole length of the new node (part of it might
95
+ // have been used to replace another node).
96
+ select.snapshotReplaceNode = function(from, to, length, offset) {
97
+ if (!currentSelection) return;
98
+
99
+ function replace(point) {
100
+ if (from == point.node) {
101
+ currentSelection.changed = true;
102
+ if (length && point.offset > length) {
103
+ point.offset -= length;
104
+ }
105
+ else {
106
+ point.node = to;
107
+ point.offset += (offset || 0);
108
+ }
109
+ }
110
+ }
111
+ replace(currentSelection.start);
112
+ replace(currentSelection.end);
113
+ };
114
+
115
+ select.snapshotMove = function(from, to, distance, relative, ifAtStart) {
116
+ if (!currentSelection) return;
117
+
118
+ function move(point) {
119
+ if (from == point.node && (!ifAtStart || point.offset == 0)) {
120
+ currentSelection.changed = true;
121
+ point.node = to;
122
+ if (relative) point.offset = Math.max(0, point.offset + distance);
123
+ else point.offset = distance;
124
+ }
125
+ }
126
+ move(currentSelection.start);
127
+ move(currentSelection.end);
128
+ };
129
+
130
+ // Most functions are defined in two ways, one for the IE selection
131
+ // model, one for the W3C one.
132
+ if (select.ie_selection) {
133
+ function selectionNode(win, start) {
134
+ var range = win.document.selection.createRange();
135
+ range.collapse(start);
136
+
137
+ function nodeAfter(node) {
138
+ var found = null;
139
+ while (!found && node) {
140
+ found = node.nextSibling;
141
+ node = node.parentNode;
142
+ }
143
+ return nodeAtStartOf(found);
144
+ }
145
+
146
+ function nodeAtStartOf(node) {
147
+ while (node && node.firstChild) node = node.firstChild;
148
+ return {node: node, offset: 0};
149
+ }
150
+
151
+ var containing = range.parentElement();
152
+ if (!isAncestor(win.document.body, containing)) return null;
153
+ if (!containing.firstChild) return nodeAtStartOf(containing);
154
+
155
+ var working = range.duplicate();
156
+ working.moveToElementText(containing);
157
+ working.collapse(true);
158
+ for (var cur = containing.firstChild; cur; cur = cur.nextSibling) {
159
+ if (cur.nodeType == 3) {
160
+ var size = cur.nodeValue.length;
161
+ working.move("character", size);
162
+ }
163
+ else {
164
+ working.moveToElementText(cur);
165
+ working.collapse(false);
166
+ }
167
+
168
+ var dir = range.compareEndPoints("StartToStart", working);
169
+ if (dir == 0) return nodeAfter(cur);
170
+ if (dir == 1) continue;
171
+ if (cur.nodeType != 3) return nodeAtStartOf(cur);
172
+
173
+ working.setEndPoint("StartToEnd", range);
174
+ return {node: cur, offset: size - working.text.length};
175
+ }
176
+ return nodeAfter(containing);
177
+ }
178
+
179
+ select.markSelection = function(win) {
180
+ currentSelection = null;
181
+ var sel = win.document.selection;
182
+ if (!sel) return;
183
+ var start = selectionNode(win, true),
184
+ end = selectionNode(win, false);
185
+ if (!start || !end) return;
186
+ currentSelection = {start: start, end: end, window: win, changed: false};
187
+ };
188
+
189
+ select.selectMarked = function() {
190
+ if (!currentSelection || !currentSelection.changed) return;
191
+ var win = currentSelection.window, doc = win.document;
192
+
193
+ function makeRange(point) {
194
+ var range = doc.body.createTextRange(),
195
+ node = point.node;
196
+ if (!node) {
197
+ range.moveToElementText(currentSelection.window.document.body);
198
+ range.collapse(false);
199
+ }
200
+ else if (node.nodeType == 3) {
201
+ range.moveToElementText(node.parentNode);
202
+ var offset = point.offset;
203
+ while (node.previousSibling) {
204
+ node = node.previousSibling;
205
+ offset += (node.innerText || "").length;
206
+ }
207
+ range.move("character", offset);
208
+ }
209
+ else {
210
+ range.moveToElementText(node);
211
+ range.collapse(true);
212
+ }
213
+ return range;
214
+ }
215
+
216
+ var start = makeRange(currentSelection.start), end = makeRange(currentSelection.end);
217
+ start.setEndPoint("StartToEnd", end);
218
+ start.select();
219
+ };
220
+
221
+ // Get the top-level node that one end of the cursor is inside or
222
+ // after. Note that this returns false for 'no cursor', and null
223
+ // for 'start of document'.
224
+ select.selectionTopNode = function(container, start) {
225
+ var selection = container.ownerDocument.selection;
226
+ if (!selection) return false;
227
+
228
+ var range = selection.createRange(), range2 = range.duplicate();
229
+ range.collapse(start);
230
+ var around = range.parentElement();
231
+ if (around && isAncestor(container, around)) {
232
+ // Only use this node if the selection is not at its start.
233
+ range2.moveToElementText(around);
234
+ if (range.compareEndPoints("StartToStart", range2) == 1)
235
+ return topLevelNodeAt(around, container);
236
+ }
237
+
238
+ // Move the start of a range to the start of a node,
239
+ // compensating for the fact that you can't call
240
+ // moveToElementText with text nodes.
241
+ function moveToNodeStart(range, node) {
242
+ if (node.nodeType == 3) {
243
+ var count = 0, cur = node.previousSibling;
244
+ while (cur && cur.nodeType == 3) {
245
+ count += cur.nodeValue.length;
246
+ cur = cur.previousSibling;
247
+ }
248
+ if (cur) {
249
+ try{range.moveToElementText(cur);}
250
+ catch(e){return false;}
251
+ range.collapse(false);
252
+ }
253
+ else range.moveToElementText(node.parentNode);
254
+ if (count) range.move("character", count);
255
+ }
256
+ else {
257
+ try{range.moveToElementText(node);}
258
+ catch(e){return false;}
259
+ }
260
+ return true;
261
+ }
262
+
263
+ // Do a binary search through the container object, comparing
264
+ // the start of each node to the selection
265
+ var start = 0, end = container.childNodes.length - 1;
266
+ while (start < end) {
267
+ var middle = Math.ceil((end + start) / 2), node = container.childNodes[middle];
268
+ if (!node) return false; // Don't ask. IE6 manages this sometimes.
269
+ if (!moveToNodeStart(range2, node)) return false;
270
+ if (range.compareEndPoints("StartToStart", range2) == 1)
271
+ start = middle;
272
+ else
273
+ end = middle - 1;
274
+ }
275
+ return container.childNodes[start] || null;
276
+ };
277
+
278
+ // Place the cursor after this.start. This is only useful when
279
+ // manually moving the cursor instead of restoring it to its old
280
+ // position.
281
+ select.focusAfterNode = function(node, container) {
282
+ var range = container.ownerDocument.body.createTextRange();
283
+ range.moveToElementText(node || container);
284
+ range.collapse(!node);
285
+ range.select();
286
+ };
287
+
288
+ select.somethingSelected = function(win) {
289
+ var sel = win.document.selection;
290
+ return sel && (sel.createRange().text != "");
291
+ };
292
+
293
+ function insertAtCursor(window, html) {
294
+ var selection = window.document.selection;
295
+ if (selection) {
296
+ var range = selection.createRange();
297
+ range.pasteHTML(html);
298
+ range.collapse(false);
299
+ range.select();
300
+ }
301
+ }
302
+
303
+ // Used to normalize the effect of the enter key, since browsers
304
+ // do widely different things when pressing enter in designMode.
305
+ select.insertNewlineAtCursor = function(window) {
306
+ insertAtCursor(window, "<br>");
307
+ };
308
+
309
+ select.insertTabAtCursor = function(window) {
310
+ insertAtCursor(window, fourSpaces);
311
+ };
312
+
313
+ // Get the BR node at the start of the line on which the cursor
314
+ // currently is, and the offset into the line. Returns null as
315
+ // node if cursor is on first line.
316
+ select.cursorPos = function(container, start) {
317
+ var selection = container.ownerDocument.selection;
318
+ if (!selection) return null;
319
+
320
+ var topNode = select.selectionTopNode(container, start);
321
+ while (topNode && !isBR(topNode))
322
+ topNode = topNode.previousSibling;
323
+
324
+ var range = selection.createRange(), range2 = range.duplicate();
325
+ range.collapse(start);
326
+ if (topNode) {
327
+ range2.moveToElementText(topNode);
328
+ range2.collapse(false);
329
+ }
330
+ else {
331
+ // When nothing is selected, we can get all kinds of funky errors here.
332
+ try { range2.moveToElementText(container); }
333
+ catch (e) { return null; }
334
+ range2.collapse(true);
335
+ }
336
+ range.setEndPoint("StartToStart", range2);
337
+
338
+ return {node: topNode, offset: range.text.length};
339
+ };
340
+
341
+ select.setCursorPos = function(container, from, to) {
342
+ function rangeAt(pos) {
343
+ var range = container.ownerDocument.body.createTextRange();
344
+ if (!pos.node) {
345
+ range.moveToElementText(container);
346
+ range.collapse(true);
347
+ }
348
+ else {
349
+ range.moveToElementText(pos.node);
350
+ range.collapse(false);
351
+ }
352
+ range.move("character", pos.offset);
353
+ return range;
354
+ }
355
+
356
+ var range = rangeAt(from);
357
+ if (to && to != from)
358
+ range.setEndPoint("EndToEnd", rangeAt(to));
359
+ range.select();
360
+ }
361
+
362
+ // Some hacks for storing and re-storing the selection when the editor loses and regains focus.
363
+ select.getBookmark = function (container) {
364
+ var from = select.cursorPos(container, true), to = select.cursorPos(container, false);
365
+ if (from && to) return {from: from, to: to};
366
+ };
367
+
368
+ // Restore a stored selection.
369
+ select.setBookmark = function(container, mark) {
370
+ if (!mark) return;
371
+ select.setCursorPos(container, mark.from, mark.to);
372
+ };
373
+ }
374
+ // W3C model
375
+ else {
376
+ // Store start and end nodes, and offsets within these, and refer
377
+ // back to the selection object from those nodes, so that this
378
+ // object can be updated when the nodes are replaced before the
379
+ // selection is restored.
380
+ select.markSelection = function (win) {
381
+ var selection = win.getSelection();
382
+ if (!selection || selection.rangeCount == 0)
383
+ return (currentSelection = null);
384
+ var range = selection.getRangeAt(0);
385
+
386
+ currentSelection = {
387
+ start: {node: range.startContainer, offset: range.startOffset},
388
+ end: {node: range.endContainer, offset: range.endOffset},
389
+ window: win,
390
+ changed: false
391
+ };
392
+
393
+ // We want the nodes right at the cursor, not one of their
394
+ // ancestors with a suitable offset. This goes down the DOM tree
395
+ // until a 'leaf' is reached (or is it *up* the DOM tree?).
396
+ function normalize(point){
397
+ while (point.node.nodeType != 3 && !isBR(point.node)) {
398
+ var newNode = point.node.childNodes[point.offset] || point.node.nextSibling;
399
+ point.offset = 0;
400
+ while (!newNode && point.node.parentNode) {
401
+ point.node = point.node.parentNode;
402
+ newNode = point.node.nextSibling;
403
+ }
404
+ point.node = newNode;
405
+ if (!newNode)
406
+ break;
407
+ }
408
+ }
409
+
410
+ normalize(currentSelection.start);
411
+ normalize(currentSelection.end);
412
+ };
413
+
414
+ select.selectMarked = function () {
415
+ var cs = currentSelection;
416
+ if (!(cs && (cs.changed || (webkit && cs.start.node == cs.end.node)))) return;
417
+ var win = cs.window, range = win.document.createRange();
418
+
419
+ function setPoint(point, which) {
420
+ if (point.node) {
421
+ // Some magic to generalize the setting of the start and end
422
+ // of a range.
423
+ if (point.offset == 0)
424
+ range["set" + which + "Before"](point.node);
425
+ else
426
+ range["set" + which](point.node, point.offset);
427
+ }
428
+ else {
429
+ range.setStartAfter(win.document.body.lastChild || win.document.body);
430
+ }
431
+ }
432
+
433
+ setPoint(cs.end, "End");
434
+ setPoint(cs.start, "Start");
435
+ selectRange(range, win);
436
+ };
437
+
438
+ // Helper for selecting a range object.
439
+ function selectRange(range, window) {
440
+ var selection = window.getSelection();
441
+ selection.removeAllRanges();
442
+ selection.addRange(range);
443
+ };
444
+ function selectionRange(window) {
445
+ var selection = window.getSelection();
446
+ if (!selection || selection.rangeCount == 0)
447
+ return false;
448
+ else
449
+ return selection.getRangeAt(0);
450
+ }
451
+
452
+ // Finding the top-level node at the cursor in the W3C is, as you
453
+ // can see, quite an involved process.
454
+ select.selectionTopNode = function(container, start) {
455
+ var range = selectionRange(container.ownerDocument.defaultView);
456
+ if (!range) return false;
457
+
458
+ var node = start ? range.startContainer : range.endContainer;
459
+ var offset = start ? range.startOffset : range.endOffset;
460
+ // Work around (yet another) bug in Opera's selection model.
461
+ if (window.opera && !start && range.endContainer == container && range.endOffset == range.startOffset + 1 &&
462
+ container.childNodes[range.startOffset] && isBR(container.childNodes[range.startOffset]))
463
+ offset--;
464
+
465
+ // For text nodes, we look at the node itself if the cursor is
466
+ // inside, or at the node before it if the cursor is at the
467
+ // start.
468
+ if (node.nodeType == 3){
469
+ if (offset > 0)
470
+ return topLevelNodeAt(node, container);
471
+ else
472
+ return topLevelNodeBefore(node, container);
473
+ }
474
+ // Occasionally, browsers will return the HTML node as
475
+ // selection. If the offset is 0, we take the start of the frame
476
+ // ('after null'), otherwise, we take the last node.
477
+ else if (node.nodeName.toUpperCase() == "HTML") {
478
+ return (offset == 1 ? null : container.lastChild);
479
+ }
480
+ // If the given node is our 'container', we just look up the
481
+ // correct node by using the offset.
482
+ else if (node == container) {
483
+ return (offset == 0) ? null : node.childNodes[offset - 1];
484
+ }
485
+ // In any other case, we have a regular node. If the cursor is
486
+ // at the end of the node, we use the node itself, if it is at
487
+ // the start, we use the node before it, and in any other
488
+ // case, we look up the child before the cursor and use that.
489
+ else {
490
+ if (offset == node.childNodes.length)
491
+ return topLevelNodeAt(node, container);
492
+ else if (offset == 0)
493
+ return topLevelNodeBefore(node, container);
494
+ else
495
+ return topLevelNodeAt(node.childNodes[offset - 1], container);
496
+ }
497
+ };
498
+
499
+ select.focusAfterNode = function(node, container) {
500
+ var win = container.ownerDocument.defaultView,
501
+ range = win.document.createRange();
502
+ range.setStartBefore(container.firstChild || container);
503
+ // In Opera, setting the end of a range at the end of a line
504
+ // (before a BR) will cause the cursor to appear on the next
505
+ // line, so we set the end inside of the start node when
506
+ // possible.
507
+ if (node && !node.firstChild)
508
+ range.setEndAfter(node);
509
+ else if (node)
510
+ range.setEnd(node, node.childNodes.length);
511
+ else
512
+ range.setEndBefore(container.firstChild || container);
513
+ range.collapse(false);
514
+ selectRange(range, win);
515
+ };
516
+
517
+ select.somethingSelected = function(win) {
518
+ var range = selectionRange(win);
519
+ return range && !range.collapsed;
520
+ };
521
+
522
+ function insertNodeAtCursor(window, node) {
523
+ var range = selectionRange(window);
524
+ if (!range) return;
525
+
526
+ range.deleteContents();
527
+ range.insertNode(node);
528
+ webkitLastLineHack(window.document.body);
529
+ range = window.document.createRange();
530
+ range.selectNode(node);
531
+ range.collapse(false);
532
+ selectRange(range, window);
533
+ }
534
+
535
+ select.insertNewlineAtCursor = function(window) {
536
+ insertNodeAtCursor(window, window.document.createElement("BR"));
537
+ };
538
+
539
+ select.insertTabAtCursor = function(window) {
540
+ insertNodeAtCursor(window, window.document.createTextNode(fourSpaces));
541
+ };
542
+
543
+ select.cursorPos = function(container, start) {
544
+ var range = selectionRange(window);
545
+ if (!range) return;
546
+
547
+ var topNode = select.selectionTopNode(container, start);
548
+ while (topNode && !isBR(topNode))
549
+ topNode = topNode.previousSibling;
550
+
551
+ range = range.cloneRange();
552
+ range.collapse(start);
553
+ if (topNode)
554
+ range.setStartAfter(topNode);
555
+ else
556
+ range.setStartBefore(container);
557
+ return {node: topNode, offset: range.toString().length};
558
+ };
559
+
560
+ select.setCursorPos = function(container, from, to) {
561
+ var win = container.ownerDocument.defaultView,
562
+ range = win.document.createRange();
563
+
564
+ function setPoint(node, offset, side) {
565
+ if (!node)
566
+ node = container.firstChild;
567
+ else
568
+ node = node.nextSibling;
569
+
570
+ if (!node)
571
+ return;
572
+
573
+ if (offset == 0) {
574
+ range["set" + side + "Before"](node);
575
+ return true;
576
+ }
577
+
578
+ var backlog = []
579
+ function decompose(node) {
580
+ if (node.nodeType == 3)
581
+ backlog.push(node);
582
+ else
583
+ forEach(node.childNodes, decompose);
584
+ }
585
+ while (true) {
586
+ while (node && !backlog.length) {
587
+ decompose(node);
588
+ node = node.nextSibling;
589
+ }
590
+ var cur = backlog.shift();
591
+ if (!cur) return false;
592
+
593
+ var length = cur.nodeValue.length;
594
+ if (length >= offset) {
595
+ range["set" + side](cur, offset);
596
+ return true;
597
+ }
598
+ offset -= length;
599
+ }
600
+ }
601
+
602
+ to = to || from;
603
+ if (setPoint(to.node, to.offset, "End") && setPoint(from.node, from.offset, "Start"))
604
+ selectRange(range, win);
605
+ };
606
+ }
607
+ })();