attribute.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. "use strict";
  2. exports.__esModule = true;
  3. var _CSSESC_QUOTE_OPTIONS;
  4. 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; }; }();
  5. exports.unescapeValue = unescapeValue;
  6. var _cssesc = require("cssesc");
  7. var _cssesc2 = _interopRequireDefault(_cssesc);
  8. var _unesc = require("../util/unesc");
  9. var _unesc2 = _interopRequireDefault(_unesc);
  10. var _namespace = require("./namespace");
  11. var _namespace2 = _interopRequireDefault(_namespace);
  12. var _types = require("./types");
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  15. 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; }
  16. 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; }
  17. var _require = require("util"),
  18. deprecate = _require.deprecate;
  19. var WRAPPED_IN_QUOTES = /^('|")(.*)\1$/;
  20. var warnOfDeprecatedValueAssignment = deprecate(function () {}, "Assigning an attribute a value containing characters that might need to be escaped is deprecated. " + "Call attribute.setValue() instead.");
  21. var warnOfDeprecatedQuotedAssignment = deprecate(function () {}, "Assigning attr.quoted is deprecated and has no effect. Assign to attr.quoteMark instead.");
  22. var warnOfDeprecatedConstructor = deprecate(function () {}, "Constructing an Attribute selector with a value without specifying quoteMark is deprecated. Note: The value should be unescaped now.");
  23. function unescapeValue(value) {
  24. var deprecatedUsage = false;
  25. var quoteMark = null;
  26. var unescaped = value;
  27. var m = unescaped.match(WRAPPED_IN_QUOTES);
  28. if (m) {
  29. quoteMark = m[1];
  30. unescaped = m[2];
  31. }
  32. unescaped = (0, _unesc2.default)(unescaped);
  33. if (unescaped !== value) {
  34. deprecatedUsage = true;
  35. }
  36. return {
  37. deprecatedUsage: deprecatedUsage,
  38. unescaped: unescaped,
  39. quoteMark: quoteMark
  40. };
  41. }
  42. function handleDeprecatedContructorOpts(opts) {
  43. if (opts.quoteMark !== undefined) {
  44. return opts;
  45. }
  46. if (opts.value === undefined) {
  47. return opts;
  48. }
  49. warnOfDeprecatedConstructor();
  50. var _unescapeValue = unescapeValue(opts.value),
  51. quoteMark = _unescapeValue.quoteMark,
  52. unescaped = _unescapeValue.unescaped;
  53. if (!opts.raws) {
  54. opts.raws = {};
  55. }
  56. if (opts.raws.value === undefined) {
  57. opts.raws.value = opts.value;
  58. }
  59. opts.value = unescaped;
  60. opts.quoteMark = quoteMark;
  61. return opts;
  62. }
  63. var Attribute = function (_Namespace) {
  64. _inherits(Attribute, _Namespace);
  65. function Attribute() {
  66. var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  67. _classCallCheck(this, Attribute);
  68. var _this = _possibleConstructorReturn(this, _Namespace.call(this, handleDeprecatedContructorOpts(opts)));
  69. _this.type = _types.ATTRIBUTE;
  70. _this.raws = _this.raws || {};
  71. Object.defineProperty(_this.raws, 'unquoted', {
  72. get: deprecate(function () {
  73. return _this.value;
  74. }, "attr.raws.unquoted is deprecated. Call attr.value instead."),
  75. set: deprecate(function () {
  76. return _this.value;
  77. }, "Setting attr.raws.unquoted is deprecated and has no effect. attr.value is unescaped by default now.")
  78. });
  79. _this._constructed = true;
  80. return _this;
  81. }
  82. /**
  83. * Returns the Attribute's value quoted such that it would be legal to use
  84. * in the value of a css file. The original value's quotation setting
  85. * used for stringification is left unchanged. See `setValue(value, options)`
  86. * if you want to control the quote settings of a new value for the attribute.
  87. *
  88. * You can also change the quotation used for the current value by setting quoteMark.
  89. *
  90. * Options:
  91. * * quoteMark {'"' | "'" | null} - Use this value to quote the value. If this
  92. * option is not set, the original value for quoteMark will be used. If
  93. * indeterminate, a double quote is used. The legal values are:
  94. * * `null` - the value will be unquoted and characters will be escaped as necessary.
  95. * * `'` - the value will be quoted with a single quote and single quotes are escaped.
  96. * * `"` - the value will be quoted with a double quote and double quotes are escaped.
  97. * * preferCurrentQuoteMark {boolean} - if true, prefer the source quote mark
  98. * over the quoteMark option value.
  99. * * smart {boolean} - if true, will select a quote mark based on the value
  100. * and the other options specified here. See the `smartQuoteMark()`
  101. * method.
  102. **/
  103. Attribute.prototype.getQuotedValue = function getQuotedValue() {
  104. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  105. var quoteMark = this._determineQuoteMark(options);
  106. var cssescopts = CSSESC_QUOTE_OPTIONS[quoteMark];
  107. var escaped = (0, _cssesc2.default)(this._value, cssescopts);
  108. return escaped;
  109. };
  110. Attribute.prototype._determineQuoteMark = function _determineQuoteMark(options) {
  111. return options.smart ? this.smartQuoteMark(options) : this.preferredQuoteMark(options);
  112. };
  113. /**
  114. * Set the unescaped value with the specified quotation options. The value
  115. * provided must not include any wrapping quote marks -- those quotes will
  116. * be interpreted as part of the value and escaped accordingly.
  117. */
  118. Attribute.prototype.setValue = function setValue(value) {
  119. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  120. this._value = value;
  121. this._quoteMark = this._determineQuoteMark(options);
  122. this._syncRawValue();
  123. };
  124. /**
  125. * Intelligently select a quoteMark value based on the value's contents. If
  126. * the value is a legal CSS ident, it will not be quoted. Otherwise a quote
  127. * mark will be picked that minimizes the number of escapes.
  128. *
  129. * If there's no clear winner, the quote mark from these options is used,
  130. * then the source quote mark (this is inverted if `preferCurrentQuoteMark` is
  131. * true). If the quoteMark is unspecified, a double quote is used.
  132. *
  133. * @param options This takes the quoteMark and preferCurrentQuoteMark options
  134. * from the quoteValue method.
  135. */
  136. Attribute.prototype.smartQuoteMark = function smartQuoteMark(options) {
  137. var v = this.value;
  138. var numSingleQuotes = v.replace(/[^']/g, '').length;
  139. var numDoubleQuotes = v.replace(/[^"]/g, '').length;
  140. if (numSingleQuotes + numDoubleQuotes === 0) {
  141. var escaped = (0, _cssesc2.default)(v, { isIdentifier: true });
  142. if (escaped === v) {
  143. return Attribute.NO_QUOTE;
  144. } else {
  145. var pref = this.preferredQuoteMark(options);
  146. if (pref === Attribute.NO_QUOTE) {
  147. // pick a quote mark that isn't none and see if it's smaller
  148. var quote = this.quoteMark || options.quoteMark || Attribute.DOUBLE_QUOTE;
  149. var opts = CSSESC_QUOTE_OPTIONS[quote];
  150. var quoteValue = (0, _cssesc2.default)(v, opts);
  151. if (quoteValue.length < escaped.length) {
  152. return quote;
  153. }
  154. }
  155. return pref;
  156. }
  157. } else if (numDoubleQuotes === numSingleQuotes) {
  158. return this.preferredQuoteMark(options);
  159. } else if (numDoubleQuotes < numSingleQuotes) {
  160. return Attribute.DOUBLE_QUOTE;
  161. } else {
  162. return Attribute.SINGLE_QUOTE;
  163. }
  164. };
  165. /**
  166. * Selects the preferred quote mark based on the options and the current quote mark value.
  167. * If you want the quote mark to depend on the attribute value, call `smartQuoteMark(opts)`
  168. * instead.
  169. */
  170. Attribute.prototype.preferredQuoteMark = function preferredQuoteMark(options) {
  171. var quoteMark = options.preferCurrentQuoteMark ? this.quoteMark : options.quoteMark;
  172. if (quoteMark === undefined) {
  173. quoteMark = options.preferCurrentQuoteMark ? options.quoteMark : this.quoteMark;
  174. }
  175. if (quoteMark === undefined) {
  176. quoteMark = Attribute.DOUBLE_QUOTE;
  177. }
  178. return quoteMark;
  179. };
  180. Attribute.prototype._syncRawValue = function _syncRawValue() {
  181. var rawValue = (0, _cssesc2.default)(this._value, CSSESC_QUOTE_OPTIONS[this.quoteMark]);
  182. if (rawValue === this._value) {
  183. if (this.raws) {
  184. delete this.raws.value;
  185. }
  186. } else {
  187. this.raws.value = rawValue;
  188. }
  189. };
  190. Attribute.prototype._handleEscapes = function _handleEscapes(prop, value) {
  191. if (this._constructed) {
  192. var escaped = (0, _cssesc2.default)(value, { isIdentifier: true });
  193. if (escaped !== value) {
  194. this.raws[prop] = escaped;
  195. } else {
  196. delete this.raws[prop];
  197. }
  198. }
  199. };
  200. Attribute.prototype._spacesFor = function _spacesFor(name) {
  201. var attrSpaces = { before: '', after: '' };
  202. var spaces = this.spaces[name] || {};
  203. var rawSpaces = this.raws.spaces && this.raws.spaces[name] || {};
  204. return Object.assign(attrSpaces, spaces, rawSpaces);
  205. };
  206. Attribute.prototype._stringFor = function _stringFor(name) {
  207. var spaceName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : name;
  208. var concat = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultAttrConcat;
  209. var attrSpaces = this._spacesFor(spaceName);
  210. return concat(this.stringifyProperty(name), attrSpaces);
  211. };
  212. /**
  213. * returns the offset of the attribute part specified relative to the
  214. * start of the node of the output string.
  215. *
  216. * * "ns" - alias for "namespace"
  217. * * "namespace" - the namespace if it exists.
  218. * * "attribute" - the attribute name
  219. * * "attributeNS" - the start of the attribute or its namespace
  220. * * "operator" - the match operator of the attribute
  221. * * "value" - The value (string or identifier)
  222. * * "insensitive" - the case insensitivity flag;
  223. * @param part One of the possible values inside an attribute.
  224. * @returns -1 if the name is invalid or the value doesn't exist in this attribute.
  225. */
  226. Attribute.prototype.offsetOf = function offsetOf(name) {
  227. var count = 1;
  228. var attributeSpaces = this._spacesFor("attribute");
  229. count += attributeSpaces.before.length;
  230. if (name === "namespace" || name === "ns") {
  231. return this.namespace ? count : -1;
  232. }
  233. if (name === "attributeNS") {
  234. return count;
  235. }
  236. count += this.namespaceString.length;
  237. if (this.namespace) {
  238. count += 1;
  239. }
  240. if (name === "attribute") {
  241. return count;
  242. }
  243. count += this.stringifyProperty("attribute").length;
  244. count += attributeSpaces.after.length;
  245. var operatorSpaces = this._spacesFor("operator");
  246. count += operatorSpaces.before.length;
  247. var operator = this.stringifyProperty("operator");
  248. if (name === "operator") {
  249. return operator ? count : -1;
  250. }
  251. count += operator.length;
  252. count += operatorSpaces.after.length;
  253. var valueSpaces = this._spacesFor("value");
  254. count += valueSpaces.before.length;
  255. var value = this.stringifyProperty("value");
  256. if (name === "value") {
  257. return value ? count : -1;
  258. }
  259. count += value.length;
  260. count += valueSpaces.after.length;
  261. var insensitiveSpaces = this._spacesFor("insensitive");
  262. count += insensitiveSpaces.before.length;
  263. if (name === "insensitive") {
  264. return this.insensitive ? count : -1;
  265. }
  266. return -1;
  267. };
  268. Attribute.prototype.toString = function toString() {
  269. var _this2 = this;
  270. var selector = [this.rawSpaceBefore, '['];
  271. selector.push(this._stringFor('qualifiedAttribute', 'attribute'));
  272. if (this.operator && this.value) {
  273. selector.push(this._stringFor('operator'));
  274. selector.push(this._stringFor('value'));
  275. selector.push(this._stringFor('insensitiveFlag', 'insensitive', function (attrValue, attrSpaces) {
  276. if (attrValue.length > 0 && !_this2.quoted && attrSpaces.before.length === 0 && !(_this2.spaces.value && _this2.spaces.value.after)) {
  277. attrSpaces.before = " ";
  278. }
  279. return defaultAttrConcat(attrValue, attrSpaces);
  280. }));
  281. }
  282. selector.push(']');
  283. selector.push(this.rawSpaceAfter);
  284. return selector.join('');
  285. };
  286. _createClass(Attribute, [{
  287. key: "quoted",
  288. get: function get() {
  289. var qm = this.quoteMark;
  290. return qm === "'" || qm === '"';
  291. },
  292. set: function set(value) {
  293. warnOfDeprecatedQuotedAssignment();
  294. }
  295. /**
  296. * returns a single (`'`) or double (`"`) quote character if the value is quoted.
  297. * returns `null` if the value is not quoted.
  298. * returns `undefined` if the quotation state is unknown (this can happen when
  299. * the attribute is constructed without specifying a quote mark.)
  300. */
  301. }, {
  302. key: "quoteMark",
  303. get: function get() {
  304. return this._quoteMark;
  305. }
  306. /**
  307. * Set the quote mark to be used by this attribute's value.
  308. * If the quote mark changes, the raw (escaped) value at `attr.raws.value` of the attribute
  309. * value is updated accordingly.
  310. *
  311. * @param {"'" | '"' | null} quoteMark The quote mark or `null` if the value should be unquoted.
  312. */
  313. ,
  314. set: function set(quoteMark) {
  315. if (!this._constructed) {
  316. this._quoteMark = quoteMark;
  317. return;
  318. }
  319. if (this._quoteMark !== quoteMark) {
  320. this._quoteMark = quoteMark;
  321. this._syncRawValue();
  322. }
  323. }
  324. }, {
  325. key: "qualifiedAttribute",
  326. get: function get() {
  327. return this.qualifiedName(this.raws.attribute || this.attribute);
  328. }
  329. }, {
  330. key: "insensitiveFlag",
  331. get: function get() {
  332. return this.insensitive ? 'i' : '';
  333. }
  334. }, {
  335. key: "value",
  336. get: function get() {
  337. return this._value;
  338. }
  339. /**
  340. * Before 3.0, the value had to be set to an escaped value including any wrapped
  341. * quote marks. In 3.0, the semantics of `Attribute.value` changed so that the value
  342. * is unescaped during parsing and any quote marks are removed.
  343. *
  344. * Because the ambiguity of this semantic change, if you set `attr.value = newValue`,
  345. * a deprecation warning is raised when the new value contains any characters that would
  346. * require escaping (including if it contains wrapped quotes).
  347. *
  348. * Instead, you should call `attr.setValue(newValue, opts)` and pass options that describe
  349. * how the new value is quoted.
  350. */
  351. ,
  352. set: function set(v) {
  353. if (this._constructed) {
  354. var _unescapeValue2 = unescapeValue(v),
  355. deprecatedUsage = _unescapeValue2.deprecatedUsage,
  356. unescaped = _unescapeValue2.unescaped,
  357. quoteMark = _unescapeValue2.quoteMark;
  358. if (deprecatedUsage) {
  359. warnOfDeprecatedValueAssignment();
  360. }
  361. if (unescaped === this._value && quoteMark === this._quoteMark) {
  362. return;
  363. }
  364. this._value = unescaped;
  365. this._quoteMark = quoteMark;
  366. this._syncRawValue();
  367. } else {
  368. this._value = v;
  369. }
  370. }
  371. }, {
  372. key: "attribute",
  373. get: function get() {
  374. return this._attribute;
  375. },
  376. set: function set(name) {
  377. this._handleEscapes("attribute", name);
  378. this._attribute = name;
  379. }
  380. }]);
  381. return Attribute;
  382. }(_namespace2.default);
  383. Attribute.NO_QUOTE = null;
  384. Attribute.SINGLE_QUOTE = "'";
  385. Attribute.DOUBLE_QUOTE = '"';
  386. exports.default = Attribute;
  387. var CSSESC_QUOTE_OPTIONS = (_CSSESC_QUOTE_OPTIONS = {
  388. "'": { quotes: 'single', wrap: true },
  389. '"': { quotes: 'double', wrap: true }
  390. }, _CSSESC_QUOTE_OPTIONS[null] = { isIdentifier: true }, _CSSESC_QUOTE_OPTIONS);
  391. function defaultAttrConcat(attrValue, attrSpaces) {
  392. return "" + attrSpaces.before + attrValue + attrSpaces.after;
  393. }