Scraper / `Native.toRule` incorrectly expands CSS shorthands that contain variables
With HTML file:
<style>
#target {
--blue: #0000ff;
background: var(--blue);
color: #eeeeee;
}
</style>
<div>
<span id="target">Hello</span> World
</div>
and running: yarn alfa scrape on it, the resulting JSON contains:
{
"type": "style",
"selector": "#target",
"style": [
{ "name": "background-image", "value": "", "important": false },
{
"name": "background-position-x",
"value": "",
"important": false
},
{
"name": "background-position-y",
"value": "",
"important": false
},
{ "name": "background-size", "value": "", "important": false },
{
"name": "background-repeat-x",
"value": "",
"important": false
},
{
"name": "background-repeat-y",
"value": "",
"important": false
},
{
"name": "background-attachment",
"value": "",
"important": false
},
{ "name": "background-origin", "value": "", "important": false },
{ "name": "background-clip", "value": "", "important": false },
{ "name": "background-color", "value": "", "important": false },
{
"name": "color",
"value": "rgb(238, 238, 238)",
"important": false
}
]
}
which has incorrectly expanded the shorthand background without correctly resolving the variable.
So, it seems that CSSStyleDeclaration.style[i] only exposes longhands, but not the shorthands that are defined.
Upon looking at the declaration corresponding to that block, we get:
…
0: "background-image"
1: "background-position-x"
2: "background-position-y"
3: "background-size"
4: "background-repeat"
5: "background-attachment"
6: "background-origin"
7: "background-clip"
8: "background-color"
…
background: "var(--blue)"
backgroundAttachment: ""
backgroundBlendMode: ""
backgroundClip: ""
backgroundColor: ""
backgroundImage: ""
backgroundOrigin: ""
backgroundPosition: ""
backgroundPositionX: ""
backgroundPositionY: ""
backgroundRepeat: ""
backgroundSize: ""
…
So, only the longhands are indexed, but the shorthand is still correctly recorded. Since Alfa looks at the indexed properties, it does not see that the shorthand has been defined and should be grabbed instead.
When the variable is not used, and background: blue is used instead, we get:
…
0: "background-image"
1: "background-position-x"
2: "background-position-y"
3: "background-size"
4: "background-repeat"
5: "background-attachment"
6: "background-origin"
7: "background-clip"
8: "background-color"
…
background: "blue"
backgroundAttachment: "initial"
backgroundBlendMode: ""
backgroundClip: "initial"
backgroundColor: "blue"
backgroundImage: "initial"
backgroundOrigin: "initial"
backgroundPosition: "initial"
backgroundPositionX: "initial"
backgroundPositionY: "initial"
backgroundRepeat: "initial"
backgroundSize: "initial"
…
So, the shorthand is correctly expanded (which is possible since it's not a variable that needs cascading to be resolved first), and we grab the correct value.
We might need some special handling of shorthand when grabbing style rules…
We will likely need to have Native.toBlock send a JSON object with unparsed CSS text, so we can use CSSDeclaration.cssText instead, and then Block.fromJSON (or a new version) use alfa-css/syntax/Declaration.parse to get the correct values.