Skip to content
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

Extract CSS at-rules, move descriptors under at-rules #1033

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions src/browserlib/extract-cssdfn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,37 @@ import informativeSelector from './informative-selector.mjs';
export default function () {
let res = {
properties: extractTableDfns(document, 'propdef', { unique: true }),
descriptors: extractTableDfns(document, 'descdef', { unique: false }),
atrules: {},
valuespaces: extractValueSpaces(document)
};
let descriptors = extractTableDfns(document, 'descdef', { unique: false });

// Try old recipes if we couldn't extract anything
if ((Object.keys(res.properties).length === 0) &&
(Object.keys(res.descriptors).length === 0)) {
(Object.keys(descriptors).length === 0)) {
res.properties = extractDlDfns(document, 'propdef', { unique: true });
res.descriptors = extractDlDfns(document, 'descdef', { unique: false });
descriptors = extractDlDfns(document, 'descdef', { unique: false });
}

// Move at-rules definitions from valuespaces to at-rules structure
for (const [name, dfn] of Object.entries(res.valuespaces)) {
if (name.startsWith('@')) {
if (!res.atrules[name]) {
res.atrules[name] = Object.assign(dfn, { descriptors: [] });
}
delete res.valuespaces[name];
}
}

// Move descriptors to at-rules structure
for (const [name, desclist] of Object.entries(descriptors)) {
for (const desc of desclist) {
const rule = desc.for;
if (!res.atrules[rule]) {
res.atrules[rule] = { descriptors: [] };
}
res.atrules[rule].descriptors.push(desc);
}
}

return res;
Expand Down Expand Up @@ -198,6 +220,9 @@ const extractDlDfns = (doc, className, options) =>
* Definitions with `data-dfn-type` attribute set to `value` are not extracted
* on purpose as they are typically namespaced to another construct (through a
* `data-dfn-for` attribute.
*
* The function also extracts syntax of at-rules (name starts with '@') defined
* in "pre.prod" blocks.
*/
const extractValueSpaces = doc => {
let res = {};
Expand All @@ -213,16 +238,27 @@ const extractValueSpaces = doc => {
.replace(/\/\*[^]*?\*\//gm, '') // Drop comments
.split(/\s?=\s/)
.map(s => s.trim().replace(/\s+/g, ' '));
if (nameAndValue[0].match(/^<.*>$|^.*\(\)$/)) {
const name = nameAndValue[0].replace(/^(.*\(\))$/, '<$1>');

function addValuespace(name, value) {
if (!(name in res)) {
res[name] = {};
}
if (!res[name].value || (pureSyntax && !res[name].pureSyntax)) {
res[name].value = normalize(nameAndValue[1]);
res[name].value = normalize(value);
res[name].pureSyntax = pureSyntax;
}
}

if (nameAndValue[0].match(/^<.*>$|^.*\(\)$/)) {
// Regular valuespace
addValuespace(
nameAndValue[0].replace(/^(.*\(\))$/, '<$1>'),
nameAndValue[1]);
}
else if (nameAndValue[0].match(/^@[a-z\-]+$/)) {
// At-rule syntax
addValuespace(nameAndValue[0], nameAndValue[1]);
}
};

// Regular expression to use to split production rules:
Expand Down Expand Up @@ -351,8 +387,16 @@ const extractValueSpaces = doc => {
.map(val => val.replace(/\/\*[^]*?\*\//gm, '')) // Drop comments
.map(val => val.split(reSplitRules)) // Separate definitions
.flat()
.filter(text => text.match(/\s?=\s/))
.map(text => parseProductionRule(text, { pureSyntax: true }));
.map(text => text.trim())
.map(text => {
if (text.match(/\s?=\s/)) {
return parseProductionRule(text, { pureSyntax: true });
}
else if (text.startsWith('@')) {
const name = text.split(' ')[0];
return parseProductionRule(`${name} = ${text}`, { pureSyntax: true });
}
});

// Don't keep the info on whether value comes from a pure syntax section
Object.values(res).map(value => delete value.pureSyntax);
Expand Down
6 changes: 3 additions & 3 deletions tests/crawl-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
},
"title": "WOFF2",
"css": {
"atrules": {},
"properties": {},
"descriptors": {},
"valuespaces": {}
},
"dfns": [
Expand Down Expand Up @@ -81,8 +81,8 @@
"title": "No Title",
"generator": "respec",
"css": {
"atrules": {},
"properties": {},
"descriptors": {},
"valuespaces": {}
},
"dfns": [
Expand Down Expand Up @@ -201,8 +201,8 @@
},
"title": "[No title found for https://w3c.github.io/accelerometer/]",
"css": {
"atrules": {},
"properties": {},
"descriptors": {},
"valuespaces": {}
},
"dfns": [],
Expand Down
94 changes: 79 additions & 15 deletions tests/extract-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,64 @@ const tests = [
css: {}
},

{
title: "extracts an at-rule syntax",
html: `
<pre class="prod">
@layer <a class="production">&lt;layer-name&gt;</a>? {
<a class="production">&lt;stylesheet&gt;</a>
}
</pre>
`,
propertyName: "atrules",
css: {
"@layer": {
"value": "@layer <layer-name>? { <stylesheet> }",
"descriptors": []
}
}
},

{
title: "combines an at-rule syntax with descriptor",
html: `
<pre class="prod">
@font-face {
&lt;declaration-list&gt;
}
</pre>
<table class="def descdef">
<tbody>
<tr>
<th>Name:
</th><td><dfn class="dfn-paneled css" data-dfn-for="@font-face" data-dfn-type="descriptor" data-export="" id="descdef-font-face-font-display">font-display</dfn>
</td></tr><tr>
<th>For:
</th><td><a class="css" data-link-type="at-rule" href="#at-font-face-rule" id="ref-for-at-font-face-rule④⑥">@font-face</a>
</td></tr><tr>
<th>Value:
</th><td class="prod">auto <a data-link-type="grammar" href="https://drafts.csswg.org/css-values-4/#comb-one" id="ref-for-comb-one⑥⑨">|</a> block <span id="ref-for-comb-one⑦⓪">|</span> swap <span id="ref-for-comb-one⑦①">|</span> fallback <span id="ref-for-comb-one⑦②">|</span> optional
</td></tr><tr>
<th>Initial:
</th><td>auto
</td></tr></tbody></table>
`,
propertyName: "atrules",
css: {
"@font-face": {
"value": "@font-face { <declaration-list> }",
"descriptors": [
{
for: "@font-face",
initial: "auto",
name: "font-display",
value: "auto | block | swap | fallback | optional"
}
]
}
}
},


{
title: "extracts multiple descriptors with the same name",
Expand Down Expand Up @@ -301,22 +359,28 @@ const tests = [
<th>Initial:
</th><td>auto
</td></tr></tbody></table>`,
propertyName: "descriptors",
propertyName: "atrules",
css: {
"font-display": [
{
for: "@font-face",
initial: "auto",
name: "font-display",
value: "auto | block | swap | fallback | optional"
},
{
for: "@font-feature-values",
initial: "auto",
name: "font-display",
value: "auto | block | swap | fallback | optional"
}
]
"@font-face": {
"descriptors": [
{
for: "@font-face",
initial: "auto",
name: "font-display",
value: "auto | block | swap | fallback | optional"
}
]
},
"@font-feature-values": {
"descriptors": [
{
for: "@font-feature-values",
initial: "auto",
name: "font-display",
value: "auto | block | swap | fallback | optional"
}
]
}
}
},

Expand Down