1 /* 2 * %kadu copyright begin% 3 * Copyright 2012 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com) 4 * %kadu copyright end% 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include <QtCore/QList> 21 #include <QtXml/QDomText> 22 23 #include "dom-text-regexp-visitor.h" 24 DomTextRegexpVisitor(QRegExp regExp)25DomTextRegexpVisitor::DomTextRegexpVisitor(QRegExp regExp) : 26 RegExp(regExp) 27 { 28 } 29 ~DomTextRegexpVisitor()30DomTextRegexpVisitor::~DomTextRegexpVisitor() 31 { 32 } 33 expandFirstMatch(QDomText textNode) const34QDomText DomTextRegexpVisitor::expandFirstMatch(QDomText textNode) const 35 { 36 auto text = textNode.nodeValue(); 37 auto index = RegExp.indexIn(text); 38 if (index < 0) 39 return QDomText(); 40 41 auto length = RegExp.matchedLength(); 42 43 auto afterMatch = textNode.splitText(index + length); 44 textNode.setNodeValue(textNode.nodeValue().mid(0, index)); 45 46 auto newNodes = matchToDomNodes(textNode.ownerDocument(), RegExp); 47 for (auto newNode : newNodes) 48 textNode.parentNode().insertBefore(newNode, afterMatch); 49 50 return afterMatch; 51 } 52 visit(QDomText textNode) const53QDomNode DomTextRegexpVisitor::visit(QDomText textNode) const 54 { 55 auto result = textNode; 56 while (!textNode.isNull()) 57 { 58 result = textNode; 59 textNode = expandFirstMatch(textNode); 60 } 61 62 return result; 63 } 64 beginVisit(QDomElement elementNode) const65QDomNode DomTextRegexpVisitor::beginVisit(QDomElement elementNode) const 66 { 67 return elementNode; 68 } 69 endVisit(QDomElement elementNode) const70QDomNode DomTextRegexpVisitor::endVisit(QDomElement elementNode) const 71 { 72 return elementNode.nextSibling(); 73 } 74