本文我们分析一下Tailwindcss源码中String.raw的用法。
MDN 文档说:
“String.raw()静态方法是模板文字的标签函数。
这类似于 Python 中的 r 前缀,或 C# 中字符串文字的 @ 前缀。
它用于获取模板文字的原始字符串形式 - 即替换
(例如 ${foo})被处理,但转义序列(例如 n)未被处理。“
以下示例摘自MDN:
// Create a variable that uses a Windows // path without escaping the backslashes: const filePath = String.raw`C:\Development\profile\aboutme.html`; console.log(`The file was uploaded from: ${filePath}`); // Expected output: "The file was uploaded from: C:\Development\profile\aboutme.html"
上面示例的执行结果如下所示:
> "The file was uploaded from: C:\Development\profile\aboutme.html"
如果您使用以下代码在没有 String.raw 的情况下运行相同的示例:
// Create a variable that uses a Windows // path without escaping the backslashes: const filePath = `C:\Development\profile\aboutme.html`; console.log(`The file was uploaded from: ${filePath}`); // Expected output: "The file was uploaded from: C:\Development\profile\aboutme.html"
结果如下所示:
> "The file was uploaded from: C:Developmentprofileaboutme.html"
如果您使用以下代码运行涉及 n 的相同示例:
const filePathWithoutStringRaw = `\nC:\Development\profile\aboutme.html`; const filePathWithStringRaw = String.raw`\nC:\Development\profile\aboutme.html`; console.log("filePathWithoutStringRaw:", filePathWithoutStringRaw) console.log("*******") console.log("filePathWithStringRaw:", filePathWithStringRaw)
结果如下所示:
> "filePathWithoutStringRaw:" " C:Developmentprofileaboutme.html" > "*******" > "filePathWithStringRaw:" "\nC:\Development\profile\aboutme.html"
ui.spec.ts 测试文件将 String.raw 分配给 html 和 css。
发现很多用ui.spec.ts编写的测试都使用了html
让我们仔细看看这个测试代码:
for (let [classes, expected] of [ [ 'bg-linear-to-r from-red-500', 'linear-gradient(to right, rgb(239, 68, 68) 0%, rgba(0, 0, 0, 0) 100%)', ], [ 'bg-linear-to-r via-red-500', 'linear-gradient(to right, rgba(0, 0, 0, 0) 0%, rgb(239, 68, 68) 50%, rgba(0, 0, 0, 0) 100%)', ], [ 'bg-linear-to-r to-red-500', 'linear-gradient(to right, rgba(0, 0, 0, 0) 0%, rgb(239, 68, 68) 100%)', ], [ 'bg-linear-to-r from-red-500 to-blue-500', 'linear-gradient(to right, rgb(239, 68, 68) 0%, rgb(59, 130, 246) 100%)', ], [ 'bg-linear-to-r via-red-500 to-blue-500', 'linear-gradient(to right, rgba(0, 0, 0, 0) 0%, rgb(239, 68, 68) 50%, rgb(59, 130, 246) 100%)', ], [ 'bg-linear-to-r from-red-500 via-green-500 to-blue-500', 'linear-gradient(to right, rgb(239, 68, 68) 0%, rgb(34, 197, 94) 50%, rgb(59, 130, 246) 100%)', ], [ 'bg-linear-[to_right,var( - color-red-500),var( - color-green-500),var( - color-blue-500)]', 'linear-gradient(to right, rgb(239, 68, 68), rgb(34, 197, 94), rgb(59, 130, 246))', ], ]) { test(`background gradient, "${classes}"`, async ({ page }) => { let { getPropertyValue } = await render( page, html`<div id="x" class="${classes}">Hello world</div>`, ) expect(await getPropertyValue('#x', 'background-image')).toEqual(expected) }) }
看到在 for 循环中定义的整个数组很有趣。
html`<div id="x" class="${classes}">Hello world</div>`,
${classes} 被替换为以下数组的第一项:
[<br> 'bg-linear-to-r from-red-500',<br> 'linear-gradient(to right, rgb(239, 68, 68) 0%, rgba(0, 0, 0, 0) 100%)',<br> ],<br>
在 Think Throo,我们的使命是教授开源项目中使用的高级代码库架构概念。
通过在 Next.js/React 中练习高级架构概念,将您的编码技能提高 10 倍,学习最佳实践并构建生产级项目。
我们是开源的 — https://github.com/thinkthroo/thinkthroo (请给我们一颗星!)
我们还提供网络开发和技术写作服务。请通过hello@thinkthroo.com联系我们了解更多信息!
https://github.com/tailwindlabs/tailwindcss/blob/next/packages/tailwindcss/tests/ui.spec.ts
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw
https://stackoverflow.com/questions/34772762/what-are-the-actual-uses-of-es6-raw-string-access
以上是Tailwind CSS 源代码中的 String.raw。的详细内容。更多信息请关注PHP中文网其他相关文章!