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

This commit is contained in:
John McLear 2013-12-10 19:25:36 +00:00
commit 58cd71dcce

View file

@ -21,7 +21,6 @@ var padManager = require("../db/PadManager");
var ERR = require("async-stacktrace"); var ERR = require("async-stacktrace");
var Security = require('ep_etherpad-lite/static/js/security'); var Security = require('ep_etherpad-lite/static/js/security');
var hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks'); var hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks');
var getPadPlainText = require('./ExportHelper').getPadPlainText;
var _analyzeLine = require('./ExportHelper')._analyzeLine; var _analyzeLine = require('./ExportHelper')._analyzeLine;
var _encodeWhitespace = require('./ExportHelper')._encodeWhitespace; var _encodeWhitespace = require('./ExportHelper')._encodeWhitespace;
@ -79,6 +78,10 @@ function getHTMLFromAtext(pad, atext, authorColors)
var tags = ['h1', 'h2', 'strong', 'em', 'u', 's']; var tags = ['h1', 'h2', 'strong', 'em', 'u', 's'];
var props = ['heading1', 'heading2', 'bold', 'italic', 'underline', 'strikethrough']; 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 anumMap = {};
var css = ""; var css = "";
@ -117,6 +120,8 @@ function getHTMLFromAtext(pad, atext, authorColors)
css+="</style>"; 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) props.forEach(function (propName, i)
{ {
var propTrueNum = apool.putAttrib([propName, true], true); var propTrueNum = apool.putAttrib([propName, true], true);
@ -128,11 +133,6 @@ function getHTMLFromAtext(pad, atext, authorColors)
function getLineHTML(text, attribs) 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 // Use order of tags (b/i/u) as order of nesting, for simplicity
// and decent nesting. For example, // and decent nesting. For example,
// <b>Just bold<b> <b><i>Bold and italics</i></b> <i>Just italics</i> // <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) function emitCloseTag(i)
{ {
openTags.shift(); 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 urls = _findURLs(text);
var idx = 0; var idx = 0;
@ -219,92 +204,51 @@ function getHTMLFromAtext(pad, atext, authorColors)
var iter = Changeset.opIterator(Changeset.subattribution(attribs, idx, idx + numChars)); var iter = Changeset.opIterator(Changeset.subattribution(attribs, idx, idx + numChars));
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()) while (iter.hasNext())
{ {
var o = iter.next(); var o = iter.next();
var propChanged = false; var usedAttribs = [];
// mark all attribs as used
Changeset.eachAttribNumber(o.attribs, function (a) Changeset.eachAttribNumber(o.attribs, function (a)
{ {
if (a in anumMap) if (a in anumMap)
{ {
var i = anumMap[a]; // i = 0 => bold, etc. usedAttribs.push(anumMap[a]); // i = 0 => bold, etc.
if (!propVals[i])
{
propVals[i] = ENTER;
propChanged = true;
}
else
{
propVals[i] = STAY;
}
} }
}); });
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; outermostTag = i;
propChanged = true; break;
}
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 // close all tags upto the outer most
if (propChanged) if (outermostTag != -1)
{ {
// leaving bold (e.g.) also leaves italics, etc. while ( outermostTag >= 0 )
var left = false;
for (var i = 0; i < propVals.length; i++)
{ {
var v = propVals[i]; emitCloseTag(openTags[0]);
if (!left) outermostTag--;
{
if (v === LEAVE)
{
left = true;
}
}
else
{
if (v === true)
{
propVals[i] = STAY; // tag will be closed and re-opened
}
}
} }
}
var tags2close = []; // open all tags that are used but not open
for (i=0; i < usedAttribs.length; i++)
for (var i = propVals.length - 1; i >= 0; i--) {
if (openTags.indexOf(usedAttribs[i]) === -1)
{ {
if (propVals[i] === LEAVE) emitOpenTag(usedAttribs[i])
{
//emitCloseTag(i);
tags2close.push(i);
propVals[i] = false;
}
else if (propVals[i] === STAY)
{
//emitCloseTag(i);
tags2close.push(i);
}
} }
}
orderdCloseTags(tags2close);
for (var i = 0; i < propVals.length; i++)
{
if (propVals[i] === ENTER || propVals[i] === STAY)
{
emitOpenTag(i);
propVals[i] = true;
}
}
// propVals is now all {true,false} again
} // end if (propChanged)
var chars = o.chars; var chars = o.chars;
if (o.lines) if (o.lines)
{ {
@ -320,17 +264,11 @@ function getHTMLFromAtext(pad, atext, authorColors)
assem.append(_encodeWhitespace(Security.escapeHTML(s))); assem.append(_encodeWhitespace(Security.escapeHTML(s)));
} // end iteration over spans in line } // end iteration over spans in line
var tags2close = []; // close all the tags that are open after the last op
for (var i = propVals.length - 1; i >= 0; i--) while (openTags.length > 0)
{ {
if (propVals[i]) emitCloseTag(openTags[0])
{
tags2close.push(i);
propVals[i] = false;
}
} }
orderdCloseTags(tags2close);
} // end processNextChars } // end processNextChars
if (urls) if (urls)
{ {
@ -573,8 +511,8 @@ function _processSpaces(s){
} }
} }
// beginning of line is nbsp // beginning of line is nbsp
for (var i = 0; i < parts.length; i++){ for (i = 0; i < parts.length; i++){
var p = parts[i]; p = parts[i];
if (p == " "){ if (p == " "){
parts[i] = '&nbsp;'; parts[i] = '&nbsp;';
break; break;
@ -586,8 +524,8 @@ function _processSpaces(s){
} }
else else
{ {
for (var i = 0; i < parts.length; i++){ for (i = 0; i < parts.length; i++){
var p = parts[i]; p = parts[i];
if (p == " "){ if (p == " "){
parts[i] = '&nbsp;'; parts[i] = '&nbsp;';
} }