Subversion Repositories Applications.papyrus

Rev

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