File: HTMLoverlay.js

package info (click to toggle)
python-jtoolkit 0.7.8-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,436 kB
  • ctags: 2,536
  • sloc: python: 15,143; makefile: 20
file content (218 lines) | stat: -rwxr-xr-x 6,013 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF
 * ANY KIND, either express or implied. See the License for the specific
 * language governing rights and
 * limitations under the License.
 *
 * The Original Code is HTML Overlays.
 *
 * The Initial Developer of the Original Code is Disruptive Innovations SARL.
 * Portions created by the Initial Developer are Copyright (C) 2004.
 * The HO_addLoadEvent function's code is Copyright (C) Simon Willison 2004,
 * used and MPL'd here with written permission.
 * All Rights Reserved.
 *
 * Contributor(s):
 *   Laurent Jouanneau <laurent.jouanneau@disruptive-innovations.com>,
 *                     Original idea
 *   Daniel Glazman <glazman@disruptive-innovations.com>, Original author
 *
**/

/*
 * Change log:
 *   2004-aug-30: original version, sync mode
**/

function HO_ApplyOverlayXML(doc){
  // let's look at all the element children of the root element
  var child = doc.documentElement.firstChild;
  while (child)
  {
    if (child.nodeType == 1 &&
        child.getAttribute("id"))
    {
      // find a local element of same id
      var a = child.attributes;
      var t = getObj(child.getAttribute("id"));
      if (t)
      {
        // we found a target, let's append a clone of all the contents
        HO_cloneNodeDeep(child.firstChild, t);
      }
      // don't forget to copy all attributes
      for (var i=0; i<a.length; i++)
      {
        var sourceAtt = a[i];
        t.setAttribute(sourceAtt.nodeName, sourceAtt.nodeValue);
      }
    }
    child = child.nextSibling;
  }
}

function HO_ApplyOverlay(url)
{
  var p;
  // let's create an XMLHttpRequest
  // unfortunately, this old crap of MSIE does that through an ActiveX
  // a try/catch will help us make the difference between good browsers
  // and Internet Explorer
  try {
    p = new XMLHttpRequest();
  } catch (e) {
    p = new ActiveXObject("Msxml2.XMLHTTP");
  }

  try {
    // needed for Mozilla
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  } catch (e) {
    // ignore
  }

  // send the request
  p.open("GET", url, false);
  p.send(null);

  // find the resulting DOM document
  var doc = p.responseXML;

  // do we have a valid XML document?
  if (doc)
  {
    // is it an HTML overlay?
    if (doc.documentElement &&
        doc.documentElement.nodeName == "HTMLoverlay")
    {
      HO_ApplyOverlayXML(doc);
    }
  }
}

function HO_cloneNodeDeep(source, target)
{
  // we can't use the cloneNode API because Mozilla needs to see HTML
  // elements since Mozilla is namespace-aware, and we can't use cloneNode
  // for MSIE because it's totally unreliable...
  while (source)
  {
    switch (source.nodeType)
    {
      case 1:
        // this is an element node, create a local clone
        var clone = document.createElement(source.nodeName);
        // do we have to append or insert somewhere else?
        var refElt = null;
        var insertbefore = source.getAttribute("insertbefore");
        if (insertbefore)
          refElt = getObj(insertbefore);
        if (insertafter && !refElt)
        {
          var insertafter  = source.getAttribute("insertafter");
          if (insertafter)
            refElt = getObj(insertafter);
          if (refElt)
            refElt = refElt.nextSibling;
          else
          {
            var position = source.getAttribute("position");
            if (position)
            {
              // find the position-th child of target if it exists...
              var index = 0;
              refElt = target.firstChild;
              while (refElt)
              {
                if (refElt.nodeType == 1)
                  index++;
                if (index == position)
                  break;
                refElt = refElt.nextSibling;
              }              
            }
          }
        }
        
        // insert it
        target.insertBefore(clone, refElt);
        var a = source.attributes;
        for (var i=0; i<a.length; i++)
        {
          // clone all attributes
          var sourceAtt = a[i];
          switch (sourceAtt.nodeName.toLowerCase())
          {
            case "class":
              // MSIE sux, its setAttribute() method sux
              clone.className = sourceAtt.nodeValue;
              break;
            case "insertbefore":
            case "insertafter":
            case "position":
              // do nothing
              break;
            default:
              clone.setAttribute(sourceAtt.nodeName, sourceAtt.nodeValue);
              break;
          }
        }
        // go deeper?
        if (source.firstChild)
          HO_cloneNodeDeep(source.firstChild, clone);
        break;

      case 3:
        // this is a text node, just clone it
        clone = document.createTextNode(source.data);
        target.appendChild(clone);
        break;

      default:
        // do nothing
        break;
    }
    // iterate
    source = source.nextSibling;
  }
}

function HO_ApplyOverlays()
{
  var d = document;
  var linkCollection = d.getElementsByTagName("link");
  var linkCollectionLength = linkCollection.length;
  for (var i=0; i<linkCollectionLength; i++)
  {
    var theLink = linkCollection[i];
    if (theLink.parentNode &&
        theLink.parentNode.nodeName.toLowerCase() == "head")
    {
      if (theLink.getAttribute("rel").toLowerCase() == "overlay")
        HO_ApplyOverlay(theLink.getAttribute("href"));
    }
  }
}

function HO_addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


//HO_addLoadEvent(HO_ApplyOverlays);