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])); } }
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; }
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])); } }