aboutsummaryrefslogtreecommitdiff
path: root/node_modules/cheerio/lib/api/css.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2022-02-09 17:58:07 +0100
committerMinteck <contact@minteck.org>2022-02-09 17:58:07 +0100
commit22a25ded9f7d9c9a96cce8d1bc12475ca0434201 (patch)
tree0e33d0650fe58f41c00bbc4b8047956905766823 /node_modules/cheerio/lib/api/css.js
parent8f54d903fb3470823a5e4d6ff4655de009836245 (diff)
downloadyoutoo-22a25ded9f7d9c9a96cce8d1bc12475ca0434201.tar.gz
youtoo-22a25ded9f7d9c9a96cce8d1bc12475ca0434201.tar.bz2
youtoo-22a25ded9f7d9c9a96cce8d1bc12475ca0434201.zip
Major update
Diffstat (limited to 'node_modules/cheerio/lib/api/css.js')
-rw-r--r--node_modules/cheerio/lib/api/css.js95
1 files changed, 95 insertions, 0 deletions
diff --git a/node_modules/cheerio/lib/api/css.js b/node_modules/cheerio/lib/api/css.js
new file mode 100644
index 0000000..aa7ad09
--- /dev/null
+++ b/node_modules/cheerio/lib/api/css.js
@@ -0,0 +1,95 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.css = void 0;
+var utils_1 = require("../utils");
+function css(prop, val) {
+ if ((prop != null && val != null) ||
+ // When `prop` is a "plain" object
+ (typeof prop === 'object' && !Array.isArray(prop))) {
+ return utils_1.domEach(this, function (el, i) {
+ if (utils_1.isTag(el)) {
+ // `prop` can't be an array here anymore.
+ setCss(el, prop, val, i);
+ }
+ });
+ }
+ return getCss(this[0], prop);
+}
+exports.css = css;
+/**
+ * Set styles of all elements.
+ *
+ * @private
+ * @param el - Element to set style of.
+ * @param prop - Name of property.
+ * @param value - Value to set property to.
+ * @param idx - Optional index within the selection.
+ */
+function setCss(el, prop, value, idx) {
+ if (typeof prop === 'string') {
+ var styles = getCss(el);
+ var val = typeof value === 'function' ? value.call(el, idx, styles[prop]) : value;
+ if (val === '') {
+ delete styles[prop];
+ }
+ else if (val != null) {
+ styles[prop] = val;
+ }
+ el.attribs.style = stringify(styles);
+ }
+ else if (typeof prop === 'object') {
+ Object.keys(prop).forEach(function (k, i) {
+ setCss(el, k, prop[k], i);
+ });
+ }
+}
+function getCss(el, prop) {
+ if (!el || !utils_1.isTag(el))
+ return;
+ var styles = parse(el.attribs.style);
+ if (typeof prop === 'string') {
+ return styles[prop];
+ }
+ if (Array.isArray(prop)) {
+ var newStyles_1 = {};
+ prop.forEach(function (item) {
+ if (styles[item] != null) {
+ newStyles_1[item] = styles[item];
+ }
+ });
+ return newStyles_1;
+ }
+ return styles;
+}
+/**
+ * Stringify `obj` to styles.
+ *
+ * @private
+ * @category CSS
+ * @param obj - Object to stringify.
+ * @returns The serialized styles.
+ */
+function stringify(obj) {
+ return Object.keys(obj).reduce(function (str, prop) { return "" + str + (str ? ' ' : '') + prop + ": " + obj[prop] + ";"; }, '');
+}
+/**
+ * Parse `styles`.
+ *
+ * @private
+ * @category CSS
+ * @param styles - Styles to be parsed.
+ * @returns The parsed styles.
+ */
+function parse(styles) {
+ styles = (styles || '').trim();
+ if (!styles)
+ return {};
+ return styles.split(';').reduce(function (obj, str) {
+ var n = str.indexOf(':');
+ // Skip if there is no :, or if it is the first/last character
+ if (n < 1 || n === str.length - 1)
+ return obj;
+ obj[str.slice(0, n).trim()] = str.slice(n + 1).trim();
+ return obj;
+ }, {});
+}