Skip to content

Commit

Permalink
another stab at #1062; also fix attribute case in static HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry committed Jan 24, 2018
1 parent c35eb67 commit 94da329
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
14 changes: 2 additions & 12 deletions src/generators/nodes/Attribute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import deindent from '../../utils/deindent';
import { stringify } from '../../utils/stringify';
import fixAttributeCasing from '../../utils/fixAttributeCasing';
import getExpressionPrecedence from '../../utils/getExpressionPrecedence';
import { DomGenerator } from '../dom/index';
import Node from './shared/Node';
Expand Down Expand Up @@ -43,7 +44,7 @@ export default class Attribute {

render(block: Block) {
const node = this.parent;
const name = fixCasing(this.name);
const name = fixAttributeCasing(this.name);

if (name === 'style') {
const styleProps = optimizeStyle(this.value);
Expand Down Expand Up @@ -663,14 +664,3 @@ 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();
}
3 changes: 2 additions & 1 deletion src/generators/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import flattenReference from '../../utils/flattenReference';
import isVoidElementName from '../../utils/isVoidElementName';
import validCalleeObjects from '../../utils/validCalleeObjects';
import reservedNames from '../../utils/reservedNames';
import fixAttributeCasing from '../../utils/fixAttributeCasing';
import Node from './shared/Node';
import Block from '../dom/Block';
import Attribute from './Attribute';
Expand Down Expand Up @@ -427,7 +428,7 @@ export default class Element extends Node {
}

node.attributes.forEach((attr: Node) => {
open += ` ${attr.name}${stringifyAttributeValue(attr.value)}`
open += ` ${fixAttributeCasing(attr.name)}${stringifyAttributeValue(attr.value)}`
});

if (isVoidElementName(node.name)) return open + '>';
Expand Down
12 changes: 12 additions & 0 deletions src/utils/fixAttributeCasing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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);
});

export default function fixAttributeCasing(name) {
name = name.toLowerCase();
return svgAttributeLookup.get(name) || name;
}
14 changes: 10 additions & 4 deletions test/runtime/samples/attribute-casing/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ export default {
html: `
<div class='SHOUTY'>YELL</div>
<svg viewBox='0 0 100 100'>
<svg viewBox='0 0 100 100' id='one'>
<text textLength=100>hellooooo</text>
</svg>
<svg viewBox='0 0 100 100' id='two'>
<text textLength=100>hellooooo</text>
</svg>
`,
Expand All @@ -11,7 +15,9 @@ export default {
const attr = sel => target.querySelector(sel).attributes[0].name;

assert.equal(attr('div'), 'class');
assert.equal(attr('svg'), 'viewBox');
assert.equal(attr('text'), 'textLength');
assert.equal(attr('svg#one'), 'viewBox');
assert.equal(attr('svg#one text'), 'textLength');
assert.equal(attr('svg#two'), 'viewBox');
assert.equal(attr('svg#two text'), 'textLength');
}
};
};
8 changes: 6 additions & 2 deletions test/runtime/samples/attribute-casing/main.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<div CLASS='SHOUTY'>YELL</div>

<svg viewbox='0 0 100 100'>
<svg viewbox='0 0 100 100' id='one'>
<text textlength=100>hellooooo</text>
</svg>
</svg>

<svg viewBox='0 0 100 100' id='two'>
<text textLength=100>hellooooo</text>
</svg>

0 comments on commit 94da329

Please sign in to comment.