| 1 |
"use strict"; |
| 2 |
|
| 3 |
const { parse } = require('./style_parser'); |
| 4 |
|
| 5 |
module.exports = function (elt) { |
| 6 |
const style = new CSSStyleDeclaration(elt) |
| 7 |
const handler = { |
| 8 |
get: function(target, property) { |
| 9 |
return property in target ? target[property] : target.getPropertyValue(dasherizeProperty(property)); |
| 10 |
}, |
| 11 |
has: function(target, key) { |
| 12 |
return true; |
| 13 |
}, |
| 14 |
set: function(target, property, value) { |
| 15 |
if (property in target) { |
| 16 |
target[property] = value; |
| 17 |
} else { |
| 18 |
target.setProperty(dasherizeProperty(property), value ?? undefined); |
| 19 |
} |
| 20 |
|
| 21 |
return true; |
| 22 |
} |
| 23 |
}; |
| 24 |
|
| 25 |
return new Proxy(style, handler); |
| 26 |
}; |
| 27 |
|
| 28 |
function dasherizeProperty(property) { |
| 29 |
return property.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
function CSSStyleDeclaration(elt) { |
| 34 |
this._element = elt; |
| 35 |
} |
| 36 |
|
| 37 |
const IMPORTANT_BANG = '!important'; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
function parseStyles(value) { |
| 44 |
const result = { |
| 45 |
property: {}, |
| 46 |
priority: {}, |
| 47 |
} |
| 48 |
|
| 49 |
if (!value) { |
| 50 |
return result; |
| 51 |
} |
| 52 |
|
| 53 |
const styleValues = parse(value); |
| 54 |
if (styleValues.length < 2) { |
| 55 |
return result; |
| 56 |
} |
| 57 |
|
| 58 |
for (let i = 0; i < styleValues.length; i += 2) { |
| 59 |
const name = styleValues[i]; |
| 60 |
let value = styleValues[i+1]; |
| 61 |
|
| 62 |
if (value.endsWith(IMPORTANT_BANG)) { |
| 63 |
result.priority[name] = 'important'; |
| 64 |
value = value.slice(0, -IMPORTANT_BANG.length).trim(); |
| 65 |
} |
| 66 |
|
| 67 |
result.property[name] = value; |
| 68 |
} |
| 69 |
|
| 70 |
return result; |
| 71 |
} |
| 72 |
|
| 73 |
var NO_CHANGE = {}; |
| 74 |
|
| 75 |
CSSStyleDeclaration.prototype = Object.create(Object.prototype, { |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
_parsed: { get: function() { |
| 82 |
if (!this._parsedStyles || this.cssText !== this._lastParsedText) { |
| 83 |
var text = this.cssText; |
| 84 |
this._parsedStyles = parseStyles(text); |
| 85 |
this._lastParsedText = text; |
| 86 |
delete this._names; |
| 87 |
} |
| 88 |
return this._parsedStyles; |
| 89 |
}}, |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
_serialize: { value: function() { |
| 95 |
var styles = this._parsed; |
| 96 |
var s = ""; |
| 97 |
|
| 98 |
for(var name in styles.property) { |
| 99 |
if (s) s += " "; |
| 100 |
s += name + ": " + styles.property[name]; |
| 101 |
if (styles.priority[name]) { |
| 102 |
s += " !" + styles.priority[name]; |
| 103 |
} |
| 104 |
s += ";"; |
| 105 |
} |
| 106 |
|
| 107 |
this.cssText = s; |
| 108 |
this._lastParsedText = s; |
| 109 |
delete this._names; |
| 110 |
}}, |
| 111 |
|
| 112 |
cssText: { |
| 113 |
get: function() { |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
return this._element.getAttribute("style"); |
| 118 |
}, |
| 119 |
set: function(value) { |
| 120 |
|
| 121 |
|
| 122 |
this._element.setAttribute("style", value); |
| 123 |
} |
| 124 |
}, |
| 125 |
|
| 126 |
length: { get: function() { |
| 127 |
if (!this._names) |
| 128 |
this._names = Object.getOwnPropertyNames(this._parsed.property); |
| 129 |
return this._names.length; |
| 130 |
}}, |
| 131 |
|
| 132 |
item: { value: function(n) { |
| 133 |
if (!this._names) |
| 134 |
this._names = Object.getOwnPropertyNames(this._parsed.property); |
| 135 |
return this._names[n]; |
| 136 |
}}, |
| 137 |
|
| 138 |
getPropertyValue: { value: function(property) { |
| 139 |
property = property.toLowerCase(); |
| 140 |
return this._parsed.property[property] || ""; |
| 141 |
}}, |
| 142 |
|
| 143 |
getPropertyPriority: { value: function(property) { |
| 144 |
property = property.toLowerCase(); |
| 145 |
return this._parsed.priority[property] || ""; |
| 146 |
}}, |
| 147 |
|
| 148 |
setProperty: { value: function(property, value, priority) { |
| 149 |
property = property.toLowerCase(); |
| 150 |
if (value === null || value === undefined) { |
| 151 |
value = ""; |
| 152 |
} |
| 153 |
if (priority === null || priority === undefined) { |
| 154 |
priority = ""; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
if (value !== NO_CHANGE) { |
| 159 |
value = "" + value; |
| 160 |
} |
| 161 |
|
| 162 |
value = value.trim(); |
| 163 |
if (value === "") { |
| 164 |
this.removeProperty(property); |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
if (priority !== "" && priority !== NO_CHANGE && |
| 169 |
!/^important$/i.test(priority)) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
var styles = this._parsed; |
| 174 |
if (value === NO_CHANGE) { |
| 175 |
if (!styles.property[property]) { |
| 176 |
return; |
| 177 |
} |
| 178 |
if (priority !== "") { |
| 179 |
styles.priority[property] = "important"; |
| 180 |
} else { |
| 181 |
delete styles.priority[property]; |
| 182 |
} |
| 183 |
} else { |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if (value.indexOf(";") !== -1) return; |
| 188 |
|
| 189 |
var newprops = parseStyles(property + ":" + value); |
| 190 |
if (Object.getOwnPropertyNames(newprops.property).length === 0) { |
| 191 |
return; |
| 192 |
} |
| 193 |
if (Object.getOwnPropertyNames(newprops.priority).length !== 0) { |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
for (var p in newprops.property) { |
| 200 |
styles.property[p] = newprops.property[p]; |
| 201 |
if (priority === NO_CHANGE) { |
| 202 |
continue; |
| 203 |
} else if (priority !== "") { |
| 204 |
styles.priority[p] = "important"; |
| 205 |
} else if (styles.priority[p]) { |
| 206 |
delete styles.priority[p]; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
this._serialize(); |
| 213 |
}}, |
| 214 |
|
| 215 |
setPropertyValue: { value: function(property, value) { |
| 216 |
return this.setProperty(property, value, NO_CHANGE); |
| 217 |
}}, |
| 218 |
|
| 219 |
setPropertyPriority: { value: function(property, priority) { |
| 220 |
return this.setProperty(property, NO_CHANGE, priority); |
| 221 |
}}, |
| 222 |
|
| 223 |
removeProperty: { value: function(property) { |
| 224 |
property = property.toLowerCase(); |
| 225 |
var styles = this._parsed; |
| 226 |
if (property in styles.property) { |
| 227 |
delete styles.property[property]; |
| 228 |
delete styles.priority[property]; |
| 229 |
|
| 230 |
|
| 231 |
this._serialize(); |
| 232 |
} |
| 233 |
}}, |
| 234 |
}); |