Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for svg #35

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 48 additions & 34 deletions dist/index.es6.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import _extend from 'extend';

var undefinedv = function (v) { return v === undefined; };

var number = function (v) { return typeof v === 'number'; };

var string = function (v) { return typeof v === 'string'; };
Expand All @@ -16,12 +14,8 @@ var fun = function (v) { return typeof v === 'function'; };

var vnode = function (v) { return object(v) && 'sel' in v && 'data' in v && 'children' in v && 'text' in v; };

var svgPropsMap = { svg: 1, circle: 1, ellipse: 1, line: 1, polygon: 1,
polyline: 1, rect: 1, g: 1, path: 1, text: 1 };

var svg = function (v) { return v.sel in svgPropsMap; };

// TODO: stop using extend here

var extend = function () {
var objs = [], len = arguments.length;
while ( len-- ) objs[ len ] = arguments[ len ];
Expand Down Expand Up @@ -58,30 +52,39 @@ var mapObject = function (obj, fn) { return Object.keys(obj).map(

var deepifyKeys = function (obj) { return mapObject(obj,
function (key, val) {
var obj, obj$1;

var dashIndex = key.indexOf('-');
if (dashIndex > -1) {
var moduleData = {};
moduleData[key.slice(dashIndex + 1)] = val;
return ( obj = {}, obj[key.slice(0, dashIndex)] = moduleData, obj )
var obj;
}
return ( obj$1 = {}, obj$1[key] = val, obj$1 )
var obj$1;
}
); };

var flatifyKeys = function (obj) { return mapObject(obj,
function (mod, data) { return !object(data) ? (( obj = {}, obj[mod] = data, obj )) : mapObject(
function (mod, data) {
var obj;

return !object(data) ? (( obj = {}, obj[mod] = data, obj )) : mapObject(
flatifyKeys(data),
function (key, val) { return (( obj = {}, obj[(mod + "-" + key)] = val, obj ))
var obj; }
)
var obj; }
function (key, val) {
var obj;

return (( obj = {}, obj[(mod + "-" + key)] = val, obj ));
}
);
}
); };

var omit = function (key, obj) { return mapObject(obj,
function (mod, data) { return mod !== key ? (( obj = {}, obj[mod] = data, obj )) : {}
var obj; }
function (mod, data) {
var obj;

return mod !== key ? (( obj = {}, obj[mod] = data, obj )) : {};
}
); };

// Const fnName = (...params) => guard ? default : ...
Expand All @@ -95,23 +98,25 @@ var createTextElement = function (text$$1) { return !text(text$$1) ? undefined :
key: undefined
}; };

var considerSvg = function (vnode$$1) { return !svg(vnode$$1) ? vnode$$1 :
var transformSvg = function (vnode$$1) {
assign(vnode$$1,
{ data: omit('props', extend(vnode$$1.data,
{ ns: 'http://www.w3.org/2000/svg', attrs: omit('className', extend(vnode$$1.data.props,
{ class: vnode$$1.data.props ? vnode$$1.data.props.className : undefined }
)) }
)) },
{ children: undefinedv(vnode$$1.children) ? undefined :
vnode$$1.children.map(function (child) { return considerSvg(child); })
}
); };
)) }
);
if (vnode$$1.children) {
vnode$$1.children.forEach(transformSvg);
}
};

var considerData = function (data) {
return !data.data ? data : mapObject(data, function (mod, data) {
var obj;

var key = mod === 'data' ? 'dataset' : mod;
return (( obj = {}, obj[key] = data, obj ))
var obj;
})
};

Expand All @@ -122,20 +127,24 @@ var considerAria = function (data) { return data.attrs || data.aria ? omit('aria
) : data; };

var considerProps = function (data) { return mapObject(data,
function (key, val) { return object(val) ? ( obj = {}, obj[key] = val, obj ) :
{ props: ( obj$1 = {}, obj$1[key] = val, obj$1 ) }
var obj;
var obj$1; }
function (key, val) {
var obj, obj$1;

return object(val) ? ( obj = {}, obj[key] = val, obj ) :
{ props: ( obj$1 = {}, obj$1[key] = val, obj$1 ) };
}
); };

var rewritesMap = { for: 1, role: 1, tabindex: 1 };

var considerAttrs = function (data) { return mapObject(data,
function (key, data) { return !(key in rewritesMap) ? ( obj = {}, obj[key] = data, obj ) : {
function (key, data) {
var obj, obj$1;

return !(key in rewritesMap) ? ( obj = {}, obj[key] = data, obj ) : {
attrs: extend(data.attrs, ( obj$1 = {}, obj$1[key] = data, obj$1 ))
}
var obj;
var obj$1; }
};
}
); };

