From a7528ebea471a83e532128f08309960aa36bbf86 Mon Sep 17 00:00:00 2001 From: Ika Date: Mon, 26 Nov 2018 12:09:19 +0800 Subject: [PATCH] fix(ng,vue): do not normalize attribute names (#5549) --- src/language-html/parser-html.js | 44 +++++++------ .../__snapshots__/jsfmt.spec.js.snap | 61 +++++++++++++++++++ tests/html_angular/attr-name.component.html | 1 + 3 files changed, 89 insertions(+), 17 deletions(-) create mode 100644 tests/html_angular/attr-name.component.html diff --git a/src/language-html/parser-html.js b/src/language-html/parser-html.js index 57a8697e0351..ca88e3e4d670 100644 --- a/src/language-html/parser-html.js +++ b/src/language-html/parser-html.js @@ -7,7 +7,10 @@ const createError = require("../common/parser-create-error"); const { Node } = require("./ast"); const { parseIeConditionalComment } = require("./conditional-comment"); -function ngHtmlParser(input, { recognizeSelfClosing, normalizeTagName }) { +function ngHtmlParser( + input, + { recognizeSelfClosing, normalizeTagName, normalizeAttributeName } +) { const parser = require("angular-html-parser"); const { RecursiveVisitor, @@ -109,19 +112,21 @@ function ngHtmlParser(input, { recognizeSelfClosing, normalizeTagName }) { ); } - const CURRENT_HTML_ELEMENT_ATTRIBUTES = - HTML_ELEMENT_ATTRIBUTES[node.name] || Object.create(null); - node.attrs.forEach(attr => { - if (!attr.namespace) { - attr.name = lowerCaseIfFn( - attr.name, - lowerCasedAttrName => - node.name in HTML_ELEMENT_ATTRIBUTES && - (lowerCasedAttrName in HTML_ELEMENT_ATTRIBUTES["*"] || - lowerCasedAttrName in CURRENT_HTML_ELEMENT_ATTRIBUTES) - ); - } - }); + if (normalizeAttributeName) { + const CURRENT_HTML_ELEMENT_ATTRIBUTES = + HTML_ELEMENT_ATTRIBUTES[node.name] || Object.create(null); + node.attrs.forEach(attr => { + if (!attr.namespace) { + attr.name = lowerCaseIfFn( + attr.name, + lowerCasedAttrName => + node.name in HTML_ELEMENT_ATTRIBUTES && + (lowerCasedAttrName in HTML_ELEMENT_ATTRIBUTES["*"] || + lowerCasedAttrName in CURRENT_HTML_ELEMENT_ATTRIBUTES) + ); + } + }); + } } }; @@ -249,14 +254,16 @@ function locEnd(node) { function createParser({ recognizeSelfClosing = false, - normalizeTagName = false + normalizeTagName = false, + normalizeAttributeName = false } = {}) { return { preprocess: text => text.replace(/\r\n?/g, "\n"), parse: (text, parsers, options) => _parse(text, options, { recognizeSelfClosing, - normalizeTagName + normalizeTagName, + normalizeAttributeName }), hasPragma, astFormat: "html", @@ -267,7 +274,10 @@ function createParser({ module.exports = { parsers: { - html: createParser({ normalizeTagName: true }), + html: createParser({ + normalizeTagName: true, + normalizeAttributeName: true + }), angular: createParser(), vue: createParser({ recognizeSelfClosing: true }) } diff --git a/tests/html_angular/__snapshots__/jsfmt.spec.js.snap b/tests/html_angular/__snapshots__/jsfmt.spec.js.snap index 420ac5342181..5f08ee7f9821 100644 --- a/tests/html_angular/__snapshots__/jsfmt.spec.js.snap +++ b/tests/html_angular/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,66 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`attr-name.component.html 1`] = ` +====================================options===================================== +parsers: ["angular"] +printWidth: 80 + | printWidth +=====================================input====================================== +
+ +=====================================output===================================== +
+ +================================================================================ +`; + +exports[`attr-name.component.html 2`] = ` +====================================options===================================== +parsers: ["angular"] +printWidth: 80 +trailingComma: "es5" + | printWidth +=====================================input====================================== +
+ +=====================================output===================================== +
+ +================================================================================ +`; + +exports[`attr-name.component.html 3`] = ` +====================================options===================================== +parsers: ["angular"] +printWidth: 1 + | printWidth +=====================================input====================================== +
+ +=====================================output===================================== +
+ +================================================================================ +`; + +exports[`attr-name.component.html 4`] = ` +====================================options===================================== +htmlWhitespaceSensitivity: "ignore" +parsers: ["angular"] +printWidth: 80 + | printWidth +=====================================input====================================== +
+ +=====================================output===================================== +
+ +================================================================================ +`; + exports[`attributes.component.html 1`] = ` ====================================options===================================== parsers: ["angular"] diff --git a/tests/html_angular/attr-name.component.html b/tests/html_angular/attr-name.component.html new file mode 100644 index 000000000000..18c4a3782284 --- /dev/null +++ b/tests/html_angular/attr-name.component.html @@ -0,0 +1 @@ +