Skip to content

Commit 569cc2b

Browse files
Parse width/height HTML attributes as string
1 parent b86cab1 commit 569cc2b

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

src/ImageSharp.Web/TagHelpers/ImageTagHelper.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,20 @@ private void AddResizeCommands(TagHelperOutput output, CommandCollection command
253253
{
254254
// If no explicit width/height has been set on the image, set the attributes to match the
255255
// width/height from the process commands if present.
256-
int? width = output.Attributes[ResizeWebProcessor.Width]?.Value as int?;
256+
int? width = output.Attributes.ContainsName(ResizeWebProcessor.Width)
257+
? int.Parse(output.Attributes[ResizeWebProcessor.Width].Value.ToString(), this.parserCulture)
258+
: null;
259+
257260
if (this.Width.HasValue)
258261
{
259262
commands.Add(ResizeWebProcessor.Width, this.Width.Value.ToString(this.parserCulture));
260263
output.Attributes.SetAttribute(ResizeWebProcessor.Width, width ?? this.Width);
261264
}
262265

263-
int? height = output.Attributes[ResizeWebProcessor.Height]?.Value as int?;
266+
int? height = output.Attributes.ContainsName(ResizeWebProcessor.Height)
267+
? int.Parse(output.Attributes[ResizeWebProcessor.Height].Value.ToString(), this.parserCulture)
268+
: null;
269+
264270
if (this.Height.HasValue)
265271
{
266272
commands.Add(ResizeWebProcessor.Height, this.Height.Value.ToString(this.parserCulture));

tests/ImageSharp.Web.Tests/TagHelpers/ImageTagHelperTests.cs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void PreservesOrderOfSourceAttributesWhenRun()
103103
{ "data-extra", new HtmlString("something") },
104104
{ "title", new HtmlString("Image title") },
105105
{ "src", "testimage.png" },
106-
{ "width", 50 },
106+
{ "width", "50" },
107107
{ "height", 60 }
108108
});
109109

@@ -113,7 +113,7 @@ public void PreservesOrderOfSourceAttributesWhenRun()
113113
{ "alt", new HtmlString("alt text") },
114114
{ "data-extra", new HtmlString("something") },
115115
{ "title", new HtmlString("Image title") },
116-
{ "width", 50 },
116+
{ "width", "50" },
117117
{ "height", 60 }
118118
});
119119

@@ -124,7 +124,7 @@ public void PreservesOrderOfSourceAttributesWhenRun()
124124
{ "data-extra", new HtmlString("something") },
125125
{ "title", new HtmlString("Image title") },
126126
{ "src", "testimage.png?width=100&height=120" },
127-
{ "width", 50 },
127+
{ "width", "50" },
128128
{ "height", 60 }
129129
});
130130

@@ -192,20 +192,20 @@ public void RendersImageTag_SrcIncludes_ResizeMode()
192192
attributes: new TagHelperAttributeList
193193
{
194194
{ "src", "testimage.png" },
195-
{ "width", 50 }
195+
{ "width", "50" }
196196
});
197197

198198
TagHelperOutput output = MakeImageTagHelperOutput(
199199
attributes: new TagHelperAttributeList
200200
{
201-
{ "width", 50 }
201+
{ "width", "50" }
202202
});
203203

204204
TagHelperOutput expectedOutput = MakeImageTagHelperOutput(
205205
attributes: new TagHelperAttributeList
206206
{
207207
{ "src", $"testimage.png?width=100&{ResizeWebProcessor.Mode}={nameof(ResizeMode.Stretch)}" },
208-
{ "width", 50 }
208+
{ "width", "50" }
209209
});
210210

211211
ImageTagHelper helper = this.GetHelper();
@@ -237,20 +237,20 @@ public void RendersImageTag_SrcIncludes_ResizePosition()
237237
attributes: new TagHelperAttributeList
238238
{
239239
{ "src", "testimage.png" },
240-
{ "width", 50 }
240+
{ "width", "50" }
241241
});
242242

243243
TagHelperOutput output = MakeImageTagHelperOutput(
244244
attributes: new TagHelperAttributeList
245245
{
246-
{ "width", 50 }
246+
{ "width", "50" }
247247
});
248248

249249
TagHelperOutput expectedOutput = MakeImageTagHelperOutput(
250250
attributes: new TagHelperAttributeList
251251
{
252252
{ "src", $"testimage.png?width=100&{ResizeWebProcessor.Xy}=20,50" },
253-
{ "width", 50 }
253+
{ "width", "50" }
254254
});
255255

