Skip to content

Commit

Permalink
Docs: add examples to getUrlsFromSrcset
Browse files Browse the repository at this point in the history
  • Loading branch information
Juice10 committed Feb 5, 2024
1 parent 0115cb6 commit 4484a25
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/rrweb-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ export function getInputType(element: HTMLElement): Lowercase<string> | null {
: null;
}

/**
* Extracts the URLs from a srcset attribute.
* @param srcset - The srcset attribute value. eg. `image.jpg 2x, image2.jpg 3x`
* @returns An array of URLs. eg. `['image.jpg', 'image2.jpg']`
*/
export function getUrlsFromSrcset(srcset: string): string[] {
const urls: string[] = [];
const parts = srcset.split(',');
Expand All @@ -338,11 +343,13 @@ export function getUrlsFromSrcset(srcset: string): string[] {
const spaceIndex = trimmed.indexOf(' ');
if (spaceIndex === -1) {
// If no descriptor is specified, it's a single URL.
// eg. `image.jpg`
urls.push(trimmed);
} else {
// Otherwise, it's one or more URLs followed by a single descriptor.
// Otherwise, it's a URL followed by a single descriptor.
// Since we don't know how long the URL will be, we'll assume it's everything
// after the first space.
// before the first space.
// eg. `image.jpg 2x`
urls.push(trimmed.substring(0, spaceIndex));
}
}
Expand Down

0 comments on commit 4484a25

Please sign in to comment.