Repository: sveltejs/svelte (JavaScript)
Date: 2018-01-11 03:34:18+00:00
Task type: Bug Fix

Issue:
case-sensitivity of SVG viewBox likely to cause silent errors
In most 'try online' tools, the SVG `viewBox` attribute is handled the same whether properly camel-cased or not (`viewbox`). Svelte only recognizes `viewBox`. This can make it difficult for developers new to SVG - especially when they are starting with random sample code from blogs/tutorials - as improper use simply doesn't show up. Either an compile-time warning or acceptance of both variants would be helpful.

Example:
When I try to render `<svg viewbox="-250 -250 500 500">` with the [Svelte](https://svelte.technology/repl?version=1.50.0&gist=3547a556f7fcdf284516a39d24fbff24), my image is cutoff. Whereas on W3Schools or [CodePen](https://codepen.io/anon/pen/opWRbY), it renders fine, indicating that `width` and `height` aren't inherently mandatory. I had the same results testing locally and with REPL.

When I try `<svg width=500 height=500 viewbox="-250 -250 500 500">` it will adjust the size correctly, but doesn't respect the viewbox.

Since I started with pasting a blog example, I mistakenly thought svelte couldn't handle viewbox. 


Issue discussion:
I take it from what's been said on the closed PR #1077 that what we'd want to do is lowercase all of the attribute names in HTML, and to lowercase SVG ones, apart from those with mixed case [here](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute) and to instead make those conform to the case in that list?

Solution patch:
diff --git a/src/generators/nodes/Attribute.ts b/src/generators/nodes/Attribute.ts
--- a/src/generators/nodes/Attribute.ts
+++ b/src/generators/nodes/Attribute.ts
@@ -43,7 +43,7 @@ export default class Attribute {
 
 	render(block: Block) {
 		const node = this.parent;
-		const name = this.name;
+		const name = fixCasing(this.name);
 
 		if (name === 'style') {
 			const styleProps = optimizeStyle(this.value);
@@ -662,4 +662,15 @@ function getStyleValue(chunks: Node[]) {
 
 function isDynamic(value: Node[]) {
 	return value.length > 1 || value[0].type !== 'Text';
+}
+
+const svgAttributes = 'accent-height accumulate additive alignment-baseline allowReorder alphabetic amplitude arabic-form ascent attributeName attributeType autoReverse azimuth baseFrequency baseline-shift baseProfile bbox begin bias by calcMode cap-height class clip clipPathUnits clip-path clip-rule color color-interpolation color-interpolation-filters color-profile color-rendering contentScriptType contentStyleType cursor cx cy d decelerate descent diffuseConstant direction display divisor dominant-baseline dur dx dy edgeMode elevation enable-background end exponent externalResourcesRequired fill fill-opacity fill-rule filter filterRes filterUnits flood-color flood-opacity font-family font-size font-size-adjust font-stretch font-style font-variant font-weight format from fr fx fy g1 g2 glyph-name glyph-orientation-horizontal glyph-orientation-vertical glyphRef gradientTransform gradientUnits hanging height href horiz-adv-x horiz-origin-x id ideographic image-rendering in in2 intercept k k1 k2 k3 k4 kernelMatrix kernelUnitLength kerning keyPoints keySplines keyTimes lang lengthAdjust letter-spacing lighting-color limitingConeAngle local marker-end marker-mid marker-start markerHeight markerUnits markerWidth mask maskContentUnits maskUnits mathematical max media method min mode name numOctaves offset onabort onactivate onbegin onclick onend onerror onfocusin onfocusout onload onmousedown onmousemove onmouseout onmouseover onmouseup onrepeat onresize onscroll onunload opacity operator order orient orientation origin overflow overline-position overline-thickness panose-1 paint-order pathLength patternContentUnits patternTransform patternUnits pointer-events points pointsAtX pointsAtY pointsAtZ preserveAlpha preserveAspectRatio primitiveUnits r radius refX refY rendering-intent repeatCount repeatDur requiredExtensions requiredFeatures restart result rotate rx ry scale seed shape-rendering slope spacing specularConstant specularExponent speed spreadMethod startOffset stdDeviation stemh stemv stitchTiles stop-color stop-opacity strikethrough-position strikethrough-thickness string stroke stroke-dasharray stroke-dashoffset stroke-linecap stroke-linejoin stroke-miterlimit stroke-opacity stroke-width style surfaceScale systemLanguage tabindex tableValues target targetX targetY text-anchor text-decoration text-rendering textLength to transform type u1 u2 underline-position underline-thickness unicode unicode-bidi unicode-range units-per-em v-alphabetic v-hanging v-ideographic v-mathematical values version vert-adv-y vert-origin-x vert-origin-y viewBox viewTarget visibility width widths word-spacing writing-mode x x-height x1 x2 xChannelSelector xlink:actuate xlink:arcrole xlink:href xlink:role xlink:show xlink:title xlink:type xml:base xml:lang xml:space y y1 y2 yChannelSelector z zoomAndPan'.split(' ');
+const svgAttributeLookup = new Map();
+svgAttributes.forEach(name => {
+	svgAttributeLookup.set(name.toLowerCase(), name);
+});
+
+function fixCasing(name) {
+	if (svgAttributeLookup.has(name)) return svgAttributeLookup.get(name);
+	return name.toLowerCase();
 }
\ No newline at end of file


Test patch:
diff --git a/test/runtime/samples/attribute-casing/_config.js b/test/runtime/samples/attribute-casing/_config.js
new file mode 100644
--- /dev/null
+++ b/test/runtime/samples/attribute-casing/_config.js
@@ -0,0 +1,17 @@
+export default {
+	html: `
+		<div class='SHOUTY'>YELL</div>
+
+		<svg viewBox='0 0 100 100'>
+			<text textLength=100>hellooooo</text>
+		</svg>
+	`,
+
+	test(assert, component, target) {
+		const attr = sel => target.querySelector(sel).attributes[0].name;
+
+		assert.equal(attr('div'), 'class');
+		assert.equal(attr('svg'), 'viewBox');
+		assert.equal(attr('text'), 'textLength');
+	}
+};
\ No newline at end of file
diff --git a/test/runtime/samples/attribute-casing/main.html b/test/runtime/samples/attribute-casing/main.html
new file mode 100644
--- /dev/null
+++ b/test/runtime/samples/attribute-casing/main.html
@@ -0,0 +1,5 @@
+<div CLASS='SHOUTY'>YELL</div>
+
+<svg viewbox='0 0 100 100'>
+	<text textlength=100>hellooooo</text>
+</svg>
\ No newline at end of file