polyfill.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /* jshint ignore:start */
  2. // PhantomJS doesn't have support for Function.prototype.bind, which has caused confusion. Use
  3. // this polyfill to avoid the confusion.
  4. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Polyfill
  5. if (!Function.prototype.bind) {
  6. Function.prototype.bind = function(oThis) {
  7. if (typeof this !== 'function') {
  8. // closest thing possible to the ECMAScript 5
  9. // internal IsCallable function
  10. throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
  11. }
  12. var aArgs = Array.prototype.slice.call(arguments, 1),
  13. fToBind = this,
  14. fNOP = function() {},
  15. fBound = function() {
  16. return fToBind.apply(this instanceof fNOP ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)));
  17. };
  18. fNOP.prototype = this.prototype;
  19. fBound.prototype = new fNOP();
  20. return fBound;
  21. };
  22. }
  23. // SVGPathSeg API polyfill
  24. // https://github.com/progers/pathseg
  25. //
  26. // This is a drop-in replacement for the SVGPathSeg and SVGPathSegList APIs that were removed from
  27. // SVG2 (https://lists.w3.org/Archives/Public/www-svg/2015Jun/0044.html), including the latest spec
  28. // changes which were implemented in Firefox 43 and Chrome 46.
  29. (function() { "use strict";
  30. if (!("SVGPathSeg" in window)) {
  31. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGPathSeg
  32. window.SVGPathSeg = function(type, typeAsLetter, owningPathSegList) {
  33. this.pathSegType = type;
  34. this.pathSegTypeAsLetter = typeAsLetter;
  35. this._owningPathSegList = owningPathSegList;
  36. }
  37. window.SVGPathSeg.prototype.classname = "SVGPathSeg";
  38. window.SVGPathSeg.PATHSEG_UNKNOWN = 0;
  39. window.SVGPathSeg.PATHSEG_CLOSEPATH = 1;
  40. window.SVGPathSeg.PATHSEG_MOVETO_ABS = 2;
  41. window.SVGPathSeg.PATHSEG_MOVETO_REL = 3;
  42. window.SVGPathSeg.PATHSEG_LINETO_ABS = 4;
  43. window.SVGPathSeg.PATHSEG_LINETO_REL = 5;
  44. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS = 6;
  45. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL = 7;
  46. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS = 8;
  47. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL = 9;
  48. window.SVGPathSeg.PATHSEG_ARC_ABS = 10;
  49. window.SVGPathSeg.PATHSEG_ARC_REL = 11;
  50. window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS = 12;
  51. window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL = 13;
  52. window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS = 14;
  53. window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL = 15;
  54. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS = 16;
  55. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL = 17;
  56. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS = 18;
  57. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL = 19;
  58. // Notify owning PathSegList on any changes so they can be synchronized back to the path element.
  59. window.SVGPathSeg.prototype._segmentChanged = function() {
  60. if (this._owningPathSegList)
  61. this._owningPathSegList.segmentChanged(this);
  62. }
  63. window.SVGPathSegClosePath = function(owningPathSegList) {
  64. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CLOSEPATH, "z", owningPathSegList);
  65. }
  66. window.SVGPathSegClosePath.prototype = Object.create(window.SVGPathSeg.prototype);
  67. window.SVGPathSegClosePath.prototype.toString = function() { return "[object SVGPathSegClosePath]"; }
  68. window.SVGPathSegClosePath.prototype._asPathString = function() { return this.pathSegTypeAsLetter; }
  69. window.SVGPathSegClosePath.prototype.clone = function() { return new window.SVGPathSegClosePath(undefined); }
  70. window.SVGPathSegMovetoAbs = function(owningPathSegList, x, y) {
  71. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_MOVETO_ABS, "M", owningPathSegList);
  72. this._x = x;
  73. this._y = y;
  74. }
  75. window.SVGPathSegMovetoAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  76. window.SVGPathSegMovetoAbs.prototype.toString = function() { return "[object SVGPathSegMovetoAbs]"; }
  77. window.SVGPathSegMovetoAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x + " " + this._y; }
  78. window.SVGPathSegMovetoAbs.prototype.clone = function() { return new window.SVGPathSegMovetoAbs(undefined, this._x, this._y); }
  79. Object.defineProperty(window.SVGPathSegMovetoAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  80. Object.defineProperty(window.SVGPathSegMovetoAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  81. window.SVGPathSegMovetoRel = function(owningPathSegList, x, y) {
  82. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_MOVETO_REL, "m", owningPathSegList);
  83. this._x = x;
  84. this._y = y;
  85. }
  86. window.SVGPathSegMovetoRel.prototype = Object.create(window.SVGPathSeg.prototype);
  87. window.SVGPathSegMovetoRel.prototype.toString = function() { return "[object SVGPathSegMovetoRel]"; }
  88. window.SVGPathSegMovetoRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x + " " + this._y; }
  89. window.SVGPathSegMovetoRel.prototype.clone = function() { return new window.SVGPathSegMovetoRel(undefined, this._x, this._y); }
  90. Object.defineProperty(window.SVGPathSegMovetoRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  91. Object.defineProperty(window.SVGPathSegMovetoRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  92. window.SVGPathSegLinetoAbs = function(owningPathSegList, x, y) {
  93. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_LINETO_ABS, "L", owningPathSegList);
  94. this._x = x;
  95. this._y = y;
  96. }
  97. window.SVGPathSegLinetoAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  98. window.SVGPathSegLinetoAbs.prototype.toString = function() { return "[object SVGPathSegLinetoAbs]"; }
  99. window.SVGPathSegLinetoAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x + " " + this._y; }
  100. window.SVGPathSegLinetoAbs.prototype.clone = function() { return new window.SVGPathSegLinetoAbs(undefined, this._x, this._y); }
  101. Object.defineProperty(window.SVGPathSegLinetoAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  102. Object.defineProperty(window.SVGPathSegLinetoAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  103. window.SVGPathSegLinetoRel = function(owningPathSegList, x, y) {
  104. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_LINETO_REL, "l", owningPathSegList);
  105. this._x = x;
  106. this._y = y;
  107. }
  108. window.SVGPathSegLinetoRel.prototype = Object.create(window.SVGPathSeg.prototype);
  109. window.SVGPathSegLinetoRel.prototype.toString = function() { return "[object SVGPathSegLinetoRel]"; }
  110. window.SVGPathSegLinetoRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x + " " + this._y; }
  111. window.SVGPathSegLinetoRel.prototype.clone = function() { return new window.SVGPathSegLinetoRel(undefined, this._x, this._y); }
  112. Object.defineProperty(window.SVGPathSegLinetoRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  113. Object.defineProperty(window.SVGPathSegLinetoRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  114. window.SVGPathSegCurvetoCubicAbs = function(owningPathSegList, x, y, x1, y1, x2, y2) {
  115. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS, "C", owningPathSegList);
  116. this._x = x;
  117. this._y = y;
  118. this._x1 = x1;
  119. this._y1 = y1;
  120. this._x2 = x2;
  121. this._y2 = y2;
  122. }
  123. window.SVGPathSegCurvetoCubicAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  124. window.SVGPathSegCurvetoCubicAbs.prototype.toString = function() { return "[object SVGPathSegCurvetoCubicAbs]"; }
  125. window.SVGPathSegCurvetoCubicAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x1 + " " + this._y1 + " " + this._x2 + " " + this._y2 + " " + this._x + " " + this._y; }
  126. window.SVGPathSegCurvetoCubicAbs.prototype.clone = function() { return new window.SVGPathSegCurvetoCubicAbs(undefined, this._x, this._y, this._x1, this._y1, this._x2, this._y2); }
  127. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  128. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  129. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, "x1", { get: function() { return this._x1; }, set: function(x1) { this._x1 = x1; this._segmentChanged(); }, enumerable: true });
  130. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, "y1", { get: function() { return this._y1; }, set: function(y1) { this._y1 = y1; this._segmentChanged(); }, enumerable: true });
  131. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, "x2", { get: function() { return this._x2; }, set: function(x2) { this._x2 = x2; this._segmentChanged(); }, enumerable: true });
  132. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, "y2", { get: function() { return this._y2; }, set: function(y2) { this._y2 = y2; this._segmentChanged(); }, enumerable: true });
  133. window.SVGPathSegCurvetoCubicRel = function(owningPathSegList, x, y, x1, y1, x2, y2) {
  134. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL, "c", owningPathSegList);
  135. this._x = x;
  136. this._y = y;
  137. this._x1 = x1;
  138. this._y1 = y1;
  139. this._x2 = x2;
  140. this._y2 = y2;
  141. }
  142. window.SVGPathSegCurvetoCubicRel.prototype = Object.create(window.SVGPathSeg.prototype);
  143. window.SVGPathSegCurvetoCubicRel.prototype.toString = function() { return "[object SVGPathSegCurvetoCubicRel]"; }
  144. window.SVGPathSegCurvetoCubicRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x1 + " " + this._y1 + " " + this._x2 + " " + this._y2 + " " + this._x + " " + this._y; }
  145. window.SVGPathSegCurvetoCubicRel.prototype.clone = function() { return new window.SVGPathSegCurvetoCubicRel(undefined, this._x, this._y, this._x1, this._y1, this._x2, this._y2); }
  146. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  147. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  148. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, "x1", { get: function() { return this._x1; }, set: function(x1) { this._x1 = x1; this._segmentChanged(); }, enumerable: true });
  149. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, "y1", { get: function() { return this._y1; }, set: function(y1) { this._y1 = y1; this._segmentChanged(); }, enumerable: true });
  150. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, "x2", { get: function() { return this._x2; }, set: function(x2) { this._x2 = x2; this._segmentChanged(); }, enumerable: true });
  151. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, "y2", { get: function() { return this._y2; }, set: function(y2) { this._y2 = y2; this._segmentChanged(); }, enumerable: true });
  152. window.SVGPathSegCurvetoQuadraticAbs = function(owningPathSegList, x, y, x1, y1) {
  153. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS, "Q", owningPathSegList);
  154. this._x = x;
  155. this._y = y;
  156. this._x1 = x1;
  157. this._y1 = y1;
  158. }
  159. window.SVGPathSegCurvetoQuadraticAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  160. window.SVGPathSegCurvetoQuadraticAbs.prototype.toString = function() { return "[object SVGPathSegCurvetoQuadraticAbs]"; }
  161. window.SVGPathSegCurvetoQuadraticAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x1 + " " + this._y1 + " " + this._x + " " + this._y; }
  162. window.SVGPathSegCurvetoQuadraticAbs.prototype.clone = function() { return new window.SVGPathSegCurvetoQuadraticAbs(undefined, this._x, this._y, this._x1, this._y1); }
  163. Object.defineProperty(window.SVGPathSegCurvetoQuadraticAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  164. Object.defineProperty(window.SVGPathSegCurvetoQuadraticAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  165. Object.defineProperty(window.SVGPathSegCurvetoQuadraticAbs.prototype, "x1", { get: function() { return this._x1; }, set: function(x1) { this._x1 = x1; this._segmentChanged(); }, enumerable: true });
  166. Object.defineProperty(window.SVGPathSegCurvetoQuadraticAbs.prototype, "y1", { get: function() { return this._y1; }, set: function(y1) { this._y1 = y1; this._segmentChanged(); }, enumerable: true });
  167. window.SVGPathSegCurvetoQuadraticRel = function(owningPathSegList, x, y, x1, y1) {
  168. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL, "q", owningPathSegList);
  169. this._x = x;
  170. this._y = y;
  171. this._x1 = x1;
  172. this._y1 = y1;
  173. }
  174. window.SVGPathSegCurvetoQuadraticRel.prototype = Object.create(window.SVGPathSeg.prototype);
  175. window.SVGPathSegCurvetoQuadraticRel.prototype.toString = function() { return "[object SVGPathSegCurvetoQuadraticRel]"; }
  176. window.SVGPathSegCurvetoQuadraticRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x1 + " " + this._y1 + " " + this._x + " " + this._y; }
  177. window.SVGPathSegCurvetoQuadraticRel.prototype.clone = function() { return new window.SVGPathSegCurvetoQuadraticRel(undefined, this._x, this._y, this._x1, this._y1); }
  178. Object.defineProperty(window.SVGPathSegCurvetoQuadraticRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  179. Object.defineProperty(window.SVGPathSegCurvetoQuadraticRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  180. Object.defineProperty(window.SVGPathSegCurvetoQuadraticRel.prototype, "x1", { get: function() { return this._x1; }, set: function(x1) { this._x1 = x1; this._segmentChanged(); }, enumerable: true });
  181. Object.defineProperty(window.SVGPathSegCurvetoQuadraticRel.prototype, "y1", { get: function() { return this._y1; }, set: function(y1) { this._y1 = y1; this._segmentChanged(); }, enumerable: true });
  182. window.SVGPathSegArcAbs = function(owningPathSegList, x, y, r1, r2, angle, largeArcFlag, sweepFlag) {
  183. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_ARC_ABS, "A", owningPathSegList);
  184. this._x = x;
  185. this._y = y;
  186. this._r1 = r1;
  187. this._r2 = r2;
  188. this._angle = angle;
  189. this._largeArcFlag = largeArcFlag;
  190. this._sweepFlag = sweepFlag;
  191. }
  192. window.SVGPathSegArcAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  193. window.SVGPathSegArcAbs.prototype.toString = function() { return "[object SVGPathSegArcAbs]"; }
  194. window.SVGPathSegArcAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._r1 + " " + this._r2 + " " + this._angle + " " + (this._largeArcFlag ? "1" : "0") + " " + (this._sweepFlag ? "1" : "0") + " " + this._x + " " + this._y; }
  195. window.SVGPathSegArcAbs.prototype.clone = function() { return new window.SVGPathSegArcAbs(undefined, this._x, this._y, this._r1, this._r2, this._angle, this._largeArcFlag, this._sweepFlag); }
  196. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  197. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  198. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "r1", { get: function() { return this._r1; }, set: function(r1) { this._r1 = r1; this._segmentChanged(); }, enumerable: true });
  199. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "r2", { get: function() { return this._r2; }, set: function(r2) { this._r2 = r2; this._segmentChanged(); }, enumerable: true });
  200. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "angle", { get: function() { return this._angle; }, set: function(angle) { this._angle = angle; this._segmentChanged(); }, enumerable: true });
  201. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "largeArcFlag", { get: function() { return this._largeArcFlag; }, set: function(largeArcFlag) { this._largeArcFlag = largeArcFlag; this._segmentChanged(); }, enumerable: true });
  202. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "sweepFlag", { get: function() { return this._sweepFlag; }, set: function(sweepFlag) { this._sweepFlag = sweepFlag; this._segmentChanged(); }, enumerable: true });
  203. window.SVGPathSegArcRel = function(owningPathSegList, x, y, r1, r2, angle, largeArcFlag, sweepFlag) {
  204. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_ARC_REL, "a", owningPathSegList);
  205. this._x = x;
  206. this._y = y;
  207. this._r1 = r1;
  208. this._r2 = r2;
  209. this._angle = angle;
  210. this._largeArcFlag = largeArcFlag;
  211. this._sweepFlag = sweepFlag;
  212. }
  213. window.SVGPathSegArcRel.prototype = Object.create(window.SVGPathSeg.prototype);
  214. window.SVGPathSegArcRel.prototype.toString = function() { return "[object SVGPathSegArcRel]"; }
  215. window.SVGPathSegArcRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._r1 + " " + this._r2 + " " + this._angle + " " + (this._largeArcFlag ? "1" : "0") + " " + (this._sweepFlag ? "1" : "0") + " " + this._x + " " + this._y; }
  216. window.SVGPathSegArcRel.prototype.clone = function() { return new window.SVGPathSegArcRel(undefined, this._x, this._y, this._r1, this._r2, this._angle, this._largeArcFlag, this._sweepFlag); }
  217. Object.defineProperty(window.SVGPathSegArcRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  218. Object.defineProperty(window.SVGPathSegArcRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  219. Object.defineProperty(window.SVGPathSegArcRel.prototype, "r1", { get: function() { return this._r1; }, set: function(r1) { this._r1 = r1; this._segmentChanged(); }, enumerable: true });
  220. Object.defineProperty(window.SVGPathSegArcRel.prototype, "r2", { get: function() { return this._r2; }, set: function(r2) { this._r2 = r2; this._segmentChanged(); }, enumerable: true });
  221. Object.defineProperty(window.SVGPathSegArcRel.prototype, "angle", { get: function() { return this._angle; }, set: function(angle) { this._angle = angle; this._segmentChanged(); }, enumerable: true });
  222. Object.defineProperty(window.SVGPathSegArcRel.prototype, "largeArcFlag", { get: function() { return this._largeArcFlag; }, set: function(largeArcFlag) { this._largeArcFlag = largeArcFlag; this._segmentChanged(); }, enumerable: true });
  223. Object.defineProperty(window.SVGPathSegArcRel.prototype, "sweepFlag", { get: function() { return this._sweepFlag; }, set: function(sweepFlag) { this._sweepFlag = sweepFlag; this._segmentChanged(); }, enumerable: true });
  224. window.SVGPathSegLinetoHorizontalAbs = function(owningPathSegList, x) {
  225. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS, "H", owningPathSegList);
  226. this._x = x;
  227. }
  228. window.SVGPathSegLinetoHorizontalAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  229. window.SVGPathSegLinetoHorizontalAbs.prototype.toString = function() { return "[object SVGPathSegLinetoHorizontalAbs]"; }
  230. window.SVGPathSegLinetoHorizontalAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x; }
  231. window.SVGPathSegLinetoHorizontalAbs.prototype.clone = function() { return new window.SVGPathSegLinetoHorizontalAbs(undefined, this._x); }
  232. Object.defineProperty(window.SVGPathSegLinetoHorizontalAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  233. window.SVGPathSegLinetoHorizontalRel = function(owningPathSegList, x) {
  234. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL, "h", owningPathSegList);
  235. this._x = x;
  236. }
  237. window.SVGPathSegLinetoHorizontalRel.prototype = Object.create(window.SVGPathSeg.prototype);
  238. window.SVGPathSegLinetoHorizontalRel.prototype.toString = function() { return "[object SVGPathSegLinetoHorizontalRel]"; }
  239. window.SVGPathSegLinetoHorizontalRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x; }
  240. window.SVGPathSegLinetoHorizontalRel.prototype.clone = function() { return new window.SVGPathSegLinetoHorizontalRel(undefined, this._x); }
  241. Object.defineProperty(window.SVGPathSegLinetoHorizontalRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  242. window.SVGPathSegLinetoVerticalAbs = function(owningPathSegList, y) {
  243. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS, "V", owningPathSegList);
  244. this._y = y;
  245. }
  246. window.SVGPathSegLinetoVerticalAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  247. window.SVGPathSegLinetoVerticalAbs.prototype.toString = function() { return "[object SVGPathSegLinetoVerticalAbs]"; }
  248. window.SVGPathSegLinetoVerticalAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._y; }
  249. window.SVGPathSegLinetoVerticalAbs.prototype.clone = function() { return new window.SVGPathSegLinetoVerticalAbs(undefined, this._y); }
  250. Object.defineProperty(window.SVGPathSegLinetoVerticalAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  251. window.SVGPathSegLinetoVerticalRel = function(owningPathSegList, y) {
  252. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL, "v", owningPathSegList);
  253. this._y = y;
  254. }
  255. window.SVGPathSegLinetoVerticalRel.prototype = Object.create(window.SVGPathSeg.prototype);
  256. window.SVGPathSegLinetoVerticalRel.prototype.toString = function() { return "[object SVGPathSegLinetoVerticalRel]"; }
  257. window.SVGPathSegLinetoVerticalRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._y; }
  258. window.SVGPathSegLinetoVerticalRel.prototype.clone = function() { return new window.SVGPathSegLinetoVerticalRel(undefined, this._y); }
  259. Object.defineProperty(window.SVGPathSegLinetoVerticalRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  260. window.SVGPathSegCurvetoCubicSmoothAbs = function(owningPathSegList, x, y, x2, y2) {
  261. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS, "S", owningPathSegList);
  262. this._x = x;
  263. this._y = y;
  264. this._x2 = x2;
  265. this._y2 = y2;
  266. }
  267. window.SVGPathSegCurvetoCubicSmoothAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  268. window.SVGPathSegCurvetoCubicSmoothAbs.prototype.toString = function() { return "[object SVGPathSegCurvetoCubicSmoothAbs]"; }
  269. window.SVGPathSegCurvetoCubicSmoothAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x2 + " " + this._y2 + " " + this._x + " " + this._y; }
  270. window.SVGPathSegCurvetoCubicSmoothAbs.prototype.clone = function() { return new window.SVGPathSegCurvetoCubicSmoothAbs(undefined, this._x, this._y, this._x2, this._y2); }
  271. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  272. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  273. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothAbs.prototype, "x2", { get: function() { return this._x2; }, set: function(x2) { this._x2 = x2; this._segmentChanged(); }, enumerable: true });
  274. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothAbs.prototype, "y2", { get: function() { return this._y2; }, set: function(y2) { this._y2 = y2; this._segmentChanged(); }, enumerable: true });
  275. window.SVGPathSegCurvetoCubicSmoothRel = function(owningPathSegList, x, y, x2, y2) {
  276. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL, "s", owningPathSegList);
  277. this._x = x;
  278. this._y = y;
  279. this._x2 = x2;
  280. this._y2 = y2;
  281. }
  282. window.SVGPathSegCurvetoCubicSmoothRel.prototype = Object.create(window.SVGPathSeg.prototype);
  283. window.SVGPathSegCurvetoCubicSmoothRel.prototype.toString = function() { return "[object SVGPathSegCurvetoCubicSmoothRel]"; }
  284. window.SVGPathSegCurvetoCubicSmoothRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x2 + " " + this._y2 + " " + this._x + " " + this._y; }
  285. window.SVGPathSegCurvetoCubicSmoothRel.prototype.clone = function() { return new window.SVGPathSegCurvetoCubicSmoothRel(undefined, this._x, this._y, this._x2, this._y2); }
  286. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  287. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  288. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothRel.prototype, "x2", { get: function() { return this._x2; }, set: function(x2) { this._x2 = x2; this._segmentChanged(); }, enumerable: true });
  289. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothRel.prototype, "y2", { get: function() { return this._y2; }, set: function(y2) { this._y2 = y2; this._segmentChanged(); }, enumerable: true });
  290. window.SVGPathSegCurvetoQuadraticSmoothAbs = function(owningPathSegList, x, y) {
  291. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS, "T", owningPathSegList);
  292. this._x = x;
  293. this._y = y;
  294. }
  295. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  296. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype.toString = function() { return "[object SVGPathSegCurvetoQuadraticSmoothAbs]"; }
  297. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x + " " + this._y; }
  298. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype.clone = function() { return new window.SVGPathSegCurvetoQuadraticSmoothAbs(undefined, this._x, this._y); }
  299. Object.defineProperty(window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  300. Object.defineProperty(window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  301. window.SVGPathSegCurvetoQuadraticSmoothRel = function(owningPathSegList, x, y) {
  302. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL, "t", owningPathSegList);
  303. this._x = x;
  304. this._y = y;
  305. }
  306. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype = Object.create(window.SVGPathSeg.prototype);
  307. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype.toString = function() { return "[object SVGPathSegCurvetoQuadraticSmoothRel]"; }
  308. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x + " " + this._y; }
  309. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype.clone = function() { return new window.SVGPathSegCurvetoQuadraticSmoothRel(undefined, this._x, this._y); }
  310. Object.defineProperty(window.SVGPathSegCurvetoQuadraticSmoothRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  311. Object.defineProperty(window.SVGPathSegCurvetoQuadraticSmoothRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  312. // Add createSVGPathSeg* functions to window.SVGPathElement.
  313. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-Interfacewindow.SVGPathElement.
  314. window.SVGPathElement.prototype.createSVGPathSegClosePath = function() { return new window.SVGPathSegClosePath(undefined); }
  315. window.SVGPathElement.prototype.createSVGPathSegMovetoAbs = function(x, y) { return new window.SVGPathSegMovetoAbs(undefined, x, y); }
  316. window.SVGPathElement.prototype.createSVGPathSegMovetoRel = function(x, y) { return new window.SVGPathSegMovetoRel(undefined, x, y); }
  317. window.SVGPathElement.prototype.createSVGPathSegLinetoAbs = function(x, y) { return new window.SVGPathSegLinetoAbs(undefined, x, y); }
  318. window.SVGPathElement.prototype.createSVGPathSegLinetoRel = function(x, y) { return new window.SVGPathSegLinetoRel(undefined, x, y); }
  319. window.SVGPathElement.prototype.createSVGPathSegCurvetoCubicAbs = function(x, y, x1, y1, x2, y2) { return new window.SVGPathSegCurvetoCubicAbs(undefined, x, y, x1, y1, x2, y2); }
  320. window.SVGPathElement.prototype.createSVGPathSegCurvetoCubicRel = function(x, y, x1, y1, x2, y2) { return new window.SVGPathSegCurvetoCubicRel(undefined, x, y, x1, y1, x2, y2); }
  321. window.SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticAbs = function(x, y, x1, y1) { return new window.SVGPathSegCurvetoQuadraticAbs(undefined, x, y, x1, y1); }
  322. window.SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticRel = function(x, y, x1, y1) { return new window.SVGPathSegCurvetoQuadraticRel(undefined, x, y, x1, y1); }
  323. window.SVGPathElement.prototype.createSVGPathSegArcAbs = function(x, y, r1, r2, angle, largeArcFlag, sweepFlag) { return new window.SVGPathSegArcAbs(undefined, x, y, r1, r2, angle, largeArcFlag, sweepFlag); }
  324. window.SVGPathElement.prototype.createSVGPathSegArcRel = function(x, y, r1, r2, angle, largeArcFlag, sweepFlag) { return new window.SVGPathSegArcRel(undefined, x, y, r1, r2, angle, largeArcFlag, sweepFlag); }
  325. window.SVGPathElement.prototype.createSVGPathSegLinetoHorizontalAbs = function(x) { return new window.SVGPathSegLinetoHorizontalAbs(undefined, x); }
  326. window.SVGPathElement.prototype.createSVGPathSegLinetoHorizontalRel = function(x) { return new window.SVGPathSegLinetoHorizontalRel(undefined, x); }
  327. window.SVGPathElement.prototype.createSVGPathSegLinetoVerticalAbs = function(y) { return new window.SVGPathSegLinetoVerticalAbs(undefined, y); }
  328. window.SVGPathElement.prototype.createSVGPathSegLinetoVerticalRel = function(y) { return new window.SVGPathSegLinetoVerticalRel(undefined, y); }
  329. window.SVGPathElement.prototype.createSVGPathSegCurvetoCubicSmoothAbs = function(x, y, x2, y2) { return new window.SVGPathSegCurvetoCubicSmoothAbs(undefined, x, y, x2, y2); }
  330. window.SVGPathElement.prototype.createSVGPathSegCurvetoCubicSmoothRel = function(x, y, x2, y2) { return new window.SVGPathSegCurvetoCubicSmoothRel(undefined, x, y, x2, y2); }
  331. window.SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticSmoothAbs = function(x, y) { return new window.SVGPathSegCurvetoQuadraticSmoothAbs(undefined, x, y); }
  332. window.SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticSmoothRel = function(x, y) { return new window.SVGPathSegCurvetoQuadraticSmoothRel(undefined, x, y); }
  333. if (!("getPathSegAtLength" in window.SVGPathElement.prototype)) {
  334. // Add getPathSegAtLength to SVGPathElement.
  335. // Spec: https://www.w3.org/TR/SVG11/single-page.html#paths-__svg__SVGPathElement__getPathSegAtLength
  336. // This polyfill requires SVGPathElement.getTotalLength to implement the distance-along-a-path algorithm.
  337. window.SVGPathElement.prototype.getPathSegAtLength = function(distance) {
  338. if (distance === undefined || !isFinite(distance))
  339. throw "Invalid arguments.";
  340. var measurementElement = document.createElementNS("http://www.w3.org/2000/svg", "path");
  341. measurementElement.setAttribute("d", this.getAttribute("d"));
  342. var lastPathSegment = measurementElement.pathSegList.numberOfItems - 1;
  343. // If the path is empty, return 0.
  344. if (lastPathSegment <= 0)
  345. return 0;
  346. do {
  347. measurementElement.pathSegList.removeItem(lastPathSegment);
  348. if (distance > measurementElement.getTotalLength())
  349. break;
  350. lastPathSegment--;
  351. } while (lastPathSegment > 0);
  352. return lastPathSegment;
  353. }
  354. }
  355. }
  356. if (!("SVGPathSegList" in window)) {
  357. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGPathSegList
  358. window.SVGPathSegList = function(pathElement) {
  359. this._pathElement = pathElement;
  360. this._list = this._parsePath(this._pathElement.getAttribute("d"));
  361. // Use a MutationObserver to catch changes to the path's "d" attribute.
  362. this._mutationObserverConfig = { "attributes": true, "attributeFilter": ["d"] };
  363. this._pathElementMutationObserver = new MutationObserver(this._updateListFromPathMutations.bind(this));
  364. this._pathElementMutationObserver.observe(this._pathElement, this._mutationObserverConfig);
  365. }
  366. window.SVGPathSegList.prototype.classname = "SVGPathSegList";
  367. Object.defineProperty(window.SVGPathSegList.prototype, "numberOfItems", {
  368. get: function() {
  369. this._checkPathSynchronizedToList();
  370. return this._list.length;
  371. },
  372. enumerable: true
  373. });
  374. // Add the pathSegList accessors to window.SVGPathElement.
  375. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGAnimatedPathData
  376. Object.defineProperty(window.SVGPathElement.prototype, "pathSegList", {
  377. get: function() {
  378. if (!this._pathSegList)
  379. this._pathSegList = new window.SVGPathSegList(this);
  380. return this._pathSegList;
  381. },
  382. enumerable: true
  383. });
  384. // FIXME: The following are not implemented and simply return window.SVGPathElement.pathSegList.
  385. Object.defineProperty(window.SVGPathElement.prototype, "normalizedPathSegList", { get: function() { return this.pathSegList; }, enumerable: true });
  386. Object.defineProperty(window.SVGPathElement.prototype, "animatedPathSegList", { get: function() { return this.pathSegList; }, enumerable: true });
  387. Object.defineProperty(window.SVGPathElement.prototype, "animatedNormalizedPathSegList", { get: function() { return this.pathSegList; }, enumerable: true });
  388. // Process any pending mutations to the path element and update the list as needed.
  389. // This should be the first call of all public functions and is needed because
  390. // MutationObservers are not synchronous so we can have pending asynchronous mutations.
  391. window.SVGPathSegList.prototype._checkPathSynchronizedToList = function() {
  392. this._updateListFromPathMutations(this._pathElementMutationObserver.takeRecords());
  393. }
  394. window.SVGPathSegList.prototype._updateListFromPathMutations = function(mutationRecords) {
  395. if (!this._pathElement)
  396. return;
  397. var hasPathMutations = false;
  398. mutationRecords.forEach(function(record) {
  399. if (record.attributeName == "d")
  400. hasPathMutations = true;
  401. });
  402. if (hasPathMutations)
  403. this._list = this._parsePath(this._pathElement.getAttribute("d"));
  404. }
  405. // Serialize the list and update the path's 'd' attribute.
  406. window.SVGPathSegList.prototype._writeListToPath = function() {
  407. this._pathElementMutationObserver.disconnect();
  408. this._pathElement.setAttribute("d", window.SVGPathSegList._pathSegArrayAsString(this._list));
  409. this._pathElementMutationObserver.observe(this._pathElement, this._mutationObserverConfig);
  410. }
  411. // When a path segment changes the list needs to be synchronized back to the path element.
  412. window.SVGPathSegList.prototype.segmentChanged = function(pathSeg) {
  413. this._writeListToPath();
  414. }
  415. window.SVGPathSegList.prototype.clear = function() {
  416. this._checkPathSynchronizedToList();
  417. this._list.forEach(function(pathSeg) {
  418. pathSeg._owningPathSegList = null;
  419. });
  420. this._list = [];
  421. this._writeListToPath();
  422. }
  423. window.SVGPathSegList.prototype.initialize = function(newItem) {
  424. this._checkPathSynchronizedToList();
  425. this._list = [newItem];
  426. newItem._owningPathSegList = this;
  427. this._writeListToPath();
  428. return newItem;
  429. }
  430. window.SVGPathSegList.prototype._checkValidIndex = function(index) {
  431. if (isNaN(index) || index < 0 || index >= this.numberOfItems)
  432. throw "INDEX_SIZE_ERR";
  433. }
  434. window.SVGPathSegList.prototype.getItem = function(index) {
  435. this._checkPathSynchronizedToList();
  436. this._checkValidIndex(index);
  437. return this._list[index];
  438. }
  439. window.SVGPathSegList.prototype.insertItemBefore = function(newItem, index) {
  440. this._checkPathSynchronizedToList();
  441. // Spec: If the index is greater than or equal to numberOfItems, then the new item is appended to the end of the list.
  442. if (index > this.numberOfItems)
  443. index = this.numberOfItems;
  444. if (newItem._owningPathSegList) {
  445. // SVG2 spec says to make a copy.
  446. newItem = newItem.clone();
  447. }
  448. this._list.splice(index, 0, newItem);
  449. newItem._owningPathSegList = this;
  450. this._writeListToPath();
  451. return newItem;
  452. }
  453. window.SVGPathSegList.prototype.replaceItem = function(newItem, index) {
  454. this._checkPathSynchronizedToList();
  455. if (newItem._owningPathSegList) {
  456. // SVG2 spec says to make a copy.
  457. newItem = newItem.clone();
  458. }
  459. this._checkValidIndex(index);
  460. this._list[index] = newItem;
  461. newItem._owningPathSegList = this;
  462. this._writeListToPath();
  463. return newItem;
  464. }
  465. window.SVGPathSegList.prototype.removeItem = function(index) {
  466. this._checkPathSynchronizedToList();
  467. this._checkValidIndex(index);
  468. var item = this._list[index];
  469. this._list.splice(index, 1);
  470. this._writeListToPath();
  471. return item;
  472. }
  473. window.SVGPathSegList.prototype.appendItem = function(newItem) {
  474. this._checkPathSynchronizedToList();
  475. if (newItem._owningPathSegList) {
  476. // SVG2 spec says to make a copy.
  477. newItem = newItem.clone();
  478. }
  479. this._list.push(newItem);
  480. newItem._owningPathSegList = this;
  481. // TODO: Optimize this to just append to the existing attribute.
  482. this._writeListToPath();
  483. return newItem;
  484. }
  485. window.SVGPathSegList._pathSegArrayAsString = function(pathSegArray) {
  486. var string = "";
  487. var first = true;
  488. pathSegArray.forEach(function(pathSeg) {
  489. if (first) {
  490. first = false;
  491. string += pathSeg._asPathString();
  492. } else {
  493. string += " " + pathSeg._asPathString();
  494. }
  495. });
  496. return string;
  497. }
  498. // This closely follows SVGPathParser::parsePath from Source/core/svg/SVGPathParser.cpp.
  499. window.SVGPathSegList.prototype._parsePath = function(string) {
  500. if (!string || string.length == 0)
  501. return [];
  502. var owningPathSegList = this;
  503. var Builder = function() {
  504. this.pathSegList = [];
  505. }
  506. Builder.prototype.appendSegment = function(pathSeg) {
  507. this.pathSegList.push(pathSeg);
  508. }
  509. var Source = function(string) {
  510. this._string = string;
  511. this._currentIndex = 0;
  512. this._endIndex = this._string.length;
  513. this._previousCommand = window.SVGPathSeg.PATHSEG_UNKNOWN;
  514. this._skipOptionalSpaces();
  515. }
  516. Source.prototype._isCurrentSpace = function() {
  517. var character = this._string[this._currentIndex];
  518. return character <= " " && (character == " " || character == "\n" || character == "\t" || character == "\r" || character == "\f");
  519. }
  520. Source.prototype._skipOptionalSpaces = function() {
  521. while (this._currentIndex < this._endIndex && this._isCurrentSpace())
  522. this._currentIndex++;
  523. return this._currentIndex < this._endIndex;
  524. }
  525. Source.prototype._skipOptionalSpacesOrDelimiter = function() {
  526. if (this._currentIndex < this._endIndex && !this._isCurrentSpace() && this._string.charAt(this._currentIndex) != ",")
  527. return false;
  528. if (this._skipOptionalSpaces()) {
  529. if (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) == ",") {
  530. this._currentIndex++;
  531. this._skipOptionalSpaces();
  532. }
  533. }
  534. return this._currentIndex < this._endIndex;
  535. }
  536. Source.prototype.hasMoreData = function() {
  537. return this._currentIndex < this._endIndex;
  538. }
  539. Source.prototype.peekSegmentType = function() {
  540. var lookahead = this._string[this._currentIndex];
  541. return this._pathSegTypeFromChar(lookahead);
  542. }
  543. Source.prototype._pathSegTypeFromChar = function(lookahead) {
  544. switch (lookahead) {
  545. case "Z":
  546. case "z":
  547. return window.SVGPathSeg.PATHSEG_CLOSEPATH;
  548. case "M":
  549. return window.SVGPathSeg.PATHSEG_MOVETO_ABS;
  550. case "m":
  551. return window.SVGPathSeg.PATHSEG_MOVETO_REL;
  552. case "L":
  553. return window.SVGPathSeg.PATHSEG_LINETO_ABS;
  554. case "l":
  555. return window.SVGPathSeg.PATHSEG_LINETO_REL;
  556. case "C":
  557. return window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS;
  558. case "c":
  559. return window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL;
  560. case "Q":
  561. return window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS;
  562. case "q":
  563. return window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL;
  564. case "A":
  565. return window.SVGPathSeg.PATHSEG_ARC_ABS;
  566. case "a":
  567. return window.SVGPathSeg.PATHSEG_ARC_REL;
  568. case "H":
  569. return window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS;
  570. case "h":
  571. return window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL;
  572. case "V":
  573. return window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS;
  574. case "v":
  575. return window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL;
  576. case "S":
  577. return window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS;
  578. case "s":
  579. return window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL;
  580. case "T":
  581. return window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS;
  582. case "t":
  583. return window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL;
  584. default:
  585. return window.SVGPathSeg.PATHSEG_UNKNOWN;
  586. }
  587. }
  588. Source.prototype._nextCommandHelper = function(lookahead, previousCommand) {
  589. // Check for remaining coordinates in the current command.
  590. if ((lookahead == "+" || lookahead == "-" || lookahead == "." || (lookahead >= "0" && lookahead <= "9")) && previousCommand != window.SVGPathSeg.PATHSEG_CLOSEPATH) {
  591. if (previousCommand == window.SVGPathSeg.PATHSEG_MOVETO_ABS)
  592. return window.SVGPathSeg.PATHSEG_LINETO_ABS;
  593. if (previousCommand == window.SVGPathSeg.PATHSEG_MOVETO_REL)
  594. return window.SVGPathSeg.PATHSEG_LINETO_REL;
  595. return previousCommand;
  596. }
  597. return window.SVGPathSeg.PATHSEG_UNKNOWN;
  598. }
  599. Source.prototype.initialCommandIsMoveTo = function() {
  600. // If the path is empty it is still valid, so return true.
  601. if (!this.hasMoreData())
  602. return true;
  603. var command = this.peekSegmentType();
  604. // Path must start with moveTo.
  605. return command == window.SVGPathSeg.PATHSEG_MOVETO_ABS || command == window.SVGPathSeg.PATHSEG_MOVETO_REL;
  606. }
  607. // Parse a number from an SVG path. This very closely follows genericParseNumber(...) from Source/core/svg/SVGParserUtilities.cpp.
  608. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-PathDataBNF
  609. Source.prototype._parseNumber = function() {
  610. var exponent = 0;
  611. var integer = 0;
  612. var frac = 1;
  613. var decimal = 0;
  614. var sign = 1;
  615. var expsign = 1;
  616. var startIndex = this._currentIndex;
  617. this._skipOptionalSpaces();
  618. // Read the sign.
  619. if (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) == "+")
  620. this._currentIndex++;
  621. else if (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) == "-") {
  622. this._currentIndex++;
  623. sign = -1;
  624. }
  625. if (this._currentIndex == this._endIndex || ((this._string.charAt(this._currentIndex) < "0" || this._string.charAt(this._currentIndex) > "9") && this._string.charAt(this._currentIndex) != "."))
  626. // The first character of a number must be one of [0-9+-.].
  627. return undefined;
  628. // Read the integer part, build right-to-left.
  629. var startIntPartIndex = this._currentIndex;
  630. while (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) >= "0" && this._string.charAt(this._currentIndex) <= "9")
  631. this._currentIndex++; // Advance to first non-digit.
  632. if (this._currentIndex != startIntPartIndex) {
  633. var scanIntPartIndex = this._currentIndex - 1;
  634. var multiplier = 1;
  635. while (scanIntPartIndex >= startIntPartIndex) {
  636. integer += multiplier * (this._string.charAt(scanIntPartIndex--) - "0");
  637. multiplier *= 10;
  638. }
  639. }
  640. // Read the decimals.
  641. if (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) == ".") {
  642. this._currentIndex++;
  643. // There must be a least one digit following the .
  644. if (this._currentIndex >= this._endIndex || this._string.charAt(this._currentIndex) < "0" || this._string.charAt(this._currentIndex) > "9")
  645. return undefined;
  646. while (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) >= "0" && this._string.charAt(this._currentIndex) <= "9") {
  647. frac *= 10;
  648. decimal += (this._string.charAt(this._currentIndex) - "0") / frac;
  649. this._currentIndex += 1;
  650. }
  651. }
  652. // Read the exponent part.
  653. if (this._currentIndex != startIndex && this._currentIndex + 1 < this._endIndex && (this._string.charAt(this._currentIndex) == "e" || this._string.charAt(this._currentIndex) == "E") && (this._string.charAt(this._currentIndex + 1) != "x" && this._string.charAt(this._currentIndex + 1) != "m")) {
  654. this._currentIndex++;
  655. // Read the sign of the exponent.
  656. if (this._string.charAt(this._currentIndex) == "+") {
  657. this._currentIndex++;
  658. } else if (this._string.charAt(this._currentIndex) == "-") {
  659. this._currentIndex++;
  660. expsign = -1;
  661. }
  662. // There must be an exponent.
  663. if (this._currentIndex >= this._endIndex || this._string.charAt(this._currentIndex) < "0" || this._string.charAt(this._currentIndex) > "9")
  664. return undefined;
  665. while (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) >= "0" && this._string.charAt(this._currentIndex) <= "9") {
  666. exponent *= 10;
  667. exponent += (this._string.charAt(this._currentIndex) - "0");
  668. this._currentIndex++;
  669. }
  670. }
  671. var number = integer + decimal;
  672. number *= sign;
  673. if (exponent)
  674. number *= Math.pow(10, expsign * exponent);
  675. if (startIndex == this._currentIndex)
  676. return undefined;
  677. this._skipOptionalSpacesOrDelimiter();
  678. return number;
  679. }
  680. Source.prototype._parseArcFlag = function() {
  681. if (this._currentIndex >= this._endIndex)
  682. return undefined;
  683. var flag = false;
  684. var flagChar = this._string.charAt(this._currentIndex++);
  685. if (flagChar == "0")
  686. flag = false;
  687. else if (flagChar == "1")
  688. flag = true;
  689. else
  690. return undefined;
  691. this._skipOptionalSpacesOrDelimiter();
  692. return flag;
  693. }
  694. Source.prototype.parseSegment = function() {
  695. var lookahead = this._string[this._currentIndex];
  696. var command = this._pathSegTypeFromChar(lookahead);
  697. if (command == window.SVGPathSeg.PATHSEG_UNKNOWN) {
  698. // Possibly an implicit command. Not allowed if this is the first command.
  699. if (this._previousCommand == window.SVGPathSeg.PATHSEG_UNKNOWN)
  700. return null;
  701. command = this._nextCommandHelper(lookahead, this._previousCommand);
  702. if (command == window.SVGPathSeg.PATHSEG_UNKNOWN)
  703. return null;
  704. } else {
  705. this._currentIndex++;
  706. }
  707. this._previousCommand = command;
  708. switch (command) {
  709. case window.SVGPathSeg.PATHSEG_MOVETO_REL:
  710. return new window.SVGPathSegMovetoRel(owningPathSegList, this._parseNumber(), this._parseNumber());
  711. case window.SVGPathSeg.PATHSEG_MOVETO_ABS:
  712. return new window.SVGPathSegMovetoAbs(owningPathSegList, this._parseNumber(), this._parseNumber());
  713. case window.SVGPathSeg.PATHSEG_LINETO_REL:
  714. return new window.SVGPathSegLinetoRel(owningPathSegList, this._parseNumber(), this._parseNumber());
  715. case window.SVGPathSeg.PATHSEG_LINETO_ABS:
  716. return new window.SVGPathSegLinetoAbs(owningPathSegList, this._parseNumber(), this._parseNumber());
  717. case window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL:
  718. return new window.SVGPathSegLinetoHorizontalRel(owningPathSegList, this._parseNumber());
  719. case window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS:
  720. return new window.SVGPathSegLinetoHorizontalAbs(owningPathSegList, this._parseNumber());
  721. case window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL:
  722. return new window.SVGPathSegLinetoVerticalRel(owningPathSegList, this._parseNumber());
  723. case window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS:
  724. return new window.SVGPathSegLinetoVerticalAbs(owningPathSegList, this._parseNumber());
  725. case window.SVGPathSeg.PATHSEG_CLOSEPATH:
  726. this._skipOptionalSpaces();
  727. return new window.SVGPathSegClosePath(owningPathSegList);
  728. case window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL:
  729. var points = {x1: this._parseNumber(), y1: this._parseNumber(), x2: this._parseNumber(), y2: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber()};
  730. return new window.SVGPathSegCurvetoCubicRel(owningPathSegList, points.x, points.y, points.x1, points.y1, points.x2, points.y2);
  731. case window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS:
  732. var points = {x1: this._parseNumber(), y1: this._parseNumber(), x2: this._parseNumber(), y2: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber()};
  733. return new window.SVGPathSegCurvetoCubicAbs(owningPathSegList, points.x, points.y, points.x1, points.y1, points.x2, points.y2);
  734. case window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL:
  735. var points = {x2: this._parseNumber(), y2: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber()};
  736. return new window.SVGPathSegCurvetoCubicSmoothRel(owningPathSegList, points.x, points.y, points.x2, points.y2);
  737. case window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS:
  738. var points = {x2: this._parseNumber(), y2: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber()};
  739. return new window.SVGPathSegCurvetoCubicSmoothAbs(owningPathSegList, points.x, points.y, points.x2, points.y2);
  740. case window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL:
  741. var points = {x1: this._parseNumber(), y1: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber()};
  742. return new window.SVGPathSegCurvetoQuadraticRel(owningPathSegList, points.x, points.y, points.x1, points.y1);
  743. case window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS:
  744. var points = {x1: this._parseNumber(), y1: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber()};
  745. return new window.SVGPathSegCurvetoQuadraticAbs(owningPathSegList, points.x, points.y, points.x1, points.y1);
  746. case window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL:
  747. return new window.SVGPathSegCurvetoQuadraticSmoothRel(owningPathSegList, this._parseNumber(), this._parseNumber());
  748. case window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS:
  749. return new window.SVGPathSegCurvetoQuadraticSmoothAbs(owningPathSegList, this._parseNumber(), this._parseNumber());
  750. case window.SVGPathSeg.PATHSEG_ARC_REL:
  751. var points = {x1: this._parseNumber(), y1: this._parseNumber(), arcAngle: this._parseNumber(), arcLarge: this._parseArcFlag(), arcSweep: this._parseArcFlag(), x: this._parseNumber(), y: this._parseNumber()};
  752. return new window.SVGPathSegArcRel(owningPathSegList, points.x, points.y, points.x1, points.y1, points.arcAngle, points.arcLarge, points.arcSweep);
  753. case window.SVGPathSeg.PATHSEG_ARC_ABS:
  754. var points = {x1: this._parseNumber(), y1: this._parseNumber(), arcAngle: this._parseNumber(), arcLarge: this._parseArcFlag(), arcSweep: this._parseArcFlag(), x: this._parseNumber(), y: this._parseNumber()};
  755. return new window.SVGPathSegArcAbs(owningPathSegList, points.x, points.y, points.x1, points.y1, points.arcAngle, points.arcLarge, points.arcSweep);
  756. default:
  757. throw "Unknown path seg type."
  758. }
  759. }
  760. var builder = new Builder();
  761. var source = new Source(string);
  762. if (!source.initialCommandIsMoveTo())
  763. return [];
  764. while (source.hasMoreData()) {
  765. var pathSeg = source.parseSegment();
  766. if (!pathSeg)
  767. return [];
  768. builder.appendSegment(pathSeg);
  769. }
  770. return builder.pathSegList;
  771. }
  772. }
  773. }());
  774. /* jshint ignore:end */