react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / cssstyle / lib / CSSStyleDeclaration.js
81141 views/*********************************************************************1* This is a fork from the CSS Style Declaration part of2* https://github.com/NV/CSSOM3********************************************************************/4"use strict";5var CSSOM = require('cssom');6var fs = require('fs');7var path = require('path');89var camelToDashed = require('./parsers').camelToDashed;10var dashedToCamelCase = require('./parsers').dashedToCamelCase;1112/**13* @constructor14* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration15*/16var CSSStyleDeclaration = function CSSStyleDeclaration(onChangeCallback) {17this._values = {};18this._importants = {};19this._length = 0;20this._onChange = onChangeCallback || function () { return; };21};22CSSStyleDeclaration.prototype = {23constructor: CSSStyleDeclaration,2425/**26*27* @param {string} name28* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-getPropertyValue29* @return {string} the value of the property if it has been explicitly set for this declaration block.30* Returns the empty string if the property has not been set.31*/32getPropertyValue: function (name) {33if (!this._values.hasOwnProperty(name)) {34return "";35}36return this._values[name].toString();37},3839/**40*41* @param {string} name42* @param {string} value43* @param {string} [priority=null] "important" or null44* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-setProperty45*/46setProperty: function (name, value, priority) {47if (value === undefined) {48return;49}50if (value === null || value === '') {51this.removeProperty(name);52return;53}54var camel_case = dashedToCamelCase(name);55this[camel_case] = value;56this._importants[name] = priority;57},58_setProperty: function (name, value, priority) {59if (value === undefined) {60return;61}62if (value === null || value === '') {63this.removeProperty(name);64return;65}66if (this._values[name]) {67// Property already exist. Overwrite it.68var index = Array.prototype.indexOf.call(this, name);69if (index < 0) {70this[this._length] = name;71this._length++;72}73} else {74// New property.75this[this._length] = name;76this._length++;77}78this._values[name] = value;79this._importants[name] = priority;80this._onChange(this.cssText);81},8283/**84*85* @param {string} name86* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-removeProperty87* @return {string} the value of the property if it has been explicitly set for this declaration block.88* Returns the empty string if the property has not been set or the property name does not correspond to a known CSS property.89*/90removeProperty: function (name) {91if (!this._values.hasOwnProperty(name)) {92return "";93}9495var prevValue = this._values[name];96delete this._values[name];9798var index = Array.prototype.indexOf.call(this, name);99if (index < 0) {100return prevValue;101}102103// That's what WebKit and Opera do104Array.prototype.splice.call(this, index, 1);105106// That's what Firefox does107//this[index] = ""108109this._onChange(this.cssText);110return prevValue;111},112113114/**115*116* @param {String} name117*/118getPropertyPriority: function (name) {119return this._importants[name] || "";120},121122123getPropertyCSSValue: function () {124//FIXME125return;126},127128/**129* element.style.overflow = "auto"130* element.style.getPropertyShorthand("overflow-x")131* -> "overflow"132*/133getPropertyShorthand: function () {134//FIXME135return;136},137138isPropertyImplicit: function () {139//FIXME140return;141},142143/**144* http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-item145*/146item: function (index) {147index = parseInt(index, 10);148if (index < 0 || index >= this._length) {149return '';150}151return this[index];152}153};154155Object.defineProperties(CSSStyleDeclaration.prototype, {156cssText: {157get: function () {158var properties = [];159var i;160var name;161var value;162var priority;163for (i = 0; i < this._length; i++) {164name = this[i];165value = this.getPropertyValue(name);166priority = this.getPropertyPriority(name);167if (priority !== '') {168priority = " !" + priority;169}170properties.push([name, ': ', value, priority, ';'].join(''));171}172return properties.join(' ');173},174set: function (value) {175var i;176this._values = {};177Array.prototype.splice.call(this, 0, this._length);178this._importants = {};179var dummyRule;180try {181dummyRule = CSSOM.parse('#bogus{' + value + '}').cssRules[0].style;182} catch (err) {183// malformed css, just return184return;185}186var rule_length = dummyRule.length;187var name;188for (i = 0; i < rule_length; ++i) {189name = dummyRule[i];190this.setProperty(dummyRule[i], dummyRule.getPropertyValue(name), dummyRule.getPropertyPriority(name));191}192this._onChange(this.cssText);193},194enumerable: true,195configurable: true196},197parentRule: {198get: function () { return null; },199enumerable: true,200configurable: true201},202length: {203get: function () { return this._length; },204/**205* This deletes indices if the new length is less then the current206* length. If the new length is more, it does nothing, the new indices207* will be undefined until set.208**/209set: function (value) {210var i;211for (i = value; i < this._length; i++) {212delete this[i];213}214this._length = value;215},216enumerable: true,217configurable: true218},219'float': {220get: function () { return this.cssFloat; },221set: function (value) {222this.cssFloat = value;223},224enumerable: true,225configurable: true226}227});228229require('./properties')(CSSStyleDeclaration.prototype);230231exports.CSSStyleDeclaration = CSSStyleDeclaration;232233234