<commit_before>module("Slowparse");

test("Stream.match()", function() {
  var stream = new Slowparse.Stream("blArgle");
  ok(stream.match("blArgle"));
  equal(stream.pos, 0);
  ok(!stream.match("blargle"));
  equal(stream.pos, 0);
  ok(stream.match("blargle", false, true));
  equal(stream.pos, 0);
  ok(stream.match("bla", true, true));
  equal(stream.pos, 3);
  ok(stream.match("rgle", true));
  equal(stream.pos, 7);
});

test("Stream.eat() works at EOF", function() {
  var stream = new Slowparse.Stream("");
  ok(!stream.eat(/blah/));
});

test("parsing of valid DOCTYPE", function() {
  var html = '<!DOCTYPE html><p>hi</p>';
  var doc = parseWithoutErrors(html);
  assertParseIntervals(html, doc, "document", {
    'parseInfo.doctype': '<!DOCTYPE html>'
  });
});

test("parsing of misplaced DOCTYPE", function() {
  var html = '<p>hi</p><!DOCTYPE html>';
  var result = Slowparse.HTML(document, html);
  deepEqual(result.error, {
    "openTag": {
      "end": 10,
      "name": "",
      "start": 9
    },
    "type": "INVALID_TAG_NAME"
  });
});

test("parsing of HTML comments", function() {
  var html = 'hi<!--testing-->there';
  var doc = parseWithoutErrors(html);
  assertParseIntervals(html, doc.childNodes[1], "comment", {
    'parseInfo': '<!--testing-->'
  });
});

test("UNQUOTED_ATTR_VALUE in <h2><span start=</h2>", function() {
  // https://github.com/toolness/slowparse/issues/6
  var err = Slowparse.HTML(document, '<h2><span start=</h2>').error;
  equal(err.type, "UNQUOTED_ATTR_VALUE");
});

test("parsing of elements with boolean attributes", function() {
  var html = '<a href></a>';
  var doc = parseWithoutErrors(html);
  var attr = doc.childNodes[0].attributes[0];
  equal(attr.nodeName, 'href');
  equal(attr.nodeValue, '');
  assertParseIntervals(html, attr, "attr", {
    'parseInfo.name': 'href'
  });
  
  var error = Slowparse.HTML(document, '<a href+></a>').error;
  equal(error.type, 'UNTERMINATED_OPEN_TAG');

  html = '<a href class="foo"></a>';
  doc = parseWithoutErrors(html);
  var attr1 = doc.childNodes[0].attributes[0];
  var attr2 = doc.childNodes[0].attributes[1];
  equal(attr1.nodeName, 'href');
  equal(attr1.nodeValue, '');
  equal(attr2.nodeName, 'class');
  equal(attr2.nodeValue, 'foo');
  assertParseIntervals(html, attr1, "attr1", {<commit_msg>Fixed #39. All tests now pass on IE9.<commit_after>module("Slowparse");

test("Stream.match()", function() {
  var stream = new Slowparse.Stream("blArgle");
  ok(stream.match("blArgle"));
  equal(stream.pos, 0);
  ok(!stream.match("blargle"));
  equal(stream.pos, 0);
  ok(stream.match("blargle", false, true));
  equal(stream.pos, 0);
  ok(stream.match("bla", true, true));
  equal(stream.pos, 3);
  ok(stream.match("rgle", true));
  equal(stream.pos, 7);
});

test("Stream.eat() works at EOF", function() {
  var stream = new Slowparse.Stream("");
  ok(!stream.eat(/blah/));
});

test("parsing of valid DOCTYPE", function() {
  var html = '<!DOCTYPE html><p>hi</p>';
  var doc = parseWithoutErrors(html);
  assertParseIntervals(html, doc, "document", {
    'parseInfo.doctype': '<!DOCTYPE html>'
  });
});

test("parsing of misplaced DOCTYPE", function() {
  var html = '<p>hi</p><!DOCTYPE html>';
  var result = Slowparse.HTML(document, html);
  deepEqual(result.error, {
    "openTag": {
      "end": 10,
      "name": "",
      "start": 9
    },
    "type": "INVALID_TAG_NAME"
  });
});

test("parsing of HTML comments", function() {
  var html = 'hi<!--testing-->there';
  var doc = parseWithoutErrors(html);
  assertParseIntervals(html, doc.childNodes[1], "comment", {
    'parseInfo': '<!--testing-->'
  });
});

test("UNQUOTED_ATTR_VALUE in <h2><span start=</h2>", function() {
  // https://github.com/toolness/slowparse/issues/6
  var err = Slowparse.HTML(document, '<h2><span start=</h2>').error;
  equal(err.type, "UNQUOTED_ATTR_VALUE");
});

test("parsing of elements with boolean attributes", function() {
  var html = '<a href></a>';
  var doc = parseWithoutErrors(html);
  var attr = doc.childNodes[0].attributes[0];
  equal(attr.nodeName, 'href');
  equal(attr.nodeValue, '');
  assertParseIntervals(html, attr, "attr", {
    'parseInfo.name': 'href'
  });
  
  var error = Slowparse.HTML(document, '<a href+></a>').error;
  equal(error.type, 'UNTERMINATED_OPEN_TAG');

  html = '<a href class="foo"></a>';
  doc = parseWithoutErrors(html);
  var attr1 = doc.childNodes[0].attributes[0];
  var attr2 = doc.childNodes[0].attributes[1];
  
  // Apparently NamedNodeMap entries are not in any particular order:
  // https://developer.mozilla.org/en/DOM/NamedNodeMap
  // 
  // So, we'll swap these values if they're not in the order we expect
  // them to be in, which is the case in IE9, at the very least.
  if (attr1.nodeName == 'class') {
    var temp = attr1;
    attr1 = attr2;
    attr2 = temp;
  }
  
  equal(attr1.nodeName, 'href');
  equal(attr1.nodeValue, '');
  equal(attr2.nodeName, 'class');
  equal(attr2.nodeValue, 'foo');
  assertParseIntervals(html, attr1, "attr1", {
{'fraction_non_alphanumeric': 0.20007995202878273, 'fraction_numerical': 0.012992204677193683, 'mean_word_length': 4.289640591966173, 'pattern_counts': {'":': 10, '<': 45, '<?xml version=': 0, '>': 41, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6018951', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1753, 'n_words': 374}