Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
**/*.map
5 changes: 5 additions & 0 deletions bili.config.es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
format: 'es',
js: false,
input: 'src/xmlion.js'
};
347 changes: 140 additions & 207 deletions dist/xmlion.js
Original file line number Diff line number Diff line change
@@ -1,236 +1,169 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.xmlion = global.xmlion || {})));
(factory((global.xmlion = {})));
}(this, (function (exports) { 'use strict';

var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
var isArray = Array.isArray;
var clone = function (array) { return array.slice(0); };
var join = function (array) { return array.join(''); };
var map = function (fn, array) { return array.map(fn); };
var filter = function (fn, array) { return array.filter(fn); };
var concat = function (array) {
var arrays = [], len = arguments.length - 1;
while ( len-- > 0 ) arrays[ len ] = arguments[ len + 1 ];

return array.concat.apply(array, arrays);
};
var toStringCub = function (c) { return c == null ? '' : c.ROARS ? c._buildElementString() : c; };
var mapCubsToString = function (cubs) { return map(toStringCub, cubs); };

var Lion = function Lion (tagName, attributes, cubs) {
this.tagName = tagName;
this.attr = attributes || {};
this.cubs = cubs || [];
};

var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
var prototypeAccessors = { ROARS: {},cubs: {},attributes: {} };

prototypeAccessors.ROARS.get = function () {
return true;
};

prototypeAccessors.cubs.set = function (cubs) {
if (isArray(cubs)) {
this._cubs = cubs;
} else {
throw new TypeError('Lion wants cub[]');
}
};

return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
prototypeAccessors.cubs.get = function () {
return clone(this._cubs);
};

var isArray = Array.isArray;
prototypeAccessors.attributes.set = function (attr) {
this.attr = attr;
};

prototypeAccessors.attributes.get = function () {
return this.attr;
};

var clone = function clone(array) {
return array.slice(0);
Lion.prototype.setCubs = function setCubs (cubs) {
this.cubs = cubs;
return this;
};
var join = function join(array) {
return array.join('');

Lion.prototype.getCubs = function getCubs () {
return this.cubs;
};
var map = function map(fn, array) {
return array.map(fn);

Lion.prototype.setAttributes = function setAttributes (attr) {
this.attributes = attr;
return this;
};
var filter = function filter(fn, array) {
return array.filter(fn);

Lion.prototype.getAttributes = function getAttributes () {
return this.attributes;
};
var concat = function concat(array) {
for (var _len = arguments.length, arrays = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
arrays[_key - 1] = arguments[_key];

Lion.prototype.handleBuildAttribute = function handleBuildAttribute (key, value) {
if (value === null || value === undefined) {
return key;
}
return (key + "=\"" + value + "\"");
};

return array.concat.apply(array, arrays);
Lion.prototype._buildAttributeString = function _buildAttributeString () {
var this$1 = this;

var attr = this.attr;
var keys = Object.keys(attr).sort();
if (keys.length > 0) {
return join(map(function (key) { return (" " + (this$1.handleBuildAttribute(key, attr[key]))); }, keys));
}
return '';
};
var toStringCub = function toStringCub(c) {
return c == null ? '' : c.ROARS ? c._buildElementString() : c;

Lion.prototype._buildContentString = function _buildContentString () {
return join(mapCubsToString(this._cubs));
};
var mapCubsToString = function mapCubsToString(cubs) {
return map(toStringCub, cubs);

Lion.prototype._buildElementString = function _buildElementString () {
var me = this;
var tagName = me.tagName;
var attributes = me._buildAttributeString();

if (me.hasCubs()) {
var content = me._buildContentString();

return ("<" + tagName + attributes + ">" + content + "</" + tagName + ">");
} else {
return ("<" + tagName + attributes + "/>");
}
};

var Lion = function () {
function Lion(tagName, attributes, cubs) {
classCallCheck(this, Lion);
Lion.prototype.addAttribute = function addAttribute (name, value) {
var lion = this;
lion.attr[name] = value;
return lion;
};

this.tagName = tagName;
this.attr = attributes || {};
this.cubs = cubs || [];
Lion.prototype.removeAttribute = function removeAttribute (name) {
var lion = this;
if (lion.attr[name]) {
delete lion.attr[name];
}
return lion;
};

Lion.prototype.addAttributes = function addAttributes (attr) {
var lion = this;
var names = Object.keys(attr);
map(function (name) { return lion.addAttribute(name, attr[name]); }, names);
return lion;
};

createClass(Lion, [{
key: 'setCubs',
value: function setCubs(cubs) {
this.cubs = cubs;
return this;
}
}, {
key: 'getCubs',
value: function getCubs() {
return this.cubs;
}
}, {
key: 'setAttributes',
value: function setAttributes(attr) {
this.attributes = attr;
return this;
}
}, {
key: 'getAttributes',
value: function getAttributes() {
return this.attributes;
}
}, {
key: 'handleBuildAttribute',
value: function handleBuildAttribute(key, value) {
if (value === null || value === undefined) {
return key;
}
return key + '="' + value + '"';
}
}, {
key: '_buildAttributeString',
value: function _buildAttributeString() {
var _this = this;

var attr = this.attr;
var keys = Object.keys(attr).sort();
if (keys.length > 0) {
return join(map(function (key) {
return ' ' + _this.handleBuildAttribute(key, attr[key]);
}, keys));
}
return '';
}
}, {
key: '_buildContentString',
value: function _buildContentString() {
return join(mapCubsToString(this._cubs));
}
}, {
key: '_buildElementString',
value: function _buildElementString() {
var me = this;
var tagName = me.tagName;
var attributes = me._buildAttributeString();

if (me.hasCubs()) {
var content = me._buildContentString();

return '<' + tagName + attributes + '>' + content + '</' + tagName + '>';
} else {
return '<' + tagName + attributes + '/>';
}
}
}, {
key: 'addAttribute',
value: function addAttribute(name, value) {
var lion = this;
lion.attr[name] = value;
return lion;
}
}, {
key: 'removeAttribute',
value: function removeAttribute(name) {
var lion = this;
if (lion.attr[name]) {
delete lion.attr[name];
}
return lion;
}
}, {
key: 'addAttributes',
value: function addAttributes(attr) {
var lion = this;
var names = Object.keys(attr);
map(function (name) {
return lion.addAttribute(name, attr[name]);
}, names);
return lion;
}
}, {
key: 'addCub',
value: function addCub(cub) {
var lion = this;
lion._cubs.push(cub);
return lion;
}
}, {
key: 'removeCub',
value: function removeCub(cub) {
var lion = this;
var index = lion._cubs.indexOf(cub);
if (index >= 0) {
lion.cubs = filter(function (c, i) {
return i !== index;
}, lion.cubs);
}
return lion;
}
}, {
key: 'addCubs',
value: function addCubs(cubs) {
if (isArray(cubs)) {
this.cubs = concat(this.cubs, cubs);
}
return this;
}
}, {
key: 'removeAllCubs',
value: function removeAllCubs() {
this._cubs = [];
return this;
}
}, {
key: 'hasCubs',
value: function hasCubs() {
return this._cubs && this._cubs.length > 0;
}
}, {
key: 'value',
value: function value() {
return this._buildElementString();
}
}, {
key: 'ROARS',
get: function get$$1() {
return true;
}
}, {
key: 'cubs',
set: function set$$1(cubs) {
if (isArray(cubs)) {
this._cubs = cubs;
} else {
throw new TypeError('Lion wants cub[]');
}
},
get: function get$$1() {
return clone(this._cubs);
}
}, {
key: 'attributes',
set: function set$$1(attr) {
this.attr = attr;
},
get: function get$$1() {
return this.attr;
}
}]);
return Lion;
}();

var xmlion = function xmlion(tagName, attributes, cubs) {
return new Lion(tagName, attributes, cubs);
Lion.prototype.addCub = function addCub (cub) {
var lion = this;
lion._cubs.push(cub);
return lion;
};

Lion.prototype.removeCub = function removeCub (cub) {
var lion = this;
var index = lion._cubs.indexOf(cub);
if (index >= 0) {
lion.cubs = filter(function (c, i) { return (i !== index); }, lion.cubs);
}
return lion;
};

Lion.prototype.addCubs = function addCubs (cubs) {
if (isArray(cubs)) {
this.cubs = concat(this.cubs, cubs);
}
return this;
};

Lion.prototype.removeAllCubs = function removeAllCubs () {
this._cubs = [];
return this;
};

Lion.prototype.hasCubs = function hasCubs () {
return this._cubs && this._cubs.length > 0;
};

Lion.prototype.value = function value () {
return this._buildElementString();
};

Object.defineProperties( Lion.prototype, prototypeAccessors );

var xmlion = function (tagName, attributes, cubs) { return new Lion(tagName, attributes, cubs); };

exports.Lion = Lion;
exports.xmlion = xmlion;

Expand Down
3 changes: 2 additions & 1 deletion dist/xmlion.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading