3 exports.__esModule = true;
5 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
7 var _namespace = require('./namespace');
9 var _namespace2 = _interopRequireDefault(_namespace);
11 var _types = require('./types');
13 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
17 function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
19 function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
21 var Attribute = function (_Namespace) {
22 _inherits(Attribute, _Namespace);
24 function Attribute() {
25 var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
27 _classCallCheck(this, Attribute);
29 var _this = _possibleConstructorReturn(this, _Namespace.call(this, opts));
31 _this.type = _types.ATTRIBUTE;
32 _this.raws = _this.raws || {};
33 _this._constructed = true;
37 Attribute.prototype._spacesFor = function _spacesFor(name) {
38 var attrSpaces = { before: '', after: '' };
39 var spaces = this.spaces[name] || {};
40 var rawSpaces = this.raws.spaces && this.raws.spaces[name] || {};
41 return Object.assign(attrSpaces, spaces, rawSpaces);
44 Attribute.prototype._valueFor = function _valueFor(name) {
45 return this.raws[name] || this[name];
48 Attribute.prototype._stringFor = function _stringFor(name) {
49 var spaceName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : name;
50 var concat = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultAttrConcat;
52 var attrSpaces = this._spacesFor(spaceName);
53 return concat(this._valueFor(name), attrSpaces);
57 * returns the offset of the attribute part specified relative to the
58 * start of the node of the output string.
60 * * "ns" - alias for "namespace"
61 * * "namespace" - the namespace if it exists.
62 * * "attribute" - the attribute name
63 * * "attributeNS" - the start of the attribute or its namespace
64 * * "operator" - the match operator of the attribute
65 * * "value" - The value (string or identifier)
66 * * "insensitive" - the case insensitivity flag;
67 * @param part One of the possible values inside an attribute.
68 * @returns -1 if the name is invalid or the value doesn't exist in this attribute.
72 Attribute.prototype.offsetOf = function offsetOf(name) {
74 var attributeSpaces = this._spacesFor("attribute");
75 count += attributeSpaces.before.length;
76 if (name === "namespace" || name === "ns") {
77 return this.namespace ? count : -1;
79 if (name === "attributeNS") {
83 count += this.namespaceString.length;
87 if (name === "attribute") {
91 count += this._valueFor("attribute").length;
92 count += attributeSpaces.after.length;
93 var operatorSpaces = this._spacesFor("operator");
94 count += operatorSpaces.before.length;
95 var operator = this._valueFor("operator");
96 if (name === "operator") {
97 return operator ? count : -1;
100 count += operator.length;
101 count += operatorSpaces.after.length;
102 var valueSpaces = this._spacesFor("value");
103 count += valueSpaces.before.length;
104 var value = this._valueFor("value");
105 if (name === "value") {
106 return value ? count : -1;
109 count += value.length;
110 count += valueSpaces.after.length;
111 var insensitiveSpaces = this._spacesFor("insensitive");
112 count += insensitiveSpaces.before.length;
113 if (name === "insensitive") {
114 return this.insensitive ? count : -1;
119 Attribute.prototype.toString = function toString() {
122 var selector = [this.spaces.before, '['];
124 selector.push(this._stringFor('qualifiedAttribute', 'attribute'));
126 if (this.operator && this.value) {
127 selector.push(this._stringFor('operator'));
128 selector.push(this._stringFor('value'));
129 selector.push(this._stringFor('insensitiveFlag', 'insensitive', function (attrValue, attrSpaces) {
130 if (attrValue.length > 0 && !_this2.quoted && attrSpaces.before.length === 0 && !(_this2.spaces.value && _this2.spaces.value.after)) {
131 attrSpaces.before = " ";
133 return defaultAttrConcat(attrValue, attrSpaces);
138 selector.push(this.spaces.after);
139 return selector.join('');
142 _createClass(Attribute, [{
143 key: 'qualifiedAttribute',
144 get: function get() {
145 return this.qualifiedName(this.raws.attribute || this.attribute);
148 key: 'insensitiveFlag',
149 get: function get() {
150 return this.insensitive ? 'i' : '';
154 get: function get() {
157 set: function set(v) {
159 if (this._constructed) {
160 delete this.raws.value;
165 get: function get() {
166 return this._namespace;
168 set: function set(v) {
170 if (this._constructed) {
171 delete this.raws.namespace;
176 get: function get() {
177 return this._attribute;
179 set: function set(v) {
181 if (this._constructed) {
182 delete this.raws.attibute;
188 }(_namespace2.default);
190 exports.default = Attribute;
193 function defaultAttrConcat(attrValue, attrSpaces) {
194 return '' + attrSpaces.before + attrValue + attrSpaces.after;
196 module.exports = exports['default'];