Merge branch 'fix-exporthtml-styling-tags' of https://github.com/webzwo0i/etherpad-lite into develop

pull/2041/head
John McLear 2013-12-10 19:25:36 +00:00
commit 58cd71dcce
1 changed files with 58 additions and 120 deletions

View File

@ -21,7 +21,6 @@ var padManager = require("../db/PadManager");
var ERR = require("async-stacktrace");
var Security = require('ep_etherpad-lite/static/js/security');
var hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks');
var getPadPlainText = require('./ExportHelper').getPadPlainText;
var _analyzeLine = require('./ExportHelper')._analyzeLine;
var _encodeWhitespace = require('./ExportHelper')._encodeWhitespace;
@ -79,6 +78,10 @@ function getHTMLFromAtext(pad, atext, authorColors)
var tags = ['h1', 'h2', 'strong', 'em', 'u', 's'];
var props = ['heading1', 'heading2', 'bold', 'italic', 'underline', 'strikethrough'];
// holds a map of used styling attributes (*1, *2, etc) in the apool
// and maps them to an index in props
// *3:2 -> the attribute *3 means strong
// *2:5 -> the attribute *2 means s(trikethrough)
var anumMap = {};
var css = "";
@ -117,6 +120,8 @@ function getHTMLFromAtext(pad, atext, authorColors)
css+="</style>";
}
// iterates over all props(h1,h2,strong,...), checks if it is used in
// this pad, and if yes puts its attrib id->props value into anumMap
props.forEach(function (propName, i)
{
var propTrueNum = apool.putAttrib([propName, true], true);
@ -128,11 +133,6 @@ function getHTMLFromAtext(pad, atext, authorColors)
function getLineHTML(text, attribs)
{
var propVals = [false, false, false];
var ENTER = 1;
var STAY = 2;
var LEAVE = 0;
// Use order of tags (b/i/u) as order of nesting, for simplicity
// and decent nesting. For example,
// <b>Just bold<b> <b><i>Bold and italics</i></b> <i>Just italics</i>
@ -175,6 +175,7 @@ function getHTMLFromAtext(pad, atext, authorColors)
}
}
// this closes an open tag and removes its reference from openTags
function emitCloseTag(i)
{
openTags.shift();
@ -189,22 +190,6 @@ function getHTMLFromAtext(pad, atext, authorColors)
}
}
function orderdCloseTags(tags2close)
{
for(var i=0;i<openTags.length;i++)
{
for(var j=0;j<tags2close.length;j++)
{
if(tags2close[j] == openTags[i])
{
emitCloseTag(tags2close[j]);
i--;
break;
}
}
}
}
var urls = _findURLs(text);
var idx = 0;
@ -219,92 +204,51 @@ function getHTMLFromAtext(pad, atext, authorColors)
var iter = Changeset.opIterator(Changeset.subattribution(attribs, idx, idx + numChars));
idx += numChars;
// this iterates over every op string and decides which tags to open or to close
// based on the attribs used
while (iter.hasNext())
{
var o = iter.next();
var propChanged = false;
var usedAttribs = [];
// mark all attribs as used
Changeset.eachAttribNumber(o.attribs, function (a)
{
if (a in anumMap)
{
var i = anumMap[a]; // i = 0 => bold, etc.
if (!propVals[i])
{
propVals[i] = ENTER;
propChanged = true;
}
else
{
propVals[i] = STAY;
}
usedAttribs.push(anumMap[a]); // i = 0 => bold, etc.
}
});
for (var i = 0; i < propVals.length; i++)
var outermostTag = -1;
// find the outer most open tag that is no longer used
for (var i = openTags.length - 1; i >= 0; i--)
{
if (propVals[i] === true)
if (usedAttribs.indexOf(openTags[i]) === -1)
{
propVals[i] = LEAVE;
propChanged = true;
}
else if (propVals[i] === STAY)
{
propVals[i] = true; // set it back
}
}
// now each member of propVal is in {false,LEAVE,ENTER,true}
// according to what happens at start of span
if (propChanged)
{
// leaving bold (e.g.) also leaves italics, etc.
var left = false;
for (var i = 0; i < propVals.length; i++)
{
var v = propVals[i];
if (!left)
{
if (v === LEAVE)
{
left = true;
}
}
else
{
if (v === true)
{
propVals[i] = STAY; // tag will be closed and re-opened
}
}
}
var tags2close = [];
for (var i = propVals.length - 1; i >= 0; i--)
{
if (propVals[i] === LEAVE)
{
//emitCloseTag(i);
tags2close.push(i);
propVals[i] = false;
}
else if (propVals[i] === STAY)
{
//emitCloseTag(i);
tags2close.push(i);
outermostTag = i;
break;
}
}
orderdCloseTags(tags2close);
// close all tags upto the outer most
if (outermostTag != -1)
{
while ( outermostTag >= 0 )
{
emitCloseTag(openTags[0]);
outermostTag--;
}
}
for (var i = 0; i < propVals.length; i++)
// open all tags that are used but not open
for (i=0; i < usedAttribs.length; i++)
{
if (propVals[i] === ENTER || propVals[i] === STAY)
if (openTags.indexOf(usedAttribs[i]) === -1)
{
emitOpenTag(i);
propVals[i] = true;
emitOpenTag(usedAttribs[i])
}
}
// propVals is now all {true,false} again
} // end if (propChanged)
var chars = o.chars;
if (o.lines)
{
@ -320,17 +264,11 @@ function getHTMLFromAtext(pad, atext, authorColors)
assem.append(_encodeWhitespace(Security.escapeHTML(s)));
} // end iteration over spans in line
var tags2close = [];
for (var i = propVals.length - 1; i >= 0; i--)
// close all the tags that are open after the last op
while (openTags.length > 0)
{
if (propVals[i])
{
tags2close.push(i);
propVals[i] = false;
emitCloseTag(openTags[0])
}
}
orderdCloseTags(tags2close);
} // end processNextChars
if (urls)
{
@ -573,8 +511,8 @@ function _processSpaces(s){
}
}
// beginning of line is nbsp
for (var i = 0; i < parts.length; i++){
var p = parts[i];
for (i = 0; i < parts.length; i++){
p = parts[i];
if (p == " "){
parts[i] = '&nbsp;';
break;
@ -586,8 +524,8 @@ function _processSpaces(s){
}
else
{
for (var i = 0; i < parts.length; i++){
var p = parts[i];
for (i = 0; i < parts.length; i++){
p = parts[i];
if (p == " "){
parts[i] = '&nbsp;';
}