handleBracketLinks(matchText);
continue;
case 20: // [=#anchor label]
handleWikiAnchor(matchText);
continue;
case 1001: // escaping double escape
break;
case -1: // citation
if (escapeNewlines && inParagraph) {
holder.appendChild(contentDocument.createElement("br"));
}
handleCitation(matchText);
if (escapeNewlines) {
openParagraph();
}
continue;
case -2: // header
currentHeader = handleHeader(matchText);
if (currentHeader) {
line = line.replace(/(?:[ \t\r\f\v]+#[^ \t\r\f\v]+)?[ \t\r\f\v]*$/, "");
var m = /^\s*(=+)[ \t\r\f\v]+/.exec(line);
if (line.slice(-m[1].length) == m[1]) {
line = line.slice(0, -m[1].length).replace(/[ \t\r\f\v]+$/, "");
}
wikiRulesPattern.lastIndex = prevIndex = m[0].length;
continue;
}
break;
case -3: // list
handleList(matchText)
continue;
case -4: // definition
handleDefinition(matchText);
continue;
case -5: // leading space
if (listDepth.length == 0 && !inDefList) {
handleIndent(matchText);
continue;
}
if (!this.isInlineNode(holder.lastChild)) {
continue;
}
matchText = matchText.replace(/^\s+/, " ");
break;
case -6: // closing table row
if (inTable) {
if (matchText.slice(-1) != "\\") {
handleTableCell(-1);
}
else {
continueTableRow = true;
}
continue;
}
break;
case -7: // cell
if (quoteDepth.length > 0 && match.index == 0) {
closeToFragment();
}
var align = null;
for ( ; ; ) { // lookahead next double pipes
var m = wikiRulesPattern.exec(line);
switch (m ? getMatchNumber(m) : 0) {
case 0: case -6: case -7:
var end = m ? m.index : line.length;
if (prevIndex < end) {
var tmp = line.substring(prevIndex, end);
var m = /^([ \t\r\n\f\v]*)(.*?)([ \t\r\n\f\v]*)$/.exec(tmp);
if (m) {
if (m[1].length == tmp.length) {
align = null;
}
else if ((m[1].length == 0) === (m[3].length == 0)) {
align = m[1].length >= 2 && m[3].length >= 2 ? 0 : null;
}
else {
align = m[1].length == 0 ? -1 : 1;
}
tmp = m[2];
}
line = line.substring(0, prevIndex) + tmp + line.substring(end);
}
break;
default:
continue;
}
break;
}
wikiRulesPattern.lastIndex = prevIndex;
handleTableCell(inTableRow ? 0 : 1,
matchText.replace(/^=|=$/g, '').length / 2, matchText.slice(-1) == "=", align);
continue;
}
}
if (matchText) {
if (listDepth.length == 0 && !currentHeader && !inDefList && !inTable) {
openParagraph();
}
var tmp;
if (matchNumber == 16) {
tmp = /^!?\[\[br\]\]$/i.test(matchText)
? (matchText.charCodeAt(0) == 0x21
? contentDocument.createTextNode(matchText.substring(1))
: contentDocument.createElement("br"))
: contentDocument.createTextNode(matchText);
}
else {
tmp = contentDocument.createTextNode(/^!/.test(matchText) ? matchText.substring(1) : matchText);
}
holder.appendChild(tmp);
}
}
if (currentHeader) {
closeHeader();
}
if (inTable) {
if (continueTableRow) {
continueTableRow = false;
}
else {
handleTableCell(-1);
}
}
}
closeToFragment();
return fragment;
};
TracWysiwyg.prototype.wikitextToOnelinerFragment = function(wikitext, contentDocument, options) {
var source = this.wikitextToFragment(wikitext, contentDocument, options);
var fragment = contentDocument.createDocumentFragment();
this.collectChildNodes(fragment, source.firstChild);
return fragment;
};
TracWysiwyg.prototype.wikiOpenTokens = {
"h1": "= ", "h2": "== ", "h3": "=== ", "h4": "==== ", "h5": "===== ", "h6": "====== ",
"b": "'''", "strong": "'''",
"i": "''", "em": "''",
"u": "__",
"del": "~~", "strike": "~~",
"sub": ",,",
"sup": "^",
"hr": "----\n",
"dl": true,
"dt": " ",
"dd": " ",
"table": true,
"tbody": true };
TracWysiwyg.prototype.wikiCloseTokens = {
"#text": true,
"a": true,
"tt": true,
"h1": " =", "h2": " ==", "h3": " ===", "h4": " ====", "h5": " =====", "h6": " ======",
"b": "'''", "strong": "'''",
"i": "''", "em": "''",
"u": "__",
"del": "~~", "strike": "~~",
"sub": ",,",
"sup": "^",
"br": true,
"hr": true,
"dl": "\n",
"dt": "::",
"dd": "\n",
"tbody": true,
"tr": "||\n",
"td": true, "th": true };
TracWysiwyg.prototype.wikiBlockTags = {
"h1": true, "h2": true, "h3": true, "h4": true, "h5": true, "h6": true,
"table": true, "dl": true, "hr": true };
TracWysiwyg.prototype.wikiInlineTags = {
"a": true, "tt": true, "b": true, "strong": true, "i": true, "em": true,
"u": true, "del": true, "strike": true, "sub": true, "sup": true,
"br": true, "span": true };
TracWysiwyg.prototype.domToWikitext = function(root, options) {
options = options || {};
var formatCodeBlock = !!options.formatCodeBlock;
var escapeNewlines = !!options.escapeNewlines;
var self = this;
var getTextContent = TracWysiwyg.getTextContent;
var getSelfOrAncestor = TracWysiwyg.getSelfOrAncestor;
var wikiOpenTokens = this.wikiOpenTokens;
var wikiCloseTokens = this.wikiCloseTokens;
var wikiInlineTags = this.wikiInlineTags;
var wikiBlockTags = this.wikiBlockTags;
var xmlNamePattern = this.xmlNamePattern;
var domToWikiInlinePattern = this.domToWikiInlinePattern;
var wikiSyntaxPattern = this.wikiSyntaxPattern;
var tracLinkPattern = new RegExp("^" + this._tracLink + "$");
var wikiPageNamePattern = new RegExp("^" + this._wikiPageName + "$");
var decorationTokenPattern = /^(?:'''|''|__|\^|,,)$/;
var texts = [];
var stack = [];
var last = root;
var listDepth = 0;
var quoteDepth = 0;
var quoteCitation = false;
var inCodeBlock = false;
var skipNode = null;
var openBracket = false;
function escapeText(s) {
var match = /^!?\[\[(.+)\]\]$/.exec(s);
if (match) {
return match[1].toLowerCase() != "br" ? s : "!" + s;
}
if (/^\d+/.test(s)) {
return s;
}
return "!" + s;
}
function isTailEscape() {
var t = texts;
var length = t.length;
return length > 0 ? /!$/.test(t[length - 1]) : false;
}
function tokenFromSpan(node) {
if (node.className == "underline") {
return wikiOpenTokens["u"];
}
var style = node.style;
if (style.fontWeight == "bold") {
return wikiOpenTokens["b"];
}
if (style.fontStyle == "italic") {
return wikiOpenTokens["i"];
}
switch (style.textDecoration) {
case "underline":
return wikiOpenTokens["u"];
case "line-through":
return wikiOpenTokens["del"];
}
switch (style.verticalAlign) {
case "sub":
return wikiOpenTokens["sub"];
case "sup":
return wikiOpenTokens["sup"];
}
return undefined;
}
function nodeDecorations(node) {
var _wikiOpenTokens = wikiOpenTokens;
var _decorationTokenPattern = decorationTokenPattern;
var hash = {};
for ( ; ; ) {
var childNodes = node.childNodes;
if (!childNodes || childNodes.length != 1) {
break;
}
var child = childNodes[0];
if (child.nodeType != 1) {
break;
}
var token = _wikiOpenTokens[child.tagName.toLowerCase()];
if (_decorationTokenPattern.test(token)) {
hash[token] = true;
}
node = child;
}
return hash;
}
function pushTextWithDecorations(text, node, traclink) {
var _texts = texts;
var _decorationTokenPattern = decorationTokenPattern;
var decorationsHash = nodeDecorations(node);
var decorations = [];
var cancelDecorations = [];
while (_texts.length > 0) {
var token = _texts[_texts.length - 1];
if (_decorationTokenPattern.test(token)) {
if (decorationsHash[token]) {
delete decorationsHash[token];
cancelDecorations.push(_texts.pop());
continue;
}
if ((token == "'''" || token == "''") && _texts.length > 1) {
var moreToken = _texts[_texts.length - 2];
if (_decorationTokenPattern.test(moreToken)
&& token + moreToken == "'''''"
&& decorationsHash[moreToken])
{
delete decorationsHash[moreToken];
cancelDecorations.push(moreToken);
_texts[_texts.length - 2] = _texts[_texts.length - 1];
_texts.pop();
}
}
}
break;
}
for (var token in decorationsHash) {
decorations.push(token);
}
decorations.sort();
if (decorations.length > 0) {
_texts.push.apply(_texts, decorations);
}
if (traclink) {
if (_texts.length > 0 && /[\w.+-]$/.test(_texts[_texts.length - 1])) {
_texts.push(traclink);
}
else {
text = new String(text);
text["tracwysiwyg-traclink"] = traclink;
_texts.push(text);
}
}
else {
_texts.push(text);
}
if (decorations.length > 0) {
decorations.reverse();
_texts.push.apply(_texts, decorations);
}
if (cancelDecorations.length > 0) {
cancelDecorations.reverse();
_texts.push.apply(_texts, cancelDecorations);
}
}
function pushToken(token) {
var _texts = texts;
var _decorationTokenPattern = decorationTokenPattern;
var length = _texts.length;
if (length == 0 || !_decorationTokenPattern.test(token)) {
_texts.push(token);
return;
}
var last = _texts[length - 1];
if (!_decorationTokenPattern.test(last)) {
_texts.push(token);
return;
}
if (last == token) {
_texts.pop();
return;
}
if (length < 2 || last + token != "'''''") {
_texts.push(token);
return;
}
if (_texts[length - 2] == token) {
_texts[length - 2] = _texts[length - 1];
_texts.pop();
}
else {
_texts.push(token);
}
}
function tracLinkText(link, label) {
if (!/\]/.test(label) && !/^[\"\']/.test(label)) {
return "[" + link + " " + label + "]";
}
if (!/\"/.test(label)) {
return "[" + link + ' "' + label + '"]';
}
if (!/\'/.test(label)) {
return "[" + link + " '" + label + "']";
}
return "[" + link + ' "' + label.replace(/"+/g, "") + '"]';
}
function pushAnchor(node, bracket) {
var link = node.getAttribute("data-tracwysiwyg-link");
var autolink = node.getAttribute("data-tracwysiwyg-autolink");
var attrs;
if (link === null) {
attrs = TracWysiwyg.unserializeFromHref(node.href);
link = attrs["data-tracwysiwyg-link"];
autolink = attrs["data-tracwysiwyg-autolink"];
}
link = (link || node.href).replace(/^\s+|\s+$/g, "");
var label = getTextContent(node).replace(/^\s+|\s+$/g, "");
if (!label) {
return;
}
var text = null;
var traclink = null;
if (autolink == "true") {
if (wikiPageNamePattern.test(label)) {
text = label;
link = "wiki:" + label;
traclink = "[wiki:" + label + "]";
}
else if (wikiSyntaxPattern.test(label)) {
text = label;
link = self.convertWikiSyntax(label);
}
else if (tracLinkPattern.test(label)) {
text = link = label;
}
}
else {
if (link == label) {
if (bracket) {
text = label;
}
else if (tracLinkPattern.test(label)) {
text = label;
}
}
}
if (!text) {
var match = /^([\w.+-]+):(@?(.*))$/.exec(link);
if (match) {
if (label == match[2]) {
if (match[1] == "wiki" && wikiPageNamePattern.test(match[2])) {
text = match[2];
traclink = "[wiki:" + text + "]";
}
else {
text = "[" + link + "]";
}
}
else {
var usingLabel = false;
switch (match[1]) {
case "changeset":
usingLabel = label == "[" + match[2] + "]" || /^\d+$/.test(match[2]) && label == "r" + match[2];
break;
case "log":
usingLabel = label == "[" + match[3] + "]" || label == "r" + match[3];
break;
case "report":
usingLabel = label == "{" + match[2] + "}";
break;
case "ticket":
usingLabel = label == "#" + match[2];
break;
}
if (usingLabel) {
text = label;
}
}
}
}
if (isTailEscape()) {
texts.push(" ");
}
if (text === null) {
text = tracLinkText(link, label);
}
if (!traclink && /^[\w.+-]/.test(text)) {
traclink = tracLinkText(link, label);
}
pushTextWithDecorations(text, node, traclink);
}
function string(source, times) {
var value = (1 << times) - 1;
if (value <= 0) {
return "";
}
else {
return value.toString(2).replace(/1/g, source);
}
}
function open(name, node) {
if (skipNode !== null) {
return;
}
var _texts = texts;
var token = wikiOpenTokens[name];
if (token !== undefined) {
if (name in wikiBlockTags && self.isInlineNode(node.previousSibling)) {
_texts.push("\n");
}
if (token !== true) {
if (name in wikiInlineTags && isTailEscape()) {
_texts.push(" ");
}
pushToken(token);
}
openBracket = false;
}
else {
switch (name) {
case "#text":
var value = node.nodeValue;
if (value) {
if (!inCodeBlock) {
if (value && !self.isInlineNode(node.previousSibling || node.parentNode)) {
value = value.replace(/^[ \t\r\n\f\v]+/g, "");
}
if (value && !self.isInlineNode(node.nextSibling || node.parentNode)) {
value = value.replace(/[ \t\r\n\f\v]+$/g, "");
}
value = value.replace(/\r?\n/g, " ");
if (!formatCodeBlock) {
value = value.replace(domToWikiInlinePattern, escapeText);
}
openBracket = /<$/.test(value);
}
if (value) {
var length = _texts.length;
var prev = length > 0 ? _texts[length - 1] : null;
if (prev && prev["tracwysiwyg-traclink"] && tracLinkPattern.test(prev + value.substring(0, 1))) {
_texts[length - 1] = prev["tracwysiwyg-traclink"];
}
_texts.push(value);
}
}
break;
case "p":
if (quoteDepth > 0) {
_texts.push(string(quoteCitation ? "> " : " ", quoteDepth));
}
else if (!/[^ \t\r\n\f\v]/.test(getTextContent(node))) {
skipNode = node;
}
break;
case "a":
skipNode = node;
var bracket = false;
if (openBracket) {
var nextSibling = node.nextSibling;
bracket = nextSibling && nextSibling.nodeType == 3 && /^>/.test(nextSibling.nodeValue);
openBracket = false;
}
pushAnchor(node, bracket);
break;
case "li":
_texts.push(" " + string(" ", listDepth - 1));
var container = node.parentNode;
if ((container.tagName || "").toLowerCase() == "ol") {
var start = container.getAttribute("start") || "";
if (start != "1" && /^(?:[0-9]+|[a-zA-Z]|[ivxIVX]{1,5})$/.test(start)) {
_texts.push(start, ". ");
}
else {
switch (container.className) {
case "arabiczero": _texts.push("0. "); break;
case "lowerroman": _texts.push("i. "); break;
case "upperroman": _texts.push("I. "); break;
case "loweralpha": _texts.push("a. "); break;
case "upperalpha": _texts.push("A. "); break;
default: _texts.push("1. "); break;
}
}
}
else {
_texts.push("* ");
}
break;
case "ul": case "ol":
if (listDepth == 0) {
if (self.isInlineNode(node.previousSibling)) {
_texts.push("\n");
}
}
else if (listDepth > 0) {
if (node.parentNode.tagName.toLowerCase() == "li") {
_texts.push("\n");
}
}
listDepth++;
break;
case "br":
if (!self.isBogusLineBreak(node)) {
var value = null;
if (inCodeBlock) {
value = "\n";
}
else if (formatCodeBlock) {
switch (((node.parentNode || {}).tagName || "").toLowerCase()) {
case "li":
value = "\n " + string(" ", listDepth);
break;
case "p": case "blockquote":
value = "\n";
if (quoteDepth > 0) {
value += string(quoteCitation ? "> " : " ", quoteDepth);
}
break;
case "dd":
value = "\n ";
break;
case "dt":
case "h1": case "h2": case "h3": case "h4": case "h5": case "h6":
value = " ";
break;
default:
value = "\n";
break;
}
}
else {
if (escapeNewlines && getSelfOrAncestor(node, /^(?:p|blockquote)$/)) {
value = quoteDepth > 0
? "\n" + string(quoteCitation ? "> " : " ", quoteDepth)
: "\n";
}
if (!value) {
value = "[[BR]]";
var length = _texts.length;
if (length > 0) {
var lastText = _texts[length - 1];
var tmp = lastText + "[[BR]]";
var _pattern = domToWikiInlinePattern;
_pattern.lastIndex = 0;
var lastMatch, match;
while (match = _pattern.exec(tmp)) {
lastMatch = match;
}
if (lastMatch && lastMatch.index < lastText.length
&& lastMatch.index + lastMatch[0].length > lastText.length)
{
value = " [[BR]]";
}
}
}
}
_texts.push(value);
}
break;
case "pre":
_texts.push(
/^(?:li|dd)$/i.test(node.parentNode.tagName) || self.isInlineNode(node.previousSibling)
? "\n{{{\n" : "{{{\n");
inCodeBlock = true;
break;
case "blockquote":
if (self.isInlineNode(node.previousSibling)) {
_texts.push("\n");
}
quoteDepth++;
if (quoteDepth == 1) {
quoteCitation = (node.className == "citation");
}
break;
case "th":
var header = true;
case "td":
skipNode = node;
var colspan = node.getAttribute("colSpan");
colspan = colspan ? parseInt(colspan, 10) : 0;
_texts.push(colspan > 1 ? string("||", colspan) : "||");
if (header) {
_texts.push("=");
}
var align = node.style.textAlign;
if (!align) {
align = (node.getAttribute("align") || "").toLowerCase();
}
var text = self.domToWikitext(node, self.options).replace(/ *\n/g, "[[BR]]").replace(/^ +| +$/g, "");
if (text) {
switch (align) {
case "left": _texts.push(text, " "); break;
case "center": _texts.push(" ", text, " "); break;
case "right": _texts.push(" ", text); break;
default: _texts.push(" ", text, " "); break;
}
}
else {
_texts.push(" ");
}
if (header) {
_texts.push("=");
}
break;
case "tr":
if (quoteDepth > 0) {
_texts.push(string(quoteCitation ? ">" : " ", quoteDepth));
}
break;
case "tt":
skipNode = node;
var value = getTextContent(node);
var text;
if (value) {
if (isTailEscape()) {
_texts.push(" ");
}
if (!/`/.test(value)) {
text = "`" + value + "`";
}
else if (!/\{\{\{|\}\}\}/.test(value)) {
text = "{{{" + value + "}}}";
}
else {
text = value.replace(/[^`]+|`+/g, function(m) {
return m.charCodeAt(0) != 0x60 ? "`" + m + "`" : "{{{" + m + "}}}";
});
}
pushTextWithDecorations(text, node);
}
break;
case "span":
if (node.className == "wikianchor" && xmlNamePattern.test(node.id || "")) {
skipNode = node;
var text = self.domToWikitext(node, self.options).replace(/^ +| +$|\]/g, "");
_texts.push("[=#", node.id, text ? " " + text + "]" : "]");
}
else {
var token = tokenFromSpan(node);
if (token !== undefined) {
if (name in wikiInlineTags && isTailEscape()) {
_texts.push(" ");
}
pushToken(token);
}
}
break;
case "script":
case "style":
skipNode = node;
break;
}
if (name != "#text") {
openBracket = false;
}
}
}
function close(name, node) {
if (skipNode !== null) {
if (skipNode == node) {
skipNode = null;
}
return;
}
var _texts = texts;
var token = wikiCloseTokens[name];
if (token === true) {
// nothing to do
}
else if (token !== undefined) {
if (name in wikiInlineTags && isTailEscape()) {
_texts.push(" ");
}
pushToken(token);
}
else {
switch (name) {
case "p":
_texts.push(quoteDepth == 0 ? "\n\n" : "\n");
break;
case "li":
if (node.getElementsByTagName("li").length == 0) {
_texts.push("\n");
}
break;
case "ul": case "ol":
listDepth--;
if (listDepth == 0) {
_texts.push("\n");
}
break;
case "pre":
var text;
var parentNode = node.parentNode;
if (parentNode && /^(?:li|dd)$/i.test(parentNode.tagName)) {
var nextSibling = node.nextSibling;
if (!nextSibling) {
text = "\n}}}";
}
else if (nextSibling.nodeType != 1) {
text = "\n}}}\n";
}
else if (nextSibling.tagName.toLowerCase() == "pre") {
text = "\n}}}";
}
else {
text = "\n}}}\n";
}
if (text.slice(-1) == "\n") {
text += listDepth > 0 ? " " + string(" ", listDepth) : " ";
}
}
else {
text = "\n}}}\n";
}
_texts.push(text);
inCodeBlock = false;
break;
case "blockquote":
quoteDepth--;
if (quoteDepth == 0) {
_texts.push("\n");
}
break;
case "span":
var token = tokenFromSpan(node);
if (token !== undefined) {
if (name in wikiInlineTags && isTailEscape()) {
_texts.push(" ");
}
_texts.push(token);
}
break;
case "table":
if (quoteDepth == 0) {
_texts.push("\n");
}
break;
}
}
if (/^h[1-6]$/.test(name)) {
if (xmlNamePattern.test(node.id || "")) {
_texts.push(" #", node.id);
}
_texts.push("\n");
}
}
function iterator(node) {
var name = null;
switch (node && node.nodeType) {
case 1: // element
name = node.tagName.toLowerCase();
break;
case 3: // text
name = "#text";
break;
}
if (node && last == node.parentNode) { // down
// nothing to do
}
else if (node && last == node.previousSibling) { // forward
close(stack.pop(), last);
}
else { // up, forward
var tmp = last;
var nodeParent = node ? node.parentNode : root;
for ( ; ; ) {
var parent = tmp.parentNode;
if (parent == node) {
break;
}
close(stack.pop(), tmp);
if (parent == nodeParent || !parent) {
if (!node) {
return;
}
break;
}
tmp = parent;
}
}
open(name, node);
stack.push(name);
last = node;
}
this.treeWalk(root, iterator);
return texts.join("").replace(/^(?: *\n)+|(?: *\n)+$/g, "");
};
if (window.getSelection) {
TracWysiwyg.prototype.appendBogusLineBreak = function(element) {
var wikiInlineTags = this.wikiInlineTags;
var last = element.lastChild;
for ( ; ; ) {
if (!last) {
break;
}
if (last.nodeType != 1) {
return;
}
var name = last.tagName.toLowerCase();
if (name == "br") {
break;
}
if (!(name in wikiInlineTags)) {
return;
}
last = last.lastChild || last.previousSibling;
}
var br = this.contentDocument.createElement("br");
element.appendChild(br);
};
TracWysiwyg.prototype.isBogusLineBreak = TracWysiwyg.prototype.isLastChildInBlockNode;
TracWysiwyg.prototype.insertParagraphOnEnter = function(event) {
var range = this.getSelectionRange();
var node = range.endContainer;
var header = null;
if (node && node.nodeType == 3 && range.endOffset == node.nodeValue.length) {
var nextSibling = node.nextSibling;
if (!nextSibling || nextSibling.tagName.toLowerCase() == "br") {
while (node) {
if (node.nodeType == 1 && /^h[1-6]$/i.exec(node.tagName)) {
header = node;
break;
}
node = node.parentNode;
}
if (header) {
var parent = header.parentNode;
var childNodes = parent.childNodes;
var length = childNodes.length;
for (var offset = 0; offset < length; offset++) {
if (childNodes[offset] == header) {
offset++;
break;
}
}
this.selectRange(parent, offset, parent, offset);
this.insertHTML('
');
TracWysiwyg.stopEvent(event);
}
}
}
};
TracWysiwyg.prototype.tableHTML = function(id, row, col) {
var html = this._tableHTML(row, col);
return html.replace(/<\/td>/g, ' |
| ').replace(//, ' | ');
};
TracWysiwyg.prototype.insertTableCell = function(row, index) {
var cell = row.insertCell(index);
this.appendBogusLineBreak(cell);
return cell;
};
TracWysiwyg.prototype.getFocusNode = function() {
return this.contentWindow.getSelection().focusNode;
};
if (window.opera) {
TracWysiwyg.prototype.insertLineBreak = function() {
this.execCommand("inserthtml", " ");
};
TracWysiwyg.prototype.insertLineBreakOnShiftEnter = null;
}
else if (window.getSelection().setBaseAndExtent) { // Safari 2+
TracWysiwyg.prototype.insertLineBreak = function() {
this.execCommand("insertlinebreak");
};
TracWysiwyg.prototype.insertLineBreakOnShiftEnter = function(event) {
this.insertLineBreak();
TracWysiwyg.stopEvent(event);
};
}
else { // Firefox 2+
TracWysiwyg.prototype.insertLineBreak = function() {
var d = this.contentDocument;
var event = d.createEvent("KeyboardEvent");
event.initKeyEvent("keypress", true, true, null, false, false, true, false, 0x000d, 0);
d.body.dispatchEvent(event);
};
TracWysiwyg.prototype.insertLineBreakOnShiftEnter = null;
}
if (window.getSelection().removeAllRanges) {
TracWysiwyg.prototype.selectNode = function(node) {
var selection = this.contentWindow.getSelection();
selection.removeAllRanges();
var range = this.contentDocument.createRange();
range.selectNode(node);
selection.addRange(range);
};
TracWysiwyg.prototype.selectNodeContents = function(node) {
var selection = this.contentWindow.getSelection();
selection.removeAllRanges();
var range = this.contentDocument.createRange();
range.selectNodeContents(node);
selection.addRange(range);
};
TracWysiwyg.prototype.selectRange = function(start, startOffset, end, endOffset) {
var selection = this.contentWindow.getSelection();
selection.removeAllRanges();
var range = this.contentDocument.createRange();
range.setStart(start, startOffset);
range.setEnd(end, endOffset);
selection.addRange(range);
};
TracWysiwyg.prototype.getNativeSelectionRange = function() {
var selection = this.contentWindow.getSelection();
return selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
};
TracWysiwyg.prototype.expandSelectionToElement = function(arg) {
if (arg.start || arg.end) {
var selection = this.contentWindow.getSelection();
var range = this.getNativeSelectionRange() || this.contentDocument.createRange();
selection.removeAllRanges();
if (arg.start) {
range.setStartBefore(arg.start);
}
if (arg.end) {
range.setEndAfter(arg.end);
}
selection.addRange(range);
}
};
TracWysiwyg.prototype.insertHTML = function(html) {
this.execCommand("inserthtml", html);
};
}
else { // Safari 2
TracWysiwyg.prototype.selectNode = function(node) {
var selection = this.contentWindow.getSelection();
var range = this.contentDocument.createRange();
range.selectNode(node);
selection.setBaseAndExtent(range.startContainer, range.startOffset, range.endContainer, range.endOffset);
range.detach();
};
TracWysiwyg.prototype.selectNodeContents = function(node) {
this.selectRange(node, 0, node, node.childNodes.length);
};
TracWysiwyg.prototype.selectRange = function(start, startOffset, end, endOffset) {
var selection = this.contentWindow.getSelection();
selection.setBaseAndExtent(start, startOffset, end, endOffset);
};
TracWysiwyg.prototype.getNativeSelectionRange = function() {
var selection = this.contentWindow.getSelection();
if (selection.anchorNode) {
var range = this.contentDocument.createRange();
range.setStart(selection.baseNode, selection.baseOffset);
range.setEnd(selection.extentNode, selection.extentOffset);
if (range.collapsed && !selection.isCollapsed) {
range.setStart(selection.extentNode, selection.extentOffset);
range.setEnd(selection.baseNode, selection.baseOffset);
}
return range;
}
return null;
};
TracWysiwyg.prototype.expandSelectionToElement = function(arg) {
if (arg.start || arg.end) {
var selection = this.contentWindow.getSelection();
var range = this.getNativeSelectionRange();
if (arg.start) {
range.setStartBefore(arg.start);
}
if (arg.end) {
range.setEndAfter(arg.end);
}
selection.setBaseAndExtent(range.startContainer, range.startOffset, range.endContainer, range.endOffset);
range.detach();
}
};
TracWysiwyg.prototype.insertHTML = function(html) {
var range = this.getNativeSelectionRange();
if (range) {
var d = this.contentDocument;
var tmp = d.createRange();
tmp.setStart(d.body, 0);
tmp.setEnd(d.body, 0);
var fragment = tmp.createContextualFragment(html);
range.deleteContents();
range.insertNode(fragment);
range.detach();
tmp.detach();
}
};
}
TracWysiwyg.prototype.getSelectionRange = TracWysiwyg.prototype.getNativeSelectionRange;
TracWysiwyg.prototype.getSelectionText = function() {
var range = this.getNativeSelectionRange();
return range ? range.toString() : null;
};
TracWysiwyg.prototype.getSelectionHTML = function() {
var fragment = this.getSelectionFragment();
var anonymous = this.contentDocument.createElement("div");
anonymous.appendChild(fragment);
return anonymous.innerHTML;
};
TracWysiwyg.prototype.getSelectionFragment = function() {
var range = this.getNativeSelectionRange();
return range ? range.cloneContents() : this.contentDocument.createDocumentFragment();
};
TracWysiwyg.prototype.getSelectionPosition = function() {
var range = this.getNativeSelectionRange();
var position = { start: null, end: null };
if (range) {
position.start = range.startContainer;
position.end = range.endContainer;
}
return position;
};
TracWysiwyg.prototype.selectionContainsTagName = function(name) {
var selection = this.contentWindow.getSelection();
var range = this.getNativeSelectionRange();
if (!range) {
return false;
}
var ancestor = range.commonAncestorContainer;
if (!ancestor) {
return false;
}
if (TracWysiwyg.getSelfOrAncestor(ancestor, name)) {
return true;
}
if (ancestor.nodeType != 1) {
return false;
}
var elements = ancestor.getElementsByTagName(name);
var length = elements.length;
for (var i = 0; i < length; i++) {
if (selection.containsNode(elements[i], true)) {
return true;
}
}
return false;
};
}
else if (document.selection) {
TracWysiwyg.prototype.appendBogusLineBreak = function(element) { };
TracWysiwyg.prototype.isBogusLineBreak = function(node) { return false };
TracWysiwyg.prototype.insertParagraphOnEnter = null;
TracWysiwyg.prototype.insertLineBreak = function() {
this.insertHTML(" ");
};
TracWysiwyg.prototype.insertLineBreakOnShiftEnter = null;
TracWysiwyg.prototype.tableHTML = function(id, row, col) {
var html = this._tableHTML(row, col);
return html.replace(/ | /, ' | ');
};
TracWysiwyg.prototype.insertTableCell = function(row, index) {
return row.insertCell(index);
};
TracWysiwyg.prototype.getFocusNode = function() {
this.contentWindow.focus();
var d = this.contentDocument;
var range = d.selection.createRange();
var node = range.item ? range.item(0) : range.parentElement();
return node.ownerDocument == d ? node : null;
};
TracWysiwyg.prototype.selectNode = function(node) {
var d = this.contentDocument;
var body = d.body;
var range;
d.selection.empty();
try {
range = body.createControlRange();
range.addElement(node);
}
catch (e) {
range = body.createTextRange();
range.moveToElementText(node);
}
range.select();
};
TracWysiwyg.prototype.selectNodeContents = function(node) {
var d = this.contentDocument;
d.selection.empty();
var range = d.body.createTextRange();
range.moveToElementText(node);
range.select();
};
TracWysiwyg.prototype.selectRange = function(start, startOffset, end, endOffset) {
var d = this.contentDocument;
var body = d.body;
d.selection.empty();
var range = endPoint(start, startOffset);
if (start != end || startOffset != endOffset) {
range.setEndPoint("EndToEnd", endPoint(end, endOffset));
}
range.select();
function endPoint(node, offset) {
var range;
if (node.nodeType == 1) {
var childNodes = node.childNodes;
if (offset >= childNodes.length) {
range = body.createTextRange();
range.moveToElementText(node);
range.collapse(false);
return range;
}
node = childNodes[offset];
if (node.nodeType == 1) {
range = body.createTextRange();
range.moveToElementText(node);
range.collapse(true);
switch (node.tagName.toLowerCase()) {
case "table":
range.move("character", -1);
break;
}
return range;
}
return endPoint(node, 0);
}
if (node.nodeType != 3) {
throw "selectRange: nodeType != @".replace(/@/, node.nodeType);
}
range = body.createTextRange();
var element = node.previousSibling;
while (element) {
var nodeType = element.nodeType;
if (nodeType == 1) {
range.moveToElementText(element);
range.collapse(false);
break;
}
if (nodeType == 3) {
offset += element.nodeValue.length;
}
element = element.previousSibling;
}
if (!element) {
range.moveToElementText(node.parentNode);
range.collapse(true);
}
if (offset != 0) {
range.move("character", offset);
}
return range;
}
};
TracWysiwyg.prototype.getSelectionRange = function() {
var body = this.contentDocument.body;
var pseudo = {};
var start = this.getNativeSelectionRange();
if (start.item) {
var element = start.item(0);
var parent = element.parentNode;
var childNodes = parent.childNodes;
var length = childNodes.length;
for (var i = 0; i < length; i++) {
if (childNodes[i] == element) {
pseudo.startOffset = i;
pseudo.endOffset = i + 1;
break;
}
}
pseudo.collapsed = false;
pseudo.startContainer = pseudo.endContainer = parent;
return pseudo;
}
var end = start.duplicate();
pseudo.collapsed = start.compareEndPoints("StartToEnd", end) == 0;
start.collapse(true);
end.collapse(false);
function nextElement(range) {
var parent = range.parentElement();
var childNodes = parent.childNodes;
var length = childNodes.length;
for (var i = 0; i < length; i++) {
var node = childNodes[i];
if (node.nodeType == 1) {
var tmp = body.createTextRange();
tmp.moveToElementText(node);
if (range.compareEndPoints("EndToStart", tmp) <= 0) {
return node;
}
}
}
return null;
}
function nodeOffset(range, parent, element, index, length) {
var tmp = body.createTextRange();
tmp.moveToElementText(element || parent);
tmp.collapse(!!element);
tmp.move("character", -index);
if (!element) {
length++;
}
for ( ; length >= 0; length--) {
if (tmp.compareEndPoints("EndToStart", range) == 0) {
return length;
}
tmp.move("character", -1);
}
return null;
}
function setContainerOffset(range, containerKey, offsetKey) {
var parent = range.parentElement();
var element = nextElement(range);
var index = 0;
var node = element ? element.previousSibling : parent.lastChild;
var offset, length;
while (node && node.nodeType == 3) {
length = node.nodeValue.length;
offset = nodeOffset(range, parent, element, index, length);
if (offset !== null) {
pseudo[containerKey] = node;
pseudo[offsetKey] = offset;
return;
}
index += length;
node = node.previousSibling;
}
var childNodes = parent.childNodes;
length = childNodes.length;
if (length > 0) {
pseudo[containerKey] = parent;
pseudo[offsetKey] = containerKey == "startContainer" ? 0 : length - 1;
return;
}
element = parent;
parent = element.parentNode;
childNodes = parent.childNodes;
length = childNodes.length;
for (offset = 0; offset < length; offset++) {
if (element == childNodes[offset]) {
pseudo[containerKey] = parent;
pseudo[offsetKey] = offset;
return;
}
}
}
setContainerOffset(start, "startContainer", "startOffset");
setContainerOffset(end, "endContainer", "endOffset");
return pseudo;
};
TracWysiwyg.prototype.getNativeSelectionRange = function() {
this.contentWindow.focus();
return this.contentDocument.selection.createRange();
};
TracWysiwyg.prototype.getSelectionText = function() {
var range = this.getNativeSelectionRange();
if (range) {
return range.item ? range.item(0).innerText : range.text;
}
return null;
};
TracWysiwyg.prototype.getSelectionHTML = function() {
var range = this.getNativeSelectionRange();
if (range) {
return range.item ? range.item(0).innerHTML : range.htmlText;
}
return null;
};
TracWysiwyg.prototype.getSelectionFragment = function() {
var d = this.contentDocument;
var fragment = d.createDocumentFragment();
var anonymous = d.createElement("div");
anonymous.innerHTML = this.getSelectionHTML();
this.collectChildNodes(fragment, anonymous);
return fragment;
};
TracWysiwyg.prototype.getSelectionPosition = function() {
this.contentWindow.focus();
var d = this.contentDocument;
var range = d.selection.createRange();
var startNode = null;
var endNode = null;
if (range.item) {
if (range.item(0).ownerDocument == d) {
startNode = range.item(0);
endNode = range.item(range.length - 1);
}
}
else {
if (range.parentElement().ownerDocument == d) {
var startRange = range.duplicate();
startRange.collapse(true);
startNode = startRange.parentElement();
var endRange = range.duplicate();
endRange.collapse(false);
endNode = endRange.parentElement();
}
}
return { start: startNode, end: endNode };
};
TracWysiwyg.prototype.expandSelectionToElement = function(arg) {
this.contentWindow.focus();
var d = this.contentDocument;
var body = d.body;
var range = d.selection.createRange();
var tmp;
if (arg.start) {
tmp = body.createTextRange();
tmp.moveToElementText(arg.start);
range.setEndPoint("StartToStart", tmp);
}
if (arg.end) {
tmp = body.createTextRange();
tmp.moveToElementText(arg.end);
range.setEndPoint("EndToEnd", tmp);
}
if (tmp) {
range.select();
}
};
TracWysiwyg.prototype.selectionContainsTagName = function(name) {
this.contentWindow.focus();
var d = this.contentDocument;
var selection = d.selection;
var range = selection.createRange();
var parent = range.item ? range.item(0) : range.parentElement();
if (!parent) {
return false;
}
if (TracWysiwyg.getSelfOrAncestor(parent, name)) {
return true;
}
var elements = parent.getElementsByTagName(name);
var length = elements.length;
for (var i = 0; i < length; i++) {
var testRange = selection.createRange();
testRange.moveToElementText(elements[i]);
if (range.compareEndPoints("StartToEnd", testRange) <= 0
&& range.compareEndPoints("EndToStart", testRange) >= 0)
{
return true;
}
}
return false;
};
TracWysiwyg.prototype.insertHTML = function(html) {
this.contentWindow.focus();
var selection = this.contentDocument.selection;
var range = selection.createRange();
range.pasteHTML(html.replace(/\t/g, " "));
range.collapse(false);
range.select();
range = this.contentDocument.selection.createRange();
};
}
else {
TracWysiwyg.prototype.appendBogusLineBreak = function(element) { };
TracWysiwyg.prototype.insertParagraphOnEnter = null;
TracWysiwyg.prototype.insertLineBreak = function() { };
TracWysiwyg.prototype.insertTableCell = function(row, index) { return null };
TracWysiwyg.prototype.getFocusNode = function() { return null };
TracWysiwyg.prototype.selectNode = function(node) { };
TracWysiwyg.prototype.selectNodeContents = function(node) { return null };
TracWysiwyg.prototype.selectRange = function(start, startOffset, end, endOffset) { };
TracWysiwyg.prototype.getSelectionRange = function() { return null };
TracWysiwyg.prototype.getNativeSelectionRange = function() { return null };
TracWysiwyg.prototype.getSelectionText = function() { return null };
TracWysiwyg.prototype.getSelectionHTML = function() { return null };
TracWysiwyg.prototype.getSelectionFragment = function() { return null };
TracWysiwyg.prototype.getSelectionPosition = function() { return null };
TracWysiwyg.prototype.expandSelectionToElement = function(arg) { };
TracWysiwyg.prototype.selectionContainsTagName = function(name) { return false };
TracWysiwyg.prototype.insertHTML = function(html) { };
}
TracWysiwyg.prototype._treeWalkEmulation = function(root, iterator) {
if (!root.firstChild) {
iterator(null);
return;
}
var element = root;
var tmp;
while (element) {
if (tmp = element.firstChild) {
element = tmp;
}
else if (tmp = element.nextSibling) {
element = tmp;
}
else {
for ( ; ; ) {
element = element.parentNode;
if (element == root || !element) {
iterator(null);
return;
}
if (tmp = element.nextSibling) {
element = tmp;
break;
}
}
}
iterator(element);
}
};
if (document.createTreeWalker) {
TracWysiwyg.prototype.treeWalk = function(root, iterator) {
var walker = root.ownerDocument.createTreeWalker(
root, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, null, true);
while (walker.nextNode()) {
iterator(walker.currentNode);
}
iterator(null);
};
}
else {
TracWysiwyg.prototype.treeWalk = TracWysiwyg.prototype._treeWalkEmulation;
}
TracWysiwyg.instances = [];
TracWysiwyg.count = 0;
TracWysiwyg.tracPaths = null;
TracWysiwyg.newInstance = function(textarea, options) {
var instance = new TracWysiwyg(textarea, options);
TracWysiwyg.instances.push(instance);
return instance;
};
TracWysiwyg.findInstance = function(textarea) {
var instances = TracWysiwyg.instances;
var length = instances.length;
for (var i = 0; i < length; i++) {
var instance = instances[i];
if (instance.textarea == textarea) {
return instance;
}
}
return null;
};
TracWysiwyg.getTracPaths = function() {
var stylesheets = [];
var paths = { stylesheets: stylesheets };
var d = document;
var head = d.getElementsByTagName("head")[0];
var links = head.getElementsByTagName("link");
var length = links.length;
for (var i = 0; i < length; i++) {
var link = links[i];
var href = link.getAttribute("href") || "";
var type = link.getAttribute("type") || "";
switch ((link.getAttribute("rel") || "").toLowerCase()) {
case "tracwysiwyg.base":
paths.base = href;
break;
case "tracwysiwyg.stylesheet":
stylesheets.push(href);
break;
}
}
if (paths.base && stylesheets.length > 0) {
return paths;
}
return null;
};
TracWysiwyg.getOptions = function() {
var options = {};
if (typeof window._tracwysiwyg != "undefined") {
options = _tracwysiwyg;
}
return options;
};
TracWysiwyg.getEditorMode = function() {
if (TracWysiwyg.editorMode) {
return TracWysiwyg.editorMode;
}
var mode = null;
var cookies = (document.cookie || "").split(";");
var length = cookies.length;
for (var i = 0; i < length; i++) {
var match = /^\s*tracwysiwyg=(\S*)/.exec(cookies[i]);
if (match) {
switch (match[1]) {
case "wysiwyg":
mode = match[1];
break;
default: // "textarea"
mode = null;
break;
}
break;
}
}
TracWysiwyg.editorMode = mode || "textarea";
return TracWysiwyg.editorMode;
};
TracWysiwyg.setEditorMode = function(mode) {
switch (mode) {
case "wysiwyg":
break;
default: // "textarea"
mode = "textarea";
break;
}
TracWysiwyg.editorMode = mode;
var now = new Date();
if (!/\/$/.test(TracWysiwyg.tracPaths.base)) {
expires = new Date(now.getTime() - 86400000);
pieces = [ "tracwysiwyg=",
"path=" + TracWysiwyg.tracPaths.base + "/",
"expires=" + expires.toUTCString() ];
document.cookie = pieces.join("; ");
}
var expires = new Date(now.getTime() + 365 * 86400 * 1000);
var pieces = [ "tracwysiwyg=" + mode,
"path=" + TracWysiwyg.tracPaths.base,
"expires=" + expires.toUTCString() ];
document.cookie = pieces.join("; ");
};
TracWysiwyg.removeEvent = function(element, type, func) {
jQuery(element).unbind(type, func);
};
TracWysiwyg.stopEvent = function(event) {
if (event.preventDefault) {
event.preventDefault();
event.stopPropagation();
}
else {
event.returnValue = false;
event.cancelBubble = true;
}
};
TracWysiwyg.setStyle = function(element, object) {
var style = element.style;
for (var name in object) {
style[name] = object[name];
}
};
if (document.defaultView) {
TracWysiwyg.getStyle = function(element, name) {
var value = element.style[name];
if (!value) {
var style = element.ownerDocument.defaultView.getComputedStyle(element, null)
value = style ? style[name] : null;
}
return value;
};
}
else {
TracWysiwyg.getStyle = function(element, name) {
return element.style[name] || element.currentStyle[name];
};
}
TracWysiwyg.elementPosition = function(element) {
function vector(left, top) {
var value = [ left, top ];
value.left = left;
value.top = top;
return value;
}
var position = TracWysiwyg.getStyle(element, "position");
var left = 0, top = 0;
for (var node = element; node; node = node.offsetParent) {
left += node.offsetLeft || 0;
top += node.offsetTop || 0;
}
if (position != "absolute") {
return vector(left, top);
}
var offset = TracWysiwyg.elementPosition(element.offsetParent);
return vector(left - offset.left, top - offset.top);
};
TracWysiwyg.getSelfOrAncestor = function(element, name) {
var target = element;
var d = element.ownerDocument;
if (name instanceof RegExp) {
while (target && target != d) {
switch (target.nodeType) {
case 1: // element
if (name.test(target.tagName.toLowerCase())) {
return target;
}
break;
case 11: // fragment
return null;
}
target = target.parentNode;
}
}
else {
name = name.toLowerCase();
while (target && target != d) {
switch (target.nodeType) {
case 1: // element
if (target.tagName.toLowerCase() == name) {
return target;
}
break;
case 11: // fragment
return null;
}
target = target.parentNode;
}
}
return null;
};
TracWysiwyg.serializeToHref = function(attrs) {
var texts = [];
for (var name in attrs) {
if (/^data(?:-|$)/.exec(name)) {
texts.push(encodeURIComponent(name) + "=" + encodeURIComponent(attrs[name]));
}
}
return "#" + texts.join("&");
};
TracWysiwyg.unserializeFromHref = function(href, name) {
var attrs = {};
if (href.indexOf("#") !== -1) {
var pieces = href.replace(/^[^#]*#/, '').split(/&/g);
var length = pieces.length;
for (var i = 0; i < length; i++) {
var pair = pieces[i].split(/=/g, 2);
attrs[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
}
return name ? attrs[name] : attrs;
};
TracWysiwyg.getTextContent = (function() {
var anonymous = document.createElement("div");
if (typeof anonymous.textContent != "undefined") {
return function(element) { return element.textContent };
}
else if (typeof anonymous.innerText != "undefined") {
return function(element) { return element.innerText };
}
else {
return function(element) { return null };
}
})();
TracWysiwyg.initialize = function() {
if ("replace".replace(/[a-e]/g, function(m) { return "*" }) != "r*pl***") {
return;
}
if (typeof document.designMode == "undefined") {
return;
}
TracWysiwyg.tracPaths = TracWysiwyg.getTracPaths();
if (!TracWysiwyg.tracPaths) {
return;
}
var options = TracWysiwyg.getOptions();
var textareas = document.getElementsByTagName("textarea");
for (var i = 0; i < textareas.length; i++) {
var textarea = textareas[i];
if (/\bwikitext\b/.test(textarea.className || "")) {
TracWysiwyg.newInstance(textarea, options);
}
}
};
|