MCPcopy Create free account
hub / github.com/angular/angular / updateStylingMap

Function updateStylingMap

packages/core/src/render3/instructions/styling.ts:805–862  ·  view source on GitHub ↗

* Update map based styling. * * Map based styling could be anything which contains more than one binding. For example `string`, * or object literal. Dealing with all of these types would complicate the logic so * instead this function expects that the complex input is first converted into normal

(
  tView: TView,
  tNode: TNode,
  lView: LView,
  renderer: Renderer,
  oldKeyValueArray: KeyValueArray<any>,
  newKeyValueArray: KeyValueArray<any>,
  isClassBased: boolean,
  bindingIndex: number,
)

Source from the content-addressed store, hash-verified

803 * @param bindingIndex Binding index of the binding.
804 */
805function updateStylingMap(
806 tView: TView,
807 tNode: TNode,
808 lView: LView,
809 renderer: Renderer,
810 oldKeyValueArray: KeyValueArray<any>,
811 newKeyValueArray: KeyValueArray<any>,
812 isClassBased: boolean,
813 bindingIndex: number,
814) {
815 if ((oldKeyValueArray as KeyValueArray<any> | NO_CHANGE) === NO_CHANGE) {
816 // On first execution the oldKeyValueArray is NO_CHANGE => treat it as empty KeyValueArray.
817 oldKeyValueArray = EMPTY_ARRAY as any;
818 }
819 let oldIndex = 0;
820 let newIndex = 0;
821 let oldKey: string | null = 0 < oldKeyValueArray.length ? oldKeyValueArray[0] : null;
822 let newKey: string | null = 0 < newKeyValueArray.length ? newKeyValueArray[0] : null;
823 while (oldKey !== null || newKey !== null) {
824 ngDevMode && assertLessThan(oldIndex, 999, 'Are we stuck in infinite loop?');
825 ngDevMode && assertLessThan(newIndex, 999, 'Are we stuck in infinite loop?');
826 const oldValue =
827 oldIndex < oldKeyValueArray.length ? oldKeyValueArray[oldIndex + 1] : undefined;
828 const newValue =
829 newIndex < newKeyValueArray.length ? newKeyValueArray[newIndex + 1] : undefined;
830 let setKey: string | null = null;
831 let setValue: any = undefined;
832 if (oldKey === newKey) {
833 // UPDATE: Keys are equal => new value is overwriting old value.
834 oldIndex += 2;
835 newIndex += 2;
836 if (oldValue !== newValue) {
837 setKey = newKey;
838 setValue = newValue;
839 }
840 } else if (newKey === null || (oldKey !== null && oldKey < newKey!)) {
841 // DELETE: oldKey key is missing or we did not find the oldKey in the newValue
842 // (because the keyValueArray is sorted and `newKey` is found later alphabetically).
843 // `"background" < "color"` so we need to delete `"background"` because it is not found in the
844 // new array.
845 oldIndex += 2;
846 setKey = oldKey;
847 } else {
848 // CREATE: newKey's is earlier alphabetically than oldKey's (or no oldKey) => we have new key.
849 // `"color" > "background"` so we need to add `color` because it is in new array but not in
850 // old array.
851 ngDevMode && assertDefined(newKey, 'Expecting to have a valid key');
852 newIndex += 2;
853 setKey = newKey;
854 setValue = newValue;
855 }
856 if (setKey !== null) {
857 updateStyling(tView, tNode, lView, renderer, setKey, setValue, isClassBased, bindingIndex);
858 }
859 oldKey = oldIndex < oldKeyValueArray.length ? oldKeyValueArray[oldIndex] : null;
860 newKey = newIndex < newKeyValueArray.length ? newKeyValueArray[newIndex] : null;
861 }
862}

Callers 1

checkStylingMapFunction · 0.85

Calls 3

assertLessThanFunction · 0.90
assertDefinedFunction · 0.90
updateStylingFunction · 0.85

Tested by

no test coverage detected