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.
- data/Manifest.txt +31 -0
- data/lib/w2tags/parser.rb +2 -5
- data/lib/w2tags/try/public/css/csscolors.css +47 -0
- data/lib/w2tags/try/public/css/jquery.cluetip.css +130 -0
- data/lib/w2tags/try/public/css/jscolors.css +55 -0
- data/lib/w2tags/try/public/css/rubycolors.css +82 -0
- data/lib/w2tags/try/public/css/xmlcolors.css +51 -0
- data/lib/w2tags/try/public/img/loading.gif +0 -0
- data/lib/w2tags/try/public/js/codemirror.js +308 -0
- data/lib/w2tags/try/public/js/editor.js +1340 -0
- data/lib/w2tags/try/public/js/highlight.js +68 -0
- data/lib/w2tags/try/public/js/jquery.cluetip.js +470 -0
- data/lib/w2tags/try/public/js/jquery.js +4376 -0
- data/lib/w2tags/try/public/js/mirrorframe.js +81 -0
- data/lib/w2tags/try/public/js/parsecss.js +155 -0
- data/lib/w2tags/try/public/js/parsehtmlmixed.js +74 -0
- data/lib/w2tags/try/public/js/parsejavascript.js +350 -0
- data/lib/w2tags/try/public/js/parsew2tags.js +157 -0
- data/lib/w2tags/try/public/js/parsexml.js +292 -0
- data/lib/w2tags/try/public/js/select.js +607 -0
- data/lib/w2tags/try/public/js/stringstream.js +140 -0
- data/lib/w2tags/try/public/js/tokenize.js +57 -0
- data/lib/w2tags/try/public/js/tokenizejavascript.js +175 -0
- data/lib/w2tags/try/public/js/undo.js +404 -0
- data/lib/w2tags/try/public/js/util.js +134 -0
- data/lib/w2tags/try/public/w2/basic.w2erb +8 -2
- data/lib/w2tags/try/public/w2/erb_base.hot.html +167 -0
- data/lib/w2tags/try/public/w2/erb_rails.hot.html +59 -0
- data/lib/w2tags/try/public/w2/html.hot.html +1 -0
- data/lib/w2tags/try/public/w2/rails.hot.html +37 -0
- data/lib/w2tags/try/public/w2/try.rb.hot.html +50 -0
- data/lib/w2tags/try/try.rb +3 -2
- data/lib/w2tags/try/views/index.erb +85 -15
- data/lib/w2tags/try/views/layout.erb +4 -5
- data/lib/w2tags/try/views/parse.erb +1 -0
- data/tasks/setup.rb +1 -1
- metadata +58 -2
@@ -0,0 +1,140 @@
|
|
1
|
+
/* String streams are the things fed to parsers (which can feed them
|
2
|
+
* to a tokenizer if they want). They provide peek and next methods
|
3
|
+
* for looking at the current character (next 'consumes' this
|
4
|
+
* character, peek does not), and a get method for retrieving all the
|
5
|
+
* text that was consumed since the last time get was called.
|
6
|
+
*
|
7
|
+
* An easy mistake to make is to let a StopIteration exception finish
|
8
|
+
* the token stream while there are still characters pending in the
|
9
|
+
* string stream (hitting the end of the buffer while parsing a
|
10
|
+
* token). To make it easier to detect such errors, the stringstreams
|
11
|
+
* throw an exception when this happens.
|
12
|
+
*/
|
13
|
+
|
14
|
+
// Make a stringstream stream out of an iterator that returns strings.
|
15
|
+
// This is applied to the result of traverseDOM (see codemirror.js),
|
16
|
+
// and the resulting stream is fed to the parser.
|
17
|
+
window.stringStream = function(source){
|
18
|
+
// String that's currently being iterated over.
|
19
|
+
var current = "";
|
20
|
+
// Position in that string.
|
21
|
+
var pos = 0;
|
22
|
+
// Accumulator for strings that have been iterated over but not
|
23
|
+
// get()-ed yet.
|
24
|
+
var accum = "";
|
25
|
+
// Make sure there are more characters ready, or throw
|
26
|
+
// StopIteration.
|
27
|
+
function ensureChars() {
|
28
|
+
while (pos == current.length) {
|
29
|
+
accum += current;
|
30
|
+
current = ""; // In case source.next() throws
|
31
|
+
pos = 0;
|
32
|
+
try {current = source.next();}
|
33
|
+
catch (e) {
|
34
|
+
if (e != StopIteration) throw e;
|
35
|
+
else return false;
|
36
|
+
}
|
37
|
+
}
|
38
|
+
return true;
|
39
|
+
}
|
40
|
+
|
41
|
+
return {
|
42
|
+
// Return the next character in the stream.
|
43
|
+
peek: function() {
|
44
|
+
if (!ensureChars()) return null;
|
45
|
+
return current.charAt(pos);
|
46
|
+
},
|
47
|
+
// Get the next character, throw StopIteration if at end, check
|
48
|
+
// for unused content.
|
49
|
+
next: function() {
|
50
|
+
if (!ensureChars()) {
|
51
|
+
if (accum.length > 0)
|
52
|
+
throw "End of stringstream reached without emptying buffer ('" + accum + "').";
|
53
|
+
else
|
54
|
+
throw StopIteration;
|
55
|
+
}
|
56
|
+
return current.charAt(pos++);
|
57
|
+
},
|
58
|
+
// Return the characters iterated over since the last call to
|
59
|
+
// .get().
|
60
|
+
get: function() {
|
61
|
+
var temp = accum;
|
62
|
+
accum = "";
|
63
|
+
if (pos > 0){
|
64
|
+
temp += current.slice(0, pos);
|
65
|
+
current = current.slice(pos);
|
66
|
+
pos = 0;
|
67
|
+
}
|
68
|
+
return temp;
|
69
|
+
},
|
70
|
+
// Push a string back into the stream.
|
71
|
+
push: function(str) {
|
72
|
+
current = current.slice(0, pos) + str + current.slice(pos);
|
73
|
+
},
|
74
|
+
lookAhead: function(str, consume, skipSpaces, caseInsensitive) {
|
75
|
+
function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}
|
76
|
+
str = cased(str);
|
77
|
+
var found = false;
|
78
|
+
|
79
|
+
var _accum = accum, _pos = pos;
|
80
|
+
if (skipSpaces) this.nextWhileMatches(/[\s\u00a0]/);
|
81
|
+
|
82
|
+
while (true) {
|
83
|
+
var end = pos + str.length, left = current.length - pos;
|
84
|
+
if (end <= current.length) {
|
85
|
+
found = str == cased(current.slice(pos, end));
|
86
|
+
pos = end;
|
87
|
+
break;
|
88
|
+
}
|
89
|
+
else if (str.slice(0, left) == cased(current.slice(pos))) {
|
90
|
+
accum += current; current = "";
|
91
|
+
try {current = source.next();}
|
92
|
+
catch (e) {break;}
|
93
|
+
pos = 0;
|
94
|
+
str = str.slice(left);
|
95
|
+
}
|
96
|
+
else {
|
97
|
+
break;
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
if (!(found && consume)) {
|
102
|
+
current = accum.slice(_accum.length) + current;
|
103
|
+
pos = _pos;
|
104
|
+
accum = _accum;
|
105
|
+
}
|
106
|
+
|
107
|
+
return found;
|
108
|
+
},
|
109
|
+
|
110
|
+
// Utils built on top of the above
|
111
|
+
more: function() {
|
112
|
+
return this.peek() !== null;
|
113
|
+
},
|
114
|
+
applies: function(test) {
|
115
|
+
var next = this.peek();
|
116
|
+
return (next !== null && test(next));
|
117
|
+
},
|
118
|
+
nextWhile: function(test) {
|
119
|
+
var next;
|
120
|
+
while ((next = this.peek()) !== null && test(next))
|
121
|
+
this.next();
|
122
|
+
},
|
123
|
+
matches: function(re) {
|
124
|
+
var next = this.peek();
|
125
|
+
return (next !== null && re.test(next));
|
126
|
+
},
|
127
|
+
nextWhileMatches: function(re) {
|
128
|
+
var next;
|
129
|
+
while ((next = this.peek()) !== null && re.test(next))
|
130
|
+
this.next();
|
131
|
+
},
|
132
|
+
equals: function(ch) {
|
133
|
+
return ch === this.peek();
|
134
|
+
},
|
135
|
+
endOfLine: function() {
|
136
|
+
var next = this.peek();
|
137
|
+
return next == null || next == "\n";
|
138
|
+
}
|
139
|
+
};
|
140
|
+
};
|
@@ -0,0 +1,57 @@
|
|
1
|
+
// A framework for simple tokenizers. Takes care of newlines and
|
2
|
+
// white-space, and of getting the text from the source stream into
|
3
|
+
// the token object. A state is a function of two arguments -- a
|
4
|
+
// string stream and a setState function. The second can be used to
|
5
|
+
// change the tokenizer's state, and can be ignored for stateless
|
6
|
+
// tokenizers. This function should advance the stream over a token
|
7
|
+
// and return a string or object containing information about the next
|
8
|
+
// token, or null to pass and have the (new) state be called to finish
|
9
|
+
// the token. When a string is given, it is wrapped in a {style, type}
|
10
|
+
// object. In the resulting object, the characters consumed are stored
|
11
|
+
// under the content property. Any whitespace following them is also
|
12
|
+
// automatically consumed, and added to the value property. (Thus,
|
13
|
+
// content is the actual meaningful part of the token, while value
|
14
|
+
// contains all the text it spans.)
|
15
|
+
|
16
|
+
function tokenizer(source, state) {
|
17
|
+
// Newlines are always a separate token.
|
18
|
+
function isWhiteSpace(ch) {
|
19
|
+
// The messy regexp is because IE's regexp matcher is of the
|
20
|
+
// opinion that non-breaking spaces are no whitespace.
|
21
|
+
return ch != "\n" && /^[\s\u00a0]*$/.test(ch);
|
22
|
+
}
|
23
|
+
|
24
|
+
var tokenizer = {
|
25
|
+
state: state,
|
26
|
+
|
27
|
+
take: function(type) {
|
28
|
+
if (typeof(type) == "string")
|
29
|
+
type = {style: type, type: type};
|
30
|
+
|
31
|
+
type.content = (type.content || "") + source.get();
|
32
|
+
if (!/\n$/.test(type.content))
|
33
|
+
source.nextWhile(isWhiteSpace);
|
34
|
+
type.value = type.content + source.get();
|
35
|
+
return type;
|
36
|
+
},
|
37
|
+
|
38
|
+
next: function () {
|
39
|
+
if (!source.more()) throw StopIteration;
|
40
|
+
|
41
|
+
var type;
|
42
|
+
if (source.equals("\n")) {
|
43
|
+
source.next();
|
44
|
+
return this.take("whitespace");
|
45
|
+
}
|
46
|
+
|
47
|
+
if (source.applies(isWhiteSpace))
|
48
|
+
type = "whitespace";
|
49
|
+
else
|
50
|
+
while (!type)
|
51
|
+
type = this.state(source, function(s) {tokenizer.state = s;});
|
52
|
+
|
53
|
+
return this.take(type);
|
54
|
+
}
|
55
|
+
};
|
56
|
+
return tokenizer;
|
57
|
+
}
|
@@ -0,0 +1,175 @@
|
|
1
|
+
/* Tokenizer for JavaScript code */
|
2
|
+
|
3
|
+
var tokenizeJavaScript = (function() {
|
4
|
+
// Advance the stream until the given character (not preceded by a
|
5
|
+
// backslash) is encountered, or the end of the line is reached.
|
6
|
+
function nextUntilUnescaped(source, end) {
|
7
|
+
var escaped = false;
|
8
|
+
var next;
|
9
|
+
while (!source.endOfLine()) {
|
10
|
+
var next = source.next();
|
11
|
+
if (next == end && !escaped)
|
12
|
+
return false;
|
13
|
+
escaped = !escaped && next == "\\";
|
14
|
+
}
|
15
|
+
return escaped;
|
16
|
+
}
|
17
|
+
|
18
|
+
// A map of JavaScript's keywords. The a/b/c keyword distinction is
|
19
|
+
// very rough, but it gives the parser enough information to parse
|
20
|
+
// correct code correctly (we don't care that much how we parse
|
21
|
+
// incorrect code). The style information included in these objects
|
22
|
+
// is used by the highlighter to pick the correct CSS style for a
|
23
|
+
// token.
|
24
|
+
var keywords = function(){
|
25
|
+
function result(type, style){
|
26
|
+
return {type: type, style: "js-" + style};
|
27
|
+
}
|
28
|
+
// keywords that take a parenthised expression, and then a
|
29
|
+
// statement (if)
|
30
|
+
var keywordA = result("keyword a", "keyword");
|
31
|
+
// keywords that take just a statement (else)
|
32
|
+
var keywordB = result("keyword b", "keyword");
|
33
|
+
// keywords that optionally take an expression, and form a
|
34
|
+
// statement (return)
|
35
|
+
var keywordC = result("keyword c", "keyword");
|
36
|
+
var operator = result("operator", "keyword");
|
37
|
+
var atom = result("atom", "atom");
|
38
|
+
return {
|
39
|
+
"if": keywordA, "while": keywordA, "with": keywordA,
|
40
|
+
"else": keywordB, "do": keywordB, "try": keywordB, "finally": keywordB,
|
41
|
+
"return": keywordC, "break": keywordC, "continue": keywordC, "new": keywordC, "delete": keywordC, "throw": keywordC,
|
42
|
+
"in": operator, "typeof": operator, "instanceof": operator,
|
43
|
+
"var": result("var", "keyword"), "function": result("function", "keyword"), "catch": result("catch", "keyword"),
|
44
|
+
"for": result("for", "keyword"), "switch": result("switch", "keyword"),
|
45
|
+
"case": result("case", "keyword"), "default": result("default", "keyword"),
|
46
|
+
"true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom
|
47
|
+
};
|
48
|
+
}();
|
49
|
+
|
50
|
+
// Some helper regexps
|
51
|
+
var isOperatorChar = /[+\-*&%=<>!?|]/;
|
52
|
+
var isHexDigit = /[0-9A-Fa-f]/;
|
53
|
+
var isWordChar = /[\w\$_]/;
|
54
|
+
|
55
|
+
// Wrapper around jsToken that helps maintain parser state (whether
|
56
|
+
// we are inside of a multi-line comment and whether the next token
|
57
|
+
// could be a regular expression).
|
58
|
+
function jsTokenState(inside, regexp) {
|
59
|
+
return function(source, setState) {
|
60
|
+
var newInside = inside;
|
61
|
+
var type = jsToken(inside, regexp, source, function(c) {newInside = c;});
|
62
|
+
var newRegexp = type.type == "operator" || type.type == "keyword c" || type.type.match(/^[\[{}\(,;:]$/);
|
63
|
+
if (newRegexp != regexp || newInside != inside)
|
64
|
+
setState(jsTokenState(newInside, newRegexp));
|
65
|
+
return type;
|
66
|
+
};
|
67
|
+
}
|
68
|
+
|
69
|
+
// The token reader, inteded to be used by the tokenizer from
|
70
|
+
// tokenize.js (through jsTokenState). Advances the source stream
|
71
|
+
// over a token, and returns an object containing the type and style
|
72
|
+
// of that token.
|
73
|
+
function jsToken(inside, regexp, source, setInside) {
|
74
|
+
function readHexNumber(){
|
75
|
+
source.next(); // skip the 'x'
|
76
|
+
source.nextWhileMatches(isHexDigit);
|
77
|
+
return {type: "number", style: "js-atom"};
|
78
|
+
}
|
79
|
+
|
80
|
+
function readNumber() {
|
81
|
+
source.nextWhileMatches(/[0-9]/);
|
82
|
+
if (source.equals(".")){
|
83
|
+
source.next();
|
84
|
+
source.nextWhileMatches(/[0-9]/);
|
85
|
+
}
|
86
|
+
if (source.equals("e") || source.equals("E")){
|
87
|
+
source.next();
|
88
|
+
if (source.equals("-"))
|
89
|
+
source.next();
|
90
|
+
source.nextWhileMatches(/[0-9]/);
|
91
|
+
}
|
92
|
+
return {type: "number", style: "js-atom"};
|
93
|
+
}
|
94
|
+
// Read a word, look it up in keywords. If not found, it is a
|
95
|
+
// variable, otherwise it is a keyword of the type found.
|
96
|
+
function readWord() {
|
97
|
+
source.nextWhileMatches(isWordChar);
|
98
|
+
var word = source.get();
|
99
|
+
var known = keywords.hasOwnProperty(word) && keywords.propertyIsEnumerable(word) && keywords[word];
|
100
|
+
return known ? {type: known.type, style: known.style, content: word} :
|
101
|
+
{type: "variable", style: "js-variable", content: word};
|
102
|
+
}
|
103
|
+
function readRegexp() {
|
104
|
+
nextUntilUnescaped(source, "/");
|
105
|
+
source.nextWhileMatches(/[gi]/);
|
106
|
+
return {type: "regexp", style: "js-string"};
|
107
|
+
}
|
108
|
+
// Mutli-line comments are tricky. We want to return the newlines
|
109
|
+
// embedded in them as regular newline tokens, and then continue
|
110
|
+
// returning a comment token for every line of the comment. So
|
111
|
+
// some state has to be saved (inside) to indicate whether we are
|
112
|
+
// inside a /* */ sequence.
|
113
|
+
function readMultilineComment(start){
|
114
|
+
var newInside = "/*";
|
115
|
+
var maybeEnd = (start == "*");
|
116
|
+
while (true) {
|
117
|
+
if (source.endOfLine())
|
118
|
+
break;
|
119
|
+
var next = source.next();
|
120
|
+
if (next == "/" && maybeEnd){
|
121
|
+
newInside = null;
|
122
|
+
break;
|
123
|
+
}
|
124
|
+
maybeEnd = (next == "*");
|
125
|
+
}
|
126
|
+
setInside(newInside);
|
127
|
+
return {type: "comment", style: "js-comment"};
|
128
|
+
}
|
129
|
+
function readOperator() {
|
130
|
+
source.nextWhileMatches(isOperatorChar);
|
131
|
+
return {type: "operator", style: "js-operator"};
|
132
|
+
}
|
133
|
+
function readString(quote) {
|
134
|
+
var endBackSlash = nextUntilUnescaped(source, quote);
|
135
|
+
setInside(endBackSlash ? quote : null);
|
136
|
+
return {type: "string", style: "js-string"};
|
137
|
+
}
|
138
|
+
|
139
|
+
// Fetch the next token. Dispatches on first character in the
|
140
|
+
// stream, or first two characters when the first is a slash.
|
141
|
+
if (inside == "\"" || inside == "'")
|
142
|
+
return readString(inside);
|
143
|
+
var ch = source.next();
|
144
|
+
if (inside == "/*")
|
145
|
+
return readMultilineComment(ch);
|
146
|
+
else if (ch == "\"" || ch == "'")
|
147
|
+
return readString(ch);
|
148
|
+
// with punctuation, the type of the token is the symbol itself
|
149
|
+
else if (/[\[\]{}\(\),;\:\.]/.test(ch))
|
150
|
+
return {type: ch, style: "js-punctuation"};
|
151
|
+
else if (ch == "0" && (source.equals("x") || source.equals("X")))
|
152
|
+
return readHexNumber();
|
153
|
+
else if (/[0-9]/.test(ch))
|
154
|
+
return readNumber();
|
155
|
+
else if (ch == "/"){
|
156
|
+
if (source.equals("*"))
|
157
|
+
{ source.next(); return readMultilineComment(ch); }
|
158
|
+
else if (source.equals("/"))
|
159
|
+
{ nextUntilUnescaped(source, null); return {type: "comment", style: "js-comment"};}
|
160
|
+
else if (regexp)
|
161
|
+
return readRegexp();
|
162
|
+
else
|
163
|
+
return readOperator();
|
164
|
+
}
|
165
|
+
else if (isOperatorChar.test(ch))
|
166
|
+
return readOperator();
|
167
|
+
else
|
168
|
+
return readWord();
|
169
|
+
}
|
170
|
+
|
171
|
+
// The external interface to the tokenizer.
|
172
|
+
return function(source, startState) {
|
173
|
+
return tokenizer(source, startState || jsTokenState(false, true));
|
174
|
+
};
|
175
|
+
})();
|
@@ -0,0 +1,404 @@
|
|
1
|
+
/**
|
2
|
+
* Storage and control for undo information within a CodeMirror
|
3
|
+
* editor. 'Why on earth is such a complicated mess required for
|
4
|
+
* that?', I hear you ask. The goal, in implementing this, was to make
|
5
|
+
* the complexity of storing and reverting undo information depend
|
6
|
+
* only on the size of the edited or restored content, not on the size
|
7
|
+
* of the whole document. This makes it necessary to use a kind of
|
8
|
+
* 'diff' system, which, when applied to a DOM tree, causes some
|
9
|
+
* complexity and hackery.
|
10
|
+
*
|
11
|
+
* In short, the editor 'touches' BR elements as it parses them, and
|
12
|
+
* the History stores these. When nothing is touched in commitDelay
|
13
|
+
* milliseconds, the changes are committed: It goes over all touched
|
14
|
+
* nodes, throws out the ones that did not change since last commit or
|
15
|
+
* are no longer in the document, and assembles the rest into zero or
|
16
|
+
* more 'chains' -- arrays of adjacent lines. Links back to these
|
17
|
+
* chains are added to the BR nodes, while the chain that previously
|
18
|
+
* spanned these nodes is added to the undo history. Undoing a change
|
19
|
+
* means taking such a chain off the undo history, restoring its
|
20
|
+
* content (text is saved per line) and linking it back into the
|
21
|
+
* document.
|
22
|
+
*/
|
23
|
+
|
24
|
+
// A history object needs to know about the DOM container holding the
|
25
|
+
// document, the maximum amount of undo levels it should store, the
|
26
|
+
// delay (of no input) after which it commits a set of changes, and,
|
27
|
+
// unfortunately, the 'parent' window -- a window that is not in
|
28
|
+
// designMode, and on which setTimeout works in every browser.
|
29
|
+
function History(container, maxDepth, commitDelay, editor, onChange) {
|
30
|
+
this.container = container;
|
31
|
+
this.maxDepth = maxDepth; this.commitDelay = commitDelay;
|
32
|
+
this.editor = editor; this.parent = editor.parent;
|
33
|
+
this.onChange = onChange;
|
34
|
+
// This line object represents the initial, empty editor.
|
35
|
+
var initial = {text: "", from: null, to: null};
|
36
|
+
// As the borders between lines are represented by BR elements, the
|
37
|
+
// start of the first line and the end of the last one are
|
38
|
+
// represented by null. Since you can not store any properties
|
39
|
+
// (links to line objects) in null, these properties are used in
|
40
|
+
// those cases.
|
41
|
+
this.first = initial; this.last = initial;
|
42
|
+
// Similarly, a 'historyTouched' property is added to the BR in
|
43
|
+
// front of lines that have already been touched, and 'firstTouched'
|
44
|
+
// is used for the first line.
|
45
|
+
this.firstTouched = false;
|
46
|
+
// History is the set of committed changes, touched is the set of
|
47
|
+
// nodes touched since the last commit.
|
48
|
+
this.history = []; this.redoHistory = []; this.touched = [];
|
49
|
+
}
|
50
|
+
|
51
|
+
History.prototype = {
|
52
|
+
// Schedule a commit (if no other touches come in for commitDelay
|
53
|
+
// milliseconds).
|
54
|
+
scheduleCommit: function() {
|
55
|
+
var self = this;
|
56
|
+
this.parent.clearTimeout(this.commitTimeout);
|
57
|
+
this.commitTimeout = this.parent.setTimeout(function(){self.tryCommit();}, this.commitDelay);
|
58
|
+
},
|
59
|
+
|
60
|
+
// Mark a node as touched. Null is a valid argument.
|
61
|
+
touch: function(node) {
|
62
|
+
this.setTouched(node);
|
63
|
+
this.scheduleCommit();
|
64
|
+
},
|
65
|
+
|
66
|
+
// Undo the last change.
|
67
|
+
undo: function() {
|
68
|
+
// Make sure pending changes have been committed.
|
69
|
+
this.commit();
|
70
|
+
|
71
|
+
if (this.history.length) {
|
72
|
+
// Take the top diff from the history, apply it, and store its
|
73
|
+
// shadow in the redo history.
|
74
|
+
var item = this.history.pop();
|
75
|
+
this.redoHistory.push(this.updateTo(item, "applyChain"));
|
76
|
+
if (this.onChange) this.onChange();
|
77
|
+
return this.chainNode(item);
|
78
|
+
}
|
79
|
+
},
|
80
|
+
|
81
|
+
// Redo the last undone change.
|
82
|
+
redo: function() {
|
83
|
+
this.commit();
|
84
|
+
if (this.redoHistory.length) {
|
85
|
+
// The inverse of undo, basically.
|
86
|
+
var item = this.redoHistory.pop();
|
87
|
+
this.addUndoLevel(this.updateTo(item, "applyChain"));
|
88
|
+
if (this.onChange) this.onChange();
|
89
|
+
return this.chainNode(item);
|
90
|
+
}
|
91
|
+
},
|
92
|
+
|
93
|
+
clear: function() {
|
94
|
+
this.history = [];
|
95
|
+
this.redoHistory = [];
|
96
|
+
},
|
97
|
+
|
98
|
+
// Ask for the size of the un/redo histories.
|
99
|
+
historySize: function() {
|
100
|
+
return {undo: this.history.length, redo: this.redoHistory.length};
|
101
|
+
},
|
102
|
+
|
103
|
+
// Push a changeset into the document.
|
104
|
+
push: function(from, to, lines) {
|
105
|
+
var chain = [];
|
106
|
+
for (var i = 0; i < lines.length; i++) {
|
107
|
+
var end = (i == lines.length - 1) ? to : this.container.ownerDocument.createElement("BR");
|
108
|
+
chain.push({from: from, to: end, text: cleanText(lines[i])});
|
109
|
+
from = end;
|
110
|
+
}
|
111
|
+
this.pushChains([chain], from == null && to == null);
|
112
|
+
if (this.onChange) this.onChange();
|
113
|
+
},
|
114
|
+
|
115
|
+
pushChains: function(chains, doNotHighlight) {
|
116
|
+
this.commit(doNotHighlight);
|
117
|
+
this.addUndoLevel(this.updateTo(chains, "applyChain"));
|
118
|
+
this.redoHistory = [];
|
119
|
+
},
|
120
|
+
|
121
|
+
// Retrieve a DOM node from a chain (for scrolling to it after undo/redo).
|
122
|
+
chainNode: function(chains) {
|
123
|
+
for (var i = 0; i < chains.length; i++) {
|
124
|
+
var start = chains[i][0], node = start && (start.from || start.to);
|
125
|
+
if (node) return node;
|
126
|
+
}
|
127
|
+
},
|
128
|
+
|
129
|
+
// Clear the undo history, make the current document the start
|
130
|
+
// position.
|
131
|
+
reset: function() {
|
132
|
+
this.history = []; this.redoHistory = [];
|
133
|
+
},
|
134
|
+
|
135
|
+
textAfter: function(br) {
|
136
|
+
return this.after(br).text;
|
137
|
+
},
|
138
|
+
|
139
|
+
nodeAfter: function(br) {
|
140
|
+
return this.after(br).to;
|
141
|
+
},
|
142
|
+
|
143
|
+
nodeBefore: function(br) {
|
144
|
+
return this.before(br).from;
|
145
|
+
},
|
146
|
+
|
147
|
+
// Commit unless there are pending dirty nodes.
|
148
|
+
tryCommit: function() {
|
149
|
+
if (!window.History) return; // Stop when frame has been unloaded
|
150
|
+
if (this.editor.highlightDirty()) this.commit(true);
|
151
|
+
else this.scheduleCommit();
|
152
|
+
},
|
153
|
+
|
154
|
+
// Check whether the touched nodes hold any changes, if so, commit
|
155
|
+
// them.
|
156
|
+
commit: function(doNotHighlight) {
|
157
|
+
this.parent.clearTimeout(this.commitTimeout);
|
158
|
+
// Make sure there are no pending dirty nodes.
|
159
|
+
if (!doNotHighlight) this.editor.highlightDirty(true);
|
160
|
+
// Build set of chains.
|
161
|
+
var chains = this.touchedChains(), self = this;
|
162
|
+
|
163
|
+
if (chains.length) {
|
164
|
+
this.addUndoLevel(this.updateTo(chains, "linkChain"));
|
165
|
+
this.redoHistory = [];
|
166
|
+
if (this.onChange) this.onChange();
|
167
|
+
}
|
168
|
+
},
|
169
|
+
|
170
|
+
// [ end of public interface ]
|
171
|
+
|
172
|
+
// Update the document with a given set of chains, return its
|
173
|
+
// shadow. updateFunc should be "applyChain" or "linkChain". In the
|
174
|
+
// second case, the chains are taken to correspond the the current
|
175
|
+
// document, and only the state of the line data is updated. In the
|
176
|
+
// first case, the content of the chains is also pushed iinto the
|
177
|
+
// document.
|
178
|
+
updateTo: function(chains, updateFunc) {
|
179
|
+
var shadows = [], dirty = [];
|
180
|
+
for (var i = 0; i < chains.length; i++) {
|
181
|
+
shadows.push(this.shadowChain(chains[i]));
|
182
|
+
dirty.push(this[updateFunc](chains[i]));
|
183
|
+
}
|
184
|
+
if (updateFunc == "applyChain")
|
185
|
+
this.notifyDirty(dirty);
|
186
|
+
return shadows;
|
187
|
+
},
|
188
|
+
|
189
|
+
// Notify the editor that some nodes have changed.
|
190
|
+
notifyDirty: function(nodes) {
|
191
|
+
forEach(nodes, method(this.editor, "addDirtyNode"))
|
192
|
+
this.editor.scheduleHighlight();
|
193
|
+
},
|
194
|
+
|
195
|
+
// Link a chain into the DOM nodes (or the first/last links for null
|
196
|
+
// nodes).
|
197
|
+
linkChain: function(chain) {
|
198
|
+
for (var i = 0; i < chain.length; i++) {
|
199
|
+
var line = chain[i];
|
200
|
+
if (line.from) line.from.historyAfter = line;
|
201
|
+
else this.first = line;
|
202
|
+
if (line.to) line.to.historyBefore = line;
|
203
|
+
else this.last = line;
|
204
|
+
}
|
205
|
+
},
|
206
|
+
|
207
|
+
// Get the line object after/before a given node.
|
208
|
+
after: function(node) {
|
209
|
+
return node ? node.historyAfter : this.first;
|
210
|
+
},
|
211
|
+
before: function(node) {
|
212
|
+
return node ? node.historyBefore : this.last;
|
213
|
+
},
|
214
|
+
|
215
|
+
// Mark a node as touched if it has not already been marked.
|
216
|
+
setTouched: function(node) {
|
217
|
+
if (node) {
|
218
|
+
if (!node.historyTouched) {
|
219
|
+
this.touched.push(node);
|
220
|
+
node.historyTouched = true;
|
221
|
+
}
|
222
|
+
}
|
223
|
+
else {
|
224
|
+
this.firstTouched = true;
|
225
|
+
}
|
226
|
+
},
|
227
|
+
|
228
|
+
// Store a new set of undo info, throw away info if there is more of
|
229
|
+
// it than allowed.
|
230
|
+
addUndoLevel: function(diffs) {
|
231
|
+
this.history.push(diffs);
|
232
|
+
if (this.history.length > this.maxDepth)
|
233
|
+
this.history.shift();
|
234
|
+
},
|
235
|
+
|
236
|
+
// Build chains from a set of touched nodes.
|
237
|
+
touchedChains: function() {
|
238
|
+
var self = this;
|
239
|
+
|
240
|
+
// The temp system is a crummy hack to speed up determining
|
241
|
+
// whether a (currently touched) node has a line object associated
|
242
|
+
// with it. nullTemp is used to store the object for the first
|
243
|
+
// line, other nodes get it stored in their historyTemp property.
|
244
|
+
var nullTemp = null;
|
245
|
+
function temp(node) {return node ? node.historyTemp : nullTemp;}
|
246
|
+
function setTemp(node, line) {
|
247
|
+
if (node) node.historyTemp = line;
|
248
|
+
else nullTemp = line;
|
249
|
+
}
|
250
|
+
|
251
|
+
function buildLine(node) {
|
252
|
+
var text = [];
|
253
|
+
for (var cur = node ? node.nextSibling : self.container.firstChild;
|
254
|
+
cur && !isBR(cur); cur = cur.nextSibling)
|
255
|
+
if (cur.currentText) text.push(cur.currentText);
|
256
|
+
return {from: node, to: cur, text: cleanText(text.join(""))};
|
257
|
+
}
|
258
|
+
|
259
|
+
// Filter out unchanged lines and nodes that are no longer in the
|
260
|
+
// document. Build up line objects for remaining nodes.
|
261
|
+
var lines = [];
|
262
|
+
if (self.firstTouched) self.touched.push(null);
|
263
|
+
forEach(self.touched, function(node) {
|
264
|
+
if (node && node.parentNode != self.container) return;
|
265
|
+
|
266
|
+
if (node) node.historyTouched = false;
|
267
|
+
else self.firstTouched = false;
|
268
|
+
|
269
|
+
var line = buildLine(node), shadow = self.after(node);
|
270
|
+
if (!shadow || shadow.text != line.text || shadow.to != line.to) {
|
271
|
+
lines.push(line);
|
272
|
+
setTemp(node, line);
|
273
|
+
}
|
274
|
+
});
|
275
|
+
|
276
|
+
// Get the BR element after/before the given node.
|
277
|
+
function nextBR(node, dir) {
|
278
|
+
var link = dir + "Sibling", search = node[link];
|
279
|
+
while (search && !isBR(search))
|
280
|
+
search = search[link];
|
281
|
+
return search;
|
282
|
+
}
|
283
|
+
|
284
|
+
// Assemble line objects into chains by scanning the DOM tree
|
285
|
+
// around them.
|
286
|
+
var chains = []; self.touched = [];
|
287
|
+
forEach(lines, function(line) {
|
288
|
+
// Note that this makes the loop skip line objects that have
|
289
|
+
// been pulled into chains by lines before them.
|
290
|
+
if (!temp(line.from)) return;
|
291
|
+
|
292
|
+
var chain = [], curNode = line.from, safe = true;
|
293
|
+
// Put any line objects (referred to by temp info) before this
|
294
|
+
// one on the front of the array.
|
295
|
+
while (true) {
|
296
|
+
var curLine = temp(curNode);
|
297
|
+
if (!curLine) {
|
298
|
+
if (safe) break;
|
299
|
+
else curLine = buildLine(curNode);
|
300
|
+
}
|
301
|
+
chain.unshift(curLine);
|
302
|
+
setTemp(curNode, null);
|
303
|
+
if (!curNode) break;
|
304
|
+
safe = self.after(curNode);
|
305
|
+
curNode = nextBR(curNode, "previous");
|
306
|
+
}
|
307
|
+
curNode = line.to; safe = self.before(line.from);
|
308
|
+
// Add lines after this one at end of array.
|
309
|
+
while (true) {
|
310
|
+
if (!curNode) break;
|
311
|
+
var curLine = temp(curNode);
|
312
|
+
if (!curLine) {
|
313
|
+
if (safe) break;
|
314
|
+
else curLine = buildLine(curNode);
|
315
|
+
}
|
316
|
+
chain.push(curLine);
|
317
|
+
setTemp(curNode, null);
|
318
|
+
safe = self.before(curNode);
|
319
|
+
curNode = nextBR(curNode, "next");
|
320
|
+
}
|
321
|
+
chains.push(chain);
|
322
|
+
});
|
323
|
+
|
324
|
+
return chains;
|
325
|
+
},
|
326
|
+
|
327
|
+
// Find the 'shadow' of a given chain by following the links in the
|
328
|
+
// DOM nodes at its start and end.
|
329
|
+
shadowChain: function(chain) {
|
330
|
+
var shadows = [], next = this.after(chain[0].from), end = chain[chain.length - 1].to;
|
331
|
+
while (true) {
|
332
|
+
shadows.push(next);
|
333
|
+
var nextNode = next.to;
|
334
|
+
if (!nextNode || nextNode == end)
|
335
|
+
break;
|
336
|
+
else
|
337
|
+
next = nextNode.historyAfter || this.before(end);
|
338
|
+
// (The this.before(end) is a hack -- FF sometimes removes
|
339
|
+
// properties from BR nodes, in which case the best we can hope
|
340
|
+
// for is to not break.)
|
341
|
+
}
|
342
|
+
return shadows;
|
343
|
+
},
|
344
|
+
|
345
|
+
// Update the DOM tree to contain the lines specified in a given
|
346
|
+
// chain, link this chain into the DOM nodes.
|
347
|
+
applyChain: function(chain) {
|
348
|
+
// Some attempt is made to prevent the cursor from jumping
|
349
|
+
// randomly when an undo or redo happens. It still behaves a bit
|
350
|
+
// strange sometimes.
|
351
|
+
var cursor = select.cursorPos(this.container, false), self = this;
|
352
|
+
|
353
|
+
// Remove all nodes in the DOM tree between from and to (null for
|
354
|
+
// start/end of container).
|
355
|
+
function removeRange(from, to) {
|
356
|
+
var pos = from ? from.nextSibling : self.container.firstChild;
|
357
|
+
while (pos != to) {
|
358
|
+
var temp = pos.nextSibling;
|
359
|
+
removeElement(pos);
|
360
|
+
pos = temp;
|
361
|
+
}
|
362
|
+
}
|
363
|
+
|
364
|
+
var start = chain[0].from, end = chain[chain.length - 1].to;
|
365
|
+
// Clear the space where this change has to be made.
|
366
|
+
removeRange(start, end);
|
367
|
+
|
368
|
+
// Insert the content specified by the chain into the DOM tree.
|
369
|
+
for (var i = 0; i < chain.length; i++) {
|
370
|
+
var line = chain[i];
|
371
|
+
// The start and end of the space are already correct, but BR
|
372
|
+
// tags inside it have to be put back.
|
373
|
+
if (i > 0)
|
374
|
+
self.container.insertBefore(line.from, end);
|
375
|
+
|
376
|
+
// Add the text.
|
377
|
+
var node = makePartSpan(fixSpaces(line.text), this.container.ownerDocument);
|
378
|
+
self.container.insertBefore(node, end);
|
379
|
+
// See if the cursor was on this line. Put it back, adjusting
|
380
|
+
// for changed line length, if it was.
|
381
|
+
if (cursor && cursor.node == line.from) {
|
382
|
+
var cursordiff = 0;
|
383
|
+
var prev = this.after(line.from);
|
384
|
+
if (prev && i == chain.length - 1) {
|
385
|
+
// Only adjust if the cursor is after the unchanged part of
|
386
|
+
// the line.
|
387
|
+
for (var match = 0; match < cursor.offset &&
|
388
|
+
line.text.charAt(match) == prev.text.charAt(match); match++);
|
389
|
+
if (cursor.offset > match)
|
390
|
+
cursordiff = line.text.length - prev.text.length;
|
391
|
+
}
|
392
|
+
select.setCursorPos(this.container, {node: line.from, offset: Math.max(0, cursor.offset + cursordiff)});
|
393
|
+
}
|
394
|
+
// Cursor was in removed line, this is last new line.
|
395
|
+
else if (cursor && (i == chain.length - 1) && cursor.node && cursor.node.parentNode != this.container) {
|
396
|
+
select.setCursorPos(this.container, {node: line.from, offset: line.text.length});
|
397
|
+
}
|
398
|
+
}
|
399
|
+
|
400
|
+
// Anchor the chain in the DOM tree.
|
401
|
+
this.linkChain(chain);
|
402
|
+
return start;
|
403
|
+
}
|
404
|
+
};
|