var considerKey = function (data) {
Expand All @@ -161,18 +170,23 @@ var createElement = function (sel, data) {
return sel(data || {}, children)
}
var text$$1 = sanitizeText(children);
return considerSvg({
var vnode$$1 = {
sel: sel,
data: data ? sanitizeData(data) : {},
children: text$$1 ? undefined : sanitizeChildren(children),
text: text$$1,
elm: undefined,
key: data ? data.key : undefined
})
};
if (sel === 'svg') {
transformSvg(vnode$$1);
}
return vnode$$1
};

var index = {
createElement: createElement
};

export { createElement };export default index;
export default index;
export { createElement };
81 changes: 47 additions & 34 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau

var _extend = _interopDefault(require('extend'));

var undefinedv = function (v) { return v === undefined; };

var number = function (v) { return typeof v === 'number'; };

var string = function (v) { return typeof v === 'string'; };
Expand All @@ -22,12 +20,8 @@ var fun = function (v) { return typeof v === 'function'; };

var vnode = function (v) { return object(v) && 'sel' in v && 'data' in v && 'children' in v && 'text' in v; };

var svgPropsMap = { svg: 1, circle: 1, ellipse: 1, line: 1, polygon: 1,
polyline: 1, rect: 1, g: 1, path: 1, text: 1 };

var svg = function (v) { return v.sel in svgPropsMap; };

// TODO: stop using extend here

var extend = function () {
var objs = [], len = arguments.length;
while ( len-- ) objs[ len ] = arguments[ len ];
Expand Down Expand Up @@ -64,30 +58,39 @@ var mapObject = function (obj, fn) { return Object.keys(obj).map(

var deepifyKeys = function (obj) { return mapObject(obj,
function (key, val) {
var obj, obj$1;

var dashIndex = key.indexOf('-');
if (dashIndex > -1) {
var moduleData = {};
moduleData[key.slice(dashIndex + 1)] = val;
return ( obj = {}, obj[key.slice(0, dashIndex)] = moduleData, obj )
var obj;
}
return ( obj$1 = {}, obj$1[key] = val, obj$1 )
var obj$1;
}
); };

var flatifyKeys = function (obj) { return mapObject(obj,
function (mod, data) { return !object(data) ? (( obj = {}, obj[mod] = data, obj )) : mapObject(
function (mod, data) {
var obj;

return !object(data) ? (( obj = {}, obj[mod] = data, obj )) : mapObject(
flatifyKeys(data),
function (key, val) { return (( obj = {}, obj[(mod + "-" + key)] = val, obj ))
var obj; }
)
var obj; }
function (key, val) {
var obj;

return (( obj = {}, obj[(mod + "-" + key)] = val, obj ));
}
);
}
); };

var omit = function (key, obj) { return mapObject(obj,
function (mod, data) { return mod !== key ? (( obj = {}, obj[mod] = data, obj )) : {}
var obj; }
function (mod, data) {
var obj;

return mod !== key ? (( obj = {}, obj[mod] = data, obj )) : {};
}
); };

// Const fnName = (...params) => guard ? default : ...
Expand All @@ -101,23 +104,25 @@ var createTextElement = function (text$$1) { return !text(text$$1) ? undefined :
key: undefined
}; };

var considerSvg = function (vnode$$1) { return !svg(vnode$$1) ? vnode$$1 :
var transformSvg = function (vnode$$1) {
assign(vnode$$1,
{ data: omit('props', extend(vnode$$1.data,
{ ns: 'http://www.w3.org/2000/svg', attrs: omit('className', extend(vnode$$1.data.props,
{ class: vnode$$1.data.props ? vnode$$1.data.props.className : undefined }
)) }
)) },
{ children: undefinedv(vnode$$1.children) ? undefined :
vnode$$1.children.map(function (child) { return considerSvg(child); })
}
); };
)) }
);
if (vnode$$1.children) {
vnode$$1.children.forEach(transformSvg);
}
};

var considerData = function (data) {
return !data.data ? data : mapObject(data, function (mod, data) {
var obj;

var key = mod === 'data' ? 'dataset' : mod;
return (( obj = {}, obj[key] = data, obj ))
var obj;
})
};

Expand All @@ -128,20 +133,24 @@ var considerAria = function (data) { return data.attrs || data.aria ? omit('aria
) : data; };

