react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / cssstyle / tests / tests.js
81141 views"use strict";1var util = require('util');2var cssstyle = require('../lib/CSSStyleDeclaration');34var camelToDashed = require('../lib/parsers').camelToDashed;56/**7* These are the required properties8* see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties9**/10var properties = [ 'azimuth', 'background', 'backgroundAttachment', 'backgroundColor', 'backgroundImage', 'backgroundPosition', 'backgroundRepeat', 'border', 'borderCollapse', 'borderColor', 'borderSpacing', 'borderStyle', 'borderTop', 'borderRight', 'borderBottom', 'borderLeft', 'borderTopColor', 'borderRightColor', 'borderBottomColor', 'borderLeftColor', 'borderTopStyle', 'borderRightStyle', 'borderBottomStyle', 'borderLeftStyle', 'borderTopWidth', 'borderRightWidth', 'borderBottomWidth', 'borderLeftWidth', 'borderWidth', 'bottom', 'captionSide', 'clear', 'clip', 'color', 'content', 'counterIncrement', 'counterReset', 'cue', 'cueAfter', 'cueBefore', 'cursor', 'direction', 'display', 'elevation', 'emptyCells', 'cssFloat', 'font', 'fontFamily', 'fontSize', 'fontSizeAdjust', 'fontStretch', 'fontStyle', 'fontVariant', 'fontWeight', 'height', 'left', 'letterSpacing', 'lineHeight', 'listStyle', 'listStyleImage', 'listStylePosition', 'listStyleType', 'margin', 'marginTop', 'marginRight', 'marginBottom', 'marginLeft', 'markerOffset', 'marks', 'maxHeight', 'maxWidth', 'minHeight', 'minWidth', 'orphans', 'outline', 'outlineColor', 'outlineStyle', 'outlineWidth', 'overflow', 'padding', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft', 'page', 'pageBreakAfter', 'pageBreakBefore', 'pageBreakInside', 'pause', 'pauseAfter', 'pauseBefore', 'pitch', 'pitchRange', 'playDuring', 'position', 'quotes', 'richness', 'right', 'size', 'speak', 'speakHeader', 'speakNumeral', 'speakPunctuation', 'speechRate', 'stress', 'tableLayout', 'textAlign', 'textDecoration', 'textIndent', 'textShadow', 'textTransform', 'top', 'unicodeBidi', 'verticalAlign', 'visibility', 'voiceFamily', 'volume', 'whiteSpace', 'widows', 'width', 'wordSpacing', 'zIndex'];11var dashed_properties = properties.map(function (property) {12return camelToDashed(property);13});1415module.exports = {16'Verify Has Properties': function (test) {17var style = new cssstyle.CSSStyleDeclaration();18test.expect(properties.length * 2);19properties.forEach(function (property) {20test.ok(style.__lookupGetter__(property), 'missing ' + property + ' property');21test.ok(style.__lookupSetter__(property), 'missing ' + property + ' property');22});23test.done();24},25'Verify Has Dashed Properties': function (test) {26var style = new cssstyle.CSSStyleDeclaration();27test.expect(dashed_properties.length * 2);28dashed_properties.forEach(function (property) {29test.ok(style.__lookupGetter__(property), 'missing ' + property + ' property');30test.ok(style.__lookupSetter__(property), 'missing ' + property + ' property');31});32test.done();33},34'Verify Has Functions': function (test) {35var style = new cssstyle.CSSStyleDeclaration();36test.expect(6);37test.ok(typeof style.getPropertyValue === 'function', 'missing getPropertyValue()');38test.ok(typeof style.getPropertyCSSValue === 'function', 'missing getPropertyCSSValue()');39test.ok(typeof style.removeProperty === 'function', 'missing removeProperty()');40test.ok(typeof style.getPropertyPriority === 'function', 'missing getPropertyPriority()');41test.ok(typeof style.setProperty === 'function', 'missing setProperty()');42test.ok(typeof style.item === 'function', 'missing item()');43test.done();44},45'Verify Has Special Properties': function (test) {46var style = new cssstyle.CSSStyleDeclaration();47test.expect(5);48test.ok(style.__lookupGetter__('cssText'), 'missing cssText getter');49test.ok(style.__lookupSetter__('cssText'), 'missing cssText setter');50test.ok(style.__lookupGetter__('length'), 'missing length getter');51test.ok(style.__lookupSetter__('length'), 'missing length setter');52test.ok(style.__lookupGetter__('parentRule'), 'missing parentRule getter');53test.done();54},55'Test From Style String': function (test) {56var style = new cssstyle.CSSStyleDeclaration();57test.expect(8);58style.cssText = 'color: blue; background-color: red; width: 78%';59test.ok(3 === style.length, 'length is not 3');60test.ok('color: blue; background-color: red; width: 78%;' === style.cssText, 'cssText is wrong');61test.ok('blue' === style.getPropertyValue('color'), "getPropertyValue('color') failed");62test.ok('color' === style.item(0), 'item(0) failed');63test.ok('background-color' === style[1], 'style[1] failed');64test.ok('red' === style.backgroundColor, 'style.backgroundColor failed with "' + style.backgroundColor + '"');65style.cssText = '';66test.ok('' === style.cssText, 'cssText is not empty');67test.ok(0 === style.length, 'length is not 0');68test.done();69},70'Test From Properties': function (test) {71var style = new cssstyle.CSSStyleDeclaration();72test.expect(11);73style.color = 'blue';74test.ok(1 === style.length, 'length is not 1');75test.ok('color' === style[0], 'style[0] is not color');76test.ok('color: blue;' === style.cssText, 'cssText is wrong');77test.ok('color' === style.item(0), 'item(0) is not color');78test.ok('blue' === style.color, 'color is not blue');79style.backgroundColor = 'red';80test.ok(2 === style.length, 'length is not 2');81test.ok('color' === style[0], 'style[0] is not color');82test.ok('background-color' === style[1], 'style[1] is not background-color');83test.ok('color: blue; background-color: red;' === style.cssText, 'cssText is wrong');84test.ok('red' === style.backgroundColor, 'backgroundColor is not red');85style.removeProperty('color');86test.ok('background-color' === style[0], 'style[0] is not background-color');87test.done();88},89'Test Shorthand Properties': function (test) {90var style = new cssstyle.CSSStyleDeclaration();91test.expect(11);92style.background = 'blue url(http://www.example.com/some_img.jpg)';93test.ok('blue' === style.backgroundColor, 'backgroundColor is not blue');94test.ok('url(http://www.example.com/some_img.jpg)' === style.backgroundImage, 'backgroundImage is wrong');95test.ok('blue url(http://www.example.com/some_img.jpg)' === style.background, 'background is different');96style.border = '0 solid black';97test.ok('0px' === style.borderWidth, 'borderWidth is not 0px');98test.ok('solid' === style.borderStyle, 'borderStyle is not solid');99test.ok('black' === style.borderColor, 'borderColor is not black');100test.ok('0px' === style.borderTopWidth, 'borderTopWidth is not 0px');101test.ok('solid' === style.borderLeftStyle, 'borderLeftStyle is not solid');102test.ok('black' === style.borderBottomColor, 'borderBottomColor is not black');103style.font = '12em monospace';104test.ok('12em' === style.fontSize, 'fontSize is not 12em');105test.ok('monospace' === style.fontFamily, 'fontFamily is not monospace');106test.done();107},108'Test width and height Properties and null and empty strings': function (test) {109var style = new cssstyle.CSSStyleDeclaration();110test.expect(7);111style.height = 6;112test.ok('' === style.height, 'height does not remain unset');113style.width = 0;114test.ok('0px' === style.width, 'width is not 0px');115style.height = '34%';116test.ok('34%' === style.height, 'height is not 34%');117style.height = '';118test.ok(style.length === 1, 'length is not 1');119test.ok('width: 0px;' === style.cssText, 'cssText is not "width: 0px;"');120style.width = null;121test.ok(style.length === 0, 'length is not 0');122test.ok('' === style.cssText, 'cssText is not empty string');123test.done();124},125'Test Implicit Properties': function (test) {126var style = new cssstyle.CSSStyleDeclaration();127test.expect(7);128style.borderWidth = 0;129test.ok(1 === style.length, 'length is not 1');130test.ok('0px' === style.borderWidth, 'borderWidth is not 0px');131test.ok('0px' === style.borderTopWidth, 'borderTopWidth is not 0px');132test.ok('0px' === style.borderBottomWidth, 'borderBottomWidth is not 0px');133test.ok('0px' === style.borderLeftWidth, 'borderLeftWidth is not 0px');134test.ok('0px' === style.borderRightWidth, 'borderRightWidth is not 0px');135test.ok('border-width: 0px;' === style.cssText, 'cssText is not "border-width: 0px", "' + style.cssText + '"');136test.done();137},138'Test Top, Left, Right, Bottom Properties': function (test) {139var style = new cssstyle.CSSStyleDeclaration();140test.expect(6);141style.top = 0;142style.left = '0%';143style.right = '5em';144style.bottom = '12pt';145test.ok('0px' === style.top, 'top is not 0px');146test.ok('0%' === style.left, 'left is not 0%');147test.ok('5em' === style.right, 'right is not 5em');148test.ok('12pt' === style.bottom, 'bottom is not 12pt');149test.ok(4 === style.length, 'length is not 4');150test.ok('top: 0px; left: 0%; right: 5em; bottom: 12pt;' === style.cssText, 'text is not "top: 0px; left: 0%; right: 5em; bottom: 12pt;"');151test.done();152},153'Test Clear and Clip Properties': function (test) {154var style = new cssstyle.CSSStyleDeclaration();155test.expect(10);156style.clear = 'none';157test.ok('none' === style.clear, 'clear is not none');158style.clear = 'lfet'; // intentionally wrong159test.ok('none' === style.clear, 'clear is not still none');160style.clear = 'left';161test.ok('left' === style.clear, 'clear is not left');162style.clear = 'right';163test.ok('right' === style.clear, 'clear is not right');164style.clear = 'both';165test.ok('both' === style.clear, 'clear is not both');166style.clip = 'elipse(5px, 10px)';167test.ok('' === style.clip, 'clip should not be set');168test.ok(1 === style.length, 'length is not 1');169style.clip = 'rect(0, 3Em, 2pt, 50%)';170test.ok('rect(0px, 3em, 2pt, 50%)' === style.clip, 'clip is not "rect(0px, 3em, 2pt, 50%)", "' + style.clip + '"');171test.ok(2 === style.length, 'length is not 2');172test.ok('clear: both; clip: rect(0px, 3em, 2pt, 50%);' === style.cssText, 'cssText is not "clear: both; clip: rect(0px, 3em, 2pt, 50%);"');173test.done();174},175'Test colors': function (test) {176var style = new cssstyle.CSSStyleDeclaration();177test.expect(4);178style.color = 'rgba(0,0,0,0)';179test.ok('rgba(0, 0, 0, 0)' === style.color, 'color is not rgba(0, 0, 0, 0)');180style.color = 'rgba(5%, 10%, 20%, 0.4)';181test.ok('rgba(12, 25, 51, 0.4)' === style.color, 'color is not rgba(12, 25, 51, 0.4)');182style.color = 'rgb(33%, 34%, 33%)';183test.ok('rgb(84, 86, 84)' === style.color, 'color is not rgb(84, 86, 84)');184style.color = 'rgba(300, 200, 100, 1.5)';185test.ok('rgb(255, 200, 100)' === style.color, 'color is not rgb(255, 200, 100) ' + style.color);186test.done();187},188'Test short hand properties with embedded spaces': function (test) {189var style = new cssstyle.CSSStyleDeclaration();190test.expect(3);191style.background = 'rgb(0, 0, 0) url(/something/somewhere.jpg)';192test.ok('rgb(0, 0, 0)' === style.backgroundColor, 'backgroundColor is not rgb(0, 0, 0): ' + style.backgroundColor);193test.ok('url(/something/somewhere.jpg)' === style.backgroundImage, 'backgroundImage is not url(/something/somewhere.jpg): ' + style.backgroundImage);194test.ok('background: rgb(0, 0, 0) url(/something/somewhere.jpg);' === style.cssText, 'cssText is not correct: ' + style.cssText);195test.done();196},197'Setting shorthand properties to an empty string should clear all dependent properties': function (test) {198var style = new cssstyle.CSSStyleDeclaration();199test.expect(2);200style.borderWidth = '1px';201test.ok('border-width: 1px;' === style.cssText, 'cssText is not "border-width: 1px;": ' + style.cssText);202style.border = '';203test.ok('' === style.cssText, 'cssText is not "": ' + style.cssText);204test.done();205},206'Setting implicit properties to an empty string should clear all dependent properties': function (test) {207var style = new cssstyle.CSSStyleDeclaration();208test.expect(2);209style.borderTopWidth = '1px';210test.ok('border-top-width: 1px;' === style.cssText, 'cssText is not "border-top-width: 1px;": ' + style.cssText);211style.borderWidth = '';212test.ok('' === style.cssText, 'cssText is not "": ' + style.cssText);213test.done();214},215'Setting a shorthand property, whose shorthands are implicit properties, to an empty string should clear all dependent properties': function (test) {216var style = new cssstyle.CSSStyleDeclaration();217test.expect(4);218style.borderTopWidth = '1px';219test.ok('border-top-width: 1px;' === style.cssText, 'cssText is not "border-top-width: 1px;": ' + style.cssText);220style.border = '';221test.ok('' === style.cssText, 'cssText is not "": ' + style.cssText);222style.borderTop = '1px solid black';223test.ok('border-top: 1px solid black;' === style.cssText, 'cssText is not "border-top: 1px solid black;": ' + style.cssText);224style.border = '';225test.ok('' === style.cssText, 'cssText is not "": ' + style.cssText);226test.done();227},228'Setting border values to "none" should clear dependent values': function (test) {229var style = new cssstyle.CSSStyleDeclaration();230test.expect(8);231style.borderTopWidth = '1px';232test.ok('border-top-width: 1px;' === style.cssText, 'cssText is not "border-top-width: 1px;": ' + style.cssText);233style.border = 'none';234test.ok('' === style.cssText, 'cssText is not "": ' + style.cssText);235style.borderTopWidth = '1px';236test.ok('border-top-width: 1px;' === style.cssText, 'cssText is not "border-top-width: 1px;": ' + style.cssText);237style.borderTopStyle = 'none';238test.ok('' === style.cssText, 'cssText is not "": ' + style.cssText);239style.borderTopWidth = '1px';240test.ok('border-top-width: 1px;' === style.cssText, 'cssText is not "border-top-width: 1px;": ' + style.cssText);241style.borderTop = 'none';242test.ok('' === style.cssText, 'cssText is not "": ' + style.cssText);243style.borderTopWidth = '1px';244style.borderLeftWidth = '1px';245test.ok('border-top-width: 1px; border-left-width: 1px;' === style.cssText, 'cssText is not "border-top-width: 1px; border-left-width: 1px;": ' + style.cssText);246style.borderTop = 'none';247test.ok('border-left-width: 1px;' === style.cssText, 'cssText is not "border-left-width: 1px;": ' + style.cssText);248test.done();249},250'Setting border to 0 should be okay': function (test) {251var style = new cssstyle.CSSStyleDeclaration();252test.expect(1);253style.border = 0;254test.ok('border: 0px;' === style.cssText, 'cssText is not "border: 0px;": ' + style.cssText);255test.done();256},257'Setting values implicit and shorthand properties via cssText and setProperty should propagate to dependent properties': function (test) {258var style = new cssstyle.CSSStyleDeclaration();259test.expect(4);260style.cssText = 'border: 1px solid black;';261test.ok('border: 1px solid black;' === style.cssText, 'cssText is not "border: 1px solid black;": ' + style.cssText);262test.ok('1px solid black' === style.borderTop, 'borderTop is not "1px solid black": ' + style.borderTop);263style.border = '';264test.ok('' === style.cssText, 'cssText is not "": ' + style.cssText);265style.setProperty('border', '1px solid black');266test.ok('border: 1px solid black;' === style.cssText, 'cssText is not "border: 1px solid black;": ' + style.cssText);267test.done();268},269'Setting opacity should work': function (test) {270var style = new cssstyle.CSSStyleDeclaration();271test.expect(3);272style.setProperty('opacity', 0.75);273test.ok('opacity: 0.75;' === style.cssText, 'cssText is not "opacity: 0.75;": ' + style.cssText);274style.opacity = '0.50';275test.ok('opacity: 0.5;' === style.cssText, 'cssText is not "opacity: 0.5;": ' + style.cssText);276style.opacity = 1.0;277test.ok('opacity: 1;' === style.cssText, 'cssText is not "opacity: 1;": ' + style.cssText);278test.done();279},280'Setting a value to 0 should return the string value': function (test) {281var style = new cssstyle.CSSStyleDeclaration();282test.expect(1);283style.setProperty('fill-opacity', 0);284test.ok('0' === style.fillOpacity, 'fillOpacity is not "0": ' + style.fillOpacity);285test.done();286},287'onChange callback should be called when the cssText changes': function (test) {288var style = new cssstyle.CSSStyleDeclaration(function (cssText) {289test.ok('opacity: 0;' === cssText, 'cssText is not "opacity: 0;": ' + cssText);290test.done();291});292test.expect(1);293style.setProperty('opacity', 0);294},295'Setting float should work the same as cssFloat': function (test) {296var style = new cssstyle.CSSStyleDeclaration();297test.expect(1);298style.float = 'left';299test.ok('left' === style.cssFloat, 'cssFloat is not "left": ' + style.cssFloat);300test.done();301},302'Setting improper css to cssText should not throw': function (test) {303var style = new cssstyle.CSSStyleDeclaration();304test.expect(2);305style.cssText = 'color: ';306test.ok('' === style.cssText, 'cssText wasn\'t cleared: ' + style.cssText);307style.color = 'black';308style.cssText = 'float: ';309test.ok('' === style.cssText, 'cssText wasn\'t cleared: ' + style.cssText);310test.done();311}312};313314315