Fix backtick string literals not being recognized for the svg property

Failing test for #1284
Fixes #1284
pull/1285/head
Josh Duff 7 years ago
parent b506e5a596
commit e81fb88f41

@ -9,6 +9,7 @@ import flattenReference from '../utils/flattenReference';
import reservedNames from '../utils/reservedNames'; import reservedNames from '../utils/reservedNames';
import namespaces from '../utils/namespaces'; import namespaces from '../utils/namespaces';
import { removeNode, removeObjectKey } from '../utils/removeNode'; import { removeNode, removeObjectKey } from '../utils/removeNode';
import nodeToString from '../utils/nodeToString';
import wrapModule from './wrapModule'; import wrapModule from './wrapModule';
import annotateWithScopes, { Scope } from '../utils/annotateWithScopes'; import annotateWithScopes, { Scope } from '../utils/annotateWithScopes';
import getName from '../utils/getName'; import getName from '../utils/getName';
@ -599,7 +600,7 @@ export default class Generator {
} }
if (templateProperties.namespace) { if (templateProperties.namespace) {
const ns = templateProperties.namespace.value.value; const ns = nodeToString(templateProperties.namespace);
this.namespace = namespaces[ns] || ns; this.namespace = namespaces[ns] || ns;
} }

@ -0,0 +1,11 @@
import { Node } from '../interfaces';
export default function nodeToString(prop: Node) {
if (prop.value.type === 'Literal' && typeof prop.value.value === 'string') {
return prop.value.value;
} else if (prop.value.type === 'TemplateLiteral'
&& prop.value.quasis.length === 1
&& prop.value.expressions.length === 0) {
return prop.value.quasis[0].value.raw;
}
}

@ -3,6 +3,7 @@ import fuzzymatch from '../utils/fuzzymatch';
import checkForDupes from './utils/checkForDupes'; import checkForDupes from './utils/checkForDupes';
import checkForComputedKeys from './utils/checkForComputedKeys'; import checkForComputedKeys from './utils/checkForComputedKeys';
import namespaces from '../../utils/namespaces'; import namespaces from '../../utils/namespaces';
import nodeToString from '../../utils/nodeToString';
import getName from '../../utils/getName'; import getName from '../../utils/getName';
import { Validator } from '../'; import { Validator } from '../';
import { Node } from '../../interfaces'; import { Node } from '../../interfaces';
@ -77,7 +78,7 @@ export default function validateJs(validator: Validator, js: Node) {
}); });
if (props.has('namespace')) { if (props.has('namespace')) {
const ns = props.get('namespace').value.value; const ns = nodeToString(props.get('namespace'));
validator.namespace = namespaces[ns] || ns; validator.namespace = namespaces[ns] || ns;
} }

@ -1,4 +1,5 @@
import * as namespaces from '../../../utils/namespaces'; import * as namespaces from '../../../utils/namespaces';
import nodeToString from '../../../utils/nodeToString'
import fuzzymatch from '../../utils/fuzzymatch'; import fuzzymatch from '../../utils/fuzzymatch';
import { Validator } from '../../'; import { Validator } from '../../';
import { Node } from '../../../interfaces'; import { Node } from '../../../interfaces';
@ -6,9 +7,9 @@ import { Node } from '../../../interfaces';
const valid = new Set(namespaces.validNamespaces); const valid = new Set(namespaces.validNamespaces);
export default function namespace(validator: Validator, prop: Node) { export default function namespace(validator: Validator, prop: Node) {
const ns = prop.value.value; const ns = nodeToString(prop);
if (prop.value.type !== 'Literal' || typeof ns !== 'string') { if (typeof ns !== 'string') {
validator.error( validator.error(
`The 'namespace' property must be a string literal representing a valid namespace`, `The 'namespace' property must be a string literal representing a valid namespace`,
prop prop

@ -0,0 +1,7 @@
<rect x='{{x}}' y='{{y}}' width='{{width}}' height='{{height}}'/>
<script>
export default {
namespace: `svg`
};
</script>

@ -0,0 +1,21 @@
export default {
data: {
x: 0,
y: 0,
width: 100,
height: 100
},
html: `<svg><rect x="0" y="0" width="100" height="100"></rect></svg>`,
test ( assert, component, target ) {
const svg = target.querySelector( 'svg' );
const rect = target.querySelector( 'rect' );
assert.equal( svg.namespaceURI, 'http://www.w3.org/2000/svg' );
assert.equal( rect.namespaceURI, 'http://www.w3.org/2000/svg' );
component.set({ width: 150, height: 50 });
assert.equal( target.innerHTML, `<svg><rect x="0" y="0" width="150" height="50"></rect></svg>` );
},
};

@ -0,0 +1,11 @@
<svg>
<Rect x='{{x}}' y='{{y}}' width='{{width}}' height='{{height}}'/>
</svg>
<script>
import Rect from './Rect.html';
export default {
components: { Rect }
};
</script>

After

Width:  |  Height:  |  Size: 179 B

Loading…
Cancel
Save