Validate w/h and avoid use of properties object.

This commit is contained in:
Danila Fedorin 2021-07-12 13:42:39 -07:00
parent 1e9cdbafd4
commit 012ef2b215
3 changed files with 12 additions and 10 deletions

View file

@ -81,9 +81,12 @@ export class FormatPart {
} }
export class ImagePart { export class ImagePart {
constructor(src, properties) { constructor(src, width, height, alt, title) {
this.src = src; this.src = src;
this.properties = properties; this.width = width;
this.height = height;
this.alt = alt;
this.title = title;
} }
get type() { return "image"; } get type() { return "image"; }

View file

@ -73,11 +73,11 @@ class Deserializer {
if (!url) { if (!url) {
return null; return null;
} }
const width = result.getAttributeValue(node, "width"); const width = parseInt(result.getAttributeValue(node, "width")) || null;
const height = result.getAttributeValue(node, "height"); const height = parseInt(result.getAttributeValue(node, "height")) || null;
const alt = result.getAttributeValue(node, "alt"); const alt = result.getAttributeValue(node, "alt");
const title = result.getAttributeValue(node, "title"); const title = result.getAttributeValue(node, "title");
return new ImagePart(url, { width, height, alt, title }); return new ImagePart(url, width, height, alt, title);
} }
parseElement(node) { parseElement(node) {

View file

@ -43,13 +43,12 @@ function renderList(listBlock) {
} }
function renderImage(imagePart) { function renderImage(imagePart) {
const props = imagePart.properties;
const attributes = Object.assign( const attributes = Object.assign(
{ src: imagePart.src }, { src: imagePart.src },
props.width && { width: props.width }, imagePart.width && { width: imagePart.width },
props.height && { height: props.height }, imagePart.height && { height: imagePart.height },
props.alt && { alt: props.alt }, imagePart.alt && { alt: imagePart.alt },
props.title && { title: props.title } imagePart.title && { title: imagePart.title }
); );
return tag.img(attributes, []); return tag.img(attributes, []);
} }