QDomText EmoticonExpander::insertEmoticon(QDomText textNode, const Emoticon &emoticon, int index) const { int emoticonLength = emoticon.triggerText().length(); QDomText afterEmoticon = textNode.splitText(index + emoticonLength); textNode.setNodeValue(textNode.nodeValue().mid(0, index)); QDomElement emoticonElement = textNode.ownerDocument().createElement("img"); emoticonElement.setAttribute("emoticon", emoticon.triggerText()); emoticonElement.setAttribute("title", emoticon.triggerText()); emoticonElement.setAttribute("alt", emoticon.triggerText()); emoticonElement.setAttribute("src", "file:///" + m_pathProvider->emoticonPath(emoticon)); textNode.parentNode().insertBefore(emoticonElement, afterEmoticon); return afterEmoticon; }
void EmoticonPrefixTreeBuilder::addEmoticon(const Emoticon &emoticon) { Q_ASSERT(Root); QString text = emoticon.triggerText().toLower(); int length = text.length(); EmoticonPrefixTree *node = Root.data(); for (int i = 0; i < length; i++) { QChar c = extractLetter(text.at(i)); EmoticonPrefixTree *child = node->child(c); if (!child) child = node->createChild(c); node = child; } if (node->nodeEmoticon().isNull()) node->setNodeEmoticon(emoticon); }
QDomText EmoticonExpander::expandFirstEmoticon(QDomText textNode) const { QString text = textNode.nodeValue().toLower(); int textLength = text.length(); if (0 == textLength) return QDomText(); int currentEmoticonStart = -1; Emoticon currentEmoticon; EmoticonWalker walker(m_tree); for (int i = 0; i < textLength; i++) { Emoticon emoticon = walker.matchEmoticon(text.at(i), (i < textLength - 1) && text.at(i + 1).isLetter()); if (emoticon.isNull()) continue; // TODO: remove this dependency int emoticonStart = i - emoticon.triggerText().length() + 1; if (currentEmoticon.isNull() || currentEmoticonStart >= emoticonStart) { currentEmoticon = emoticon; currentEmoticonStart = emoticonStart; continue; } return insertEmoticon(textNode, currentEmoticon, currentEmoticonStart); } if (!currentEmoticon.isNull()) insertEmoticon(textNode, currentEmoticon, currentEmoticonStart); return QDomText(); }