1+ //
2+ // Patch for CSSStyleDeclaration padding property so that it sets/clears
3+ // the Top, Right, Bottom, and Left properties (and also validates the
4+ // padding value)
5+ //
6+ var PADDING = ( function ( ) {
7+ var parsers = require ( 'jsdom/node_modules/cssstyle/lib/parsers.js' ) ;
8+ var TYPES = parsers . TYPES ;
9+
10+ var isValid = function ( v ) {
11+ var type = parsers . valueType ( v ) ;
12+ return type === TYPES . LENGTH || type === TYPES . PERCENT ;
13+ } ;
14+
15+ var parser = function ( v ) {
16+ return parsers . parseMeasurement ( v ) ;
17+ }
18+
19+ var mySetter = parsers . implicitSetter ( 'padding' , '' , isValid , parser ) ;
20+ var myGlobal = parsers . implicitSetter ( 'padding' , '' , function ( ) { return true } , function ( v ) { return v } ) ;
21+
22+ return {
23+ set : function ( v ) {
24+ if ( typeof v === "number" ) v = String ( v ) ;
25+ if ( typeof v !== "string" ) return ;
26+ var V = v . toLowerCase ( ) ;
27+ switch ( V ) {
28+ case 'inherit' :
29+ case 'initial' :
30+ case 'unset' :
31+ case '' :
32+ myGlobal . call ( this , V ) ;
33+ break ;
34+
35+ default :
36+ mySetter . call ( this , v ) ;
37+ break ;
38+ }
39+ } ,
40+ get : function ( ) {
41+ return this . getPropertyValue ( 'padding' ) ;
42+ } ,
43+ enumerable : true ,
44+ configurable : true
45+ } ;
46+ } ) ( ) ;
47+
48+ //
49+ // Patch for CSSStyleDeclaration margin property so that it sets/clears
50+ // the Top, Right, Bottom, and Left properties (and also validates the
51+ // margin value)
52+ //
53+ var MARGIN = ( function ( ) {
54+ var parsers = require ( 'jsdom/node_modules/cssstyle/lib/parsers.js' ) ;
55+ var TYPES = parsers . TYPES ;
56+
57+ var isValid = function ( v ) {
58+ if ( v . toLowerCase ( ) === "auto" ) return true ;
59+ var type = parsers . valueType ( v ) ;
60+ return type === TYPES . LENGTH || type === TYPES . PERCENT ;
61+ } ;
62+
63+ var parser = function ( v ) {
64+ var V = v . toLowerCase ( ) ;
65+ if ( V === "auto" ) return V ;
66+ return parsers . parseMeasurement ( v ) ;
67+ }
68+
69+ var mySetter = parsers . implicitSetter ( 'margin' , '' , isValid , parser ) ;
70+ var myGlobal = parsers . implicitSetter ( 'margin' , '' , function ( ) { return true } , function ( v ) { return v } ) ;
71+
72+ return {
73+ set : function ( v ) {
74+ if ( typeof v === "number" ) v = String ( v ) ;
75+ if ( typeof v !== "string" ) return ;
76+ var V = v . toLowerCase ( ) ;
77+ switch ( V ) {
78+ case 'inherit' :
79+ case 'initial' :
80+ case 'unset' :
81+ case '' :
82+ myGlobal . call ( this , V ) ;
83+ break ;
84+
85+ default :
86+ mySetter . call ( this , v ) ;
87+ break ;
88+ }
89+ } ,
90+ get : function ( ) {
91+ return this . getPropertyValue ( 'margin' ) ;
92+ } ,
93+ enumerable : true ,
94+ configurable : true
95+ } ;
96+ } ) ( ) ;
97+
98+
99+ //
100+ // Patch jsdom functions
101+ //
102+ exports . patch = function ( jsdom ) {
103+ var document = jsdom ( '' ) ;
104+ var window = document . defaultView ;
105+ //
106+ // Fix setting of style attributes so shorthands work properly.
107+ //
108+ var div = document . createElement ( "div" ) ;
109+ div . style . border = "1px solid black" ;
110+ if ( div . style . border !== "1px solid black" ) {
111+ var INIT = window . HTMLElement . _init ;
112+ window . HTMLElement . _init = function ( ) {
113+ INIT . apply ( this , arguments ) ;
114+ var that = this ;
115+ this . style . _onChange = function ( csstext ) {
116+ if ( ! that . _settingCssText ) {
117+ that . _settingCssText = true ;
118+ that . setAttribute ( 'style' , csstext ) ;
119+ that . _settingCssText = false ;
120+ }
121+ } ;
122+ }
123+ }
124+ //
125+ // Add missing nodeName to Attr (after jsdom 7.1.0, it is no longer defined)
126+ // since this is used in mml2jax.
127+ //
128+ if ( ! ( "nodeName" in window . Attr . prototype ) ) {
129+ Object . defineProperties ( window . Attr . prototype , {
130+ nodeName : { get : function ( ) { return this . name } }
131+ } ) ;
132+ }
133+ //
134+ // Fix CSSStyleDeclaration properties that are broken (padding, margin)
135+ //
136+ div . style . paddingTop = "10px" ;
137+ div . style . padding = "1px" ;
138+ if ( div . style . paddingTop !== "1px" ) {
139+ var core = require ( "jsdom/lib/jsdom/level1/core" ) ;
140+ Object . defineProperties ( core . CSSStyleDeclaration . prototype , {
141+ padding : PADDING ,
142+ margin : MARGIN
143+ } ) ;
144+ }
145+ }
0 commit comments