From 89412e370e90942c7852528b7ec385df4199b058 Mon Sep 17 00:00:00 2001 From: Admin Date: Sun, 5 Aug 2018 00:29:38 -0500 Subject: [PATCH 1/4] Adds validation for invalid reference names like foo-bar. --- src/validate/html/validateComponent.ts | 2 ++ src/validate/html/validateElement.ts | 20 +++++++++++++++++-- .../reference-must-be-valid-name/errors.json | 15 ++++++++++++++ .../reference-must-be-valid-name/input.html | 8 ++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 test/validator/samples/reference-must-be-valid-name/errors.json create mode 100644 test/validator/samples/reference-must-be-valid-name/input.html diff --git a/src/validate/html/validateComponent.ts b/src/validate/html/validateComponent.ts index e5cab392a6..48df1ec9d7 100644 --- a/src/validate/html/validateComponent.ts +++ b/src/validate/html/validateComponent.ts @@ -22,6 +22,8 @@ export default function validateComponent( node.attributes.forEach((attribute: Node) => { if (attribute.type === 'Ref') { + if (attribute.name.includes('-')) + console.dir(attribute); if (!refs.has(attribute.name)) refs.set(attribute.name, []); refs.get(attribute.name).push(node); } diff --git a/src/validate/html/validateElement.ts b/src/validate/html/validateElement.ts index a05214c6a5..e7edfae0ea 100644 --- a/src/validate/html/validateElement.ts +++ b/src/validate/html/validateElement.ts @@ -4,6 +4,7 @@ import validate, { Validator } from '../index'; import { Node } from '../../interfaces'; import { dimensions } from '../../utils/patterns'; import isVoidElementName from '../../utils/isVoidElementName'; +import isValidIdentifier from '../../utils/isValidIdentifier'; const svg = /^(?:altGlyph|altGlyphDef|altGlyphItem|animate|animateColor|animateMotion|animateTransform|circle|clipPath|color-profile|cursor|defs|desc|discard|ellipse|feBlend|feColorMatrix|feComponentTransfer|feComposite|feConvolveMatrix|feDiffuseLighting|feDisplacementMap|feDistantLight|feDropShadow|feFlood|feFuncA|feFuncB|feFuncG|feFuncR|feGaussianBlur|feImage|feMerge|feMergeNode|feMorphology|feOffset|fePointLight|feSpecularLighting|feSpotLight|feTile|feTurbulence|filter|font|font-face|font-face-format|font-face-name|font-face-src|font-face-uri|foreignObject|g|glyph|glyphRef|hatch|hatchpath|hkern|image|line|linearGradient|marker|mask|mesh|meshgradient|meshpatch|meshrow|metadata|missing-glyph|mpath|path|pattern|polygon|polyline|radialGradient|rect|set|solidcolor|stop|switch|symbol|text|textPath|tref|tspan|unknown|use|view|vkern)$/; @@ -82,8 +83,23 @@ export default function validateElement( node.attributes.forEach((attribute: Node) => { if (attribute.type === 'Ref') { - if (!refs.has(attribute.name)) refs.set(attribute.name, []); - refs.get(attribute.name).push(node); + // console.dir(isValidIdentifier(attribute.name)); + if (!isValidIdentifier(attribute.name)) { + const suggestion = attribute.name.replace(/[^_$a-z0-9]/ig, '_').replace(/^\d/, '_$&'); + + const key = { + start: attribute.start, + end: attribute.end + }; + + validator.error(key, { + code: `invalid-reference-name`, + message: `Reference name '${attribute.name}' is invalid — must be a valid identifier such as ${suggestion}` + }); + } else { + if (!refs.has(attribute.name)) refs.set(attribute.name, []); + refs.get(attribute.name).push(node); + } } if (attribute.type === 'Binding') { diff --git a/test/validator/samples/reference-must-be-valid-name/errors.json b/test/validator/samples/reference-must-be-valid-name/errors.json new file mode 100644 index 0000000000..eb1c92d6d2 --- /dev/null +++ b/test/validator/samples/reference-must-be-valid-name/errors.json @@ -0,0 +1,15 @@ +[{ + "message": "Reference name 'foo-bar' is invalid — must be a valid identifier such as foo_bar", + "start": { + "line": 1, + "column": 5, + "character": 5 + }, + "end": { + "line": 1, + "column": 16, + "character": 16 + }, + "pos": 5, + "code": "invalid-reference-name" +}] diff --git a/test/validator/samples/reference-must-be-valid-name/input.html b/test/validator/samples/reference-must-be-valid-name/input.html new file mode 100644 index 0000000000..a995bd8e6e --- /dev/null +++ b/test/validator/samples/reference-must-be-valid-name/input.html @@ -0,0 +1,8 @@ +
+
+ + From 7df6de62bbf5864b833c0d33e63dad52a59b3bc3 Mon Sep 17 00:00:00 2001 From: Admin Date: Sun, 5 Aug 2018 00:34:43 -0500 Subject: [PATCH 2/4] Adds validation check to checkComponent function and removes console.logs. --- src/validate/html/validateComponent.ts | 21 +++++++++++++++++---- src/validate/html/validateElement.ts | 1 - 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/validate/html/validateComponent.ts b/src/validate/html/validateComponent.ts index 48df1ec9d7..a6a2cdea3a 100644 --- a/src/validate/html/validateComponent.ts +++ b/src/validate/html/validateComponent.ts @@ -2,6 +2,7 @@ import * as namespaces from '../../utils/namespaces'; import validateEventHandler from './validateEventHandler'; import validate, { Validator } from '../index'; import { Node } from '../../interfaces'; +import isValidIdentifier from '../../utils/isValidIdentifier'; export default function validateComponent( validator: Validator, @@ -22,10 +23,22 @@ export default function validateComponent( node.attributes.forEach((attribute: Node) => { if (attribute.type === 'Ref') { - if (attribute.name.includes('-')) - console.dir(attribute); - if (!refs.has(attribute.name)) refs.set(attribute.name, []); - refs.get(attribute.name).push(node); + if (!isValidIdentifier(attribute.name)) { + const suggestion = attribute.name.replace(/[^_$a-z0-9]/ig, '_').replace(/^\d/, '_$&'); + + const key = { + start: attribute.start, + end: attribute.end + }; + + validator.error(key, { + code: `invalid-reference-name`, + message: `Reference name '${attribute.name}' is invalid — must be a valid identifier such as ${suggestion}` + }); + } else { + if (!refs.has(attribute.name)) refs.set(attribute.name, []); + refs.get(attribute.name).push(node); + } } if (attribute.type === 'EventHandler') { diff --git a/src/validate/html/validateElement.ts b/src/validate/html/validateElement.ts index e7edfae0ea..8d5e1bc1ba 100644 --- a/src/validate/html/validateElement.ts +++ b/src/validate/html/validateElement.ts @@ -83,7 +83,6 @@ export default function validateElement( node.attributes.forEach((attribute: Node) => { if (attribute.type === 'Ref') { - // console.dir(isValidIdentifier(attribute.name)); if (!isValidIdentifier(attribute.name)) { const suggestion = attribute.name.replace(/[^_$a-z0-9]/ig, '_').replace(/^\d/, '_$&'); From e487931a7cf3d0e307209cb96b7278b25d2d4d6e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 5 Aug 2018 11:05:44 -0400 Subject: [PATCH 3/4] pass `attribute` straight to `validator.error` --- src/validate/html/validateComponent.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/validate/html/validateComponent.ts b/src/validate/html/validateComponent.ts index a6a2cdea3a..77d9cc275f 100644 --- a/src/validate/html/validateComponent.ts +++ b/src/validate/html/validateComponent.ts @@ -26,12 +26,7 @@ export default function validateComponent( if (!isValidIdentifier(attribute.name)) { const suggestion = attribute.name.replace(/[^_$a-z0-9]/ig, '_').replace(/^\d/, '_$&'); - const key = { - start: attribute.start, - end: attribute.end - }; - - validator.error(key, { + validator.error(attribute, { code: `invalid-reference-name`, message: `Reference name '${attribute.name}' is invalid — must be a valid identifier such as ${suggestion}` }); @@ -56,4 +51,4 @@ export default function validateComponent( }); } }); -} \ No newline at end of file +} From ad91f55ac9661b43f670c256bfe7697110de863a Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 5 Aug 2018 11:08:50 -0400 Subject: [PATCH 4/4] pass `attribute` straight to `validator.error` --- src/validate/html/validateElement.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/validate/html/validateElement.ts b/src/validate/html/validateElement.ts index 8d5e1bc1ba..3d2d96f380 100644 --- a/src/validate/html/validateElement.ts +++ b/src/validate/html/validateElement.ts @@ -86,12 +86,7 @@ export default function validateElement( if (!isValidIdentifier(attribute.name)) { const suggestion = attribute.name.replace(/[^_$a-z0-9]/ig, '_').replace(/^\d/, '_$&'); - const key = { - start: attribute.start, - end: attribute.end - }; - - validator.error(key, { + validator.error(attribute, { code: `invalid-reference-name`, message: `Reference name '${attribute.name}' is invalid — must be a valid identifier such as ${suggestion}` });