Beispiel #1
0
// FIXME: Can't really do this work without taking whitespace mode into account.
// This means that eventually this function needs to be eliminated or at least have
// its parameters changed because it can't do its work on the string without knowing
// what parts are in what whitespace mode.
DeprecatedString convertHTMLTextToInterchangeFormat(const DeprecatedString &in)
{
    DeprecatedString s;

    unsigned int i = 0;
    unsigned int consumed = 0;
    while (i < in.length()) {
        consumed = 1;
        if (isCollapsibleWhitespace(in[i].unicode())) {
            // count number of adjoining spaces
            unsigned int j = i + 1;
            while (j < in.length() && isCollapsibleWhitespace(in[j].unicode()))
                j++;
            unsigned int count = j - i;
            consumed = count;
            while (count) {
                unsigned int add = count % 3;
                switch (add) {
                    case 0:
                        s += convertedSpaceString();
                        s += ' ';
                        s += convertedSpaceString();
                        add = 3;
                        break;
                    case 1:
                        if (i == 0 || i + 1 == in.length()) // at start or end of string
                            s += convertedSpaceString();
                        else
                            s += ' ';
                        break;
                    case 2:
                        if (i == 0) {
                             // at start of string
                            s += convertedSpaceString();
                            s += ' ';
                        }
                        else if (i + 2 == in.length()) {
                             // at end of string
                            s += convertedSpaceString();
                            s += convertedSpaceString();
                        }
                        else {
                            s += convertedSpaceString();
                            s += ' ';
                        }
                        break;
                }
                count -= add;
            }
        }
        else {
            s += in[i];
        }
        i += consumed;
    }

    return s;
}
String convertHTMLTextToInterchangeFormat(const String& in, const Text* node)
{
    // Assume all the text comes from 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])) {
            // count number of adjoining spaces
            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()) // at start or end of string
                            append(s, convertedSpaceString());
                        else
                            s.append(' ');
                        break;
                    case 2:
                        if (i == 0) {
                             // at start of string
                            append(s, convertedSpaceString());
                            s.append(' ');
                        } else if (i + 2 == in.length()) {
                             // at end of string
                            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);
}
String convertHTMLTextToInterchangeFormat(const String& in, const Text& node)
{
    // Assume all the text comes from node.
    if (node.layoutObject() && node.layoutObject()->style()->preserveNewline())
        return in;

    const char convertedSpaceString[] = "<span class=\"" AppleConvertedSpace "\">\xA0</span>";
    static_assert((static_cast<unsigned char>('\xA0') == noBreakSpaceCharacter), "\\xA0 should be non-breaking space");

    StringBuilder s;

    unsigned i = 0;
    unsigned consumed = 0;
    while (i < in.length()) {
        consumed = 1;
        if (isCollapsibleWhitespace(in[i])) {
            // count number of adjoining spaces
            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:
                    s.append(convertedSpaceString);
                    s.append(' ');
                    s.append(convertedSpaceString);
                    add = 3;
                    break;
                case 1:
                    if (i == 0 || i + 1 == in.length()) // at start or end of string
                        s.append(convertedSpaceString);
                    else
                        s.append(' ');
                    break;
                case 2:
                    if (i == 0) {
                        // at start of string
                        s.append(convertedSpaceString);
                        s.append(' ');
                    } else if (i + 2 == in.length()) {
                        // at end of string
                        s.append(convertedSpaceString);
                        s.append(convertedSpaceString);
                    } else {
                        s.append(convertedSpaceString);
                        s.append(' ');
                    }
                    break;
                }
                count -= add;
            }
        } else {
            s.append(in[i]);
        }
        i += consumed;
    }

    return s.toString();
}
Position Position::trailingWhitespacePosition(EAffinity affinity, bool considerNonCollapsibleWhitespace) const
{
    if (isNull())
        return Position();

    if (node()->isTextNode()) {
        TextImpl *textNode = static_cast<TextImpl *>(node());
        if (offset() < (long)textNode->length()) {
            DOMString string = static_cast<TextImpl *>(node())->data();
            const QChar &c = string[offset()];
            if (considerNonCollapsibleWhitespace ? (c.isSpace() || c.unicode() == 0xa0) : isCollapsibleWhitespace(c))
                return *this;
            return Position();
        }
    }

    if (downstream(StayInBlock).node()->id() == ID_BR)
        return Position();

    Position next = nextCharacterPosition(affinity);
    if (next != *this && next.node()->inSameContainingBlockFlowElement(node()) && next.node()->isTextNode()) {
        DOMString string = static_cast<TextImpl *>(next.node())->data();
        const QChar &c = string[0];
        if (considerNonCollapsibleWhitespace ? (c.isSpace() || c.unicode() == 0xa0) : isCollapsibleWhitespace(c))
            return next;
    }

    return Position();
}
Position Position::leadingWhitespacePosition(EAffinity affinity, bool considerNonCollapsibleWhitespace) const
{
    if (isNull())
        return Position();
    
    if (upstream(StayInBlock).node()->id() == ID_BR)
        return Position();

    Position prev = previousCharacterPosition(affinity);
    if (prev != *this && prev.node()->inSameContainingBlockFlowElement(node()) && prev.node()->isTextNode()) {
        DOMString string = static_cast<TextImpl *>(prev.node())->data();
        const QChar &c = string[prev.offset()];
        if (considerNonCollapsibleWhitespace ? (c.isSpace() || c.unicode() == 0xa0) : isCollapsibleWhitespace(c))
            return prev;
    }

    return Position();
}