| | 83 | * Builds a DOM document suitable for processing by |
| | 84 | * web page dump facilities. |
| | 85 | * @param {doc} A DOM element or collection |
| | 86 | * @param {string} A title string for use in the document |
| | 87 | * @param {object} A DOM element or collection, or an array |
| | 88 | * containing DOM elements or collections. |
| | 89 | */ |
| | 90 | "composeDOM":function(doc, title, object) { |
| | 91 | var o; |
| | 92 | var content = false; |
| | 93 | |
| | 94 | // (object) is either a single DOM element, a DOM |
| | 95 | // collection, or an array of DOM elements or DOM |
| | 96 | // collections. Only the first element of a DOM collection |
| | 97 | // is used in the constructed document. |
| | 98 | |
| | 99 | // Punch out early if there is nothing here. |
| | 100 | if (!object || !object.length) { |
| | 101 | Zotero.debug("XXX fail 1"); |
| | 102 | return false; |
| | 103 | } else if (!object.tagName) { |
| | 104 | Zotero.debug("XXX fail 2?"); |
| | 105 | var fail = true; |
| | 106 | for (var i = 0, ilen = object.length; i < ilen; i += 1) { |
| | 107 | if (object[i] && (object[i].tagName || object[i].length)) { |
| | 108 | Zotero.debug("XXX fail 2 not"); |
| | 109 | fail = false; |
| | 110 | break; |
| | 111 | } |
| | 112 | } |
| | 113 | if (fail) { |
| | 114 | Zotero.debug("fail 2!"); |
| | 115 | return false; |
| | 116 | } |
| | 117 | } |
| | 118 | Zotero.debug("XXX do"); |
| | 119 | // Cast a namespace object |
| | 120 | var myns = doc.documentElement.namespaceURI; |
| | 121 | |
| | 122 | // Cast a document type for a new custom-spun HTML document |
| | 123 | var newDoctype = doc.implementation.createDocumentType("html:html", "-//W3C//DTD HTML 4.01 Transitional//EN", "http://www.w3.org/TR/html4/loose.dtd"); |
| | 124 | |
| | 125 | // Create an empty HTML document |
| | 126 | var newDoc = doc.implementation.createDocument(myns, 'html', newDoctype); |
| | 127 | |
| | 128 | // Get the HTML section of the document, into which we will insert things. |
| | 129 | var html = newDoc.getElementsByTagName("html")[0]; |
| | 130 | |
| | 131 | // Cast a header and a title element, |
| | 132 | // fill in some details in both, |
| | 133 | // merge the set, and insert into the html element |
| | 134 | var head = newDoc.createElementNS(myns, "head"); |
| | 135 | var base = newDoc.createElementNS(myns, "base"); |
| | 136 | var header_title = newDoc.createElementNS(myns, "title"); |
| | 137 | header_title_text = newDoc.createTextNode(title); |
| | 138 | header_title.appendChild(header_title_text); |
| | 139 | head.appendChild(header_title); |
| | 140 | base.setAttribute("target", "_blank"); |
| | 141 | base.setAttribute("href", doc.location.href); |
| | 142 | head.appendChild(base); |
| | 143 | html.appendChild(head); |
| | 144 | |
| | 145 | // Cast a body element, insert the content node |
| | 146 | // into it, and insert the body into the document. |
| | 147 | var body = newDoc.createElementNS(myns, "body"); |
| | 148 | |
| | 149 | // Cast a content node and populate it. |
| | 150 | contentNode = newDoc.createElementNS(myns, "div"); |
| | 151 | if (object.tagName) { |
| | 152 | // Object is a DOM node. Clone and wrap. |
| | 153 | content = object.cloneNode(true); |
| | 154 | contentNode.appendChild(content); |
| | 155 | } else if (object.length) { |
| | 156 | for (var i = 0, ilen = object.length; i < ilen; i += 1) { |
| | 157 | o = object[i]; |
| | 158 | if (o.tagName) { |
| | 159 | // Object is a DOM node. Clone and wrap. |
| | 160 | content = o.cloneNode(true); |
| | 161 | contentNode.appendChild(content); |
| | 162 | } else { |
| | 163 | // Object is a DOM-list consisting of elements. |
| | 164 | // If non-zero, clone the first and wrap. |
| | 165 | if (o.length) { |
| | 166 | content = o[0].cloneNode(true); |
| | 167 | contentNode.appendChild(content); |
| | 168 | } |
| | 169 | } |
| | 170 | } |
| | 171 | } |
| | 172 | body.appendChild(contentNode); |
| | 173 | |
| | 174 | // Cast a footer div for use in the document, |
| | 175 | // with a horizontal rule at the top |
| | 176 | var footer = newDoc.createElementNS(myns, "div"); |
| | 177 | var hr = newDoc.createElementNS(myns, "hr"); |
| | 178 | footer.appendChild(hr); |
| | 179 | |
| | 180 | // Cast a div for the title, populate it with the title |
| | 181 | // text, and insert the unit into the footer object |
| | 182 | var footer_title_div = newDoc.createElementNS(myns, "div"); |
| | 183 | var footer_title = newDoc.createTextNode(title); |
| | 184 | footer_title_div.appendChild(footer_title); |
| | 185 | footer.appendChild(footer_title); |
| | 186 | |
| | 187 | // Cast a source div, populate it with a simple |
| | 188 | // label and the URL of the document from which |
| | 189 | // the text is extracted, bundle the unit up |
| | 190 | // and insert it into the document HTML node. |
| | 191 | var source_div = newDoc.createElementNS(myns, "div"); |
| | 192 | var source_label = newDoc.createTextNode("Source: "); |
| | 193 | var source_anchor = newDoc.createElementNS(myns, "a"); |
| | 194 | source_anchor.setAttribute("href", doc.location.href); |
| | 195 | var source_anchor_text = newDoc.createTextNode(doc.location.href); |
| | 196 | source_anchor.appendChild(source_anchor_text); |
| | 197 | source_div.appendChild(source_anchor); |
| | 198 | footer.appendChild(source_div); |
| | 199 | body.appendChild(footer); |
| | 200 | |
| | 201 | // Insert the body into the document HTML node |
| | 202 | html.appendChild(body); |
| | 203 | |
| | 204 | return newDoc; |
| | 205 | }, |
| | 206 | |
| | 207 | /** |