diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2020-05-04 21:35:21 +1000 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2020-05-04 21:35:21 +1000 |
commit | c8cb22863bf8c3ac157f6098be9154908aea9ec2 (patch) | |
tree | b27b1bf6ec4c90bbd0cb335e26bb9ece504285d2 /src | |
parent | 59f09a1a2317e57d13bbe8f60e1949cc82199ead (diff) | |
download | svg.js-c8cb22863bf8c3ac157f6098be9154908aea9ec2.tar.gz svg.js-c8cb22863bf8c3ac157f6098be9154908aea9ec2.zip |
Fixed IE Polyfills, got rid of ArrayPolyfill in favour of babels own transforms, updated dependencies, finished tests, removed old es5 tests
Diffstat (limited to 'src')
-rw-r--r-- | src/animation/Runner.js | 8 | ||||
-rw-r--r-- | src/elements/Dom.js | 7 | ||||
-rw-r--r-- | src/elements/Fragment.js | 25 | ||||
-rw-r--r-- | src/elements/TextPath.js | 7 | ||||
-rw-r--r-- | src/polyfills/innerHTML.js | 8 | ||||
-rw-r--r-- | src/types/ArrayPolyfill.js | 36 | ||||
-rw-r--r-- | src/types/List.js | 19 | ||||
-rw-r--r-- | src/types/PathArray.js | 18 | ||||
-rw-r--r-- | src/types/PointArray.js | 26 | ||||
-rw-r--r-- | src/types/SVGArray.js | 27 | ||||
-rw-r--r-- | src/utils/adopter.js | 40 |
11 files changed, 74 insertions, 147 deletions
diff --git a/src/animation/Runner.js b/src/animation/Runner.js index e2ba380..e0ac5a8 100644 --- a/src/animation/Runner.js +++ b/src/animation/Runner.js @@ -9,11 +9,10 @@ import Animator from './Animator.js' import Box from '../types/Box.js' import EventTarget from '../types/EventTarget.js' import Matrix from '../types/Matrix.js' -import Morphable, { TransformBag } from './Morphable.js' +import Morphable, { TransformBag, ObjectBag } from './Morphable.js' import Point from '../types/Point.js' import SVGNumber from '../types/SVGNumber.js' import Timeline from './Timeline.js' -import { ObjectBag } from '../main.js' export default class Runner extends EventTarget { constructor (options) { @@ -654,11 +653,6 @@ registerMethods({ } }) -const intersect = (a, b) => { - var setB = new Set(b) - return [ ...new Set(a) ].filter(x => setB.has(x)) -} - // Will output the elements from array A that are not in the array B const difference = (a, b) => a.filter(x => !b.includes(x)) diff --git a/src/elements/Dom.js b/src/elements/Dom.js index 1f25c0b..eb59418 100644 --- a/src/elements/Dom.js +++ b/src/elements/Dom.js @@ -264,8 +264,9 @@ export default class Dom extends EventTarget { xml (xmlOrFn, outerXML, ns) { var well, len, fragment - if (xmlOrFn === false) { - outerXML = false + if (typeof xmlOrFn === 'boolean') { + ns = outerXML + outerXML = xmlOrFn xmlOrFn = null } @@ -319,7 +320,7 @@ export default class Dom extends EventTarget { outerXML = outerXML == null ? false : outerXML // Create temporary holder - well = globals.document.createElementNS(ns, 'svg') + well = create('wrapper', ns) fragment = globals.document.createDocumentFragment() // Dump raw svg diff --git a/src/elements/Fragment.js b/src/elements/Fragment.js index 228e93f..d2c4764 100644 --- a/src/elements/Fragment.js +++ b/src/elements/Fragment.js @@ -1,30 +1,31 @@ import Dom from './Dom.js' import { globals } from '../utils/window.js' -import { register } from '../utils/adopter.js' -import Svg from './Svg.js' +import { register, create } from '../utils/adopter.js' class Fragment extends Dom { constructor (node = globals.document.createDocumentFragment()) { super(node) } - // Import / Export raw svg - svg (svgOrFn, outerHTML) { - if (svgOrFn === false) { - outerHTML = false - svgOrFn = null + // Import / Export raw xml + xml (xmlOrFn, outerXML, ns) { + if (typeof xmlOrFn === 'boolean') { + ns = outerXML + outerXML = xmlOrFn + xmlOrFn = null } - // act as getter if no svg string is given - if (svgOrFn == null || typeof svgOrFn === 'function') { - const wrapper = new Svg() + // because this is a fragment we have to put all elements into a wrapper first + // before we can get the innerXML from it + if (xmlOrFn == null || typeof xmlOrFn === 'function') { + const wrapper = new Dom(create('wrapper', ns)) wrapper.add(this.node.cloneNode(true)) - return wrapper.svg(svgOrFn, false) + return wrapper.xml(false, ns) } // Act as setter if we got a string - return super.svg(svgOrFn, false) + return super.xml(xmlOrFn, false, ns) } } diff --git a/src/elements/TextPath.js b/src/elements/TextPath.js index e4208c2..9ed4a71 100644 --- a/src/elements/TextPath.js +++ b/src/elements/TextPath.js @@ -92,7 +92,12 @@ registerMethods({ }), targets () { - return baseFind('svg textPath[*|href*="' + this.id() + '"]') + return baseFind('svg textPath').filter((node) => { + return node.attr('href').includes(this.id()) + }) + + // Does not work in IE11. Use when IE support is dropped + // return baseFind('svg textPath[*|href*="' + this.id() + '"]') } } }) diff --git a/src/polyfills/innerHTML.js b/src/polyfills/innerHTML.js index 4be7f1b..138277e 100644 --- a/src/polyfills/innerHTML.js +++ b/src/polyfills/innerHTML.js @@ -13,18 +13,18 @@ output.push('<', node.tagName) if (node.hasAttributes()) { [].forEach.call(node.attributes, function (attrNode) { - output.push(' ', attrNode.name, '=\'', attrNode.value, '\'') + output.push(' ', attrNode.name, '="', attrNode.value, '"') }) } + output.push('>') if (node.hasChildNodes()) { - output.push('>'); [].forEach.call(node.childNodes, function (childNode) { serializeXML(childNode, output) }) - output.push('</', node.tagName, '>') } else { - output.push('/>') + // output.push('/>') } + output.push('</', node.tagName, '>') } else if (nodeType === 8) { output.push('<!--', node.nodeValue, '-->') } diff --git a/src/types/ArrayPolyfill.js b/src/types/ArrayPolyfill.js deleted file mode 100644 index 4d2309f..0000000 --- a/src/types/ArrayPolyfill.js +++ /dev/null @@ -1,36 +0,0 @@ -/* eslint no-new-func: "off" */ -export const subClassArray = (function () { - try { - // try es6 subclassing - return Function('name', 'baseClass', '_constructor', [ - 'baseClass = baseClass || Array', - 'return {', - ' [name]: class extends baseClass {', - ' constructor (...args) {', - ' super(...args)', - ' _constructor && _constructor.apply(this, args)', - ' }', - ' }', - '}[name]' - ].join('\n')) - } catch (e) { - // Use es5 approach - return (name, baseClass = Array, _constructor) => { - const Arr = function () { - baseClass.apply(this, arguments) - _constructor && _constructor.apply(this, arguments) - } - - Arr.prototype = Object.create(baseClass.prototype) - Arr.prototype.constructor = Arr - - Arr.prototype.map = function (fn) { - const arr = new Arr() - arr.push.apply(arr, Array.prototype.map.call(this, fn)) - return arr - } - - return Arr - } - } -})() diff --git a/src/types/List.js b/src/types/List.js index b68e362..197a155 100644 --- a/src/types/List.js +++ b/src/types/List.js @@ -1,16 +1,25 @@ import { extend } from '../utils/adopter.js' -import { subClassArray } from './ArrayPolyfill.js' +// import { subClassArray } from './ArrayPolyfill.js' + +class List extends Array { + constructor (arr = [], ...args) { + super(arr, ...args) + if (typeof arr === 'number') return this + this.length = 0 + this.push(...arr) + } +} -const List = subClassArray('List', Array, function (arr = []) { +/* = subClassArray('List', Array, function (arr = []) { // This catches the case, that native map tries to create an array with new Array(1) if (typeof arr === 'number') return this this.length = 0 this.push(...arr) -}) +}) */ export default List -extend(List, { +extend([ List ], { each (fnOrMethodName, ...args) { if (typeof fnOrMethodName === 'function') { return this.map((el, i, arr) => { @@ -45,5 +54,5 @@ List.extend = function (methods) { return obj }, {}) - extend(List, methods) + extend([ List ], methods) } diff --git a/src/types/PathArray.js b/src/types/PathArray.js index 03fdee3..d9c1eb2 100644 --- a/src/types/PathArray.js +++ b/src/types/PathArray.js @@ -6,17 +6,11 @@ import { numbersWithDots, pathLetters } from '../modules/core/regex.js' -import { extend } from '../utils/adopter.js' -import { subClassArray } from './ArrayPolyfill.js' import Point from './Point.js' import SVGArray from './SVGArray.js' import parser from '../modules/core/parser.js' import Box from './Box.js' -const PathArray = subClassArray('PathArray', SVGArray) - -export default PathArray - export function pathRegReplace (a, b, c, d) { return c + d.replace(dots, ' .') } @@ -130,11 +124,11 @@ for (var i = 0, il = mlhvqtcsaz.length; i < il; ++i) { })(mlhvqtcsaz[i].toUpperCase()) } -extend(PathArray, { +export default class PathArray extends SVGArray { // Convert array to string toString () { return arrayToString(this) - }, + } // Move path string move (x, y) { @@ -175,7 +169,7 @@ extend(PathArray, { } return this - }, + } // Resize path string size (width, height) { @@ -221,7 +215,7 @@ extend(PathArray, { } return this - }, + } // Absolutize and parse path to array parse (array = [ 'M', 0, 0 ]) { @@ -268,11 +262,11 @@ extend(PathArray, { } while (len > index) return result - }, + } // Get bounding box of path bbox () { parser().path.setAttribute('d', this.toString()) return new Box(parser.nodes.path.getBBox()) } -}) +} diff --git a/src/types/PointArray.js b/src/types/PointArray.js index 54df08f..27a2076 100644 --- a/src/types/PointArray.js +++ b/src/types/PointArray.js @@ -1,15 +1,9 @@ import { delimiter } from '../modules/core/regex.js' -import { extend } from '../utils/adopter.js' -import { subClassArray } from './ArrayPolyfill.js' import SVGArray from './SVGArray.js' -import { Matrix } from '../main.js' import Box from './Box.js' +import Matrix from './Matrix.js' -const PointArray = subClassArray('PointArray', SVGArray) - -export default PointArray - -extend(PointArray, { +export default class PointArray extends SVGArray { // Convert array to string toString () { // convert to a poly point string @@ -18,7 +12,7 @@ extend(PointArray, { } return array.join(' ') - }, + } // Convert array to line object toLine () { @@ -28,7 +22,7 @@ extend(PointArray, { x2: this[1][0], y2: this[1][1] } - }, + } // Parse point string and flat array parse (array = [ 0, 0 ]) { @@ -52,11 +46,11 @@ extend(PointArray, { } return points - }, + } transform (m) { return this.clone().transformO(m) - }, + } // transform points with matrix (similar to Point.transform) transformO (m) { @@ -72,7 +66,7 @@ extend(PointArray, { } return this - }, + } // Move point string move (x, y) { @@ -90,7 +84,7 @@ extend(PointArray, { } return this - }, + } // Resize poly string size (width, height) { @@ -104,7 +98,7 @@ extend(PointArray, { } return this - }, + } // Get bounding box of points bbox () { @@ -120,4 +114,4 @@ extend(PointArray, { }) return new Box(minX, minY, maxX - minX, maxY - minY) } -}) +} diff --git a/src/types/SVGArray.js b/src/types/SVGArray.js index 7f27ec4..dafa2d4 100644 --- a/src/types/SVGArray.js +++ b/src/types/SVGArray.js @@ -1,36 +1,33 @@ import { delimiter } from '../modules/core/regex.js' -import { extend } from '../utils/adopter.js' -import { subClassArray } from './ArrayPolyfill.js' -const SVGArray = subClassArray('SVGArray', Array, function (arr) { - this.init(arr) -}) - -export default SVGArray +export default class SVGArray extends Array { + constructor (...args) { + super(...args) + this.init(...args) + } -extend(SVGArray, { init (arr) { // This catches the case, that native map tries to create an array with new Array(1) if (typeof arr === 'number') return this this.length = 0 this.push(...this.parse(arr)) return this - }, + } toArray () { return Array.prototype.concat.apply([], this) - }, + } toString () { return this.join(' ') - }, + } // Flattens the array if needed valueOf () { const ret = [] ret.push(...this) return ret - }, + } // Parse whitespace separated string parse (array = []) { @@ -38,13 +35,13 @@ extend(SVGArray, { if (array instanceof Array) return array return array.trim().split(delimiter).map(parseFloat) - }, + } clone () { return new this.constructor(this) - }, + } toSet () { return new Set(this) } -}) +} diff --git a/src/utils/adopter.js b/src/utils/adopter.js index a136655..2de1b27 100644 --- a/src/utils/adopter.js +++ b/src/utils/adopter.js @@ -8,9 +8,9 @@ const elements = {} export const root = '___SYMBOL___ROOT___' // Method for element creation -export function create (name) { +export function create (name, ns = svg) { // create element - return globals.document.createElementNS(svg, name) + return globals.document.createElementNS(ns, name) } export function makeInstance (element, isHTML = false) { @@ -114,26 +114,18 @@ export function assignNewId (node) { } // Method for extending objects -export function extend (modules, methods, attrCheck) { +export function extend (modules, methods) { var key, i modules = Array.isArray(modules) ? modules : [ modules ] for (i = modules.length - 1; i >= 0; i--) { for (key in methods) { - let method = methods[key] - if (attrCheck) { - method = wrapWithAttrCheck(methods[key]) - } - modules[i].prototype[key] = method + modules[i].prototype[key] = methods[key] } } } -// export function extendWithAttrCheck (...args) { -// extend(...args, true) -// } - export function wrapWithAttrCheck (fn) { return function (...args) { const o = args[args.length - 1] @@ -145,27 +137,3 @@ export function wrapWithAttrCheck (fn) { } } } - -export function invent (config) { - // Create element initializer - var initializer = typeof config.create === 'function' - ? config.create - : function (node) { - this.constructor(node || create(config.create)) - } - - // Inherit prototype - if (config.inherit) { - /* eslint new-cap: off */ - initializer.prototype = new config.inherit() - initializer.prototype.constructor = initializer - } - - // Extend with methods - if (config.extend) { extend(initializer, config.extend) } - - // Attach construct method to parent - if (config.construct) { extend(config.parent || elements.Container, config.construct) } - - return initializer -} |