Added srcset support (#18)
* added src set as a parsed attribute * added tests * changed to /a * added multiple attribute handling * added better comment * made snapshot ignore invalid input as if it is invalid input in the original DOM, it should stay invalid in the recreated DOM * added extra absolute test case * code style * addressed comments
This commit is contained in:
@@ -85,7 +85,36 @@ export function absoluteToStylesheet(cssText: string, href: string): string {
|
||||
});
|
||||
}
|
||||
|
||||
function getAbsoluteSrcsetString(doc: Document, attributeValue: string) {
|
||||
if(attributeValue.trim() === "") {
|
||||
return attributeValue
|
||||
}
|
||||
|
||||
const srcsetValues = attributeValue.split(",")
|
||||
// srcset attributes is defined as such:
|
||||
// srcset = "url size,url1 size1"
|
||||
const resultingSrcsetString = srcsetValues.map((srcItem) => {
|
||||
// removing all but middle spaces
|
||||
const trimmedSrcItem = srcItem.trimLeft().trimRight()
|
||||
const urlAndSize = trimmedSrcItem.split(" ")
|
||||
// this means we have both 0:url and 1:size
|
||||
if (urlAndSize.length === 2) {
|
||||
const absUrl = absoluteToDoc(doc, urlAndSize[0])
|
||||
return `${absUrl} ${urlAndSize[1]}`
|
||||
} else if(urlAndSize.length === 1){
|
||||
const absUrl = absoluteToDoc(doc, urlAndSize[0])
|
||||
return `${absUrl}`
|
||||
}
|
||||
return ""
|
||||
}).join(',')
|
||||
|
||||
return resultingSrcsetString
|
||||
}
|
||||
|
||||
function absoluteToDoc(doc: Document, attributeValue: string): string {
|
||||
if (attributeValue.trim() === ""){
|
||||
return attributeValue
|
||||
}
|
||||
const a: HTMLAnchorElement = doc.createElement('a');
|
||||
a.href = attributeValue;
|
||||
return a.href;
|
||||
@@ -132,6 +161,8 @@ function serializeNode(
|
||||
// relative path in attribute
|
||||
if (name === 'src' || name === 'href') {
|
||||
attributes[name] = absoluteToDoc(doc, value);
|
||||
} else if (name == 'srcset') {
|
||||
attributes[name] = getAbsoluteSrcsetString(doc, value)
|
||||
} else if (name === 'style') {
|
||||
attributes[name] = absoluteToStylesheet(value, location.href);
|
||||
} else {
|
||||
|
||||
@@ -181,7 +181,10 @@ exports[`[html file]: with-relative-res.html 1`] = `
|
||||
</head>
|
||||
<body>
|
||||
<a href=\\"http://localhost:3030/basic.html\\"></a>
|
||||
<img src=\\"http://localhost:3030/a.jpg\\" alt=\\"\\" srcset=\\"\\" /></body></html>"
|
||||
<img src=\\"http://localhost:3030/a.jpg\\" alt=\\"\\" srcset=\\"\\" />
|
||||
<img src=\\"http://localhost:3030/a.jpg\\" alt=\\"\\" srcset=\\"http://localhost:3030/a.jpg\\" />
|
||||
<img src=\\"http://localhost:3030/a.jpg\\" alt=\\"\\" srcset=\\"http://exmple.com/a.jpg\\" />
|
||||
<img src=\\"http://localhost:3030/a.jpg\\" alt=\\"\\" srcset=\\"http://localhost:3030/a.jpg 3x,http://localhost:3030/a.jpg 45x,http://localhost:3030/b.png\\" /></body></html>"
|
||||
`;
|
||||
|
||||
exports[`[html file]: with-script.html 1`] = `
|
||||
|
||||
@@ -9,5 +9,8 @@
|
||||
<body>
|
||||
<a href="./basic.html"></a>
|
||||
<img src="./a.jpg" alt="" srcset="">
|
||||
<img src="./a.jpg" alt="" srcset="/a.jpg">
|
||||
<img src="./a.jpg" alt="" srcset="http://exmple.com/a.jpg ">
|
||||
<img src="./a.jpg" alt="" srcset="/a.jpg 3x, /a.jpg 45x , /b.png">
|
||||
</body>
|
||||
</html>
|
||||
@@ -5,7 +5,7 @@ import { absoluteToStylesheet } from '../src/snapshot';
|
||||
describe('absolute url to stylesheet', () => {
|
||||
const href = 'http://localhost/css/style.css';
|
||||
|
||||
it('cam handle relative path', () => {
|
||||
it('can handle relative path', () => {
|
||||
expect(absoluteToStylesheet('url(a.jpg)', href)).to.equal(
|
||||
`url('http://localhost/css/a.jpg')`,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user