var considerProps = function (data) { return mapObject(data,
function (key, val) { return object(val) ? ( obj = {}, obj[key] = val, obj ) :
{ props: ( obj$1 = {}, obj$1[key] = val, obj$1 ) }
var obj;
var obj$1; }
function (key, val) {
var obj, obj$1;

return object(val) ? ( obj = {}, obj[key] = val, obj ) :
{ props: ( obj$1 = {}, obj$1[key] = val, obj$1 ) };
}
); };

var rewritesMap = { for: 1, role: 1, tabindex: 1 };

var considerAttrs = function (data) { return mapObject(data,
function (key, data) { return !(key in rewritesMap) ? ( obj = {}, obj[key] = data, obj ) : {
function (key, data) {
var obj, obj$1;

return !(key in rewritesMap) ? ( obj = {}, obj[key] = data, obj ) : {
attrs: extend(data.attrs, ( obj$1 = {}, obj$1[key] = data, obj$1 ))
}
var obj;
var obj$1; }
};
}
); };

var considerKey = function (data) {
Expand All @@ -167,19 +176,23 @@ var createElement = function (sel, data) {
return sel(data || {}, children)
}
var text$$1 = sanitizeText(children);
return considerSvg({
var vnode$$1 = {
sel: sel,
data: data ? sanitizeData(data) : {},
children: text$$1 ? undefined : sanitizeChildren(children),
text: text$$1,
elm: undefined,
key: data ? data.key : undefined
})
};
if (sel === 'svg') {
transformSvg(vnode$$1);
}
return vnode$$1
};

var index = {
createElement: createElement
};

exports.createElement = createElement;
exports['default'] = index;
exports.default = index;
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"benchmark": "^2.1.4",
"buble": "^0.17.3",
"buble": "^0.19.3",
"eslint-config-xo-swizz": "^0.11.0",
"rimraf": "^2.6.1",
"rollup": "^0.41.3",
"rollup": "^0.60.7",
"rollup-plugin-buble": "^0.19.2",
"simple-git": "^1.92.0",
"snabbdom": "^0.6.7",
"traceur": "^0.0.111",
Expand All @@ -42,10 +42,7 @@
"lint": "./node_modules/.bin/xo",
"test:prebuild": "ava --match \"trans -*\" --match \"src -*\" --match \"utils -*\"",
"test:postbuild": "ava --match \"dist -*\"",
"transpile": "./node_modules/.bin/buble -i src/ -o lib/ --no modules --objectAssign fn.assign",
"modules": "rollup -i lib/index.js -o dist/index.js -f cjs && rollup -i lib/index.js -o dist/index.es6.js -f es",
"clean": "./node_modules/.bin/rimraf 'lib/'",
"build": "npm run transpile && npm run modules && npm run clean",
"build": "rollup --config",
"make": "npm run lint && npm run test:prebuild && npm run build && npm run test:postbuild",
"perf:all": "node perf/build-branches && node perf/run",
"perf": "node perf/run"
Expand All @@ -64,7 +61,8 @@
"extends": "xo-swizz",
"rules": {
"import/no-dynamic-require": "off",
"capitalized-comments": "off"
"capitalized-comments": "off",
"linebreak-style": "off"
},
"ignores": [
"test/**/transform-*.js"
Expand Down
4 changes: 2 additions & 2 deletions perf/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/snnabdom-new.js
/snnabdom-base.js
/snabbdom-new.js
/snabbdom-base.js
4 changes: 1 addition & 3 deletions perf/build-branches.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* eslint unicorn/no-process-exit: 0 */

const execSync = require('child_process').execSync
const fs = require('fs')
const simpleGit = require('simple-git')()

const baseBranch = process.argv[2] || 'develop'

function buildSnabbdom(suffix) {
execSync('npm run build', { stdio: [0, 1, 2] })
fs.writeFileSync(`perf/snnabdom-${suffix}.js`, fs.readFileSync('dist/index.js', 'utf-8'), 'utf-8')
execSync(`npm run build -- --file perf/snabbdom-${suffix}.js`, { stdio: [0, 1, 2] })
}

simpleGit.status((err, status) => {
Expand Down
4 changes: 2 additions & 2 deletions perf/modules-attribute-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const Benchmark = require('benchmark')
const h = require('snabbdom/h').default
const baseCreateElement = require('./snnabdom-base').createElement
const newCreateElement = require('./snnabdom-new').createElement
const baseCreateElement = require('./snabbdom-base').createElement
const newCreateElement = require('./snabbdom-new').createElement

const isVisible = true
const isEnabled = false
Expand Down
Loading