yari
yari copied to clipboard
fix(macros/cssxref): correct URL generation for data types and functions
Summary
Fixes #8755.
Problem
-
{{CSSXref}}
Generated URLs for CSS functions contain extra parentheses. -
:host()
andfit-content()
are not handled correctly. -
<overflow>
is not handled correctly. - The examples in
cssxref.ejs
are outdated.
Solution
- Remove parentheses in
slug
. - Append
_funtion
toslug
. - Add the data type into the special-case block.
- Update examples according to current results.
How did you test this change?
Run the following code in the console:
// Only return the URL
function CSSXref(arg0, arg1 = "", arg2 = "") {
const lang = "en-US"; // for convenience
let url = "";
let urlWithoutAnchor = "";
let displayName = (arg1 || arg0);
// Deal with CSS data types and functions by removing <> and ()
let slug = arg0.replace(/<(.*)>/g, "$1")
.replace(/\(\)/g, "");
// Special case <color>, <flex>, <overflow>, and <position>
if (/^<(color|flex|overflow|position)>$/.test(arg0)) {
slug += "_value";
}
// Special case :host() and fit-content()
if (/^(:host|fit-content)\(\)$/.test(arg0)) {
slug += "_function";
}
const basePath = `/${lang}/docs/Web/CSS/`;
urlWithoutAnchor = basePath + slug;
url = urlWithoutAnchor + arg2;
return url;
}
// Test cases adpated from 'cssxref.test.ts'
const TEST_CASE = [
"display",
"attr()",
":host()",
"length",
"<length>",
"<color>",
"<overflow>"
];
for(const caseInput of TEST_CASE) {
console.log(CSSXref(caseInput));
}
/* Expected output:
/en-US/docs/Web/CSS/display
/en-US/docs/Web/CSS/attr
/en-US/docs/Web/CSS/:host_function
/en-US/docs/Web/CSS/length
/en-US/docs/Web/CSS/length
/en-US/docs/Web/CSS/color_value
/en-US/docs/Web/CSS/overflow_value
*/
The unit test is complaining about pageType
(?)
@yarusome The unit test does not access mdn/content, but instead uses some mock pages, and the mocked attr
page still uses parentheses:
https://github.com/mdn/yari/blob/f6e5179dfa8574bb30ed29b1b4c6a1a47a6e70d7/kumascript/tests/macros/cssxref.test.ts#L37-L45
Updating those urls should fix the failing test.
@caugner Thanks for the hint. All unit tests pass now.