Skip to content

Commit 38d7ace

Browse files
benleshjasonaden
authored andcommitted
refactor(ivy): add new ɵɵattribute instruction (angular#30503)
- adds the ɵɵattribute instruction - adds compilation handling for Δattribute instruction - updates tests PR Close angular#30503
1 parent 10f4827 commit 38d7ace

File tree

10 files changed

+657
-634
lines changed

10 files changed

+657
-634
lines changed

packages/compiler-cli/test/compliance/r3_view_compiler_styling_spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ describe('compiler compliance: styling', () => {
547547
$r3$.ɵɵstyleProp(0, $ctx$.myWidth);
548548
$r3$.ɵɵstyleProp(1, $ctx$.myHeight);
549549
$r3$.ɵɵstylingApply();
550-
$r3$.ɵɵelementAttribute(0, "style", $r3$.ɵɵbind("border-width: 10px"), $r3$.ɵɵsanitizeStyle);
550+
$r3$.ɵɵattribute("style", "border-width: 10px", $r3$.ɵɵsanitizeStyle);
551551
}
552552
},
553553
encapsulation: 2
@@ -772,7 +772,7 @@ describe('compiler compliance: styling', () => {
772772
$r3$.ɵɵclassProp(0, $ctx$.yesToApple);
773773
$r3$.ɵɵclassProp(1, $ctx$.yesToOrange);
774774
$r3$.ɵɵstylingApply();
775-
$r3$.ɵɵelementAttribute(0, "class", $r3$.ɵɵbind("banana"));
775+
$r3$.ɵɵattribute("class", "banana");
776776
}
777777
},
778778
encapsulation: 2
@@ -822,8 +822,8 @@ describe('compiler compliance: styling', () => {
822822
}
823823
if (rf & 2) {
824824
$r3$.ɵɵselect(0);
825-
$r3$.ɵɵelementAttribute(0, "class", $r3$.ɵɵbind("round"));
826-
$r3$.ɵɵelementAttribute(0, "style", $r3$.ɵɵbind("height:100px"), $r3$.ɵɵsanitizeStyle);
825+
$r3$.ɵɵattribute("class", "round");
826+
$r3$.ɵɵattribute("style", "height:100px", $r3$.ɵɵsanitizeStyle);
827827
}
828828
},
829829
encapsulation: 2

packages/compiler/src/render3/r3_identifiers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export class Identifiers {
4141

4242
static elementAttribute: o.ExternalReference = {name: 'ɵɵelementAttribute', moduleName: CORE};
4343

44+
static attribute: o.ExternalReference = {name: 'ɵɵattribute', moduleName: CORE};
45+
4446
static classProp: o.ExternalReference = {name: 'ɵɵclassProp', moduleName: CORE};
4547

4648
static elementContainerStart:

packages/compiler/src/render3/view/template.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
753753

754754
if (inputType === BindingType.Property) {
755755
if (value instanceof Interpolation) {
756+
// prop="{{value}}" and friends
756757
this.updateInstruction(
757758
elementIndex, input.sourceSpan, getPropertyInterpolationExpression(value),
758759
() =>
@@ -761,23 +762,33 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
761762
...params]);
762763

763764
} else {
764-
// Bound, un-interpolated properties
765+
// [prop]="value"
765766
this.updateInstruction(elementIndex, input.sourceSpan, R3.property, () => {
766767
return [
767768
o.literal(attrName), this.convertPropertyBinding(implicit, value, true), ...params
768769
];
769770
});
770771
}
771-
} else {
772-
let instruction: any;
773-
774-
if (inputType === BindingType.Class) {
775-
instruction = R3.classProp;
772+
} else if (inputType === BindingType.Attribute) {
773+
if (value instanceof Interpolation) {
774+
// attr.name="{{value}}" and friends
775+
this.updateInstruction(elementIndex, input.sourceSpan, R3.elementAttribute, () => {
776+
return [
777+
o.literal(elementIndex), o.literal(attrName),
778+
this.convertPropertyBinding(implicit, value), ...params
779+
];
780+
});
776781
} else {
777-
instruction = R3.elementAttribute;
782+
// [attr.name]="value"
783+
this.updateInstruction(elementIndex, input.sourceSpan, R3.attribute, () => {
784+
return [
785+
o.literal(attrName), this.convertPropertyBinding(implicit, value, true), ...params
786+
];
787+
});
778788
}
779-
780-
this.updateInstruction(elementIndex, input.sourceSpan, instruction, () => {
789+
} else {
790+
// class prop
791+
this.updateInstruction(elementIndex, input.sourceSpan, R3.classProp, () => {
781792
return [
782793
o.literal(elementIndex), o.literal(attrName),
783794
this.convertPropertyBinding(implicit, value), ...params

packages/core/src/core_render3_private_export.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
// clang-format off
1010
export {
11+
ɵɵattribute,
1112
ɵɵdefineBase,
1213
ɵɵdefineComponent,
1314
ɵɵdefineDirective,

packages/core/src/render3/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export {
2121
markDirty,
2222
store,
2323
tick,
24+
2425
ɵɵallocHostVars,
26+
ɵɵattribute,
2527
ɵɵbind,
2628
ɵɵclassMap,
2729
ɵɵclassProp,

packages/core/src/render3/instructions/all.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Jira Issue = FW-1184
2727
*/
2828
export * from './alloc_host_vars';
29+
export * from './attribute';
2930
export * from './change_detection';
3031
export * from './container';
3132
export * from './storage';
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {SanitizerFn} from '../interfaces/sanitization';
9+
import {getSelectedIndex} from '../state';
10+
11+
import {ΔelementAttribute} from './element';
12+
import {Δbind} from './property';
13+
14+
/**
15+
* Updates the value of or removes a bound attribute on an Element.
16+
*
17+
* Used in the case of `[attr.title]="value"`
18+
*
19+
* @param name name The name of the attribute.
20+
* @param value value The attribute is removed when value is `null` or `undefined`.
21+
* Otherwise the attribute value is set to the stringified value.
22+
* @param sanitizer An optional function used to sanitize the value.
23+
* @param namespace Optional namespace to use when setting the attribute.
24+
*
25+
* @codeGenApi
26+
*/
27+
export function Δattribute(
28+
name: string, value: any, sanitizer?: SanitizerFn | null, namespace?: string) {
29+
const index = getSelectedIndex();
30+
return ΔelementAttribute(index, name, Δbind(value), sanitizer, namespace);
31+
}

packages/core/src/render3/instructions/element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export function ɵɵelement(
198198

199199

200200
/**
201-
* Updates the value of removes an attribute on an Element.
201+
* Updates the value or removes an attribute on an Element.
202202
*
203203
* @param number index The index of the element in the data array
204204
* @param name name The name of the attribute.

packages/core/src/render3/jit/environment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as r3 from '../index';
2020
*/
2121
export const angularCoreEnv: {[name: string]: Function} =
2222
(() => ({
23+
'ɵɵattribute': r3.ɵɵattribute,
2324
'ɵɵdefineBase': r3.ɵɵdefineBase,
2425
'ɵɵdefineComponent': r3.ɵɵdefineComponent,
2526
'ɵɵdefineDirective': r3.ɵɵdefineDirective,

0 commit comments

Comments
 (0)