attribute.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. 'use strict';
  2. exports.__esModule = true;
  3. 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; }; }();
  4. var _namespace = require('./namespace');
  5. var _namespace2 = _interopRequireDefault(_namespace);
  6. var _types = require('./types');
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9. 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; }
  10. 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; }
  11. var Attribute = function (_Namespace) {
  12. _inherits(Attribute, _Namespace);
  13. function Attribute() {
  14. var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  15. _classCallCheck(this, Attribute);
  16. var _this = _possibleConstructorReturn(this, _Namespace.call(this, opts));
  17. _this.type = _types.ATTRIBUTE;
  18. _this.raws = _this.raws || {};
  19. _this._constructed = true;
  20. return _this;
  21. }
  22. Attribute.prototype._spacesFor = function _spacesFor(name) {
  23. var attrSpaces = { before: '', after: '' };
  24. var spaces = this.spaces[name] || {};
  25. var rawSpaces = this.raws.spaces && this.raws.spaces[name] || {};
  26. return Object.assign(attrSpaces, spaces, rawSpaces);
  27. };
  28. Attribute.prototype._valueFor = function _valueFor(name) {
  29. return this.raws[name] || this[name];
  30. };
  31. Attribute.prototype._stringFor = function _stringFor(name) {
  32. var spaceName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : name;
  33. var concat = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultAttrConcat;
  34. var attrSpaces = this._spacesFor(spaceName);
  35. return concat(this._valueFor(name), attrSpaces);
  36. };
  37. /**
  38. * returns the offset of the attribute part specified relative to the
  39. * start of the node of the output string.
  40. *
  41. * * "ns" - alias for "namespace"
  42. * * "namespace" - the namespace if it exists.
  43. * * "attribute" - the attribute name
  44. * * "attributeNS" - the start of the attribute or its namespace
  45. * * "operator" - the match operator of the attribute
  46. * * "value" - The value (string or identifier)
  47. * * "insensitive" - the case insensitivity flag;
  48. * @param part One of the possible values inside an attribute.
  49. * @returns -1 if the name is invalid or the value doesn't exist in this attribute.
  50. */
  51. Attribute.prototype.offsetOf = function offsetOf(name) {
  52. var count = 1;
  53. var attributeSpaces = this._spacesFor("attribute");
  54. count += attributeSpaces.before.length;
  55. if (name === "namespace" || name === "ns") {
  56. return this.namespace ? count : -1;
  57. }
  58. if (name === "attributeNS") {
  59. return count;
  60. }
  61. count += this.namespaceString.length;
  62. if (this.namespace) {
  63. count += 1;
  64. }
  65. if (name === "attribute") {
  66. return count;
  67. }
  68. count += this._valueFor("attribute").length;
  69. count += attributeSpaces.after.length;
  70. var operatorSpaces = this._spacesFor("operator");
  71. count += operatorSpaces.before.length;
  72. var operator = this._valueFor("operator");
  73. if (name === "operator") {
  74. return operator ? count : -1;
  75. }
  76. count += operator.length;
  77. count += operatorSpaces.after.length;
  78. var valueSpaces = this._spacesFor("value");
  79. count += valueSpaces.before.length;
  80. var value = this._valueFor("value");
  81. if (name === "value") {
  82. return value ? count : -1;
  83. }
  84. count += value.length;
  85. count += valueSpaces.after.length;
  86. var insensitiveSpaces = this._spacesFor("insensitive");
  87. count += insensitiveSpaces.before.length;
  88. if (name === "insensitive") {
  89. return this.insensitive ? count : -1;
  90. }
  91. return -1;
  92. };
  93. Attribute.prototype.toString = function toString() {
  94. var _this2 = this;
  95. var selector = [this.spaces.before, '['];
  96. selector.push(this._stringFor('qualifiedAttribute', 'attribute'));
  97. if (this.operator && this.value) {
  98. selector.push(this._stringFor('operator'));
  99. selector.push(this._stringFor('value'));
  100. selector.push(this._stringFor('insensitiveFlag', 'insensitive', function (attrValue, attrSpaces) {
  101. if (attrValue.length > 0 && !_this2.quoted && attrSpaces.before.length === 0 && !(_this2.spaces.value && _this2.spaces.value.after)) {
  102. attrSpaces.before = " ";
  103. }
  104. return defaultAttrConcat(attrValue, attrSpaces);
  105. }));
  106. }
  107. selector.push(']');
  108. selector.push(this.spaces.after);
  109. return selector.join('');
  110. };
  111. _createClass(Attribute, [{
  112. key: 'qualifiedAttribute',
  113. get: function get() {
  114. return this.qualifiedName(this.raws.attribute || this.attribute);
  115. }
  116. }, {
  117. key: 'insensitiveFlag',
  118. get: function get() {
  119. return this.insensitive ? 'i' : '';
  120. }
  121. }, {
  122. key: 'value',
  123. get: function get() {
  124. return this._value;
  125. },
  126. set: function set(v) {
  127. this._value = v;
  128. if (this._constructed) {
  129. delete this.raws.value;
  130. }
  131. }
  132. }, {
  133. key: 'namespace',
  134. get: function get() {
  135. return this._namespace;
  136. },
  137. set: function set(v) {
  138. this._namespace = v;
  139. if (this._constructed) {
  140. delete this.raws.namespace;
  141. }
  142. }
  143. }, {
  144. key: 'attribute',
  145. get: function get() {
  146. return this._attribute;
  147. },
  148. set: function set(v) {
  149. this._attribute = v;
  150. if (this._constructed) {
  151. delete this.raws.attibute;
  152. }
  153. }
  154. }]);
  155. return Attribute;
  156. }(_namespace2.default);
  157. exports.default = Attribute;
  158. function defaultAttrConcat(attrValue, attrSpaces) {
  159. return '' + attrSpaces.before + attrValue + attrSpaces.after;
  160. }
  161. module.exports = exports['default'];