Skip to content

Commit c3a6348

Browse files
author
Anatoly Ostrovsky
committed
Refactor compile
1 parent 4366089 commit c3a6348

File tree

1 file changed

+34
-45
lines changed

1 file changed

+34
-45
lines changed

src/core/compile/compile.js

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ export function CompileProvider($provide, $$sanitizeUriProvider) {
723723
* Compile function matches each node in nodeList against the directives. Once all directives
724724
* for a particular node are collected their compile functions are executed. The compile
725725
* functions return values - the linking functions - are combined into a composite linking
726-
* function, which is the a linking function for the node.
726+
* function, which is the linking function for the node.
727727
*
728728
* @param {NodeList|JQLite} nodeList an array of nodes or NodeList to compile
729729
* @param {*} transcludeFn A linking function, where the
@@ -745,25 +745,27 @@ export function CompileProvider($provide, $$sanitizeUriProvider) {
745745
previousCompileContext,
746746
) {
747747
const linkFns = [];
748-
let attrs;
749-
let directives;
750748
/**
751749
* @type {any}
752750
*/
753-
var nodeLinkFn;
751+
var nodeLinkFn = null;
754752
let childNodes;
755753
let childLinkFn;
756754
let linkFnFound;
757755
let nodeLinkFnFound;
758756

759757
for (let i = 0; i < nodeList.length; i++) {
760-
attrs = new Attributes($rootScope, $animate, $exceptionHandler, $sce);
758+
const attrs = new Attributes(
759+
$rootScope,
760+
$animate,
761+
$exceptionHandler,
762+
$sce,
763+
);
761764

762765
// We must always refer to `nodeList[i]` hereafter,
763766
// since the nodes can be replaced underneath us.
764-
directives = collectDirectives(
767+
const directives = collectDirectives(
765768
/** @type Element */ (nodeList[i]),
766-
[],
767769
attrs,
768770
i === 0 ? maxPriority : undefined,
769771
ignoreDirective,
@@ -944,31 +946,30 @@ export function CompileProvider($provide, $$sanitizeUriProvider) {
944946
* the function returns.
945947
* @param {Attributes|import("./attributes").AttributeLike} attrs The shared attrs object which is used to populate the normalized attributes.
946948
* @param {number=} maxPriority Max directive priority.
947-
* @param {boolean=} ignoreDirective
949+
* @param {string} [ignoreDirective]
950+
* @return {import('../../types.js').Directive[]} An array to which the directives are added to. This array is sorted before the function returns.
948951
*/
949-
function collectDirectives(
950-
node,
951-
directives,
952-
attrs,
953-
maxPriority,
954-
ignoreDirective,
955-
) {
952+
function collectDirectives(node, attrs, maxPriority, ignoreDirective) {
953+
/**
954+
* @type {import('../../types.js').Directive[]}
955+
*/
956+
const directives = [];
956957
const { nodeType } = node;
957958
const attrsMap = attrs.$attr;
958959
let nodeName;
959960

960961
switch (nodeType) {
961962
case Node.ELEMENT_NODE /* Element */:
962963
nodeName = node.nodeName.toLowerCase();
963-
964-
// use the node name: <directive>
965-
addDirective(
966-
directives,
967-
directiveNormalize(nodeName),
968-
"E",
969-
maxPriority,
970-
ignoreDirective,
971-
);
964+
if (ignoreDirective !== directiveNormalize(nodeName)) {
965+
// use the node name: <directive>
966+
addDirective(
967+
directives,
968+
directiveNormalize(nodeName),
969+
"E",
970+
maxPriority,
971+
);
972+
}
972973

973974
// iterate over the attributes
974975
for (
@@ -1043,13 +1044,10 @@ export function CompileProvider($provide, $$sanitizeUriProvider) {
10431044
nName,
10441045
isNgAttr,
10451046
);
1046-
addDirective(
1047-
directives,
1048-
nName,
1049-
"A",
1050-
maxPriority,
1051-
ignoreDirective,
1052-
);
1047+
1048+
if (nName !== ignoreDirective) {
1049+
addDirective(directives, nName, "A", maxPriority);
1050+
}
10531051
}
10541052
}
10551053

@@ -1742,7 +1740,6 @@ export function CompileProvider($provide, $$sanitizeUriProvider) {
17421740
// - combine directives as: processed + template + unprocessed
17431741
const templateDirectives = collectDirectives(
17441742
/** @type {Element} */ (compileNode),
1745-
[],
17461743
newTemplateAttrs,
17471744
);
17481745
const unprocessedDirectives = directives.splice(
@@ -2011,22 +2008,17 @@ export function CompileProvider($provide, $$sanitizeUriProvider) {
20112008
* looks up the directive and decorates it with exception handling and proper parameters. We
20122009
* call this the boundDirective.
20132010
*
2011+
* @param tDirectives
20142012
* @param {string} name name of the directive to look up.
20152013
* @param {string} location The directive must be found in specific format.
20162014
* String containing any of these characters:
20172015
*
20182016
* * `E`: element name
20192017
* * `A': attribute
2018+
* @param maxPriority
20202019
* @returns {boolean} true if directive was added.
20212020
*/
2022-
function addDirective(
2023-
tDirectives,
2024-
name,
2025-
location,
2026-
maxPriority,
2027-
ignoreDirective,
2028-
) {
2029-
if (name === ignoreDirective) return false;
2021+
function addDirective(tDirectives, name, location, maxPriority) {
20302022
let match = false;
20312023
if (Object.prototype.hasOwnProperty.call(hasDirectives, name)) {
20322024
for (
@@ -2168,7 +2160,6 @@ export function CompileProvider($provide, $$sanitizeUriProvider) {
21682160
replaceWith($rootElement, $compileNode, compileNode);
21692161
const templateDirectives = collectDirectives(
21702162
compileNode,
2171-
[],
21722163
tempTemplateAttrs,
21732164
);
21742165

@@ -2331,15 +2322,13 @@ export function CompileProvider($provide, $$sanitizeUriProvider) {
23312322
}
23322323

23332324
function addTextInterpolateDirective(directives, text) {
2334-
const interpolateFn = $interpolate(text, true); // Create interpolation function
2325+
const interpolateFn = $interpolate(text, true);
23352326
if (interpolateFn) {
23362327
directives.push({
23372328
priority: 0,
2338-
// When transcluding a template that has bindings in the root
2339-
// we don't have a parent and thus need to add the class during linking fn.
23402329
compile: () => (scope, node) => {
23412330
scope.$watch(interpolateFn, (value) => {
2342-
node[0].nodeValue = value; // Update text node with new interpolated value
2331+
node[0].nodeValue = value;
23432332
});
23442333
},
23452334
});

0 commit comments

Comments
 (0)