Mercurial > eldonilo > blog
comparison atom.js @ 14:03be0717d3f8
Use the same code for client- and server-side Atom parsing.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 03 Nov 2011 16:42:00 -0700 |
parents | 161d4ea1c3f8 |
children |
comparison
equal
deleted
inserted
replaced
13:161d4ea1c3f8 | 14:03be0717d3f8 |
---|---|
1 'use strict'; | 1 'use strict'; |
2 | 2 |
3 if (!document) | 3 if (typeof window === 'undefined') { |
4 var Element = require('ltx').Element; | |
5 var JID = require('./jid'); | |
6 var ns = require('./ns'); | |
7 var config = require('./configuration'); | |
8 | |
4 var document = {}; | 9 var document = {}; |
5 | 10 |
6 if (!document.createElementNS) | |
7 document.createElementNS = function(ns, name) { | 11 document.createElementNS = function(ns, name) { |
8 return new Element(name, {xmlns: ns}); | 12 return new Element(name, {xmlns: ns}); |
9 }; | 13 }; |
10 | 14 |
11 if (!document.createTextNode) | |
12 document.createTextNode = function(text) { | 15 document.createTextNode = function(text) { |
13 return text; | 16 return text; |
14 }; | 17 }; |
15 | 18 |
16 parsers[ns.atom] = function(item) { | 19 Element.prototype.getAttributeNS = function(ns, name) { |
20 // TODO: use the namespace. | |
21 return this.attrs[name]; | |
22 }; | |
23 | |
24 Element.prototype.setAttributeNS = function(ns, name, value) { | |
25 // TODO: use the namespace. | |
26 this.attrs[name] = value; | |
27 }; | |
28 | |
29 Element.prototype.cloneNode = function(depth) { | |
30 // XXX | |
31 return this; | |
32 }; | |
33 | |
34 Element.prototype.appendChild = function(child) { | |
35 if (typeof child === 'string') | |
36 this.t(child); | |
37 else | |
38 this.cnode(child); | |
39 }; | |
40 | |
41 Element.prototype.getElementsByTagNameNS = function(ns, name) { | |
42 return this.getChildren(name, ns); | |
43 }; | |
44 | |
45 Element.prototype.__defineGetter__('textContent', function() { | |
46 return this.getText(); | |
47 }); | |
48 | |
49 Element.prototype.__defineGetter__('firstChild', function() { | |
50 return this.children[0]; | |
51 }); | |
52 | |
53 Element.prototype.__defineGetter__('childNodes', function() { | |
54 return this.children; | |
55 }); | |
56 } | |
57 | |
58 var atomParser = function(item) { | |
17 var toDate = function(atom) { | 59 var toDate = function(atom) { |
18 var d = new Date; | 60 var d = new Date; |
19 | 61 |
20 if (!atom) | 62 if (!atom) |
21 return d; | 63 return d; |
36 return d; | 78 return d; |
37 }; | 79 }; |
38 | 80 |
39 var toHTML = function(item, date) { | 81 var toHTML = function(item, date) { |
40 var atom = item.payload; | 82 var atom = item.payload; |
41 var id = item.id; | |
42 | 83 |
43 var article = document.createElementNS(ns.xhtml, 'article'); | 84 var article = document.createElementNS(ns.xhtml, 'article'); |
44 | 85 |
45 article.setAttributeNS(ns.e, 'id', id); | 86 article.setAttributeNS(ns.e, 'id', item.id); |
46 var d8601 = date.to8601(); | 87 var d8601 = date.to8601(); |
47 article.setAttributeNS(ns.e, 'date', d8601); | 88 article.setAttributeNS(ns.e, 'date', d8601); |
48 | 89 |
49 var aside = document.createElementNS(ns.xhtml, 'aside'); | 90 var aside = document.createElementNS(ns.xhtml, 'aside'); |
50 article.appendChild(aside); | 91 article.appendChild(aside); |
68 | 109 |
69 var cite = document.createElementNS(ns.xhtml, 'cite'); | 110 var cite = document.createElementNS(ns.xhtml, 'cite'); |
70 try { | 111 try { |
71 var atomURI = atomAuthor.getElementsByTagNameNS(ns.atom, 'uri')[0].textContent; | 112 var atomURI = atomAuthor.getElementsByTagNameNS(ns.atom, 'uri')[0].textContent; |
72 var a = document.createElementNS(ns.xhtml, 'a'); | 113 var a = document.createElementNS(ns.xhtml, 'a'); |
73 a.href = atomURI; | 114 a.setAttributeNS(null, 'href', atomURI); |
74 var atomJID = new JID; | 115 var atomJID = new JID; |
75 atomJID.uri = atomURI; | 116 atomJID.uri = atomURI; |
76 | 117 |
77 a.appendChild(document.createTextNode(atomName? atomName: atomJID.bare)); | 118 a.appendChild(document.createTextNode(atomName? atomName: atomJID.bare)); |
78 cite.appendChild(a); | 119 cite.appendChild(a); |
79 | 120 |
80 var img = document.createElementNS(ns.xhtml, 'img'); | 121 var img = document.createElementNS(ns.xhtml, 'img'); |
81 img.src = config.avatarRoot + atomJID.bare; | 122 img.setAttributeNS(null, 'src', config.avatarRoot + atomJID.bare); |
82 aside.appendChild(img); | 123 aside.appendChild(img); |
83 } catch (e) { | 124 } catch (e) { |
84 cite.appendChild(document.createTextNode(atomName)); | 125 cite.appendChild(document.createTextNode(atomName)); |
85 } | 126 } |
86 | 127 |
89 | 130 |
90 try { | 131 try { |
91 var atomEmail = atomAuthor.getElementsByTagNameNS(ns.atom, 'email')[0].textContent; | 132 var atomEmail = atomAuthor.getElementsByTagNameNS(ns.atom, 'email')[0].textContent; |
92 footer.appendChild(document.createTextNode(' (')); | 133 footer.appendChild(document.createTextNode(' (')); |
93 var a = document.createElementNS(ns.xhtml, 'a'); | 134 var a = document.createElementNS(ns.xhtml, 'a'); |
94 a.href = 'mailto:' + atomEmail; | 135 a.setAttributeNS(null, 'href', 'mailto:' + atomEmail); |
95 a.appendChild(document.createTextNode('email')); | 136 a.appendChild(document.createTextNode('email')); |
96 footer.appendChild(a); | 137 footer.appendChild(a); |
97 footer.appendChild(document.createTextNode(')')); | 138 footer.appendChild(document.createTextNode(')')); |
98 } catch (e) { | 139 } catch (e) { |
99 } | 140 } |
100 | 141 |
101 article.appendChild(footer); | 142 article.appendChild(footer); |
102 } catch (e) { | 143 } catch (e) { |
103 } | 144 } |
104 | 145 |
105 footer.innerHTML += ', <time datetime="' + d8601 + '">' + date.getRelative() + '</time>'; | 146 var time = document.createElementNS(ns.xhtml, 'time'); |
147 time.setAttributeNS(null, 'datetime', d8601); | |
148 time.appendChild(document.createTextNode(date.getRelative())); | |
149 footer.appendChild(document.createTextNode(', ')); | |
150 footer.appendChild(time); | |
106 | 151 |
107 try { | 152 try { |
108 var atomSummary = atomAuthor.getElementsByTagNameNS(ns.atom, 'summary')[0].textContent; | 153 var atomSummary = atomAuthor.getElementsByTagNameNS(ns.atom, 'summary')[0].textContent; |
109 var p = document.createElementNS(ns.xhtml, 'p'); | 154 var p = document.createElementNS(ns.xhtml, 'p'); |
110 p.appendChild(document.createTextNode(atomSummary)); | 155 p.appendChild(document.createTextNode(atomSummary)); |
118 if (/^text$/.test(contentType)) { | 163 if (/^text$/.test(contentType)) { |
119 var p = document.createElementNS(ns.xhtml, 'p'); | 164 var p = document.createElementNS(ns.xhtml, 'p'); |
120 p.appendChild(document.createTextNode(atomContent.textContent)); | 165 p.appendChild(document.createTextNode(atomContent.textContent)); |
121 article.appendChild(p); | 166 article.appendChild(p); |
122 } else if (/^html$/.test(contentType)) { | 167 } else if (/^html$/.test(contentType)) { |
123 article.insertAdjacentHTML('beforeend', atomContent.textContent); // FIXME: could be not-well-formed. | 168 if (!article.insertAdjacentHTML) |
169 article.appendChild(document.createTextNode('Inline HTML inclusion not yet supported server-side.')); | |
170 else | |
171 article.insertAdjacentHTML('beforeend', atomContent.textContent); // FIXME: could be not-well-formed. | |
124 } else if (/^xhtml$/.test(contentType)) { | 172 } else if (/^xhtml$/.test(contentType)) { |
125 var div = atomContent.firstChild; | 173 var div = atomContent.firstChild; |
126 var children = div.childNodes; | 174 var children = div.childNodes; |
127 for (var i=0; i<children.length; i++) | 175 for (var i=0; i<children.length; i++) { |
128 article.appendChild(children[i].cloneNode(true)); | 176 var child = children[i]; |
177 if (typeof child === 'string') | |
178 article.appendChild(child); | |
179 else | |
180 article.appendChild(child.cloneNode(true)); | |
181 } | |
129 } else { | 182 } else { |
130 var contentSrc = atomContent.getAttributeNS(null, 'src'); | 183 var contentSrc = atomContent.getAttributeNS(null, 'src'); |
131 if (contentSrc) { | 184 if (contentSrc) { |
132 if (/^image\//.test(contentType)) { | 185 if (/^image\//.test(contentType)) { |
133 var img = document.createElementNS(ns.xhtml, 'img'); | 186 var img = document.createElementNS(ns.xhtml, 'img'); |
164 continue; | 217 continue; |
165 | 218 |
166 var href = new JID; | 219 var href = new JID; |
167 href.uri = atomLink.getAttributeNS(null, 'href'); | 220 href.uri = atomLink.getAttributeNS(null, 'href'); |
168 | 221 |
169 article.innerHTML += '<a href="?jid=' + href.bare + ';node=' + href.query.node + ';comments=' + params.jid + '/' + params.node + '">Comments !</a>'; | 222 var a = document.createElementNS(ns.xhtml, 'a'); |
223 a.setAttributeNS(null, 'href', '?jid=' + href.bare + ';node=' + href.query.node); | |
224 a.appendChild(document.createTextNode('Comments')); | |
225 article.appendChild(a); | |
170 } | 226 } |
171 | 227 |
172 return article; | 228 return article; |
173 }; | 229 }; |
174 | 230 |
175 this.xml = item.payload; | 231 this.xml = item.payload; |
176 this.date = toDate(item.payload); | 232 this.date = toDate(item.payload); |
177 this.html = toHTML(item, this.date); | 233 this.html = toHTML(item, this.date); |
178 } | 234 }; |
235 | |
236 if (typeof parsers !== 'undefined') | |
237 parsers[ns.atom] = atomParser; | |
238 else if (typeof module !== 'undefined') | |
239 module.exports = atomParser; |