Skip to content

Commit

Permalink
add support for @starting-style (#1)
Browse files Browse the repository at this point in the history
* add support for starting style

* Create three-bears-kick.md
  • Loading branch information
jantimon committed May 2, 2024
1 parent 87e6cd0 commit d90a5d3
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/three-bears-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rrweb-cssom": minor
---

add support for @starting-style
1 change: 1 addition & 0 deletions lib/CSSRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ CSSOM.CSSRule.DOCUMENT_RULE = 13;
CSSOM.CSSRule.FONT_FEATURE_VALUES_RULE = 14;
CSSOM.CSSRule.VIEWPORT_RULE = 15;
CSSOM.CSSRule.REGION_STYLE_RULE = 16;
CSSOM.CSSRule.STARTING_STYLE_RULE = 1002;


CSSOM.CSSRule.prototype = {
Expand Down
37 changes: 37 additions & 0 deletions lib/CSSStartingStyleRule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//.CommonJS
var CSSOM = {
CSSRule: require("./CSSRule").CSSRule
};
///CommonJS


/**
* @constructor
* @see http://www.w3.org/TR/shadow-dom/#host-at-rule
*/
CSSOM.CSSStartingStyleRule = function CSSStartingStyleRule() {
CSSOM.CSSRule.call(this);
this.cssRules = [];
};

CSSOM.CSSStartingStyleRule.prototype = new CSSOM.CSSRule();
CSSOM.CSSStartingStyleRule.prototype.constructor = CSSOM.CSSStartingStyleRule;
CSSOM.CSSStartingStyleRule.prototype.type = 1002;
//FIXME
//CSSOM.CSSStartingStyleRule.prototype.insertRule = CSSStyleSheet.prototype.insertRule;
//CSSOM.CSSStartingStyleRule.prototype.deleteRule = CSSStyleSheet.prototype.deleteRule;

Object.defineProperty(CSSOM.CSSStartingStyleRule.prototype, "cssText", {
get: function() {
var cssTexts = [];
for (var i=0, length=this.cssRules.length; i < length; i++) {
cssTexts.push(this.cssRules[i].cssText);
}
return "@starting-style {" + cssTexts.join("") + "}";
}
});


//.CommonJS
exports.CSSStartingStyleRule = CSSOM.CSSStartingStyleRule;
///CommonJS
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exports.CSSSupportsRule = require('./CSSSupportsRule').CSSSupportsRule;
exports.CSSImportRule = require('./CSSImportRule').CSSImportRule;
exports.CSSFontFaceRule = require('./CSSFontFaceRule').CSSFontFaceRule;
exports.CSSHostRule = require('./CSSHostRule').CSSHostRule;
exports.CSSStartingStyleRule = require('./CSSStartingStyleRule').CSSStartingStyleRule;
exports.StyleSheet = require('./StyleSheet').StyleSheet;
exports.CSSStyleSheet = require('./CSSStyleSheet').CSSStyleSheet;
exports.CSSKeyframesRule = require('./CSSKeyframesRule').CSSKeyframesRule;
Expand Down
20 changes: 19 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CSSOM.parse = function parse(token) {
var hasAncestors = false;
var prevScope;

var name, priority="", styleRule, mediaRule, supportsRule, importRule, fontFaceRule, keyframesRule, documentRule, hostRule;
var name, priority="", styleRule, mediaRule, supportsRule, importRule, fontFaceRule, keyframesRule, documentRule, hostRule, startingStyleRule;

var atKeyframesRegExp = /@(-(?:\w+-)+)?keyframes/g;

Expand Down Expand Up @@ -171,6 +171,13 @@ CSSOM.parse = function parse(token) {
hostRule.__starts = i;
buffer = "";
break;
} else if (token.indexOf("@starting-style", i) === i) {
state = "startingStyleRule-begin";
i += "startingStyle".length;
startingStyleRule = new CSSOM.CSSStartingStyleRule();
startingStyleRule.__starts = i;
buffer = "";
break;
} else if (token.indexOf("@import", i) === i) {
state = "importRule-begin";
i += "import".length;
Expand Down Expand Up @@ -238,6 +245,16 @@ CSSOM.parse = function parse(token) {
hostRule.parentStyleSheet = styleSheet;
buffer = "";
state = "before-selector";
} else if (state === "startingStyleRule-begin") {
if (parentRule) {
ancestorRules.push(parentRule);
}

currentScope = parentRule = startingStyleRule;
startingStyleRule.parentStyleSheet = styleSheet;
buffer = "";
state = "before-selector";

} else if (state === "fontFaceRule-begin") {
if (parentRule) {
fontFaceRule.parentRule = parentRule;
Expand Down Expand Up @@ -457,6 +474,7 @@ CSSOM.CSSConditionRule = require("./CSSConditionRule").CSSConditionRule;
CSSOM.CSSSupportsRule = require("./CSSSupportsRule").CSSSupportsRule;
CSSOM.CSSFontFaceRule = require("./CSSFontFaceRule").CSSFontFaceRule;
CSSOM.CSSHostRule = require("./CSSHostRule").CSSHostRule;
CSSOM.CSSStartingStyleRule = require("./CSSStartingStyleRule").CSSStartingStyleRule;
CSSOM.CSSStyleDeclaration = require('./CSSStyleDeclaration').CSSStyleDeclaration;
CSSOM.CSSKeyframeRule = require('./CSSKeyframeRule').CSSKeyframeRule;
CSSOM.CSSKeyframesRule = require('./CSSKeyframesRule').CSSKeyframesRule;
Expand Down
27 changes: 27 additions & 0 deletions spec/parse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,33 @@ var TESTS = [
return result;
})()
},
{
input: "@starting-style { body { background: red; } }",
result: (function() {
var result = {
cssRules: [
{
cssRules: {
0: {
selectorText: "body",
style: {
0: "background",
length: 1,
parentRule: "..",
background: "red"
}
}
},
parentRule: null
}
],
parentStyleSheet: null
};
result.cssRules[0].parentStyleSheet = result.cssRules[0].cssRules[0].parentStyleSheet = result;
result.cssRules[0].cssRules[0].parentRule = result.cssRules[0];
return result;
})()
},
{
// Non-vendor prefixed @keyframes rule, from Twitter Bootstrap (progress-bars):
input: '@keyframes progress-bar-stripes {\n from { background-position: 0 0; }\n to { background-position: 40px 0; }\n}',
Expand Down
1 change: 1 addition & 0 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports.files = [
"CSSImportRule",
"CSSFontFaceRule",
"CSSHostRule",
"CSSStartingStyleRule",
"StyleSheet",
"CSSStyleSheet",
"CSSKeyframesRule",
Expand Down

0 comments on commit d90a5d3

Please sign in to comment.