1 | /**
|
---|
2 | * Package: svgedit.browser
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2
|
---|
5 | *
|
---|
6 | * Copyright(c) 2010 Jeff Schiller
|
---|
7 | * Copyright(c) 2010 Alexis Deveria
|
---|
8 | */
|
---|
9 |
|
---|
10 | // Dependencies:
|
---|
11 | // 1) jQuery (for $.alert())
|
---|
12 |
|
---|
13 | var svgedit = svgedit || {};
|
---|
14 |
|
---|
15 | (function() {
|
---|
16 |
|
---|
17 | if (!svgedit.browser) {
|
---|
18 | svgedit.browser = {};
|
---|
19 | }
|
---|
20 | var supportsSvg_ = (function() {
|
---|
21 | return !!document.createElementNS && !!document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGRect;
|
---|
22 | })();
|
---|
23 | svgedit.browser.supportsSvg = function() { return supportsSvg_; }
|
---|
24 | if(!svgedit.browser.supportsSvg()) {
|
---|
25 | window.location = "browser-not-supported.html";
|
---|
26 | }
|
---|
27 | else{
|
---|
28 |
|
---|
29 | var svgns = 'http://www.w3.org/2000/svg';
|
---|
30 | var userAgent = navigator.userAgent;
|
---|
31 | var svg = document.createElementNS(svgns, 'svg');
|
---|
32 |
|
---|
33 | // Note: Browser sniffing should only be used if no other detection method is possible
|
---|
34 | var isOpera_ = !!window.opera;
|
---|
35 | var isWebkit_ = userAgent.indexOf("AppleWebKit") >= 0;
|
---|
36 | var isGecko_ = userAgent.indexOf('Gecko/') >= 0;
|
---|
37 | var isIE_ = userAgent.indexOf('MSIE') >= 0;
|
---|
38 | var isChrome_ = userAgent.indexOf('Chrome/') >= 0;
|
---|
39 | var isWindows_ = userAgent.indexOf('Windows') >= 0;
|
---|
40 | var isMac_ = userAgent.indexOf('Macintosh') >= 0;
|
---|
41 | var isTouch_ = 'ontouchstart' in window;
|
---|
42 |
|
---|
43 | var supportsSelectors_ = (function() {
|
---|
44 | return !!svg.querySelector;
|
---|
45 | })();
|
---|
46 |
|
---|
47 | var supportsXpath_ = (function() {
|
---|
48 | return !!document.evaluate;
|
---|
49 | })();
|
---|
50 |
|
---|
51 | // segList functions (for FF1.5 and 2.0)
|
---|
52 | var supportsPathReplaceItem_ = (function() {
|
---|
53 | var path = document.createElementNS(svgns, 'path');
|
---|
54 | path.setAttribute('d','M0,0 10,10');
|
---|
55 | var seglist = path.pathSegList;
|
---|
56 | var seg = path.createSVGPathSegLinetoAbs(5,5);
|
---|
57 | try {
|
---|
58 | seglist.replaceItem(seg, 0);
|
---|
59 | return true;
|
---|
60 | } catch(err) {}
|
---|
61 | return false;
|
---|
62 | })();
|
---|
63 |
|
---|
64 | var supportsPathInsertItemBefore_ = (function() {
|
---|
65 | var path = document.createElementNS(svgns,'path');
|
---|
66 | path.setAttribute('d','M0,0 10,10');
|
---|
67 | var seglist = path.pathSegList;
|
---|
68 | var seg = path.createSVGPathSegLinetoAbs(5,5);
|
---|
69 | try {
|
---|
70 | seglist.insertItemBefore(seg, 0);
|
---|
71 | return true;
|
---|
72 | } catch(err) {}
|
---|
73 | return false;
|
---|
74 | })();
|
---|
75 |
|
---|
76 | // text character positioning (for IE9)
|
---|
77 | var supportsGoodTextCharPos_ = (function() {
|
---|
78 | var retValue = false;
|
---|
79 | var svgroot = document.createElementNS(svgns, 'svg');
|
---|
80 | var svgcontent = document.createElementNS(svgns, 'svg');
|
---|
81 | document.documentElement.appendChild(svgroot);
|
---|
82 | svgcontent.setAttribute('x', 5);
|
---|
83 | svgroot.appendChild(svgcontent);
|
---|
84 | var text = document.createElementNS(svgns,'text');
|
---|
85 | text.textContent = 'a';
|
---|
86 | svgcontent.appendChild(text);
|
---|
87 | var pos = text.getStartPositionOfChar(0)
|
---|
88 | pos = pos.x; //if you put it on one line it fails when compiled
|
---|
89 | document.documentElement.removeChild(svgroot);
|
---|
90 | return (pos === 0);
|
---|
91 | })();
|
---|
92 |
|
---|
93 | var supportsPathBBox_ = (function() {
|
---|
94 | var svgcontent = document.createElementNS(svgns, 'svg');
|
---|
95 | document.documentElement.appendChild(svgcontent);
|
---|
96 | var path = document.createElementNS(svgns, 'path');
|
---|
97 | path.setAttribute('d','M0,0 C0,0 10,10 10,0');
|
---|
98 | svgcontent.appendChild(path);
|
---|
99 | var bbox = path.getBBox();
|
---|
100 | document.documentElement.removeChild(svgcontent);
|
---|
101 | return (bbox.height > 4 && bbox.height < 5);
|
---|
102 | })();
|
---|
103 |
|
---|
104 | // Support for correct bbox sizing on groups with horizontal/vertical lines
|
---|
105 | var supportsHVLineContainerBBox_ = (function() {
|
---|
106 | var svgcontent = document.createElementNS(svgns, 'svg');
|
---|
107 | document.documentElement.appendChild(svgcontent);
|
---|
108 | var path = document.createElementNS(svgns, 'path');
|
---|
109 | path.setAttribute('d','M0,0 10,0');
|
---|
110 | var path2 = document.createElementNS(svgns, 'path');
|
---|
111 | path2.setAttribute('d','M5,0 15,0');
|
---|
112 | var g = document.createElementNS(svgns, 'g');
|
---|
113 | g.appendChild(path);
|
---|
114 | g.appendChild(path2);
|
---|
115 | svgcontent.appendChild(g);
|
---|
116 | var bbox = g.getBBox();
|
---|
117 | document.documentElement.removeChild(svgcontent);
|
---|
118 | // Webkit gives 0, FF gives 10, Opera (correctly) gives 15
|
---|
119 | return (bbox.width == 15);
|
---|
120 | })();
|
---|
121 |
|
---|
122 | var supportsEditableText_ = (function() {
|
---|
123 | // TODO: Find better way to check support for this
|
---|
124 | return isOpera_;
|
---|
125 | })();
|
---|
126 |
|
---|
127 | var supportsGoodDecimals_ = (function() {
|
---|
128 | // Correct decimals on clone attributes (Opera < 10.5/win/non-en)
|
---|
129 | var rect = document.createElementNS(svgns, 'rect');
|
---|
130 | rect.setAttribute('x',.1);
|
---|
131 | var crect = rect.cloneNode(false);
|
---|
132 | var retValue = (crect.getAttribute('x').indexOf(',') == -1);
|
---|
133 | if(!retValue) {
|
---|
134 | $.alert("NOTE: This version of Opera is known to contain bugs in SVG-edit.\n\
|
---|
135 | Please upgrade to the <a href='http://opera.com'>latest version</a> in which the problems have been fixed.");
|
---|
136 | }
|
---|
137 | return retValue;
|
---|
138 | })();
|
---|
139 |
|
---|
140 | var supportsNonScalingStroke_ = (function() {
|
---|
141 | var rect = document.createElementNS(svgns, 'rect');
|
---|
142 | rect.setAttribute('style','vector-effect:non-scaling-stroke');
|
---|
143 | return rect.style.vectorEffect === 'non-scaling-stroke';
|
---|
144 | })();
|
---|
145 |
|
---|
146 | var supportsNativeSVGTransformLists_ = (function() {
|
---|
147 | var rect = document.createElementNS(svgns, 'rect');
|
---|
148 | var rxform = rect.transform.baseVal;
|
---|
149 |
|
---|
150 | var t1 = svg.createSVGTransform();
|
---|
151 | rxform.appendItem(t1);
|
---|
152 | return rxform.getItem(0) == t1;
|
---|
153 | })();
|
---|
154 |
|
---|
155 | // Public API
|
---|
156 |
|
---|
157 | svgedit.browser.isOpera = function() { return isOpera_; }
|
---|
158 | svgedit.browser.isWebkit = function() { return isWebkit_; }
|
---|
159 | svgedit.browser.isGecko = function() { return isGecko_; }
|
---|
160 | svgedit.browser.isIE = function() { return isIE_; }
|
---|
161 | svgedit.browser.isChrome = function() { return isChrome_; }
|
---|
162 | svgedit.browser.isWindows = function() { return isWindows_; }
|
---|
163 | svgedit.browser.isMac = function() { return isMac_; }
|
---|
164 | svgedit.browser.isTouch = function() { return isTouch_; }
|
---|
165 |
|
---|
166 | svgedit.browser.supportsSelectors = function() { return supportsSelectors_; }
|
---|
167 | svgedit.browser.supportsXpath = function() { return supportsXpath_; }
|
---|
168 |
|
---|
169 | svgedit.browser.supportsPathReplaceItem = function() { return supportsPathReplaceItem_; }
|
---|
170 | svgedit.browser.supportsPathInsertItemBefore = function() { return supportsPathInsertItemBefore_; }
|
---|
171 | svgedit.browser.supportsPathBBox = function() { return supportsPathBBox_; }
|
---|
172 | svgedit.browser.supportsHVLineContainerBBox = function() { return supportsHVLineContainerBBox_; }
|
---|
173 | svgedit.browser.supportsGoodTextCharPos = function() { return supportsGoodTextCharPos_; }
|
---|
174 | svgedit.browser.supportsEditableText = function() { return supportsEditableText_; }
|
---|
175 | svgedit.browser.supportsGoodDecimals = function() { return supportsGoodDecimals_; }
|
---|
176 | svgedit.browser.supportsNonScalingStroke = function() { return supportsNonScalingStroke_; }
|
---|
177 | svgedit.browser.supportsNativeTransformLists = function() { return supportsNativeSVGTransformLists_; }
|
---|
178 |
|
---|
179 | }
|
---|
180 |
|
---|
181 | })();
|
---|