Skip to content

Commit e573f06

Browse files
fix(templateFactory): Do not prepend x- to attribute names unless necessary.
Closes #3424
1 parent 8a22d23 commit e573f06

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

src/templateFactory.ts

+32-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ng as angular } from "./angular";
44
import { IAugmentedJQuery } from "angular";
55
import {
66
isArray, isDefined, isFunction, isObject, services, Obj, IInjectable, tail, kebobString, unnestR, ResolveContext,
7-
Resolvable, RawParams, prop
7+
Resolvable, RawParams
88
} from "@uirouter/core";
99
import { Ng1ViewDeclaration, TemplateFactoryProvider } from "./interface";
1010

@@ -33,13 +33,13 @@ export class TemplateFactory implements TemplateFactoryProvider {
3333
* Creates a template from a configuration object.
3434
*
3535
* @param config Configuration object for which to load a template.
36-
* The following properties are search in the specified order, and the first one
36+
* The following properties are search in the specified order, and the first one
3737
* that is defined is used to create the template:
3838
*
3939
* @param params Parameters to pass to the template function.
4040
* @param context The resolve context associated with the template's view
4141
*
42-
* @return {string|object} The template html as a string, or a promise for
42+
* @return {string|object} The template html as a string, or a promise for
4343
* that string,or `null` if no template is configured.
4444
*/
4545
fromConfig(config: Ng1ViewDeclaration, params: any, context: ResolveContext) {
@@ -49,12 +49,12 @@ export class TemplateFactory implements TemplateFactoryProvider {
4949
const asComponent = (result) => services.$q.when(result).then(str => ({ component: str }));
5050

5151
return (
52-
isDefined(config.template) ? asTemplate(this.fromString(config.template, params)) :
53-
isDefined(config.templateUrl) ? asTemplate(this.fromUrl(config.templateUrl, params)) :
54-
isDefined(config.templateProvider) ? asTemplate(this.fromProvider(config.templateProvider, params, context)) :
55-
isDefined(config.component) ? asComponent(config.component) :
56-
isDefined(config.componentProvider) ? asComponent(this.fromComponentProvider(config.componentProvider, params, context)) :
57-
asTemplate(defaultTemplate)
52+
isDefined(config.template) ? asTemplate(this.fromString(config.template, params)) :
53+
isDefined(config.templateUrl) ? asTemplate(this.fromUrl(config.templateUrl, params)) :
54+
isDefined(config.templateProvider) ? asTemplate(this.fromProvider(config.templateProvider, params, context)) :
55+
isDefined(config.component) ? asComponent(config.component) :
56+
isDefined(config.componentProvider) ? asComponent(this.fromComponentProvider(config.componentProvider, params, context)) :
57+
asTemplate(defaultTemplate)
5858
);
5959
};
6060

@@ -64,29 +64,31 @@ export class TemplateFactory implements TemplateFactoryProvider {
6464
* @param template html template as a string or function that returns an html template as a string.
6565
* @param params Parameters to pass to the template function.
6666
*
67-
* @return {string|object} The template html as a string, or a promise for that
67+
* @return {string|object} The template html as a string, or a promise for that
6868
* string.
6969
*/
70-
fromString(template: (string|Function), params?: RawParams) {
70+
fromString(template: (string | Function), params?: RawParams) {
7171
return isFunction(template) ? (<any> template)(params) : template;
7272
};
7373

7474
/**
7575
* Loads a template from the a URL via `$http` and `$templateCache`.
7676
*
77-
* @param {string|Function} url url of the template to load, or a function
77+
* @param {string|Function} url url of the template to load, or a function
7878
* that returns a url.
7979
* @param {Object} params Parameters to pass to the url function.
80-
* @return {string|Promise.<string>} The template html as a string, or a promise
80+
* @return {string|Promise.<string>} The template html as a string, or a promise
8181
* for that string.
8282
*/
83-
fromUrl(url: (string|Function), params: any) {
83+
fromUrl(url: (string | Function), params: any) {
8484
if (isFunction(url)) url = (<any> url)(params);
8585
if (url == null) return null;
8686

8787
if (this._useHttp) {
88-
return this.$http.get(url, { cache: this.$templateCache, headers: { Accept: 'text/html' }})
89-
.then(function(response) { return response.data; });
88+
return this.$http.get(url, { cache: this.$templateCache, headers: { Accept: 'text/html' } })
89+
.then(function (response) {
90+
return response.data;
91+
});
9092
}
9193

9294
return this.$templateRequest(url);
@@ -97,7 +99,7 @@ export class TemplateFactory implements TemplateFactoryProvider {
9799
*
98100
* @param provider Function to invoke via `locals`
99101
* @param {Function} injectFn a function used to invoke the template provider
100-
* @return {string|Promise.<string>} The template html as a string, or a promise
102+
* @return {string|Promise.<string>} The template html as a string, or a promise
101103
* for that string.
102104
*/
103105
fromProvider(provider: IInjectable, params: any, context: ResolveContext) {
@@ -140,21 +142,27 @@ export class TemplateFactory implements TemplateFactoryProvider {
140142

141143
// Bind once prefix
142144
const prefix = angular.version.minor >= 3 ? "::" : "";
145+
// Convert to kebob name. Add x- prefix if the string starts with `x-` or `data-`
146+
const kebob = (camelCase: string) => {
147+
const kebobed = kebobString(camelCase);
148+
return /^(x|data)-/.exec(kebobed) ? `x-${kebobed}` : kebobed;
149+
};
150+
143151

144152
const attributeTpl = (input: BindingTuple) => {
145-
let {name, type } = input;
146-
let attrName = kebobString(name);
153+
let { name, type } = input;
154+
let attrName = kebob(name);
147155
// If the ui-view has an attribute which matches a binding on the routed component
148156
// then pass that attribute through to the routed component template.
149157
// Prefer ui-view wired mappings to resolve data, unless the resolve was explicitly bound using `bindings:`
150158
if (uiView.attr(attrName) && !bindings[name])
151-
return `x-${attrName}='${uiView.attr(attrName)}'`;
159+
return `${attrName}='${uiView.attr(attrName)}'`;
152160

153161
let resolveName = bindings[name] || name;
154162
// Pre-evaluate the expression for "@" bindings by enclosing in {{ }}
155163
// some-attr="{{ ::$resolve.someResolveName }}"
156164
if (type === '@')
157-
return `x-${attrName}='{{${prefix}$resolve.${resolveName}}}'`;
165+
return `${attrName}='{{${prefix}$resolve.${resolveName}}}'`;
158166

159167
// Wire "&" callbacks to resolves that return a callback function
160168
// Get the result of the resolve (should be a function) and annotate it to get its arguments.
@@ -165,18 +173,15 @@ export class TemplateFactory implements TemplateFactoryProvider {
165173
let args = fn && services.$injector.annotate(fn) || [];
166174
// account for array style injection, i.e., ['foo', function(foo) {}]
167175
let arrayIdxStr = isArray(fn) ? `[${fn.length - 1}]` : '';
168-
return `x-${attrName}='$resolve.${resolveName}${arrayIdxStr}(${args.join(",")})'`;
176+
return `${attrName}='$resolve.${resolveName}${arrayIdxStr}(${args.join(",")})'`;
169177
}
170178

171179
// some-attr="::$resolve.someResolveName"
172-
return `x-${attrName}='${prefix}$resolve.${resolveName}'`;
180+
return `${attrName}='${prefix}$resolve.${resolveName}'`;
173181
};
174182

175183
let attrs = getComponentBindings(component).map(attributeTpl).join(" ");
176-
let kebobName = kebobString(component);
177-
if (/^(x|data)-/.exec(kebobName)) {
178-
kebobName = "x-" + kebobName;
179-
}
184+
let kebobName = kebob(component);
180185
return `<${kebobName} ${attrs}></${kebobName}>`;
181186
};
182187
}

0 commit comments

Comments
 (0)