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 {
|
||||
|
||||
Reference in New Issue
Block a user