-
-
Notifications
You must be signed in to change notification settings - Fork 75
Migrate CSSStyleDeclaration
to WebIDL2JS
#116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ExE-Boss
wants to merge
6
commits into
jsdom:main
Choose a base branch
from
ExE-Boss:feat/migrate-to-webidl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ad0f846
Migrate `CSSStyleDeclaration` to WebIDL2JS
ExE-Boss c6b72e0
Use `[ReflectStyle]` for basic properties
ExE-Boss d077e92
Merge branch 'main' into feat/migrate-to-webidl
ExE-Boss badf2c9
fixup! Merge branch 'main' into feat/migrate-to-webidl
ExE-Boss fafcf74
Update WebIDL2JS
ExE-Boss a8c1763
Fix `.npmignore`
ExE-Boss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
node_modules | ||
lib/CSSStyleDeclaration.js | ||
lib/Function.js | ||
lib/VoidFunction.js | ||
lib/implementedProperties.js | ||
lib/properties.js | ||
lib/utils.js | ||
jest.config.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
node_modules | ||
npm-debug.log | ||
lib/CSSStyleDeclaration.js | ||
lib/Function.js | ||
lib/VoidFunction.js | ||
lib/implementedProperties.js | ||
lib/properties.js | ||
lib/utils.js | ||
coverage | ||
src/CSSStyleDeclaration-properties.webidl |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
/* | ||
!index.js | ||
!webidl2js-wrapper.js | ||
!lib/ | ||
lib/Function.js | ||
lib/VoidFunction.js | ||
!LICENSE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
const webidlWrapper = require('./webidl2js-wrapper.js'); | ||
|
||
const sharedGlobalObject = { Object, String, Number, Array, TypeError }; | ||
webidlWrapper.install(sharedGlobalObject, ['Window']); | ||
|
||
const origCSSStyleDeclaration = sharedGlobalObject.CSSStyleDeclaration; | ||
|
||
/** | ||
* @constructor | ||
* @param {((cssText: string) => void) | null} [onChangeCallback] | ||
* The callback that is invoked whenever a property changes. | ||
*/ | ||
function CSSStyleDeclaration(onChangeCallback = null) { | ||
if (new.target === undefined) { | ||
throw new TypeError("Class constructor CSSStyleDeclaration cannot be invoked without 'new'"); | ||
} | ||
|
||
if (onChangeCallback !== null && typeof onChangeCallback !== 'function') { | ||
throw new TypeError('Failed to construct CSSStyleDeclaration: parameter 1 is not a function'); | ||
} | ||
|
||
return webidlWrapper.create(sharedGlobalObject, undefined, { onChangeCallback }); | ||
} | ||
|
||
sharedGlobalObject.CSSStyleDeclaration = CSSStyleDeclaration; | ||
Object.defineProperty(CSSStyleDeclaration, 'prototype', { | ||
value: origCSSStyleDeclaration.prototype, | ||
writable: false, | ||
}); | ||
CSSStyleDeclaration.prototype.constructor = CSSStyleDeclaration; | ||
Object.setPrototypeOf(CSSStyleDeclaration, Object.getPrototypeOf(origCSSStyleDeclaration)); | ||
|
||
module.exports = CSSStyleDeclaration; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
/********************************************************************* | ||
* This is a fork from the CSS Style Declaration part of | ||
* https://github.com/NV/CSSOM | ||
********************************************************************/ | ||
'use strict'; | ||
const CSSOM = require('rrweb-cssom'); | ||
const allProperties = require('./allProperties'); | ||
const allExtraProperties = require('./allExtraProperties'); | ||
const implementedProperties = require('./implementedProperties'); | ||
const idlUtils = require('./utils.js'); | ||
|
||
class CSSStyleDeclarationImpl { | ||
/** | ||
* @constructor | ||
* @see https://drafts.csswg.org/cssom/#cssstyledeclaration | ||
* | ||
* @param {object} globalObject | ||
* @param {*[]} args | ||
* @param {object} privateData | ||
* @param {((cssText: string) => void) | null} [privateData.onChangeCallback] | ||
*/ | ||
constructor(globalObject, args, { onChangeCallback }) { | ||
this._globalObject = globalObject; | ||
this._values = Object.create(null); | ||
this._importants = Object.create(null); | ||
this._list = []; | ||
this._onChange = onChangeCallback; | ||
this._setInProgress = false; | ||
this.parentRule = null; | ||
} | ||
|
||
/** | ||
* @see https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext | ||
*/ | ||
get cssText() { | ||
const { _list } = this; | ||
const properties = []; | ||
for (let i = 0; i < _list.length; i++) { | ||
const name = _list[i]; | ||
const value = this.getPropertyValue(name); | ||
let priority = this.getPropertyPriority(name); | ||
if (priority !== '') { | ||
priority = ` !${priority}`; | ||
} | ||
properties.push(`${name}: ${value}${priority};`); | ||
} | ||
return properties.join(' '); | ||
} | ||
|
||
set cssText(value) { | ||
this._values = Object.create(null); | ||
this._importants = Object.create(null); | ||
this._list = []; | ||
let dummyRule; | ||
try { | ||
dummyRule = CSSOM.parse('#bogus{' + value + '}').cssRules[0].style; | ||
} catch (err) { | ||
// malformed css, just return | ||
return; | ||
} | ||
this._setInProgress = true; | ||
const rule_length = dummyRule.length; | ||
for (let i = 0; i < rule_length; ++i) { | ||
const name = dummyRule[i]; | ||
this.setProperty( | ||
dummyRule[i], | ||
dummyRule.getPropertyValue(name), | ||
dummyRule.getPropertyPriority(name) | ||
); | ||
} | ||
this._setInProgress = false; | ||
if (this._onChange) { | ||
this._onChange(this.cssText); | ||
} | ||
} | ||
|
||
/** | ||
* @see https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-length | ||
*/ | ||
get length() { | ||
return this._list.length; | ||
} | ||
|
||
/** | ||
* This deletes indices if the new length is less then the current | ||
* length. If the new length is more, it does nothing, the new indices | ||
* will be undefined until set. | ||
**/ | ||
set length(value) { | ||
this._list.length = value; | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} name | ||
* @see https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue | ||
* @return {string} the value of the property if it has been explicitly set for this declaration block. | ||
* Returns the empty string if the property has not been set. | ||
*/ | ||
getPropertyValue(name) { | ||
return this._values[name] || ''; | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} name | ||
* @param {string} value | ||
* @param {string} [priority=""] "important" or "" | ||
* @see https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty | ||
*/ | ||
setProperty(name, value, priority = '') { | ||
if (value === '') { | ||
this.removeProperty(name); | ||
return; | ||
} | ||
|
||
if (name.startsWith('--')) { | ||
this._setProperty(name, value, priority); | ||
return; | ||
} | ||
|
||
const lowercaseName = name.toLowerCase(); | ||
if (!allProperties.has(lowercaseName) && !allExtraProperties.has(lowercaseName)) { | ||
return; | ||
} | ||
|
||
if (implementedProperties.has(lowercaseName)) { | ||
this[lowercaseName] = value; | ||
} else { | ||
this._setProperty(lowercaseName, value, priority); | ||
} | ||
this._importants[lowercaseName] = priority; | ||
} | ||
|
||
/** | ||
* @param {string} name | ||
* @param {string | null} value | ||
* @param {string} [priority=""] | ||
*/ | ||
_setProperty(name, value, priority = '') { | ||
// FIXME: A good chunk of the implemented properties call this method | ||
// with `value = undefined`, expecting it to do nothing: | ||
if (value === undefined) { | ||
return; | ||
} | ||
if (value === null || value === '') { | ||
this.removeProperty(name); | ||
return; | ||
} | ||
|
||
let originalText; | ||
if (this._onChange) { | ||
originalText = this.cssText; | ||
} | ||
|
||
if (this._values[name]) { | ||
// Property already exist. Overwrite it. | ||
if (!this._list.includes(name)) { | ||
this._list.push(name); | ||
} | ||
} else { | ||
// New property. | ||
this._list.push(name); | ||
} | ||
this._values[name] = value; | ||
this._importants[name] = priority; | ||
if (this._onChange && this.cssText !== originalText && !this._setInProgress) { | ||
this._onChange(this.cssText); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} name | ||
* @see https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty | ||
* @return {string} the value of the property if it has been explicitly set for this declaration block. | ||
* Returns the empty string if the property has not been set or the property name does not correspond to a known CSS property. | ||
*/ | ||
removeProperty(name) { | ||
if (!idlUtils.hasOwn(this._values, name)) { | ||
return ''; | ||
} | ||
|
||
const prevValue = this._values[name]; | ||
delete this._values[name]; | ||
delete this._importants[name]; | ||
|
||
const index = this._list.indexOf(name); | ||
if (index < 0) { | ||
return prevValue; | ||
} | ||
|
||
// That's what WebKit and Opera do | ||
this._list.splice(index, 1); | ||
|
||
// That's what Firefox does | ||
//this._list[index] = '' | ||
|
||
if (this._onChange) { | ||
this._onChange(this.cssText); | ||
} | ||
return prevValue; | ||
} | ||
|
||
/** | ||
* | ||
* @param {String} name | ||
* @see https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertypriority | ||
*/ | ||
getPropertyPriority(name) { | ||
return this._importants[name] || ''; | ||
} | ||
|
||
/** | ||
* @see https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-item | ||
*/ | ||
item(index) { | ||
const { _list } = this; | ||
if (index < 0 || index >= _list.length) { | ||
return ''; | ||
} | ||
return _list[index]; | ||
} | ||
|
||
[idlUtils.supportsPropertyIndex](index) { | ||
return index >= 0 && index < this._list.length; | ||
} | ||
|
||
[idlUtils.supportedPropertyIndices]() { | ||
return this._list.keys(); | ||
} | ||
} | ||
|
||
require('./properties')(CSSStyleDeclarationImpl.prototype); | ||
|
||
exports.implementation = CSSStyleDeclarationImpl; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.