MCPcopy
hub / github.com/prettier/prettier / printSrcset

Function printSrcset

src/language-html/embed/srcset.js:20–72  ·  view source on GitHub ↗

@type {AttributeValuePrint}

(
  textToDoc,
  print,
  path,
  /* , options*/
)

Source from the content-addressed store, hash-verified

18
19/** @type {AttributeValuePrint} */
20function printSrcset(
21 textToDoc,
22 print,
23 path,
24 /* , options*/
25) {
26 const value = getUnescapedAttributeValue(path.node);
27 const srcset = parseSrcset(value);
28 const types = SRCSET_TYPES.filter((type) =>
29 srcset.some((candidate) => Object.hasOwn(candidate, type)),
30 );
31
32 if (types.length > 1) {
33 throw new Error("Mixed descriptor in srcset is not supported");
34 }
35
36 const [key] = types;
37 const unit = SRCSET_UNITS[key];
38
39 const urls = srcset.map((src) => src.source.value);
40 const maxUrlLength = Math.max(...urls.map((url) => url.length));
41
42 const descriptors = srcset.map((src) =>
43 src[key] ? String(src[key].value) : "",
44 );
45 const descriptorLeftLengths = descriptors.map((descriptor) => {
46 const index = descriptor.indexOf(".");
47 return index === -1 ? descriptor.length : index;
48 });
49 const maxDescriptorLeftLength = Math.max(...descriptorLeftLengths);
50
51 return printExpand(
52 join(
53 [",", line],
54 urls.map((url, index) => {
55 /** @type {Doc[]} */
56 const parts = [url];
57
58 const descriptor = descriptors[index];
59 if (descriptor) {
60 const urlPadding = maxUrlLength - url.length + 1;
61 const descriptorPadding =
62 maxDescriptorLeftLength - descriptorLeftLengths[index];
63
64 const alignment = " ".repeat(urlPadding + descriptorPadding);
65 parts.push(ifBreak(alignment, " "), descriptor + unit);
66 }
67
68 return parts;
69 }),
70 ),
71 );
72}
73
74export { isSrcset, printSrcset };

Callers

nothing calls this directly

Calls 5

printExpandFunction · 0.90
joinFunction · 0.90
ifBreakFunction · 0.90
mapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…