示例#1
0
void HtmlParser::mergeTags()
{
    QFETCH(QString, base);
    QFETCH(QString, add);
    QFETCH(QString, expect);

    THtmlParser actual = THtmlParser::mergeElements(base, add);
    QCOMPARE(actual.toString(), expect);
}
void THtmlParser::prepend(int parent, const THtmlParser &parser)
{
    if (parser.elementCount() <= 1)
        return;

    THtmlElement &e = insertNewElement(parent, 0);
    e.tag = parser.at(1).tag;
    e.attributes = parser.at(1).attributes;
    e.text = parser.at(1).text;
    e.selfCloseMark = parser.at(1).selfCloseMark;
    e.tagClosed = parser.at(1).tagClosed;
    int idx = lastIndex();
    for (int i = 0; i < parser.at(1).children.count(); ++i) {
        prepend(idx, parser.mid(parser.at(1).children[i]));
    }
}
void THtmlParser::merge(const THtmlParser &other)
{
    if (elementCount() <= 1 || other.elementCount() <= 1 || at(1).tag != other.at(1).tag) {
        return;
    }
    
    // Adds attributes
    for (int i = 0; i < other.at(1).attributes.count(); ++i) {
        const QPair<QString, QString> &p = other.at(1).attributes[i];
        at(1).setAttribute(p.first, THttpUtility::trimmedQuotes(p.second));
    }

    if (!other.at(1).text.isEmpty()
        || (at(1).children.isEmpty() && !other.at(1).children.isEmpty())) {
        at(1).text = other.at(1).text;
    }

    // Merges the elements
    for (int i = 0; i < other.at(1).children.count(); ++i) {
        prepend(1, other.mid(other.at(1).children[i]));
    }
}
示例#4
0
void TestTfpconverter::parse()
{
    QFETCH(QString, fileName);

    QFile file(fileName);
    QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text));
    QTextStream ts(&file);
    ts.setCodec("UTF-8");
    QString html = ts.readAll();

    THtmlParser parser;
    parser.parse(html);
    QString result = parser.toString();

    QFile res("result.phtm");
    res.open(QIODevice::WriteOnly | QIODevice::Truncate);
    QTextStream rests(&res);
    rests.setCodec("UTF-8");
    rests << result;
    res.close();

    QCOMPARE(html, result);
}
THtmlParser THtmlParser::mid(int index) const
{
    THtmlParser res;
    if (at(index).isEndElement()) {
        res.elements << elements[index];
        res.at(0).children.append(1);
        res.at(1).parent = 0;
    } else {
        res.elements += elements.mid(index);
        res.at(0).children.append(1);
        int d = index - 1;
        for (int i = 1; i < res.elementCount(); ++i) {
            res.at(i).parent -= d;
            for (int j = 0; j < res.at(i).children.count(); ++j) {
                res.at(i).children[j] -= d;
            }
        }
    }
    return res;
}
示例#6
0
void HtmlParser::prepend()
{
    QFETCH(QString, base);
    QFETCH(QString, add);
    QFETCH(QString, expect);

    THtmlParser basep;
    basep.parse(base);
    THtmlParser addp;
    addp.parse(add);
    
    basep.prepend(1, addp);
    QString actual = basep.toString();
    QCOMPARE(actual, expect);
}