256256
ImageTagHelper helper = this.GetHelper();
@@ -282,20 +282,20 @@ public void RendersImageTag_SrcIncludes_ResizeAnchor()
282282
attributes: new TagHelperAttributeList
283283
{
284284
{ "src", "testimage.png" },
285-
{ "width", 50 }
285+
{ "width", "50" }
286286
});
287287

288288
TagHelperOutput output = MakeImageTagHelperOutput(
289289
attributes: new TagHelperAttributeList
290290
{
291-
{ "width", 50 }
291+
{ "width", "50" }
292292
});
293293

294294
TagHelperOutput expectedOutput = MakeImageTagHelperOutput(
295295
attributes: new TagHelperAttributeList
296296
{
297297
{ "src", $"testimage.png?width=100&{ResizeWebProcessor.Anchor}={nameof(AnchorPositionMode.Top)}" },
298-
{ "width", 50 }
298+
{ "width", "50" }
299299
});
300300

301301
ImageTagHelper helper = this.GetHelper();
@@ -327,20 +327,20 @@ public void RendersImageTag_SrcIncludes_ResizePadColor()
327327
attributes: new TagHelperAttributeList
328328
{
329329
{ "src", "testimage.png" },
330-
{ "width", 50 }
330+
{ "width", "50" }
331331
});
332332

333333
TagHelperOutput output = MakeImageTagHelperOutput(
334334
attributes: new TagHelperAttributeList
335335
{
336-
{ "width", 50 }
336+
{ "width", "50" }
337337
});
338338

339339
TagHelperOutput expectedOutput = MakeImageTagHelperOutput(
340340
attributes: new TagHelperAttributeList
341341
{
342342
{ "src", $"testimage.png?width=100&{ResizeWebProcessor.Color}={Color.LimeGreen.ToHex()}" },
343-
{ "width", 50 }
343+
{ "width", "50" }
344344
});
345345

346346
ImageTagHelper helper = this.GetHelper();
@@ -372,20 +372,20 @@ public void RendersImageTag_SrcIncludes_ResizeCompand()
372372
attributes: new TagHelperAttributeList
373373
{
374374
{ "src", "testimage.png" },
375-
{ "width", 50 }
375+
{ "width", "50" }
376376
});
377377

378378
TagHelperOutput output = MakeImageTagHelperOutput(
379379
attributes: new TagHelperAttributeList
380380
{
381-
{ "width", 50 }
381+
{ "width", "50" }
382382
});
383383

384384
TagHelperOutput expectedOutput = MakeImageTagHelperOutput(
385385
attributes: new TagHelperAttributeList
386386
{
387387
{ "src", $"testimage.png?width=100&{ResizeWebProcessor.Compand}={bool.TrueString}" },
388-
{ "width", 50 }
388+
{ "width", "50" }
389389
});
390390

391391
ImageTagHelper helper = this.GetHelper();
@@ -417,20 +417,20 @@ public void RendersImageTag_SrcIncludes_ResizeOrient()
417417
attributes: new TagHelperAttributeList
418418
{
419419
{ "src", "testimage.png" },
420-
{ "width", 50 }
420+
{ "width", "50" }
421421
});
422422

423423
TagHelperOutput output = MakeImageTagHelperOutput(
424424
attributes: new TagHelperAttributeList
425425
{
426-
{ "width", 50 }
426+
{ "width", "50" }
427427
});
428428

429429
TagHelperOutput expectedOutput = MakeImageTagHelperOutput(
430430
attributes: new TagHelperAttributeList
431431
{
432432
{ "src", $"testimage.png?width=100&{ResizeWebProcessor.Orient}={bool.TrueString}" },
433-
{ "width", 50 }
433+
{ "width", "50" }
434434
});
435435

436436
ImageTagHelper helper = this.GetHelper();
@@ -482,20 +482,20 @@ public void RendersImageTag_SrcIncludes_Resampler(ResamplerCommand resampler)
482482
attributes: new TagHelperAttributeList
483483
{
484484
{ "src", "testimage.png" },
485-
{ "width", 50 }
485+
{ "width", "50" }
486486
});
487487

488488
TagHelperOutput output = MakeImageTagHelperOutput(
489489
attributes: new TagHelperAttributeList
490490
{
491-
{ "width", 50 }
491+
{ "width", "50" }
492492
});
493493

494494
TagHelperOutput expectedOutput = MakeImageTagHelperOutput(
495495
attributes: new TagHelperAttributeList
496496
{
497497
{ "src", $"testimage.png?width=100&{ResizeWebProcessor.Sampler}={resampler.Name}" },
498-
{ "width", 50 }
498+
{ "width", "50" }
499499
});
500500

