etherpad-lite/static/js/linestylefilter.js

366 lines
9.5 KiB
JavaScript
Raw Normal View History

/**
* This code is mostly from the old Etherpad. Please help us to comment this code.
* This helps other people to understand this code better and helps them to improve it.
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
*/
2011-03-26 14:10:41 +01:00
// THIS FILE IS ALSO AN APPJET MODULE: etherpad.collab.ace.linestylefilter
// %APPJET%: import("etherpad.collab.ace.easysync2.Changeset");
// %APPJET%: import("etherpad.admin.plugins");
/**
* Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// requires: easysync2.Changeset
// requires: top
// requires: plugins
// requires: undefined
2012-01-16 05:16:11 +01:00
var Changeset = require('/easysync2').Changeset
var plugins = undefined;
try {
plugins = require('/plugins').plugins;
} catch (e) {
// silence
}
2012-01-16 05:16:11 +01:00
2011-03-26 14:10:41 +01:00
var linestylefilter = {};
linestylefilter.ATTRIB_CLASSES = {
2011-07-07 19:59:34 +02:00
'bold': 'tag:b',
'italic': 'tag:i',
'underline': 'tag:u',
'strikethrough': 'tag:s'
2011-03-26 14:10:41 +01:00
};
2011-07-07 19:59:34 +02:00
linestylefilter.getAuthorClassName = function(author)
{
return "author-" + author.replace(/[^a-y0-9]/g, function(c)
{
2011-03-26 14:10:41 +01:00
if (c == ".") return "-";
2011-07-07 19:59:34 +02:00
return 'z' + c.charCodeAt(0) + 'z';
2011-03-26 14:10:41 +01:00
});
};
// lineLength is without newline; aline includes newline,
// but may be falsy if lineLength == 0
2011-07-07 19:59:34 +02:00
linestylefilter.getLineStyleFilter = function(lineLength, aline, textAndClassFunc, apool)
{
2011-03-26 14:10:41 +01:00
var plugins_;
2011-07-07 19:59:34 +02:00
if (typeof(plugins) != 'undefined')
{
2011-03-26 14:10:41 +01:00
plugins_ = plugins;
2011-07-07 19:59:34 +02:00
}
else
{
2011-03-26 14:10:41 +01:00
plugins_ = parent.parent.plugins;
}
if (lineLength == 0) return textAndClassFunc;
var nextAfterAuthorColors = textAndClassFunc;
2011-07-07 19:59:34 +02:00
var authorColorFunc = (function()
{
2011-03-26 14:10:41 +01:00
var lineEnd = lineLength;
var curIndex = 0;
var extraClasses;
var leftInAuthor;
2011-07-07 19:59:34 +02:00
function attribsToClasses(attribs)
{
2011-03-26 14:10:41 +01:00
var classes = '';
2011-07-07 19:59:34 +02:00
Changeset.eachAttribNumber(attribs, function(n)
{
var key = apool.getAttribKey(n);
if (key)
{
var value = apool.getAttribValue(n);
if (value)
{
if (key == 'author')
{
classes += ' ' + linestylefilter.getAuthorClassName(value);
}
else if (key == 'list')
{
classes += ' list:' + value;
}
2012-01-15 18:20:20 +01:00
else if (key == 'start')
{
classes += ' start:' + value;
}
2011-07-07 19:59:34 +02:00
else if (linestylefilter.ATTRIB_CLASSES[key])
{
classes += ' ' + linestylefilter.ATTRIB_CLASSES[key];
}
else
{
classes += plugins_.callHookStr("aceAttribsToClasses", {
linestylefilter: linestylefilter,
key: key,
value: value
}, " ", " ", "");
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
}
}
2011-03-26 14:10:41 +01:00
});
return classes.substring(1);
}
var attributionIter = Changeset.opIterator(aline);
var nextOp, nextOpClasses;
2011-07-07 19:59:34 +02:00
function goNextOp()
{
2011-03-26 14:10:41 +01:00
nextOp = attributionIter.next();
nextOpClasses = (nextOp.opcode && attribsToClasses(nextOp.attribs));
}
goNextOp();
2011-07-07 19:59:34 +02:00
function nextClasses()
{
if (curIndex < lineEnd)
{
extraClasses = nextOpClasses;
leftInAuthor = nextOp.chars;
goNextOp();
while (nextOp.opcode && nextOpClasses == extraClasses)
{
leftInAuthor += nextOp.chars;
goNextOp();
}
2011-03-26 14:10:41 +01:00
}
}
nextClasses();
2011-07-07 19:59:34 +02:00
return function(txt, cls)
{
while (txt.length > 0)
{
if (leftInAuthor <= 0)
{
// prevent infinite loop if something funny's going on
return nextAfterAuthorColors(txt, cls);
}
var spanSize = txt.length;
if (spanSize > leftInAuthor)
{
spanSize = leftInAuthor;
}
var curTxt = txt.substring(0, spanSize);
txt = txt.substring(spanSize);
nextAfterAuthorColors(curTxt, (cls && cls + " ") + extraClasses);
curIndex += spanSize;
leftInAuthor -= spanSize;
if (leftInAuthor == 0)
{
nextClasses();
}
2011-03-26 14:10:41 +01:00
}
};
})();
return authorColorFunc;
};
2011-07-07 19:59:34 +02:00
linestylefilter.getAtSignSplitterFilter = function(lineText, textAndClassFunc)
{
2011-03-26 14:10:41 +01:00
var at = /@/g;
at.lastIndex = 0;
var splitPoints = null;
var execResult;
2011-07-07 19:59:34 +02:00
while ((execResult = at.exec(lineText)))
{
if (!splitPoints)
{
2011-03-26 14:10:41 +01:00
splitPoints = [];
}
splitPoints.push(execResult.index);
}
2011-07-07 19:59:34 +02:00
if (!splitPoints) return textAndClassFunc;
2011-03-26 14:10:41 +01:00
2011-07-07 19:59:34 +02:00
return linestylefilter.textAndClassFuncSplitter(textAndClassFunc, splitPoints);
2011-03-26 14:10:41 +01:00
};
2011-07-07 19:59:34 +02:00
linestylefilter.getRegexpFilter = function(regExp, tag)
{
return function(lineText, textAndClassFunc)
{
2011-03-26 14:10:41 +01:00
regExp.lastIndex = 0;
var regExpMatchs = null;
var splitPoints = null;
var execResult;
2011-07-07 19:59:34 +02:00
while ((execResult = regExp.exec(lineText)))
{
if (!regExpMatchs)
{
regExpMatchs = [];
splitPoints = [];
2011-03-26 14:10:41 +01:00
}
var startIndex = execResult.index;
var regExpMatch = execResult[0];
regExpMatchs.push([startIndex, regExpMatch]);
splitPoints.push(startIndex, startIndex + regExpMatch.length);
}
2011-07-07 19:59:34 +02:00
if (!regExpMatchs) return textAndClassFunc;
function regExpMatchForIndex(idx)
{
for (var k = 0; k < regExpMatchs.length; k++)
{
var u = regExpMatchs[k];
if (idx >= u[0] && idx < u[0] + u[1].length)
{
return u[1];
}
2011-03-26 14:10:41 +01:00
}
return false;
}
2011-07-07 19:59:34 +02:00
var handleRegExpMatchsAfterSplit = (function()
{
2011-03-26 14:10:41 +01:00
var curIndex = 0;
2011-07-07 19:59:34 +02:00
return function(txt, cls)
{
var txtlen = txt.length;
var newCls = cls;
var regExpMatch = regExpMatchForIndex(curIndex);
if (regExpMatch)
{
newCls += " " + tag + ":" + regExpMatch;
}
textAndClassFunc(txt, newCls);
curIndex += txtlen;
2011-03-26 14:10:41 +01:00
};
})();
2011-07-07 19:59:34 +02:00
return linestylefilter.textAndClassFuncSplitter(handleRegExpMatchsAfterSplit, splitPoints);
2011-03-26 14:10:41 +01:00
};
};
linestylefilter.REGEX_WORDCHAR = /[\u0030-\u0039\u0041-\u005A\u0061-\u007A\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF\u0100-\u1FFF\u3040-\u9FFF\uF900-\uFDFF\uFE70-\uFEFE\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFDC]/;
2011-07-07 19:59:34 +02:00
linestylefilter.REGEX_URLCHAR = new RegExp('(' + /[-:@a-zA-Z0-9_.,~%+\/\\?=&#;()$]/.source + '|' + linestylefilter.REGEX_WORDCHAR.source + ')');
linestylefilter.REGEX_URL = new RegExp(/(?:(?:https?|s?ftp|ftps|file|smb|afp|nfs|(x-)?man|gopher|txmt):\/\/|mailto:|www\.)/.source + linestylefilter.REGEX_URLCHAR.source + '*(?![:.,;])' + linestylefilter.REGEX_URLCHAR.source, 'g');
2011-03-26 14:10:41 +01:00
linestylefilter.getURLFilter = linestylefilter.getRegexpFilter(
2011-07-07 19:59:34 +02:00
linestylefilter.REGEX_URL, 'url');
2011-03-26 14:10:41 +01:00
2011-07-07 19:59:34 +02:00
linestylefilter.textAndClassFuncSplitter = function(func, splitPointsOpt)
{
2011-03-26 14:10:41 +01:00
var nextPointIndex = 0;
var idx = 0;
// don't split at 0
2011-07-07 19:59:34 +02:00
while (splitPointsOpt && nextPointIndex < splitPointsOpt.length && splitPointsOpt[nextPointIndex] == 0)
{
2011-03-26 14:10:41 +01:00
nextPointIndex++;
}
2011-07-07 19:59:34 +02:00
function spanHandler(txt, cls)
{
if ((!splitPointsOpt) || nextPointIndex >= splitPointsOpt.length)
{
2011-03-26 14:10:41 +01:00
func(txt, cls);
idx += txt.length;
}
2011-07-07 19:59:34 +02:00
else
{
2011-03-26 14:10:41 +01:00
var splitPoints = splitPointsOpt;
var pointLocInSpan = splitPoints[nextPointIndex] - idx;
var txtlen = txt.length;
2011-07-07 19:59:34 +02:00
if (pointLocInSpan >= txtlen)
{
func(txt, cls);
idx += txt.length;
if (pointLocInSpan == txtlen)
{
nextPointIndex++;
}
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
else
{
if (pointLocInSpan > 0)
{
func(txt.substring(0, pointLocInSpan), cls);
idx += pointLocInSpan;
}
nextPointIndex++;
// recurse
spanHandler(txt.substring(pointLocInSpan), cls);
2011-03-26 14:10:41 +01:00
}
}
}
return spanHandler;
};
2011-07-07 19:59:34 +02:00
linestylefilter.getFilterStack = function(lineText, textAndClassFunc, browser)
{
var func = linestylefilter.getURLFilter(lineText, textAndClassFunc);
2011-03-26 14:10:41 +01:00
var plugins_;
2011-07-07 19:59:34 +02:00
if (typeof(plugins) != 'undefined')
{
2011-03-26 14:10:41 +01:00
plugins_ = plugins;
2011-07-07 19:59:34 +02:00
}
else
{
2011-03-26 14:10:41 +01:00
plugins_ = parent.parent.plugins;
}
2011-07-07 19:59:34 +02:00
var hookFilters = plugins_.callHook("aceGetFilterStack", {
linestylefilter: linestylefilter,
browser: browser
});
hookFilters.map(function(hookFilter)
{
2011-03-26 14:10:41 +01:00
func = hookFilter(lineText, func);
});
2011-07-07 19:59:34 +02:00
if (browser !== undefined && browser.msie)
{
2011-03-26 14:10:41 +01:00
// IE7+ will take an e-mail address like <foo@bar.com> and linkify it to foo@bar.com.
// We then normalize it back to text with no angle brackets. It's weird. So always
// break spans at an "at" sign.
func = linestylefilter.getAtSignSplitterFilter(
2011-07-07 19:59:34 +02:00
lineText, func);
2011-03-26 14:10:41 +01:00
}
return func;
};
// domLineObj is like that returned by domline.createDomLine
2011-07-07 19:59:34 +02:00
linestylefilter.populateDomLine = function(textLine, aline, apool, domLineObj)
{
2011-03-26 14:10:41 +01:00
// remove final newline from text if any
var text = textLine;
2011-07-07 19:59:34 +02:00
if (text.slice(-1) == '\n')
{
text = text.substring(0, text.length - 1);
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
function textAndClassFunc(tokenText, tokenClass)
{
2011-03-26 14:10:41 +01:00
domLineObj.appendSpan(tokenText, tokenClass);
}
var func = linestylefilter.getFilterStack(text, textAndClassFunc);
2011-07-07 19:59:34 +02:00
func = linestylefilter.getLineStyleFilter(text.length, aline, func, apool);
2011-03-26 14:10:41 +01:00
func(text, '');
};
exports.linestylefilter = linestylefilter;