Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.html.metrics");
12
dojo.require("dojo.html.layout");
13
dojo.html.getScrollbar = function () {
14
	var scroll = document.createElement("div");
15
	scroll.style.width = "100px";
16
	scroll.style.height = "100px";
17
	scroll.style.overflow = "scroll";
18
	scroll.style.position = "absolute";
19
	scroll.style.top = "-300px";
20
	scroll.style.left = "0px";
21
	var test = document.createElement("div");
22
	test.style.width = "400px";
23
	test.style.height = "400px";
24
	scroll.appendChild(test);
25
	dojo.body().appendChild(scroll);
26
	var width = scroll.offsetWidth - scroll.clientWidth;
27
	dojo.body().removeChild(scroll);
28
	scroll.removeChild(test);
29
	scroll = test = null;
30
	return {width:width};
31
};
32
dojo.html.getFontMeasurements = function () {
33
	var heights = {"1em":0, "1ex":0, "100%":0, "12pt":0, "16px":0, "xx-small":0, "x-small":0, "small":0, "medium":0, "large":0, "x-large":0, "xx-large":0};
34
	if (dojo.render.html.ie) {
35
		document.documentElement.style.fontSize = "100%";
36
	}
37
	var div = document.createElement("div");
38
	div.style.position = "absolute";
39
	div.style.left = "-100px";
40
	div.style.top = "0";
41
	div.style.width = "30px";
42
	div.style.height = "1000em";
43
	div.style.border = "0";
44
	div.style.margin = "0";
45
	div.style.padding = "0";
46
	div.style.outline = "0";
47
	div.style.lineHeight = "1";
48
	div.style.overflow = "hidden";
49
	dojo.body().appendChild(div);
50
	for (var p in heights) {
51
		div.style.fontSize = p;
52
		heights[p] = Math.round(div.offsetHeight * 12 / 16) * 16 / 12 / 1000;
53
	}
54
	dojo.body().removeChild(div);
55
	div = null;
56
	return heights;
57
};
58
dojo.html._fontMeasurements = null;
59
dojo.html.getCachedFontMeasurements = function (recalculate) {
60
	if (recalculate || !dojo.html._fontMeasurements) {
61
		dojo.html._fontMeasurements = dojo.html.getFontMeasurements();
62
	}
63
	return dojo.html._fontMeasurements;
64
};
65
dojo.html.measureFragment = function (node, html, boxType) {
66
	var clone = node.cloneNode(true);
67
	clone.innerHTML = html;
68
	node.parentNode.appendChild(clone);
69
	var ret = dojo.html.getElementBox(clone, boxType);
70
	node.parentNode.removeChild(clone);
71
	clone = null;
72
	return ret;
73
};
74
dojo.html.getFittedFragment = function (node, html) {
75
	function cl(node) {
76
		var element = document.createElement(node.tagName);
77
		element.id = node.id + "-clone";
78
		element.className = node.className;
79
		for (var j = 0; j < node.attributes.length; j++) {
80
			if (node.attributes[j].specified) {
81
				if (node.attributes[j].nodeName.toLowerCase() != "style" && node.attributes[j].nodeName.toLowerCase() != "edited" && node.attributes[j].nodeName.toLowerCase() != "contenteditable" && node.attributes[j].nodeName.toLowerCase() != "id" && node.attributes[j].nodeName.toLowerCase() != "class") {
82
					element.setAttribute(node.attributes[j].nodeName.toLowerCase(), node.attributes[j].nodeValue);
83
				}
84
			}
85
		}
86
		return element;
87
	}
88
	var height = dojo.html.getFontMeasurements()["16px"];
89
	var n = cl(node);
90
	n.style.width = dojo.html.getBorderBox(node).width + "px";
91
	n.style.height = (height + 4) + "px";
92
	node.parentNode.appendChild(n);
93
	var rem = dojo.html.fitToElement(n, html);
94
	var ret = n.innerHTML;
95
	n.parentNode.removeChild(n);
96
	return ret;
97
};
98
dojo.html.fitToElement = function (node, html) {
99
	function cl(node) {
100
		var element = document.createElement(node.tagName);
101
		element.id = node.id + "-clone";
102
		element.className = node.className;
103
		for (var j = 0; j < node.attributes.length; j++) {
104
			if (node.attributes[j].specified) {
105
				if (node.attributes[j].nodeName.toLowerCase() != "style" && node.attributes[j].nodeName.toLowerCase() != "edited" && node.attributes[j].nodeName.toLowerCase() != "contenteditable" && node.attributes[j].nodeName.toLowerCase() != "id" && node.attributes[j].nodeName.toLowerCase() != "class") {
106
					element.setAttribute(node.attributes[j].nodeName.toLowerCase(), node.attributes[j].nodeValue);
107
				}
108
			}
109
		}
110
		return element;
111
	}
112
	var clone = cl(node);
113
	node.parentNode.appendChild(clone);
114
	var t = dojo.html.getBorderBox(node);
115
	clone.style.width = t.width + "px";
116
	var singletons = ["br", "img", "hr", "input", "!--"];
117
	var chop = ["<BR>", "<br>", "<br/>", "<br />", "<p></p>", "<P></P>"];
118
	var openTags = [];
119
	var str = html;
120
	var i = 0;
121
	var limit = str.length;
122
	var add = 0;
123
	var doLoop = true;
124
	clone.innerHTML = str;
125
	while (doLoop) {
126
		add = Math.round((limit - i) / 2);
127
		if (add <= 1) {
128
			doLoop = false;
129
		}
130
		i += add;
131
		clone.innerHTML = str.substr(0, i);
132
		if (clone.offsetHeight > t.height) {
133
			limit = i;
134
			i -= add;
135
		}
136
	}
137
	if (str.substr(0, i) != str) {
138
		var lastSpace = str.substr(0, i).lastIndexOf(" ");
139
		var lastNewLine = str.substr(0, i).lastIndexOf("\n");
140
		var lastGreater = str.substr(0, i).lastIndexOf(">");
141
		var lastLess = str.substr(0, i).lastIndexOf("<");
142
		if (lastLess <= lastGreater && lastNewLine == i - 1) {
143
			i = i;
144
		} else {
145
			if (lastSpace != -1 && lastSpace > lastGreater && lastGreater > lastLess) {
146
				i = lastSpace + 1;
147
			} else {
148
				if (lastLess > lastGreater) {
149
					i = lastLess;
150
				} else {
151
					if (lastGreater != -1) {
152
						i = lastGreater + 1;
153
					}
154
				}
155
			}
156
		}
157
	}
158
	str = str.substr(0, i);
159
	var ret = html.substr(str.length);
160
	var doPush = true;
161
	var tags = str.split("<");
162
	tags.shift();
163
	for (var j = 0; j < tags.length; j++) {
164
		tags[j] = tags[j].split(">")[0];
165
		if (tags[j].charAt(tags[j].length - 1) == "/") {
166
			continue;
167
		}
168
		if (tags[j].charAt(0) != "/") {
169
			for (var k = 0; k < singletons.length; k++) {
170
				if (tags[j].split(" ")[0].toLowerCase() == singletons[k]) {
171
					doPush = false;
172
				}
173
			}
174
			if (doPush) {
175
				openTags.push(tags[j]);
176
			}
177
			doPush = true;
178
		} else {
179
			openTags.pop();
180
		}
181
	}
182
	for (var j = 0; j < chop.length; j++) {
183
		if (ret.charAt(0) == "\n") {
184
			ret = ret.substr(1);
185
		}
186
		while (ret.indexOf(chop[j]) == 0) {
187
			ret = ret.substr(chop[j].length);
188
		}
189
	}
190
	for (var j = openTags.length - 1; j >= 0; j--) {
191
		if (str.lastIndexOf(openTags[j]) == (str.length - openTags[j].length - 1)) {
192
			str = str.substring(0, str.lastIndexOf(openTags[j]));
193
		} else {
194
			str += "</" + openTags[j] + ">";
195
		}
196
		if (ret.length > 0) {
197
			ret = "<" + openTags[j] + ">" + ret;
198
		}
199
	}
200
	for (var j = 0; j < chop.length; j++) {
201
		if (ret.charAt(0) == "\n") {
202
			ret = ret.substr(1);
203
		}
204
		while (ret.indexOf(chop[j]) == 0) {
205
			ret = ret.substr(chop[j].length);
206
		}
207
	}
208
	node.innerHTML = str;
209
	clone.parentNode.removeChild(clone);
210
	clone = null;
211
	return ret;
212
};
213