501501
ImageTagHelper helper = this.GetHelper();
@@ -527,20 +527,20 @@ public void RendersImageTag_SrcIncludes_AutoOrient()
527527
attributes: new TagHelperAttributeList
528528
{
529529
{ "src", "testimage.png" },
530-
{ "width", 50 }
530+
{ "width", "50" }
531531
});
532532

533533
TagHelperOutput output = MakeImageTagHelperOutput(
534534
attributes: new TagHelperAttributeList
535535
{
536-
{ "width", 50 }
536+
{ "width", "50" }
537537
});
538538

539539
TagHelperOutput expectedOutput = MakeImageTagHelperOutput(
540540
attributes: new TagHelperAttributeList
541541
{
542542
{ "src", $"testimage.png?{AutoOrientWebProcessor.AutoOrient}={bool.TrueString}" },
543-
{ "width", 50 }
543+
{ "width", "50" }
544544
});
545545

546546
ImageTagHelper helper = this.GetHelper();
@@ -582,20 +582,20 @@ public void RendersImageTag_SrcIncludes_Format(FormatCommand format)
582582
attributes: new TagHelperAttributeList
583583
{
584584
{ "src", "testimage.png" },
585-
{ "width", 50 }
585+
{ "width", "50" }
586586
});
587587

588588
TagHelperOutput output = MakeImageTagHelperOutput(
589589
attributes: new TagHelperAttributeList
590590
{
591-
{ "width", 50 }
591+
{ "width", "50" }
592592
});
593593

594594
TagHelperOutput expectedOutput = MakeImageTagHelperOutput(
595595
attributes: new TagHelperAttributeList
596596
{
597597
{ "src", $"testimage.png?{FormatWebProcessor.Format}={format.Name}" },
598-
{ "width", 50 }
598+
{ "width", "50" }
599599
});
600600

601601
ImageTagHelper helper = this.GetHelper();
@@ -626,20 +626,20 @@ public void RendersImageTag_SrcIncludes_BackgroundColor()
626626
attributes: new TagHelperAttributeList
627627
{
628628
{ "src", "testimage.png" },
629-
{ "width", 50 }
629+
{ "width", "50" }
630630
});
631631

632632
TagHelperOutput output = MakeImageTagHelperOutput(
633633
attributes: new TagHelperAttributeList
634634
{
635-
{ "width", 50 }
635+
{ "width", "50" }
636636
});
637637

638638
TagHelperOutput expectedOutput = MakeImageTagHelperOutput(
639639
attributes: new TagHelperAttributeList
640640
{
641641
{ "src", $"testimage.png?{BackgroundColorWebProcessor.Color}={Color.Red.ToHex()}" },
642-
{ "width", 50 }
642+
{ "width", "50" }
643643
});
644644

645645
ImageTagHelper helper = this.GetHelper();
@@ -670,20 +670,20 @@ public void RendersImageTag_SrcIncludes_Quality()
670670
attributes: new TagHelperAttributeList
671671
{
672672
{ "src", "testimage.png" },
673-
{ "width", 50 }
673+
{ "width", "50" }
674674
});
675675

676676
TagHelperOutput output = MakeImageTagHelperOutput(
677677
attributes: new TagHelperAttributeList
678678
{
679-
{ "width", 50 }
679+
{ "width", "50" }
680680
});
681681

682682
TagHelperOutput expectedOutput = MakeImageTagHelperOutput(
683683
attributes: new TagHelperAttributeList
684684
{
685685
{ "src", $"testimage.png?{QualityWebProcessor.Quality}={42}" },
686-
{ "width", 50 }
686+
{ "width", "50" }
687687
});
688688

689689
ImageTagHelper helper = this.GetHelper();
@@ -714,21 +714,21 @@ public void RendersImageTag_SrcIncludes_HMAC()
714714
attributes: new TagHelperAttributeList
715715
{
716716
{ "src", "testimage.png" },
717-
{ "width", 50 },
717+
{ "width", "50" },
718718
{ "imagesharp-hmac", true }
719719
});
720720

721721
TagHelperOutput output = MakeImageTagHelperOutput(
722722
attributes: new TagHelperAttributeList
723723
{
724-
{ "width", 50 }
724+
{ "width", "50" }
725725
});
726726

727727
TagHelperOutput expectedOutput = MakeImageTagHelperOutput(
728728
attributes: new TagHelperAttributeList
729729
{
730730
{ "src", "testimage.png?hmac=ef09cd10210025e6d588fc2c12b91bf609c3e45f6d5d05dd650b8e6746cba288" },
731-
{ "width", 50 }
731+
{ "width", "50" }
732732
});
733733

734734
ImageTagHelper helper = this.GetHelper(hmac: true);

0 commit comments

Comments
 (0)