Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Unified Diff: tools/vulcanize/node_modules/vulcanize/node_modules/cheerio/lib/api/css.js

Issue 125733002: Add vulcanize to tools. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tools/vulcanize/node_modules/vulcanize/node_modules/cheerio/lib/api/css.js
diff --git a/tools/vulcanize/node_modules/vulcanize/node_modules/cheerio/lib/api/css.js b/tools/vulcanize/node_modules/vulcanize/node_modules/cheerio/lib/api/css.js
new file mode 100644
index 0000000000000000000000000000000000000000..a230dfe0cfdea8f9e82f3788b8d06a3aa3b188d3
--- /dev/null
+++ b/tools/vulcanize/node_modules/vulcanize/node_modules/cheerio/lib/api/css.js
@@ -0,0 +1,117 @@
+var _ = require('underscore');
+var toString = Object.prototype.toString;
+
+/**
+ * Set / Get css.
+ *
+ * @param {String|Object} prop
+ * @param {String} val
+ * @return {self}
+ * @api public
+ */
+
+exports.css = function(prop, val) {
+ if (arguments.length === 2 ||
+ // When `prop` is a "plain" object
+ (toString.call(prop) === '[object Object]')) {
+ return this.each(function(idx) {
+ this._setCss(prop, val, idx);
+ });
+ } else {
+ return this._getCss(prop);
+ }
+};
+
+/**
+ * Set styles of all elements.
+ *
+ * @param {String|Object} prop
+ * @param {String} val
+ * @param {Number} idx - optional index within the selection
+ * @return {self}
+ * @api private
+ */
+
+exports._setCss = function(prop, val, idx) {
+ if ('string' == typeof prop) {
+ var styles = this._getCss();
+ if (_.isFunction(val)) {
+ val = val.call(this[0], idx, this[0]);
+ }
+
+ if (val === '') {
+ delete styles[prop];
+ } else if (val != null) {
+ styles[prop] = val;
+ }
+
+ return this.attr('style', stringify(styles));
+ } else if ('object' == typeof prop) {
+ Object.keys(prop).forEach(function(k){
+ this._setCss(k, prop[k]);
+ }, this);
+ return this;
+ }
+};
+
+/**
+ * Get parsed styles of the first element.
+ *
+ * @param {String} prop
+ * @return {Object}
+ * @api private
+ */
+
+exports._getCss = function(prop) {
+ var styles = parse(this.attr('style'));
+ if (typeof prop === 'string') {
+ return styles[prop];
+ } else if (_.isArray(prop)) {
+ return _.pick(styles, prop);
+ } else {
+ return styles;
+ }
+};
+
+/**
+ * Stringify `obj` to styles.
+ *
+ * @param {Object} obj
+ * @return {Object}
+ * @api private
+ */
+
+function stringify(obj) {
+ return Object.keys(obj || {})
+ .reduce(function(str, prop){
+ return str += ''
+ + (str ? ' ' : '')
+ + prop
+ + ': '
+ + obj[prop]
+ + ';';
+ }, '');
+}
+
+/**
+ * Parse `styles`.
+ *
+ * @param {String} styles
+ * @return {Object}
+ * @api private
+ */
+
+function parse(styles) {
+ styles = (styles || '').trim();
+
+ if (!styles) return {};
+
+ return styles
+ .split(/\s*;\s*/)
+ .reduce(function(obj, str){
+ var parts = str.split(/\s*:\s*/);
+ if (!parts[0]) return obj;
+ obj[parts[0]] = parts[1];
+ return obj;
+ }, {});
+}

Powered by Google App Engine
This is Rietveld 408576698