attribute.js 16 KB

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