HTMLInterchange.cpp [plain text]
#include "config.h"
#include "HTMLInterchange.h"
#include "RenderObject.h"
#include "Text.h"
#include "TextIterator.h"
#include <wtf/StdLibExtras.h>
#include <wtf/unicode/CharacterNames.h>
namespace WebCore {
namespace {
String convertedSpaceString()
{
DEFINE_STATIC_LOCAL(String, convertedSpaceString, ());
if (convertedSpaceString.isNull()) {
convertedSpaceString = "<span class=\"";
convertedSpaceString += AppleConvertedSpace;
convertedSpaceString += "\">";
convertedSpaceString.append(noBreakSpace);
convertedSpaceString += "</span>";
}
return convertedSpaceString;
}
}
String convertHTMLTextToInterchangeFormat(const String& in, const Text* node)
{
if (node->renderer() && node->renderer()->style()->preserveNewline())
return in;
Vector<UChar> s;
unsigned i = 0;
unsigned consumed = 0;
while (i < in.length()) {
consumed = 1;
if (isCollapsibleWhitespace(in[i])) {
unsigned j = i + 1;
while (j < in.length() && isCollapsibleWhitespace(in[j]))
j++;
unsigned count = j - i;
consumed = count;
while (count) {
unsigned add = count % 3;
switch (add) {
case 0:
append(s, convertedSpaceString());
s.append(' ');
append(s, convertedSpaceString());
add = 3;
break;
case 1:
if (i == 0 || i + 1 == in.length()) append(s, convertedSpaceString());
else
s.append(' ');
break;
case 2:
if (i == 0) {
append(s, convertedSpaceString());
s.append(' ');
} else if (i + 2 == in.length()) {
append(s, convertedSpaceString());
append(s, convertedSpaceString());
} else {
append(s, convertedSpaceString());
s.append(' ');
}
break;
}
count -= add;
}
} else
s.append(in[i]);
i += consumed;
}
return String::adopt(s);
}
}