compareNodes method

int compareNodes (Node a, Node b)

Compares two nodes a and b

Returns:

-1: a < b
0: a = b
1: a > b
and null  for other value for a != b

Implementation

static int compareNodes(Node a, Node b) {
  //new Logger().log('${a.type}: ${a.value} - ${b.type}: ${b.value}');

  // for "symmetric results" force toCSS-based comparison
  // of Quoted or Anonymous if either value is one of those
  if ((a is CompareNode) && !(b is Quoted || b is Anonymous)) {
    return (a as CompareNode).compare(b);
  } else if (b is CompareNode) {
    return negate((b as CompareNode).compare(a)); //-null => null
  } else if (a.runtimeType != b.runtimeType) {
    return null;
  }

  final dynamic aValue = a.value;
  final dynamic bValue = b.value;

  if (aValue is! List) return (aValue == bValue) ? 0 : null;
  if (aValue is List && bValue is List) {
    if (aValue.length != bValue.length) return null;
    for (int i = 0; i < aValue.length; i++) {
      if (Node.compareNodes(aValue[i], bValue[i]) != 0) return null;
    }
  }
  return 0;

//2.3.1
//  Node.compare = function (a, b) {
//      /* returns:
//       -1: a < b
//       0: a = b
//       1: a > b
//       and *any* other value for a != b (e.g. undefined, NaN, -2 etc.) */
//
//      if ((a.compare) &&
//          // for "symmetric results" force toCSS-based comparison
//          // of Quoted or Anonymous if either value is one of those
//          !(b.type === "Quoted" || b.type === "Anonymous")) {
//          return a.compare(b);
//      } else if (b.compare) {
//          return -b.compare(a);
//      } else if (a.type !== b.type) {
//          return undefined;
//      }
//
//      a = a.value;
//      b = b.value;
//      if (!Array.isArray(a)) {
//          return a === b ? 0 : undefined;
//      }
//      if (a.length !== b.length) {
//          return undefined;
//      }
//      for (var i = 0; i < a.length; i++) {
//          if (Node.compare(a[i], b[i]) !== 0) {
//              return undefined;
//          }
//      }
//      return 0;
//  };
}