<script src="spec/mask.js"></script>
<script src="spec/matrix.js"></script>
<script src="spec/memory.js"></script>
- <script src="spec/nested.js"></script>
<script src="spec/number.js"></script>
<script src="spec/path.js"></script>
<script src="spec/pattern.js"></script>
expect(draw instanceof SVG.Doc).toBe(true)
})
- it('returns itself as Doc', function() {
+ it('returns itself as Doc when root', function() {
expect(draw.doc()).toBe(draw)
})
- it('has a defs element', function() {
+ it('has a defs element when root', function() {
expect(draw.defs() instanceof SVG.Defs).toBe(true)
})
})
})
+ describe('isRoot()', function() {
+ it('returns true when the doc is not attached to dom', function() {
+ expect(SVG().isRoot()).toBe(true)
+ })
+ it('returns true when its outer element is not an svg element', function () {
+ expect(SVG().addTo(document.createElement('div')).isRoot()).toBe(true)
+ })
+ it('returns true when its the root element of the dom', function () {
+ if(parserInDoc) {
+ expect(draw.isRoot()).toBe(true)
+ }
+ })
+ it('returns false when parent is svg element', function () {
+ expect(SVG().addTo(SVG()).isRoot()).toBe(false)
+ })
+ })
+
describe('remove()', function() {
it('removes the doc from the dom only if doc is not root element', function() {
var cnt = window.document.querySelectorAll('svg').length
it('ungroups everything to the doc root when called on SVG.Doc / does not ungroup defs/parser', function() {
draw.flatten()
+
expect(rect1.parent()).toBe(draw)
expect(rect2.parent()).toBe(draw)
expect(g1.node.parentNode).toBeFalsy()
- expect(g1.node.parentNode).toBeFalsy()
+ expect(g2.node.parentNode).toBeFalsy()
expect(nested.node.parentNode).toBeFalsy()
expect(rect1.transform()).toEqual(jasmine.objectContaining({
+++ /dev/null
-describe('Nested', function() {
-
- afterEach(function() {
- draw.clear()
- })
-
- describe('()', function() {
- it('creates a nested svg of type SVG.Nested', function() {
- expect(draw.nested() instanceof SVG.Nested).toBeTruthy()
- })
- })
-
-})
expect(SVG(rect).node).toBe(rect)
})
- it('creates an instanceof SVG.Nested when importing a whole svg', function() {
+ it('creates an instanceof SVG.Doc when importing a whole svg', function() {
var doc = SVG('<svg width="200"><rect></rect></svg>')
- expect(doc instanceof SVG.Nested).toBe(true)
+ expect(doc instanceof SVG.Doc).toBe(true)
expect(doc.node.nodeName).toBe('svg')
expect(doc.width()).toBe(200)
expect(doc.get(0).node.nodeName).toBe('rect')
extend: {
add: function (element, i) {
element = createElement(element)
- if (element instanceof SVG.Nested) {
- element = new SVG.Doc(element.node)
- element.setData(JSON.parse(element.node.getAttribute('svgjs:data')) || {})
- }
- if (i === null) {
- this.node.appendChild(element.node)
- } else if (element.node !== this.node.children[i]) {
- this.node.insertBefore(element.node, this.node.children[i])
+ if (element.node !== this.node.children[i]) {
+ this.node.insertBefore(element.node, this.node.children[i] || null)
}
return this
}
})
-SVG.extend([SVG.Doc, SVG.Nested, SVG.Symbol, SVG.Image, SVG.Pattern, SVG.Marker, SVG.ForeignObject, SVG.View], {
+SVG.extend([SVG.Doc, SVG.Symbol, SVG.Image, SVG.Pattern, SVG.Marker, SVG.ForeignObject, SVG.View], {
viewbox: function (x, y, width, height) {
// act as getter
if (x == null) return new SVG.Box(this.attr('viewBox'))
this.constructor(node || SVG.create('svg'))
// set svg element attributes and ensure defs node
- this.namespace().defs()
+ this.namespace()
},
// Inherit from
// Add class methods
extend: {
- isRoot: function() {
- return !this.node.parentNode || !this.node.parentNode instanceof window.SVGElement || this.node.parentNode.nodeName == '#document'
+ isRoot: function () {
+ return !this.node.parentNode || !(this.node.parentNode instanceof window.SVGElement) || this.node.parentNode.nodeName === '#document'
},
- doc: function() {
- if(this.isRoot()) return this
-
- var parent
- while(parent = this.parent(SVG.Doc)) {
- if(parent.isRoot()) return parent
- }
-
- // this can only happen when you have something like
- // <g><svg>...</svg></g>
- return null
+ // Check if this is a root svg. If not, call docs from this element
+ doc: function () {
+ if (this.isRoot()) return this
+ return SVG.Element.prototype.doc.call(this)
},
// Add namespaces
- namespace: function() {
- if(!this.isRoot()) return this.doc().namespace()
+ namespace: function () {
+ if (!this.isRoot()) return this.doc().namespace()
return this
.attr({ xmlns: SVG.ns, version: '1.1' })
.attr('xmlns:xlink', SVG.xlink, SVG.xmlns)
.attr('xmlns:svgjs', SVG.svgjs, SVG.xmlns)
},
// Creates and returns defs element
- defs: function() {
- if(!this.isRoot()) return this.doc().defs()
+ defs: function () {
+ if (!this.isRoot()) return this.doc().defs()
return SVG.adopt(this.node.getElementsByTagName('defs')[0]) || this.put(new SVG.Defs())
},
// custom parent method
- parent: function () {
- return this.node.parentNode.nodeName === '#document' ? null : this.node.parentNode
+ parent: function (type) {
+ if (this.isRoot()) {
+ return this.node.parentNode.nodeName === '#document' ? null : this.node.parentNode
+ }
+
+ return SVG.Element.prototype.parent.call(this, type)
},
// Removes the doc from the DOM
remove: function () {
+ if (!this.isRoot()) {
+ return SVG.Element.prototype.remove.call(this)
+ }
+
if (this.parent()) {
this.parent().removeChild(this.node)
}
},
construct: {
// Create nested svg document
- nested: function() {
- return this.put(new SVG.Doc)
+ nested: function () {
+ return this.put(new SVG.Doc())
}
}
})
// Get parent document
doc: function () {
- return this.parent(SVG.Doc).doc()
+ var p = this.parent(SVG.Doc)
+ return p && p.doc()
},
// Get defs
flatten: function (parent) {
if (this instanceof SVG.Defs) return this
- parent = parent || (this instanceof SVG.Doc ? this : this.parent(SVG.Parent))
+ parent = parent || (this instanceof SVG.Doc && this.isRoot() ? this : this.parent(SVG.Parent))
this.each(function () {
if (this instanceof SVG.Defs) return this
This is needed because FF does not return the transformation matrix
for the inner coordinate system when getScreenCTM() is called on nested svgs.
However all other Browsers do that */
- if (this instanceof SVG.Nested) {
+ if (this instanceof SVG.Doc && !this.isRoot()) {
var rect = this.rect(1, 1)
var m = rect.node.getScreenCTM()
rect.remove()
add: function (element, i) {
element = createElement(element)
- if (i == null) {
- this.node.appendChild(element.node)
- } else if (element.node !== this.node.children[i]) {
- this.node.insertBefore(element.node, this.node.children[i])
+ if (element.node !== this.node.children[i]) {
+ this.node.insertBefore(element.node, this.node.children[i] || null)
}
return this
}
SVG.parser.nodes = {
- svg: new SVG.Nested().size(2, 0).css({
+ svg: SVG().size(2, 0).css({
opacity: 0,
position: 'absolute',
left: '-100%',
var element
// adopt with element-specific settings
- if (node.nodeName == 'svg')
+ if (node.nodeName === 'svg') {
element = new SVG.Doc(node)
- else if (node.nodeName === 'linearGradient' || node.nodeName === 'radialGradient')
+ } else if (node.nodeName === 'linearGradient' || node.nodeName === 'radialGradient') {
element = new SVG.Gradient(node)
} else if (SVG[capitalize(node.nodeName)]) {
element = new SVG[capitalize(node.nodeName)](node)
- } else { element = new SVG.Parent(node) }
+ } else {
+ element = new SVG.Parent(node)
+ }
return element
}