attribute.js 15 KB

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