Mercurial > eldonilo > blog
diff 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 |
line wrap: on
line diff
--- a/atom.js +++ b/atom.js @@ -13,26 +13,33 @@ if (!document.createTextNode) return text; }; -parsers[ns.atom] = function(id, xml) { +parsers[ns.atom] = function(item) { var toDate = function(atom) { + var d = new Date; + if (!atom) - return new Date; + return d; - var last = atom.getChild('updated'); - if (!last) { - last = atom.getChild('published'); - if (!last) - return new Date; + try { + var last = atom.getElementsByTagNameNS(ns.atom, 'updated')[0].textContent; + } catch (e) { + try { + var last = atom.getElementsByTagNameNS(ns.atom, 'published')[0].textContent; + } catch (e) { + return d; + } } // var d = new Date(last); // FIXME: don't work in obsolete browsers - var d = new Date; d.set8601(last); return d; }; - var toHTML = function(atom, date) { + var toHTML = function(item, date) { + var atom = item.payload; + var id = item.id; + var article = document.createElementNS(ns.xhtml, 'article'); article.setAttributeNS(ns.e, 'id', id); @@ -42,22 +49,26 @@ parsers[ns.atom] = function(id, xml) { var aside = document.createElementNS(ns.xhtml, 'aside'); article.appendChild(aside); - var atomTitle = atom.getChild('title'); - if (atomTitle) { + try { + var atomTitle = atom.getElementsByTagNameNS(ns.atom, 'title')[0].textContent; var title = document.createElementNS(ns.xhtml, 'h2'); title.appendChild(document.createTextNode(atomTitle)); article.appendChild(title); + } catch (e) { } var footer = document.createElementNS(ns.xhtml, 'footer'); - var atomAuthor = atom.getChild('author', ns.atom); - if (atomAuthor) { - var atomName = atomAuthor.getChild('name'); + try { + var atomAuthor = atom.getElementsByTagNameNS(ns.atom, 'author')[0]; + try { + var atomName = atomAuthor.getElementsByTagNameNS(ns.atom, 'name')[0].textContent; + } catch (e) { + } - var atomURI = atomAuthor.getChild('uri'); var cite = document.createElementNS(ns.xhtml, 'cite'); - if (atomURI) { + try { + var atomURI = atomAuthor.getElementsByTagNameNS(ns.atom, 'uri')[0].textContent; var a = document.createElementNS(ns.xhtml, 'a'); a.href = atomURI; var atomJID = new JID; @@ -69,49 +80,54 @@ parsers[ns.atom] = function(id, xml) { var img = document.createElementNS(ns.xhtml, 'img'); img.src = config.avatarRoot + atomJID.bare; aside.appendChild(img); - } else + } catch (e) { cite.appendChild(document.createTextNode(atomName)); + } footer.appendChild(document.createTextNode('By ')); footer.appendChild(cite); - var atomEmail = atomAuthor.getChild('email'); - if (atomEmail) { + try { + var atomEmail = atomAuthor.getElementsByTagNameNS(ns.atom, 'email')[0].textContent; footer.appendChild(document.createTextNode(' (')); var a = document.createElementNS(ns.xhtml, 'a'); a.href = 'mailto:' + atomEmail; a.appendChild(document.createTextNode('email')); footer.appendChild(a); footer.appendChild(document.createTextNode(')')); + } catch (e) { } article.appendChild(footer); + } catch (e) { } footer.innerHTML += ', <time datetime="' + d8601 + '">' + date.getRelative() + '</time>'; - var atomSummary = atom.getChild('summary'); - if (atomSummary) { + try { + var atomSummary = atomAuthor.getElementsByTagNameNS(ns.atom, 'summary')[0].textContent; var p = document.createElementNS(ns.xhtml, 'p'); p.appendChild(document.createTextNode(atomSummary)); article.appendChild(p); + } catch (e) { } - var atomContent = atom.getChild('content'); - if (atomContent) { - var contentType = atomContent.getAttribute('type'); + try { + var atomContent = atom.getElementsByTagNameNS(ns.atom, 'content')[0]; + var contentType = atomContent.getAttributeNS(null, 'type'); if (/^text$/.test(contentType)) { var p = document.createElementNS(ns.xhtml, 'p'); - p.appendChild(document.createTextNode(atomContent.getText())); + p.appendChild(document.createTextNode(atomContent.textContent)); article.appendChild(p); } else if (/^html$/.test(contentType)) { - article.insertAdjacentHTML('beforeend', atomContent.getText()); // FIXME: could be not-well-formed. + article.insertAdjacentHTML('beforeend', atomContent.textContent); // FIXME: could be not-well-formed. } else if (/^xhtml$/.test(contentType)) { - // TODO: use a better xml2json lib that allow to json2xml - /*var div = atomContent.getChild(); - article.appendChild(div.innerXml());*/ + var div = atomContent.firstChild; + var children = div.childNodes; + for (var i=0; i<children.length; i++) + article.appendChild(children[i].cloneNode(true)); } else { - var contentSrc = atomContent.getAttribute('src'); + var contentSrc = atomContent.getAttributeNS(null, 'src'); if (contentSrc) { if (/^image\//.test(contentType)) { var img = document.createElementNS(ns.xhtml, 'img'); @@ -134,20 +150,21 @@ parsers[ns.atom] = function(id, xml) { } } } + } catch (e) { } - var atomLinks = atom.getChildren('link'); + var atomLinks = atom.getElementsByTagNameNS(ns.atom, 'link'); for (var i in atomLinks) { var atomLink = atomLinks[i]; - if (atomLink['@rel'] !== 'replies') + if (atomLink.getAttributeNS(null, 'rel') !== 'replies') continue; - if (atomLink['@title'] !== 'comments') + if (atomLink.getAttributeNS(null, 'title') !== 'comments') continue; var href = new JID; - href.uri = atomLink['@href']; + href.uri = atomLink.getAttributeNS(null, 'href'); article.innerHTML += '<a href="?jid=' + href.bare + ';node=' + href.query.node + ';comments=' + params.jid + '/' + params.node + '">Comments !</a>'; } @@ -155,7 +172,7 @@ parsers[ns.atom] = function(id, xml) { return article; }; - this.xml = xml; - this.date = toDate(xml); - this.html = toHTML(xml, this.date); + this.xml = item.payload; + this.date = toDate(item.payload); + this.html = toHTML(item, this.date); }