- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / subscribe_page_action / iframe.js
1 /* Copyright (c) 2009 The Chromium Authors. All rights reserved.
2    Use of this source code is governed by a BSD-style license that can be
3    found in the LICENSE file.
4 */
5
6 /* Use only multi-line comments in this file, since during testing
7    its contents will get read from disk and stuffed into the
8    iframe .src tag, which is a process that doesn't preserve line
9    breaks and makes single-line comment out the rest of the code.
10 */
11
12 /* The maximum number of feed items to show in the preview. */
13 var maxFeedItems = 10;
14
15 /* The maximum number of characters to show in the feed item title. */
16 var maxTitleCount = 1024;
17
18 window.addEventListener("message", function(e) {
19   var parser = new DOMParser();
20   var doc = parser.parseFromString(e.data, "text/xml");
21
22   if (doc) {
23     buildPreview(doc);
24   } else {
25     /* Already handled in subscribe.html */
26   }
27 }, false);
28
29 function buildPreview(doc) {
30   /* Start building the part we render inside an IFRAME. We use a table to
31      ensure that items are separated vertically from each other. */
32   var table = document.createElement("table");
33   var tbody = document.createElement("tbody");
34   table.appendChild(tbody);
35
36   /* Now parse the rest. Some use <entry> for each feed item, others use
37      <channel><item>. */
38   var entries = doc.getElementsByTagName('entry');
39   if (entries.length == 0)
40     entries = doc.getElementsByTagName('item');
41
42   for (i = 0; i < entries.length && i < maxFeedItems; ++i) {
43     item = entries.item(i);
44
45     /* Grab the title for the feed item. */
46     var itemTitle = item.getElementsByTagName('title')[0];
47     if (itemTitle)
48       itemTitle = itemTitle.textContent;
49     else
50       itemTitle = "Unknown title";
51
52     /* Ensure max length for title. */
53     if (itemTitle.length > maxTitleCount)
54       itemTitle = itemTitle.substring(0, maxTitleCount) + "...";
55
56     /* Grab the description.
57        TODO(aa): Do we need to check for type=html here? */
58     var itemDesc = item.getElementsByTagName('description')[0];
59     if (!itemDesc)
60       itemDesc = item.getElementsByTagName('summary')[0];
61     if (!itemDesc)
62       itemDesc = item.getElementsByTagName('content')[0];
63
64     if (itemDesc)
65       itemDesc = itemDesc.textContent;
66     else
67       itemDesc = "";
68
69     /* Grab the link URL. */
70     var itemLink = item.getElementsByTagName('link');
71     var link = "";
72     if (itemLink.length > 0) {
73       link = itemLink[0].childNodes[0];
74       if (link)
75         link = itemLink[0].childNodes[0].nodeValue;
76       else
77         link = itemLink[0].getAttribute('href');
78     }
79
80     var tr = document.createElement("tr");
81     var td = document.createElement("td");
82
83     /* If we found a link we'll create an anchor element,
84     otherwise just use a bold headline for the title. */
85     var anchor = (link != "") ? document.createElement("a") :
86                                 document.createElement("strong");
87     anchor.id = "anchor_" + String(i);
88     if (link != "")
89       anchor.href = link;
90     anchor.innerHTML = itemTitle;
91     anchor.target = "_top";
92     anchor.className = "item_title";
93
94     var span = document.createElement("span");
95     span.id = "desc_" + String(i);
96     span.className = "item_desc";
97     span.innerHTML = itemDesc;
98
99     td.appendChild(anchor);
100     td.appendChild(document.createElement("br"));
101     td.appendChild(span);
102     td.appendChild(document.createElement("br"));
103     td.appendChild(document.createElement("br"));
104
105     tr.appendChild(td);
106     tbody.appendChild(tr);
107   }
108
109   table.appendChild(tbody);
110   document.body.appendChild(table);
111 }