2 * Copyright (C) 2012 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Author: Debarshi Ray <debarshir@src.gnome.org>
22 var chat = document.getElementById("Chat");
25 function createHTMLNode(html) {
26 var range = document.createRange();
27 range.selectNode(chat);
28 return range.createContextualFragment(html);
32 function getContentsNode(node) {
33 var post = node.getElementsByClassName("post")[0];
34 return post.getElementsByClassName("post-contents")[0];
38 // Remove all but the last #insert
39 function removeInsertNodes(node) {
40 var inserts = node.querySelectorAll("#insert");
41 if (inserts.length < 2)
44 for (var i = 0; i < inserts.length - 1; i++) {
45 var insert = inserts[i];
46 insert.parentNode.removeChild(insert);
51 // Remove all but the first #prepend
52 function removePrependNodes(node) {
53 var pres = node.querySelectorAll("#prepend");
57 for (var i = 1; i < pres.length; i++) {
59 pre.parentNode.removeChild(pre);
64 function prepend(html) {
65 var node = createHTMLNode(html);
66 chat.insertBefore(node, chat.firstChild);
68 // The lastChild should retain the #insert
69 if (chat.children.length == 1)
72 removeInsertNodes(chat);
73 removePrependNodes(chat);
77 function prependPrev(html) {
78 var pre = chat.firstChild.querySelector("#prepend");
80 // For themes that don't support #prepend
86 var contents = pre.parentNode;
87 var node = createHTMLNode(html);
89 // Skip both the #prepend and #insert
90 for (var i = node.childNodes.length - 2; i > 0; i--)
91 contents.insertBefore(node.childNodes[i], pre.nextSibling);