Start removing dependencies on SVG baseVal.

This commit is contained in:
Jürg Lehni 2013-05-09 01:46:13 -07:00
parent 8cd57743c8
commit e9fb78aa08
4 changed files with 34 additions and 21 deletions

View file

@ -119,6 +119,7 @@ var paper = new function() {
/*#*/ if (options.svg) {
/*#*/ include('svg/SVGStyles.js');
/*#*/ include('svg/SVGNamespaces.js');
/*#*/ include('svg/SVGExport.js');
/*#*/ include('svg/SVGImport.js');
/*#*/ } // options.svg

View file

@ -15,16 +15,12 @@
* Paper.js DOM to a SVG DOM.
*/
new function() {
var formatter = Formatter.instance,
namespaces = {
href: 'http://www.w3.org/1999/xlink',
xlink: 'http://www.w3.org/2000/xmlns'
};
var formatter = Formatter.instance;
function setAttributes(node, attrs) {
for (var key in attrs) {
var val = attrs[key],
namespace = namespaces[key];
namespace = SVGNamespaces[key];
if (typeof val === 'number')
val = formatter.number(val);
if (namespace) {

View file

@ -20,22 +20,22 @@ new function() {
// index is option, and if passed, causes a lookup in a list.
function getValue(node, key, allowNull, index) {
// node[key].baseVal will even be set if the node did not define the
// attribute, so if allowNull is true, we need to also check
// node.getAttribute(key) == null
var base = (!allowNull || node.getAttribute(key) != null)
&& node[key] && node[key].baseVal;
var namespace = SVGNamespaces[key];
var value = namespace
? node.getAttributeNS(namespace, key)
: node.getAttribute(key);
// Note: String values are unfortunately not stored in base.value, but
// in base directly, so we need to check both, also on item lists, using
// Base.pick(base.value, base)
return base
? index !== undefined
// Item list? Look up by index:
? index < base.numberOfItems
? Base.pick((base = base.getItem(index)).value, base)
: null
: Base.pick(base.value, base)
: null;
if (index != null && value != null) {
var values = value.split(',');
value = values[index];
if (value == null)
value = values[values.length - 1];
}
if (/^[\d.+-]/.test(value))
value = parseFloat(value);
return value;
}
function getPoint(node, x, y, allowNull, index) {
@ -273,9 +273,9 @@ new function() {
function applyTransform(item, value, name, node) {
// http://www.w3.org/TR/SVG/types.html#DataTypeTransformList
var transforms = node[name].baseVal,
var transforms = node[name] && node[name].baseVal,
matrix = new Matrix();
for (var i = 0, l = transforms.numberOfItems; i < l; i++) {
for (var i = 0, l = transforms && transforms.numberOfItems; i < l; i++) {
var mx = transforms.getItem(i).matrix;
matrix.concatenate(
new Matrix(mx.a, mx.b, mx.c, mx.d, mx.e, mx.f));

16
src/svg/SVGNamespaces.js Normal file
View file

@ -0,0 +1,16 @@
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
var SVGNamespaces = {
href: 'http://www.w3.org/1999/xlink',
xlink: 'http://www.w3.org/2000/xmlns'
};