comparison atom.js @ 13:161d4ea1c3f8

Migration of the client-side to XMPP.js instead of Strophe.js. Drop BOSH support and add WebSockets support. The server-side is untested, may be broken.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 03 Nov 2011 14:23:10 -0700
parents 360186772aa3
children 03be0717d3f8
comparison
equal deleted inserted replaced
12:d67380657687 13:161d4ea1c3f8
11 if (!document.createTextNode) 11 if (!document.createTextNode)
12 document.createTextNode = function(text) { 12 document.createTextNode = function(text) {
13 return text; 13 return text;
14 }; 14 };
15 15
16 parsers[ns.atom] = function(id, xml) { 16 parsers[ns.atom] = function(item) {
17 var toDate = function(atom) { 17 var toDate = function(atom) {
18 var d = new Date;
19
18 if (!atom) 20 if (!atom)
19 return new Date; 21 return d;
20 22
21 var last = atom.getChild('updated'); 23 try {
22 if (!last) { 24 var last = atom.getElementsByTagNameNS(ns.atom, 'updated')[0].textContent;
23 last = atom.getChild('published'); 25 } catch (e) {
24 if (!last) 26 try {
25 return new Date; 27 var last = atom.getElementsByTagNameNS(ns.atom, 'published')[0].textContent;
28 } catch (e) {
29 return d;
30 }
26 } 31 }
27 32
28 // var d = new Date(last); // FIXME: don't work in obsolete browsers 33 // var d = new Date(last); // FIXME: don't work in obsolete browsers
29 var d = new Date;
30 d.set8601(last); 34 d.set8601(last);
31 35
32 return d; 36 return d;
33 }; 37 };
34 38
35 var toHTML = function(atom, date) { 39 var toHTML = function(item, date) {
40 var atom = item.payload;
41 var id = item.id;
42
36 var article = document.createElementNS(ns.xhtml, 'article'); 43 var article = document.createElementNS(ns.xhtml, 'article');
37 44
38 article.setAttributeNS(ns.e, 'id', id); 45 article.setAttributeNS(ns.e, 'id', id);
39 var d8601 = date.to8601(); 46 var d8601 = date.to8601();
40 article.setAttributeNS(ns.e, 'date', d8601); 47 article.setAttributeNS(ns.e, 'date', d8601);
41 48
42 var aside = document.createElementNS(ns.xhtml, 'aside'); 49 var aside = document.createElementNS(ns.xhtml, 'aside');
43 article.appendChild(aside); 50 article.appendChild(aside);
44 51
45 var atomTitle = atom.getChild('title'); 52 try {
46 if (atomTitle) { 53 var atomTitle = atom.getElementsByTagNameNS(ns.atom, 'title')[0].textContent;
47 var title = document.createElementNS(ns.xhtml, 'h2'); 54 var title = document.createElementNS(ns.xhtml, 'h2');
48 title.appendChild(document.createTextNode(atomTitle)); 55 title.appendChild(document.createTextNode(atomTitle));
49 article.appendChild(title); 56 article.appendChild(title);
57 } catch (e) {
50 } 58 }
51 59
52 var footer = document.createElementNS(ns.xhtml, 'footer'); 60 var footer = document.createElementNS(ns.xhtml, 'footer');
53 61
54 var atomAuthor = atom.getChild('author', ns.atom); 62 try {
55 if (atomAuthor) { 63 var atomAuthor = atom.getElementsByTagNameNS(ns.atom, 'author')[0];
56 var atomName = atomAuthor.getChild('name'); 64 try {
65 var atomName = atomAuthor.getElementsByTagNameNS(ns.atom, 'name')[0].textContent;
66 } catch (e) {
67 }
57 68
58 var atomURI = atomAuthor.getChild('uri');
59 var cite = document.createElementNS(ns.xhtml, 'cite'); 69 var cite = document.createElementNS(ns.xhtml, 'cite');
60 if (atomURI) { 70 try {
71 var atomURI = atomAuthor.getElementsByTagNameNS(ns.atom, 'uri')[0].textContent;
61 var a = document.createElementNS(ns.xhtml, 'a'); 72 var a = document.createElementNS(ns.xhtml, 'a');
62 a.href = atomURI; 73 a.href = atomURI;
63 var atomJID = new JID; 74 var atomJID = new JID;
64 atomJID.uri = atomURI; 75 atomJID.uri = atomURI;
65 76
67 cite.appendChild(a); 78 cite.appendChild(a);
68 79
69 var img = document.createElementNS(ns.xhtml, 'img'); 80 var img = document.createElementNS(ns.xhtml, 'img');
70 img.src = config.avatarRoot + atomJID.bare; 81 img.src = config.avatarRoot + atomJID.bare;
71 aside.appendChild(img); 82 aside.appendChild(img);
72 } else 83 } catch (e) {
73 cite.appendChild(document.createTextNode(atomName)); 84 cite.appendChild(document.createTextNode(atomName));
85 }
74 86
75 footer.appendChild(document.createTextNode('By ')); 87 footer.appendChild(document.createTextNode('By '));
76 footer.appendChild(cite); 88 footer.appendChild(cite);
77 89
78 var atomEmail = atomAuthor.getChild('email'); 90 try {
79 if (atomEmail) { 91 var atomEmail = atomAuthor.getElementsByTagNameNS(ns.atom, 'email')[0].textContent;
80 footer.appendChild(document.createTextNode(' (')); 92 footer.appendChild(document.createTextNode(' ('));
81 var a = document.createElementNS(ns.xhtml, 'a'); 93 var a = document.createElementNS(ns.xhtml, 'a');
82 a.href = 'mailto:' + atomEmail; 94 a.href = 'mailto:' + atomEmail;
83 a.appendChild(document.createTextNode('email')); 95 a.appendChild(document.createTextNode('email'));
84 footer.appendChild(a); 96 footer.appendChild(a);
85 footer.appendChild(document.createTextNode(')')); 97 footer.appendChild(document.createTextNode(')'));
98 } catch (e) {
86 } 99 }
87 100
88 article.appendChild(footer); 101 article.appendChild(footer);
102 } catch (e) {
89 } 103 }
90 104
91 footer.innerHTML += ', <time datetime="' + d8601 + '">' + date.getRelative() + '</time>'; 105 footer.innerHTML += ', <time datetime="' + d8601 + '">' + date.getRelative() + '</time>';
92 106
93 var atomSummary = atom.getChild('summary'); 107 try {
94 if (atomSummary) { 108 var atomSummary = atomAuthor.getElementsByTagNameNS(ns.atom, 'summary')[0].textContent;
95 var p = document.createElementNS(ns.xhtml, 'p'); 109 var p = document.createElementNS(ns.xhtml, 'p');
96 p.appendChild(document.createTextNode(atomSummary)); 110 p.appendChild(document.createTextNode(atomSummary));
97 article.appendChild(p); 111 article.appendChild(p);
112 } catch (e) {
98 } 113 }
99 114
100 var atomContent = atom.getChild('content'); 115 try {
101 if (atomContent) { 116 var atomContent = atom.getElementsByTagNameNS(ns.atom, 'content')[0];
102 var contentType = atomContent.getAttribute('type'); 117 var contentType = atomContent.getAttributeNS(null, 'type');
103 if (/^text$/.test(contentType)) { 118 if (/^text$/.test(contentType)) {
104 var p = document.createElementNS(ns.xhtml, 'p'); 119 var p = document.createElementNS(ns.xhtml, 'p');
105 p.appendChild(document.createTextNode(atomContent.getText())); 120 p.appendChild(document.createTextNode(atomContent.textContent));
106 article.appendChild(p); 121 article.appendChild(p);
107 } else if (/^html$/.test(contentType)) { 122 } else if (/^html$/.test(contentType)) {
108 article.insertAdjacentHTML('beforeend', atomContent.getText()); // FIXME: could be not-well-formed. 123 article.insertAdjacentHTML('beforeend', atomContent.textContent); // FIXME: could be not-well-formed.
109 } else if (/^xhtml$/.test(contentType)) { 124 } else if (/^xhtml$/.test(contentType)) {
110 // TODO: use a better xml2json lib that allow to json2xml 125 var div = atomContent.firstChild;
111 /*var div = atomContent.getChild(); 126 var children = div.childNodes;
112 article.appendChild(div.innerXml());*/ 127 for (var i=0; i<children.length; i++)
128 article.appendChild(children[i].cloneNode(true));
113 } else { 129 } else {
114 var contentSrc = atomContent.getAttribute('src'); 130 var contentSrc = atomContent.getAttributeNS(null, 'src');
115 if (contentSrc) { 131 if (contentSrc) {
116 if (/^image\//.test(contentType)) { 132 if (/^image\//.test(contentType)) {
117 var img = document.createElementNS(ns.xhtml, 'img'); 133 var img = document.createElementNS(ns.xhtml, 'img');
118 img.setAttributeNS(null, 'src', contentSrc); 134 img.setAttributeNS(null, 'src', contentSrc);
119 article.appendChild(img); 135 article.appendChild(img);
132 a.setAttributeNS(null, 'href', contentSrc); 148 a.setAttributeNS(null, 'href', contentSrc);
133 article.appendChild(a); 149 article.appendChild(a);
134 } 150 }
135 } 151 }
136 } 152 }
153 } catch (e) {
137 } 154 }
138 155
139 var atomLinks = atom.getChildren('link'); 156 var atomLinks = atom.getElementsByTagNameNS(ns.atom, 'link');
140 for (var i in atomLinks) { 157 for (var i in atomLinks) {
141 var atomLink = atomLinks[i]; 158 var atomLink = atomLinks[i];
142 159
143 if (atomLink['@rel'] !== 'replies') 160 if (atomLink.getAttributeNS(null, 'rel') !== 'replies')
144 continue; 161 continue;
145 162
146 if (atomLink['@title'] !== 'comments') 163 if (atomLink.getAttributeNS(null, 'title') !== 'comments')
147 continue; 164 continue;
148 165
149 var href = new JID; 166 var href = new JID;
150 href.uri = atomLink['@href']; 167 href.uri = atomLink.getAttributeNS(null, 'href');
151 168
152 article.innerHTML += '<a href="?jid=' + href.bare + ';node=' + href.query.node + ';comments=' + params.jid + '/' + params.node + '">Comments !</a>'; 169 article.innerHTML += '<a href="?jid=' + href.bare + ';node=' + href.query.node + ';comments=' + params.jid + '/' + params.node + '">Comments !</a>';
153 } 170 }
154 171
155 return article; 172 return article;
156 }; 173 };
157 174
158 this.xml = xml; 175 this.xml = item.payload;
159 this.date = toDate(xml); 176 this.date = toDate(item.payload);
160 this.html = toHTML(xml, this.date); 177 this.html = toHTML(item, this.date);
161 } 178 }