PageSourceSearch

https://digilink.net/js/setstyle.js

js digilink.net collected 2026-10-09 02:59:44 UTC 8,514 bytes, 292 lines download raw bytes

1// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
2//
3// You may incorporate this Apple sample code into your own code
4// without restriction. This Apple sample code has been provided "AS IS"
5// and the responsibility for its operation is yours. You may redistribute
6// this code, but you are not permitted to redistribute it as
7// "Apple sample code" after having made changes.
8
9// ugly workaround for missing support for selectorText in Netscape6/Mozilla
10// call onLoad() or before you need to do anything you would have otherwise used
11// selectorText for.
12var ugly_selectorText_workaround_flag = false;
13var allStyleRules;
14// code developed using the following workaround (CVS v1.15) as an example.
15// http://lxr.mozilla.org/seamonkey/source/extensions/xmlterm/ui/content/XMLTermCommands.js
16function ugly_selectorText_workaround() {
17	if((navigator.userAgent.indexOf("Gecko") == -1) ||
18	   (ugly_selectorText_workaround_flag)) {
19		return; // we've already been here or shouldn't be here
20	}
21	var styleElements = document.getElementsByTagName("style");
22	
23	for(var i = 0; i < styleElements.length; i++) {
24		var styleText = styleElements[i].firstChild.data;
25		// this should be using match(/\b[\w-.]+(?=\s*\{)/g but ?= causes an
26		// error in IE5, so we include the open brace and then strip it
27		allStyleRules = styleText.match(/\b[\w-.]+(\s*\{)/g);
28	}
29
30	for(var i = 0; i < allStyleRules.length; i++) {
31		// probably insufficient for people who like random gobs of 
32		// whitespace in their styles
33		allStyleRules[i] = allStyleRules[i].substr(0, (allStyleRules[i].length - 2));
34	}
35	ugly_selectorText_workaround_flag = true;
36}
37
38// setStyleById: given an element id, style property and 
39// value, apply the style.
40// args:
41//  i - element id
42//  p - property
43//  v - value
44//
45function setStyleById(i, p, v) {
46	var n = document.getElementById(i);
47	n.style[p] = v;
48}
49
50// getStyleById: given an element ID and style property
51// return the current setting for that property, or null.
52// args:
53//  i - element id
54//  p - property
55function getStyleById(i, p) {
56	var n = document.getElementById(i);
57	var s = eval("n.style." + p);
58
59	// try inline
60	if((s != "") && (s != null)) {
61		return s;
62	}
63
64	// try currentStyle
65	if(n.currentStyle) {
66		var s = eval("n.currentStyle." + p);
67		if((s != "") && (s != null)) {
68			return s;
69		}
70	}
71
72	// try styleSheets
73	var sheets = document.styleSheets;
74	if(sheets.length > 0) {
75		// loop over each sheet
76		for(var x = 0; x < sheets.length; x++) {
77			// grab stylesheet rules
78			var rules = sheets[x].cssRules;
79			if(rules.length > 0) {
80				// check each rule
81				for(var y = 0; y < rules.length; y++) {
82					var z = rules[y].style;
83					// selectorText broken in NS 6/Mozilla: see
84					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
85					ugly_selectorText_workaround();
86					if(allStyleRules) {
87						if(allStyleRules[y] == i) {
88							return z[p];
89						}			
90					} else {
91						// use the native selectorText and style stuff
92						if(((z[p] != "") && (z[p] != null)) ||
93						   (rules[y].selectorText == i)) {
94							return z[p];
95						}
96					}
97				}
98			}
99		}
100	}
101	return null;
102}
103
104// setStyleByClass: given an element type and a class selector,
105// style property and value, apply the style.
106// args:
107//  t - type of tag to check for (e.g., SPAN)
108//  c - class name
109//  p - CSS property
110//  v - value
111var ie = (document.all) ? true : false;
112
113function setStyleByClass(t,c,p,v){
114	var elements;
115	if(t == '*') {
116		// '*' not supported by IE/Win 5.5 and below
117		elements = (ie) ? document.all : document.getElementsByTagName('*');
118	} else {
119		elements = document.getElementsByTagName(t);
120	}
121	for(var i = 0; i < elements.length; i++){
122		var node = elements.item(i);
123		for(var j = 0; j < node.attributes.length; j++) {
124			if(node.attributes.item(j).nodeName == 'class') {
125				if(node.attributes.item(j).nodeValue == c) {
126					eval('node.style.' + p + " = '" +v + "'");
127				}
128			}
129		}
130	}
131}
132
133// getStyleByClass: given an element type, a class selector and a property,
134// return the value of the property for that element type.
135// args:
136//  t - element type
137//  c - class identifier
138//  p - CSS property
139function getStyleByClass(t, c, p) {
140	// first loop over elements, because if they've been modified they
141	// will contain style data more recent than that in the stylesheet
142	var elements;
143	if(t == '*') {
144		// '*' not supported by IE/Win 5.5 and below
145		elements = (ie) ? document.all : document.getElementsByTagName('*');
146	} else {
147		elements = document.getElementsByTagName(t);
148	}
149	for(var i = 0; i < elements.length; i++){
150		var node = elements.item(i);
151		for(var j = 0; j < node.attributes.length; j++) {
152			if(node.attributes.item(j).nodeName == 'class') {
153				if(node.attributes.item(j).nodeValue == c) {
154					var theStyle = eval('node.style.' + p);
155					if((theStyle != "") && (theStyle != null)) {
156						return theStyle;
157					}
158				}
159			}
160		}		
161	}
162	// if we got here it's because we didn't find anything
163	// try styleSheets
164	var sheets = document.styleSheets;
165	if(sheets.length > 0) {
166		// loop over each sheet
167		for(var x = 0; x < sheets.length; x++) {
168			// grab stylesheet rules
169			var rules = sheets[x].cssRules;
170			if(rules.length > 0) {
171				// check each rule
172				for(var y = 0; y < rules.length; y++) {
173					var z = rules[y].style;
174					// selectorText broken in NS 6/Mozilla: see
175					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
176					ugly_selectorText_workaround();
177					if(allStyleRules) {
178						if((allStyleRules[y] == c) ||
179						   (allStyleRules[y] == (t + "." + c))) {
180							return z[p];
181						}			
182					} else {
183						// use the native selectorText and style stuff
184						if(((z[p] != "") && (z[p] != null)) &&
185						   ((rules[y].selectorText == c) ||
186						    (rules[y].selectorText == (t + "." + c)))) {
187							return z[p];
188						}
189					}
190				}
191			}
192		}
193	}
194
195	return null;
196}
197
198// setStyleByTag: given an element type, style property and 
199// value, and whether the property should override inline styles or
200// just global stylesheet preferences, apply the style.
201// args:
202//  e - element type or id
203//  p - property
204//  v - value
205//  g - boolean 0: modify global only; 1: modify all elements in document
206function setStyleByTag(e, p, v, g) {
207	if(g) {
208		var elements = document.getElementsByTagName(e);
209		for(var i = 0; i < elements.length; i++) {
210			elements.item(i).style[p] = v;
211		}
212	} else {
213		var sheets = document.styleSheets;
214		if(sheets.length > 0) {
215			for(var i = 0; i < sheets.length; i++) {
216				var rules = sheets[i].cssRules;
217				if(rules.length > 0) {
218					for(var j = 0; j < rules.length; j++) {
219						var s = rules[j].style;
220						// selectorText broken in NS 6/Mozilla: see
221						// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
222						ugly_selectorText_workaround();
223						if(allStyleRules) {
224							if(allStyleRules[j] == e) {
225								s[p] = v;
226							}			
227						} else {
228							// use the native selectorText and style stuff
229							if(((s[p] != "") && (s[p] != null)) &&
230							   (rules[j].selectorText == e)) {
231								s[p] = v;
232							}
233						}
234
235					}
236				}
237			}
238		}
239	}
240}
241
242// getStyleByTag: given an element type and style property, return
243// the property's value
244// args:
245//  e - element type
246//  p - property
247function getStyleByTag(e, p) {
248	var sheets = document.styleSheets;
249	if(sheets.length > 0) {
250		for(var i = 0; i < sheets.length; i++) {
251			var rules = sheets[i].cssRules;
252			if(rules.length > 0) {
253				for(var j = 0; j < rules.length; j++) {
254					var s = rules[j].style;
255					// selectorText broken in NS 6/Mozilla: see
256					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
257					ugly_selectorText_workaround();
258					if(allStyleRules) {
259						if(allStyleRules[j] == e) {
260							return s[p];
261						}			
262					} else {
263						// use the native selectorText and style stuff
264						if(((s[p] != "") && (s[p] != null)) &&
265						   (rules[j].selectorText == e)) {
266							return s[p];
267						}
268					}
269
270				}
271			}
272		}
273	}
274
275
276	// if we don't find any style sheets, return the value for the first
277	// element of this type we encounter without a CLASS or STYLE attribute
278	var elements = document.getElementsByTagName(e);
279	var sawClassOrStyleAttribute = false;
280	for(var i = 0; i < elements.length; i++) {
281		var node = elements.item(i);
282		for(var j = 0; j < node.attributes.length; j++) {
283			if((node.attributes.item(j).nodeName == 'class') ||
284			   (node.attributes.item(j).nodeName == 'style')){
285			   sawClassOrStyleAttribute = true;
286			}
287		}
288		if(! sawClassOrStyleAttribute) {
289			return elements.item(i).style[p];
290		}
291	}
292}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.