mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-03 14:37:02 +03:00
Small fixes
This commit is contained in:
parent
9ac4d3ac14
commit
3b29e93db1
7 changed files with 117 additions and 279 deletions
|
|
@ -2,243 +2,44 @@
|
|||
(function() {
|
||||
|
||||
var
|
||||
|
||||
$div = $('<div></div>'),
|
||||
|
||||
/**
|
||||
* @param {string} sHtml
|
||||
* @return {string}
|
||||
*/
|
||||
htmlToPlain = function (sHtml)
|
||||
{
|
||||
var
|
||||
iPos = 0,
|
||||
iP1 = 0,
|
||||
iP2 = 0,
|
||||
iP3 = 0,
|
||||
iLimit = 0,
|
||||
|
||||
sText = '',
|
||||
|
||||
splitPlainText = function (sText)
|
||||
{
|
||||
var
|
||||
iLen = 100,
|
||||
sPrefix = '',
|
||||
sSubText = '',
|
||||
sResult = sText,
|
||||
iSpacePos = 0,
|
||||
iNewLinePos = 0
|
||||
;
|
||||
|
||||
while (sResult.length > iLen)
|
||||
{
|
||||
sSubText = sResult.substring(0, iLen);
|
||||
iSpacePos = sSubText.lastIndexOf(' ');
|
||||
iNewLinePos = sSubText.lastIndexOf('\n');
|
||||
|
||||
if (-1 !== iNewLinePos)
|
||||
{
|
||||
iSpacePos = iNewLinePos;
|
||||
}
|
||||
|
||||
if (-1 === iSpacePos)
|
||||
{
|
||||
iSpacePos = iLen;
|
||||
}
|
||||
|
||||
sPrefix += sSubText.substring(0, iSpacePos) + '\n';
|
||||
sResult = sResult.substring(iSpacePos + 1);
|
||||
}
|
||||
|
||||
return sPrefix + sResult;
|
||||
},
|
||||
|
||||
convertBlockquote = function (sText) {
|
||||
sText = splitPlainText($.trim(sText));
|
||||
sText = '> ' + sText.replace(/\n/gm, '\n> ');
|
||||
return sText.replace(/(^|\n)([> ]+)/gm, function () {
|
||||
return (arguments && 2 < arguments.length) ? arguments[1] + $.trim(arguments[2].replace(/[\s]/, '')) + ' ' : '';
|
||||
});
|
||||
},
|
||||
|
||||
convertDivs = function () {
|
||||
if (arguments && 1 < arguments.length)
|
||||
{
|
||||
var sText = $.trim(arguments[1]);
|
||||
if (0 < sText.length)
|
||||
{
|
||||
sText = sText.replace(/<div[^>]*>([\s\S\r\n]*)<\/div>/gmi, convertDivs);
|
||||
sText = '\n' + $.trim(sText) + '\n';
|
||||
}
|
||||
|
||||
return sText;
|
||||
}
|
||||
|
||||
return '';
|
||||
},
|
||||
|
||||
fixAttibuteValue = function () {
|
||||
return (arguments && 1 < arguments.length) ?
|
||||
'' + arguments[1] + arguments[2].replace(/</g, '<').replace(/>/g, '>') : '';
|
||||
},
|
||||
|
||||
convertLinks = function () {
|
||||
return (arguments && 1 < arguments.length) ? $.trim(arguments[1]) : '';
|
||||
}
|
||||
simplePlainToHtml = function (sPlain) {
|
||||
return sPlain
|
||||
.replace(/&/g, '&')
|
||||
.replace(/>/g, '>').replace(/</g, '<')
|
||||
.replace(/[\-_~]{10,}/g, '<hr />')
|
||||
.replace(/\n/g, '<br />')
|
||||
;
|
||||
|
||||
sText = sHtml
|
||||
},
|
||||
simpleHtmlToPlain = function (sHtml) {
|
||||
|
||||
var sText = sHtml
|
||||
.replace(/[\s]+/gm, ' ')
|
||||
.replace(/((?:href|data)\s?=\s?)("[^"]+?"|'[^']+?')/gmi, fixAttibuteValue)
|
||||
.replace(/<br\s?\/?>/gmi, '\n')
|
||||
.replace(/<\/h\d>/gi, '\n')
|
||||
.replace(/<\/h[\d]>/gi, '\n')
|
||||
.replace(/<\/p>/gi, '\n\n')
|
||||
.replace(/<\/li>/gi, '\n')
|
||||
.replace(/<\/td>/gi, '\n')
|
||||
.replace(/<\/tr>/gi, '\n')
|
||||
.replace(/<hr[^>]*>/gmi, '\n_______________________________\n\n')
|
||||
.replace(/<img [^>]*>/gmi, '')
|
||||
.replace(/<div[^>]*>([\s\S\r\n]*)<\/div>/gmi, convertDivs)
|
||||
.replace(/<blockquote[^>]*>/gmi, '\n__bq__start__\n')
|
||||
.replace(/<\/blockquote>/gmi, '\n__bq__end__\n')
|
||||
.replace(/<a [^>]*>([\s\S\r\n]*?)<\/a>/gmi, convertLinks)
|
||||
.replace(/<\/div>/gi, '\n')
|
||||
.replace(/<blockquote[^>]*>/gmi, '\n')
|
||||
.replace(/<\/blockquote>/gi, '\n')
|
||||
.replace(/<hr[^>]*>/gmi, '\n_______________________________\n\n')
|
||||
.replace(/ /gi, ' ')
|
||||
.replace(/"/gi, '"')
|
||||
.replace(/&/gi, '&')
|
||||
.replace(/<[^>]*>/gm, '')
|
||||
;
|
||||
|
||||
sText = $div.html(sText).text();
|
||||
sText = $('<div></div>').html(sText).text();
|
||||
|
||||
sText = sText
|
||||
.replace(/\n[ \t]+/gm, '\n')
|
||||
.replace(/[\n]{3,}/gm, '\n\n')
|
||||
.replace(/>/gi, '>')
|
||||
.replace(/</gi, '<')
|
||||
;
|
||||
|
||||
iPos = 0;
|
||||
iLimit = 100;
|
||||
|
||||
while (0 < iLimit)
|
||||
{
|
||||
iLimit--;
|
||||
iP1 = sText.indexOf('__bq__start__', iPos);
|
||||
if (-1 < iP1)
|
||||
{
|
||||
iP2 = sText.indexOf('__bq__start__', iP1 + 5);
|
||||
iP3 = sText.indexOf('__bq__end__', iP1 + 5);
|
||||
|
||||
if ((-1 === iP2 || iP3 < iP2) && iP1 < iP3)
|
||||
{
|
||||
sText = sText.substring(0, iP1) +
|
||||
convertBlockquote(sText.substring(iP1 + 13, iP3)) +
|
||||
sText.substring(iP3 + 11);
|
||||
|
||||
iPos = 0;
|
||||
}
|
||||
else if (-1 < iP2 && iP2 < iP3)
|
||||
{
|
||||
iPos = iP2 - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
iPos = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
sText = sText
|
||||
.replace(/__bq__start__/gm, '')
|
||||
.replace(/__bq__end__/gm, '')
|
||||
.replace(/&/gi, '&')
|
||||
;
|
||||
|
||||
return sText;
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} sPlain
|
||||
* @return {string}
|
||||
*/
|
||||
plainToHtml = function (sPlain)
|
||||
{
|
||||
sPlain = sPlain.toString().replace(/\r/g, '');
|
||||
|
||||
var
|
||||
bIn = false,
|
||||
bDo = true,
|
||||
bStart = true,
|
||||
aNextText = [],
|
||||
sLine = '',
|
||||
iIndex = 0,
|
||||
aText = sPlain.split("\n")
|
||||
;
|
||||
|
||||
do
|
||||
{
|
||||
bDo = false;
|
||||
aNextText = [];
|
||||
for (iIndex = 0; iIndex < aText.length; iIndex++)
|
||||
{
|
||||
sLine = aText[iIndex];
|
||||
bStart = '>' === sLine.substr(0, 1);
|
||||
if (bStart && !bIn)
|
||||
{
|
||||
bDo = true;
|
||||
bIn = true;
|
||||
aNextText.push('~~~blockquote~~~');
|
||||
aNextText.push(sLine.substr(1));
|
||||
}
|
||||
else if (!bStart && bIn)
|
||||
{
|
||||
bIn = false;
|
||||
aNextText.push('~~~/blockquote~~~');
|
||||
aNextText.push(sLine);
|
||||
}
|
||||
else if (bStart && bIn)
|
||||
{
|
||||
aNextText.push(sLine.substr(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
aNextText.push(sLine);
|
||||
}
|
||||
}
|
||||
|
||||
if (bIn)
|
||||
{
|
||||
bIn = false;
|
||||
aNextText.push('~~~/blockquote~~~');
|
||||
}
|
||||
|
||||
aText = aNextText;
|
||||
}
|
||||
while (bDo);
|
||||
|
||||
sPlain = aText.join("\n");
|
||||
|
||||
sPlain = sPlain
|
||||
.replace(/&/g, '&')
|
||||
.replace(/>/g, '>').replace(/</g, '<')
|
||||
.replace(/~~~blockquote~~~[\s]*/g, '<blockquote>')
|
||||
.replace(/[\s]*~~~\/blockquote~~~/g, '</blockquote>')
|
||||
.replace(/[\-_~]{10,}/g, '<hr />')
|
||||
.replace(/\n/g, '<br />');
|
||||
|
||||
if ($.fn && $.fn.linkify)
|
||||
{
|
||||
sPlain = $div.html(sPlain)
|
||||
.linkify().find('.linkified').removeClass('linkified').end()
|
||||
.html();
|
||||
}
|
||||
|
||||
return sPlain;
|
||||
}
|
||||
;
|
||||
|
||||
|
|
@ -322,7 +123,9 @@
|
|||
base: CKEDITOR.editable,
|
||||
proto: {
|
||||
setData: function(data) {
|
||||
this.setValue(htmlToPlain(data));
|
||||
this.setValue(window.rainloop_Utils_htmlToPlain ?
|
||||
window.rainloop_Utils_htmlToPlain(data) : simpleHtmlToPlain(data));
|
||||
|
||||
this.editor.fire('dataReady');
|
||||
},
|
||||
setRawData: function(data) {
|
||||
|
|
@ -330,7 +133,8 @@
|
|||
this.editor.fire('dataReady');
|
||||
},
|
||||
getData: function() {
|
||||
return plainToHtml(this.getValue());
|
||||
return window.rainloop_Utils_plainToHtml ?
|
||||
window.rainloop_Utils_plainToHtml(this.getValue(), true) : simplePlainToHtml(this.getValue());
|
||||
},
|
||||
getRawData: function() {
|
||||
return this.getValue();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue