Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
refactor(*): Move setAttr[NS]/createAttr to dom_helpers, refactor a bit.
Browse files Browse the repository at this point in the history
* `<AppView>.setAttr` -> `updateAttribute`
* `<AppView>.setAttrNS` -> `updateAttributeNS`
* `<AppView>.createAttr` -> `setAttribute`

Additionally, we no longer use `<Element>.attributes.remove(key)`, but instead use `@JS()`-interop to directly call `<Element>.removeAttribute` (pending dart-lang/sdk#35655).

PiperOrigin-RevId: 229275285
  • Loading branch information
matanlurey authored and alorenzen committed Jan 17, 2019
1 parent d93d5ba commit cbd0b5c
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 155 deletions.
113 changes: 34 additions & 79 deletions _goldens/test/_files/dart2js/dart2js_golden.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2714,7 +2714,7 @@
_.$ti = t2;
},
_LinkedHashSetCell: function _LinkedHashSetCell(t0) {
this._collection$_element = t0;
this._element = t0;
this._collection$_previous = this._collection$_next = null;
},
_LinkedHashSetIterator: function _LinkedHashSetIterator(t0, t1) {
Expand Down Expand Up @@ -3411,11 +3411,6 @@
},
_StyleSheetList: function _StyleSheetList() {
},
_AttributeMap: function _AttributeMap() {
},
_ElementAttributeMap: function _ElementAttributeMap(t0) {
this._element = t0;
},
_EventStreamSubscription: function _EventStreamSubscription(t0, t1, t2, t3, t4) {
var _ = this;
_._pauseCount = t0;
Expand Down Expand Up @@ -3742,18 +3737,6 @@
this.parent = t0;
},
DomSanitizationServiceImpl: function DomSanitizationServiceImpl() {
},
updateClassBinding: function(element, className, isAdd) {
if (isAdd)
element.classList.add(className);
else
element.classList.remove(className);
},
updateClassBindingNonHtml: function(element, className, isAdd) {
if (isAdd)
element.classList.add(className);
else
element.classList.remove(className);
}
},
K = {NgIf: function NgIf(t0, t1, t2) {
Expand Down Expand Up @@ -3981,7 +3964,30 @@
}, JsTestabilityRegistry: function JsTestabilityRegistry() {
}},
T = {BrowserExceptionHandler: function BrowserExceptionHandler() {
}},
},
updateClassBinding: function(element, className, isAdd) {
if (isAdd)
element.classList.add(className);
else
element.classList.remove(className);
},
updateClassBindingNonHtml: function(element, className, isAdd) {
if (isAdd)
element.classList.add(className);
else
element.classList.remove(className);
},
updateAttribute: function(element, attribute, value) {
if (value == null)
element.removeAttribute.apply(element, [attribute]);
else
T.setAttribute(element, attribute, value);
$.domRootRendererIsDirty = true;
},
setAttribute: function(element, attribute, value) {
element.setAttribute(attribute, value);
}
},
N = {
EventManager$: function(_plugins, zone) {
var t1 = new N.EventManager(zone, _plugins, P.LinkedHashMap_LinkedHashMap$_empty(P.String, N.EventManagerPlugin));
Expand Down Expand Up @@ -6781,7 +6787,7 @@
return -1;
$length = bucket.length;
for (i = 0; i < $length; ++i)
if (J.$eq$(bucket[i]._collection$_element, element))
if (J.$eq$(bucket[i]._element, element))
return i;
return -1;
}
Expand All @@ -6796,7 +6802,7 @@
return -1;
$length = bucket.length;
for (i = 0; i < $length; ++i) {
t1 = bucket[i]._collection$_element;
t1 = bucket[i]._element;
if (t1 == null ? element == null : t1 === element)
return i;
}
Expand All @@ -6818,7 +6824,7 @@
this._collection$_current = null;
return false;
} else {
this._collection$_current = t1._collection$_element;
this._collection$_current = t1._element;
this._collection$_cell = t1._collection$_next;
return true;
}
Expand Down Expand Up @@ -8438,47 +8444,6 @@
return [W.StyleSheet];
}
};
W._AttributeMap.prototype = {
forEach$1: function(_, f) {
var t1, t2, t3, _i, key;
for (t1 = this.get$keys(this), t2 = t1.length, t3 = this._element, _i = 0; _i < t1.length; t1.length === t2 || (0, H.throwConcurrentModificationError)(t1), ++_i) {
key = t1[_i];
f.call$2(key, t3.getAttribute(key));
}
},
get$keys: function(_) {
var attributes, keys, len, i, attr;
attributes = this._element.attributes;
keys = H.setRuntimeTypeInfo([], [P.String]);
for (len = attributes.length, i = 0; i < len; ++i) {
attr = attributes[i];
if (attr.namespaceURI == null)
keys.push(attr.name);
}
return keys;
},
$asMapMixin: function() {
return [P.String, P.String];
},
$asMap: function() {
return [P.String, P.String];
}
};
W._ElementAttributeMap.prototype = {
$index: function(_, key) {
return this._element.getAttribute(key);
},
remove$1: function(_, key) {
var t1, value;
t1 = this._element;
value = t1.getAttribute(key);
t1.removeAttribute(key);
return value;
},
get$length: function(_) {
return this.get$keys(this).length;
}
};
W._EventStreamSubscription.prototype = {};
W._EventStreamSubscription_closure.prototype = {
call$1: function(e) {
Expand Down Expand Up @@ -10159,15 +10124,6 @@
if (t1 != null)
hostElement.classList.add(t1);
return hostElement;
},
setAttr$3: function(renderElement, attributeName, attributeValue) {
if (attributeValue != null)
renderElement.setAttribute(attributeName, attributeValue);
else {
renderElement.toString;
new W._ElementAttributeMap(renderElement).remove$1(0, attributeName);
}
$.domRootRendererIsDirty = true;
}
};
X.View.prototype = {
Expand Down Expand Up @@ -10989,13 +10945,13 @@
currVal_2 = J.get$title$z(t1.ctx);
t2 = t1._expr_2;
if (t2 != currVal_2) {
t1.setAttr$3(t1.rootEl, "title", currVal_2);
T.updateAttribute(t1.rootEl, "title", currVal_2);
t1._expr_2 = currVal_2;
}
currVal_3 = t1.ctx.get$isFancy();
t2 = t1._expr_3;
if (t2 != currVal_3) {
R.updateClassBindingNonHtml(t1.rootEl, "fancy", currVal_3);
T.updateClassBindingNonHtml(t1.rootEl, "fancy", currVal_3);
t1._expr_3 = currVal_3;
}
this._compView_0.detectChanges$0();
Expand Down Expand Up @@ -11512,13 +11468,13 @@
t1 = self.defeatDart2JsOptimizations("title");
t2 = this._expr_0;
if (t2 != t1) {
this.setAttr$3(this._el_0, "title", t1);
T.updateAttribute(this._el_0, "title", t1);
this._expr_0 = t1;
}
t1 = self.defeatDart2JsOptimizations("fancy");
t2 = this._expr_1;
if (t2 != t1) {
R.updateClassBinding(this._el_0, "fancy", t1);
T.updateClassBinding(this._el_0, "fancy", t1);
this._expr_1 = t1;
}
},
Expand Down Expand Up @@ -11629,7 +11585,7 @@
_inheritMany(P.Error, [H.NullError, H.JsNoSuchMethodError, H.UnknownJsTypeError, H.RuntimeError, P.NullThrownError, P.ArgumentError, P.NoSuchMethodError, P.UnsupportedError, P.UnimplementedError, P.StateError, P.ConcurrentModificationError, P.CyclicInitializationError]);
_inheritMany(H.TearOffClosure, [H.StaticClosure, H.BoundClosure]);
_inherit(P.MapBase, P.MapMixin);
_inheritMany(P.MapBase, [H.JsLinkedHashMap, P._HashMap, W._AttributeMap]);
_inheritMany(P.MapBase, [H.JsLinkedHashMap, P._HashMap]);
_inherit(H.NativeTypedArray, H.NativeTypedData);
_inheritMany(H.NativeTypedArray, [H._NativeTypedArrayOfDouble_NativeTypedArray_ListMixin, H._NativeTypedArrayOfInt_NativeTypedArray_ListMixin]);
_inherit(H._NativeTypedArrayOfDouble_NativeTypedArray_ListMixin_FixedLengthListMixin, H._NativeTypedArrayOfDouble_NativeTypedArray_ListMixin);
Expand Down Expand Up @@ -11705,7 +11661,6 @@
_inherit(W._SpeechRecognitionResultList, W.__SpeechRecognitionResultList_Interceptor_ListMixin_ImmutableListMixin);
_inherit(W.__StyleSheetList_Interceptor_ListMixin_ImmutableListMixin, W.__StyleSheetList_Interceptor_ListMixin);
_inherit(W._StyleSheetList, W.__StyleSheetList_Interceptor_ListMixin_ImmutableListMixin);
_inherit(W._ElementAttributeMap, W._AttributeMap);
_inherit(W._EventStreamSubscription, P.StreamSubscription);
_inherit(P._StructuredCloneDart2Js, P._StructuredClone);
_inherit(P._AcceptStructuredCloneDart2Js, P._AcceptStructuredClone);
Expand Down Expand Up @@ -11987,10 +11942,10 @@
$.Device__cachedCssPrefix = null;
$._platformInjectorCache = null;
$.ChangeDetectionHost__current = null;
$.domRootRendererIsDirty = false;
$.appViewUtils = null;
$.AppViewUtils__nextCompTypeId = 0;
$.sharedStylesHost = null;
$.domRootRendererIsDirty = false;
$.ViewRootComponent0__renderType = null;
$.ViewUsesDefaultChangeDetectionAndInputs0__renderType = null;
$.ViewDefaultChangeDetectionAndInputs0__renderType = null;
Expand Down
4 changes: 2 additions & 2 deletions _goldens/test/_files/dart2js/dart2js_golden.template.golden
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ class ViewUsesDomBindings0 extends AppView<import1.UsesDomBindings> {
final _ctx = ctx;
final currVal_0 = _ctx.title;
if (import6.checkBinding(_expr_0, currVal_0)) {
setAttr(_el_0, 'title', currVal_0);
import16.updateAttribute(_el_0, 'title', currVal_0);
_expr_0 = currVal_0;
}
final currVal_1 = _ctx.isFancy;
Expand All @@ -1245,7 +1245,7 @@ class ViewUsesDomBindings0 extends AppView<import1.UsesDomBindings> {
void detectHostChanges(bool firstCheck) {
final currVal_2 = ctx.title;
if (import6.checkBinding(_expr_2, currVal_2)) {
setAttr(rootEl, 'title', currVal_2);
import16.updateAttribute(rootEl, 'title', currVal_2);
_expr_2 = currVal_2;
}
final currVal_3 = ctx.isFancy;
Expand Down
9 changes: 5 additions & 4 deletions _goldens/test/_files/di/inlined_providers.template.golden
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import 'dart:html' as import5;
import 'package:angular/src/core/linker/app_view_utils.dart' as import6;
import 'package:angular/src/runtime.dart' as import7;
import 'package:angular/angular.dart';
import 'package:angular/src/di/errors.dart' as import9;
import 'package:angular/src/runtime/dom_helpers.dart' as import9;
import 'package:angular/src/di/errors.dart' as import10;

final List<dynamic> styles$ParentComponent = const [];

Expand Down Expand Up @@ -168,14 +169,14 @@ class ViewParentComponent0 extends AppView<import1.ParentComponent> {
_compView_5 = ViewChildComponentProvidingA$B$C0(this, 5);
final _el_5 = _compView_5.rootEl;
parentRenderNode.append(_el_5);
createAttr(_el_5, 'directive-providing-a2-d2', '');
import9.setAttribute(_el_5, 'directive-providing-a2-d2', '');
_ChildComponentProvidingA$B$C_5_5 = import1.ChildComponentProvidingA$B$C();
_DirectiveProviding$A2$D2_5_6 = import1.DirectiveProviding$A2$D2();
_compView_5.create(_ChildComponentProvidingA$B$C_5_5, []);
_compView_6 = ViewChildComponentWithNgContentProviding$D0(this, 6);
final _el_6 = _compView_6.rootEl;
parentRenderNode.append(_el_6);
createAttr(_el_6, 'directive-providing-a2-d2', '');
import9.setAttribute(_el_6, 'directive-providing-a2-d2', '');
_ChildComponentWithNgContentProviding$D_6_5 = import1.ChildComponentWithNgContentProviding$D();
_DirectiveProviding$A2$D2_6_6 = import1.DirectiveProviding$A2$D2();
_D_6_7 = import1.D2();
Expand Down Expand Up @@ -507,7 +508,7 @@ class _ViewChildComponentInjecting$DHost0 extends AppView<import1.ChildComponent
_compView_0 = ViewChildComponentInjecting$D0(this, 0);
rootEl = _compView_0.rootEl;
_ChildComponentInjecting$D_0_5 = (import7.isDevMode
? import9.debugInjectorWrap(import1.ChildComponentInjecting$D, () {
? import10.debugInjectorWrap(import1.ChildComponentInjecting$D, () {
return import1.ChildComponentInjecting$D(this.injectorGet(import1.D, viewData.parentIndex));
})
: import1.ChildComponentInjecting$D(this.injectorGet(import1.D, viewData.parentIndex)));
Expand Down
24 changes: 12 additions & 12 deletions _goldens/test/_files/directives/directive_wrapper.template.golden
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import 'package:angular/src/core/change_detection/change_detection.dart';
import 'package:angular/src/core/linker/app_view_utils.dart' as import6;
import 'package:angular/src/runtime.dart' as import7;
import 'package:angular/angular.dart';
import 'package:angular/src/runtime/dom_helpers.dart' as import9;
import 'package:angular/src/core/linker/element_ref.dart';
import 'package:angular/src/core/change_detection/directive_change_detector.dart' as import10;
import 'package:angular/src/runtime/dom_helpers.dart' as import11;
import 'package:angular/src/core/change_detection/directive_change_detector.dart' as import11;

final List<dynamic> styles$TestFooComponent = ['div._ngcontent-%ID%{font-size:10px}'];

Expand All @@ -38,9 +38,9 @@ class ViewTestFooComponent0 extends AppView<import1.TestFooComponent> {
final import2.HtmlElement parentRenderNode = initViewRoot(_rootEl);
var doc = import2.document;
_el_0 = createDivAndAppend(doc, parentRenderNode);
createAttr(_el_0, 'directive-with-output', '');
createAttr(_el_0, 'role', import1.ChildDirective.hostRole);
createAttr(_el_0, 'some-child-directive', '');
import9.setAttribute(_el_0, 'directive-with-output', '');
import9.setAttribute(_el_0, 'role', import1.ChildDirective.hostRole);
import9.setAttribute(_el_0, 'some-child-directive', '');
addShimC(_el_0);
_ChildDirective_0_5 = ChildDirectiveNgCd(import1.ChildDirective(_el_0, ElementRef(_el_0)));
_DirectiveWithOutput_0_6 = import1.DirectiveWithOutput();
Expand Down Expand Up @@ -123,11 +123,11 @@ class ViewDirectiveContainerTest0 extends AppView<import1.DirectiveContainerTest
var doc = import2.document;
final _el_0 = createDivAndAppend(doc, parentRenderNode);
_el_0.className = 'target1';
createAttr(_el_0, 'fastDirective', '');
import9.setAttribute(_el_0, 'fastDirective', '');
_FastDirective_0_5 = FastDirectiveNgCd(import1.FastDirective(_el_0), this, _el_0);
final _el_1 = createDivAndAppend(doc, parentRenderNode);
_el_1.className = 'target2';
createAttr(_el_1, 'fastDirective', '');
import9.setAttribute(_el_1, 'fastDirective', '');
_FastDirective_1_5 = FastDirectiveNgCd(import1.FastDirective(_el_1), this, _el_1);
init(const [], null);
}
Expand Down Expand Up @@ -183,7 +183,7 @@ AppView<import1.DirectiveContainerTest> viewFactory_DirectiveContainerTestHost0(
return _ViewDirectiveContainerTestHost0(parentView, parentIndex);
}

class ChildDirectiveNgCd extends import10.DirectiveChangeDetector {
class ChildDirectiveNgCd extends import11.DirectiveChangeDetector {
final import1.ChildDirective instance;
var _expr_0;
var _expr_1;
Expand All @@ -197,18 +197,18 @@ class ChildDirectiveNgCd extends import10.DirectiveChangeDetector {
}
final currVal_1 = instance.disabledStr;
if (import6.checkBinding(_expr_1, currVal_1)) {
setAttr(el, 'aria-disabled', currVal_1);
import9.updateAttribute(el, 'aria-disabled', currVal_1);
_expr_1 = currVal_1;
}
final currVal_2 = instance.disabled;
if (import6.checkBinding(_expr_2, currVal_2)) {
import11.updateClassBindingNonHtml(el, 'is-disabled', currVal_2);
import9.updateClassBindingNonHtml(el, 'is-disabled', currVal_2);
_expr_2 = currVal_2;
}
}
}

class FastDirectiveNgCd extends import10.DirectiveChangeDetector {
class FastDirectiveNgCd extends import11.DirectiveChangeDetector {
final import1.FastDirective instance;
var _expr_0;
FastDirectiveNgCd(this.instance, AppView v, import2.Element e) {
Expand All @@ -220,7 +220,7 @@ class FastDirectiveNgCd extends import10.DirectiveChangeDetector {
void detectHostChanges(AppView<dynamic> view, import2.Element el) {
final currVal_0 = instance.msg;
if (import6.checkBinding(_expr_0, currVal_0)) {
setAttr(el, 'data-msg', currVal_0);
import9.updateAttribute(el, 'data-msg', currVal_0);
_expr_0 = currVal_0;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'dart:html' as import5;
import 'package:angular/src/core/linker/app_view_utils.dart' as import6;
import 'package:angular/src/runtime.dart' as import7;
import 'package:angular/angular.dart';
import 'package:angular/src/runtime/dom_helpers.dart' as import9;

final List<dynamic> styles$AppComponent = const [];

Expand All @@ -31,7 +32,7 @@ class ViewAppComponent0 extends AppView<import1.AppComponent> {
final import5.HtmlElement parentRenderNode = initViewRoot(_rootEl);
var doc = import5.document;
final _el_0 = createDivAndAppend(doc, parentRenderNode);
createAttr(_el_0, 'generateText', '');
import9.setAttribute(_el_0, 'generateText', '');
final _el_1 = createDivAndAppend(doc, _el_0);
import1.generateTextDirective(_el_0, parentView.injectorGet(import1.TextService, viewData.parentIndex));
init(const [], null);
Expand Down
Loading

0 comments on commit cbd0b5c

Please sign in to comment.