<commit_before>import { module, spy } from 'substance-test'
import inBrowser from '../../util/inBrowser'
import BrowserDOMElement from '../../dom/BrowserDOMElement'
import XNode from '../../dom/XNode'

if (inBrowser) {
  testDOMElement('BrowserDOMElement')
}

testDOMElement('XNode')

function testDOMElement(impl) {

  const DOMElement = impl === 'BrowserDOMElement' ? BrowserDOMElement : XNode

  const test = module('dom/DOMElement ('+impl+')')

  test("Parsing a full HTML document", function(t) {
    var html = '<html><head><title>TEST</title></head><body>TEST</body></html>'
    var doc = DOMElement.parseHTML(html)
    var head = doc.find('head')
    t.notNil(head)

    var title = head.find('title')
    t.notNil(title, '<head> should contain <title>')
    t.equal(title.text(), 'TEST')

    var body = doc.find('body')
    t.notNil(body, 'document should have a <body> element.')
    t.equal(body.text(), 'TEST', 'body content should be correct.')
    t.end()
  })

  test("Parsing one HTML element", function(t) {
    var html = '<p>TEST</p>'
    var p = DOMElement.parseHTML(html)
    t.notNil(p, 'HTML should get parsed.')
    t.equal(p.tagName, 'p', '.. providing one <p> element,')
    t.equal(p.text(), 'TEST', '.. with correct content.')
    t.end()
  })


  test("Parsing multiple HTML elements", function(t) {
    var html = '<p>TEST</p><p>TEST2</p>'
    var els = DOMElement.parseHTML(html)
    t.notNil(els, 'HTML should get parsed.')
    t.equal(els.length, 2, '.. Providing 2 elements')
    t.equal(els[0].tagName, 'p', '.. the first a <p>')
    t.equal(els[0].text(), 'TEST', '.. with correct content')
    t.equal(els[1].tagName, 'p', '.. the second a <p>')
    t.equal(els[1].text(), 'TEST2', '.. with correct content')
    t.end()
  })

  test("Parsing annotated HTML text", function(t) {
    var html = '123<b>456</b>789'
    var els = DOMElement.parseHTML(html)
    t.equal(els.length, 3, 'there are three elements')
    t.equal(els[0].nodeType, 'text', 'first is a text node')
    t.equal(els[0].text(), '123', '... it has correct content')
    t.equal(els[1].nodeType, 'element', 'second is an element')
    t.equal(els[1].tagName, 'b', '... it is a <b>')
    t.equal(els[1].text(), '456', '... it has correct content')
    t.equal(els[2].nodeType, 'text', 'third is a text node again')
    t.equal(els[2].text(), '789', '... it has correct content')
    t.end()
  })

  test("Parsing an XML document", function(t) {
    var xml = "<mydoc><myhead><mytitle>TEST</mytitle></myhead><mybody>TEST</mybody></mydoc>"
    var doc = DOMElement.parseXML(xml)
    var head = doc.find('myhead')
    t.notNil(head)
    var title = head.find('mytitle')
    t.notNil(title, '<head> should contain <title>')
    t.equal(title.text(), 'TEST')
    var body = doc.find('mybody')
    t.notNil(body, 'document should have a <body> element.')
    t.equal(body.text(), 'TEST', 'body content should be correct.')
    t.end()
  })

  test("hasClass", function(t) {
    var p = DOMElement.parseHTML('<p class="foo">TEST</p>')
    t.ok(p.hasClass('foo'), 'Element should have class "foo".')
    t.end()
  })

  test("addClass", function(t) {
    var p = DOMElement.parseHTML('<p>TEST</p>')
    p.addClass('foo')
    t.ok(p.hasClass('foo'), 'Element should have class "foo".')
    t.end()
  })

  test("removeClass", function(t) {
    var p = DOMElement.parseHTML('<p class="foo">TEST</p>')
    p.removeClass('foo')
    t.notOk(p.hasClass('foo'), 'Element should not have class "foo".')
    t.end()
  })

  test("setTagName", function(t) {
    var el = DOMElement.parseHTML('<p class="foo">ABC<b>DEF</b>GHI</p>')
    var onClick = spy()
    el.on('click', onClick)
    // this call is brutal as a new element needs to be created
    // and all the content and attributes be copied over
    el.setTagName('h1')
    t.equal(el.tagName, 'h1', 'Now the element should be a heading')
    t.equal(el.textContent, 'ABCDEFGHI', '.. its text content should have been preserved')
    t.equal(el.getChildCount(), 3, '.. and its children should still be there')
    t.notNil(el.find('b'), '.. including the <b> element')
    el.click()
    t.equal(onClick.callCount, 1, '.. and even the click handler should still work')
    t.end()
  })

  test("setTagName on XML should create XML elements", function(t) {
    var el = DOMElement.parseXML('<dummy></dummy>')
    // this call is brutal as a new element needs to be created
    // and all the content and attributes be copied over
    el.setTagName('foo')
    t.equal(el.getNativeElement().ownerDocument.contentType, 'application/xml', 'Element should still be an XML element')
    el.setInnerHTML('<link>foo</link>')
    // when using an HTML element <link> will get exported as self-closing
    t.equal(el.outerHTML, '<foo><link>foo</link></foo>')
    t.end()
  })
}<commit_msg>Added some DOMElement tests.<commit_after>import { module, spy } from 'substance-test'
import inBrowser from '../../util/inBrowser'
import BrowserDOMElement from '../../dom/BrowserDOMElement'
import XNode from '../../dom/XNode'

if (inBrowser) {
  testDOMElement('BrowserDOMElement')
}

testDOMElement('XNode')

function testDOMElement(impl) {

  const DOMElement = impl === 'BrowserDOMElement' ? BrowserDOMElement : XNode

  const test = module('dom/DOMElement ('+impl+')')

  test("Parsing a full HTML document", function(t) {
    var html = '<html><head><title>TEST</title></head><body>TEST</body></html>'
    var doc = DOMElement.parseHTML(html)
    var head = doc.find('head')
    t.notNil(head)

    var title = head.find('title')
    t.notNil(title, '<head> should contain <title>')
    t.equal(title.text(), 'TEST')

    var body = doc.find('body')
    t.notNil(body, 'document should have a <body> element.')
    t.equal(body.text(), 'TEST', 'body content should be correct.')
    t.end()
  })

  test("Parsing one HTML element", function(t) {
    var html = '<p>TEST</p>'
    var p = DOMElement.parseHTML(html)
    t.notNil(p, 'HTML should get parsed.')
    t.equal(p.tagName, 'p', '.. providing one <p> element,')
    t.equal(p.text(), 'TEST', '.. with correct content.')
    t.end()
  })


  test("Parsing multiple HTML elements", function(t) {
    var html = '<p>TEST</p><p>TEST2</p>'
    var els = DOMElement.parseHTML(html)
    t.notNil(els, 'HTML should get parsed.')
    t.equal(els.length, 2, '.. Providing 2 elements')
    t.equal(els[0].tagName, 'p', '.. the first a <p>')
    t.equal(els[0].text(), 'TEST', '.. with correct content')
    t.equal(els[1].tagName, 'p', '.. the second a <p>')
    t.equal(els[1].text(), 'TEST2', '.. with correct content')
    t.end()
  })

  test("Parsing annotated HTML text", function(t) {
    var html = '123<b>456</b>789'
    var els = DOMElement.parseHTML(html)
    t.equal(els.length, 3, 'there are three elements')
    t.equal(els[0].nodeType, 'text', 'first is a text node')
    t.equal(els[0].text(), '123', '... it has correct content')
    t.equal(els[1].nodeType, 'element', 'second is an element')
    t.equal(els[1].tagName, 'b', '... it is a <b>')
    t.equal(els[1].text(), '456', '... it has correct content')
    t.equal(els[2].nodeType, 'text', 'third is a text node again')
    t.equal(els[2].text(), '789', '... it has correct content')
    t.end()
  })

  test("Parsing an XML document", function(t) {
    var xml = "<mydoc><myhead><mytitle>TEST</mytitle></myhead><mybody>TEST</mybody></mydoc>"
    var doc = DOMElement.parseXML(xml)
    var head = doc.find('myhead')
    t.notNil(head)
    var title = head.find('mytitle')
    t.notNil(title, '<head> should contain <title>')
    t.equal(title.text(), 'TEST')
    var body = doc.find('mybody')
    t.notNil(body, 'document should have a <body> element.')
    t.equal(body.text(), 'TEST', 'body content should be correct.')
    t.end()
  })

  test("hasClass", function(t) {
    var p = DOMElement.parseHTML('<p class="foo">TEST</p>')
    t.ok(p.hasClass('foo'), 'Element should have class "foo".')
    t.end()
  })

  test("addClass", function(t) {
    var p = DOMElement.parseHTML('<p>TEST</p>')
    p.addClass('foo')
    t.ok(p.hasClass('foo'), 'Element should have class "foo".')
    t.end()
  })

  test("removeClass", function(t) {
    var p = DOMElement.parseHTML('<p class="foo">TEST</p>')
    p.removeClass('foo')
    t.notOk(p.hasClass('foo'), 'Element should not have class "foo".')
    t.end()
  })

  test("setTagName", function(t) {
    var el = DOMElement.parseHTML('<p class="foo">ABC<b>DEF</b>GHI</p>')
    var onClick = spy()
    el.on('click', onClick)
    // this call is brutal as a new element needs to be created
    // and all the content and attributes be copied over
    el.setTagName('h1')
    t.equal(el.tagName, 'h1', 'Now the element should be a heading')
    t.equal(el.textContent, 'ABCDEFGHI', '.. its text content should have been preserved')
    t.equal(el.getChildCount(), 3, '.. and its children should still be there')
    t.notNil(el.find('b'), '.. including the <b> element')
    el.click()
    t.equal(onClick.callCount, 1, '.. and even the click handler should still work')
    t.end()
  })

  test("setTagName on XML should create XML elements", function(t) {
    var el = DOMElement.parseXML('<dummy></dummy>')
    // this call is brutal as a new element needs to be created
    // and all the content and attributes be copied over
    el.setTagName('foo')
    t.equal(el.getNativeElement().ownerDocument.contentType, 'application/xml', 'Element should still be an XML element')
    el.setInnerHTML('<link>foo</link>')
    // when using an HTML element <link> will get exported as self-closing
    t.equal(el.outerHTML, '<foo><link>foo</link></foo>')
    t.end()
  })

  test("find via tagname", function(t) {
    let el = DOMElement.parseXML('<dummy><foo></foo></dummy>')
    let foo = el.find('foo')
    t.notNil(foo, 'Should find a <foo> element')
    t.end()
  })

  test("find multiple elements", function(t) {
    let el = DOMElement.parseXML('<dummy><foo>Bla</foo><foo>Blupp</foo></dummy>')
    let foos = el.findAll('foo')
    t.equal(foos.length, 2, 'Should find two <foo> elements')
    t.equal(foos[0].textContent, 'Bla', '... with correct textContent')
    t.equal(foos[1].textContent, 'Blupp', '... with correct textContent')
    t.end()
  })

  test("find via #id", function(t) {
    let el = DOMElement.parseXML('<dummy><bla id="foo"></bla></dummy>')
    let foo = el.find('#foo')
    t.notNil(foo, 'Should find a #foo element')
    t.end()
  })

  test("find via attr", function(t) {
    let el = DOMElement.parseXML('<dummy><bla data-id="foo"></bla></dummy>')
    let foo = el.find('[data-id=foo]')
    t.notNil(foo, 'Should find a element with data-id=foo')
    t.end()
  })

}
{'fraction_non_alphanumeric': 0.18848853532990173, 'fraction_numerical': 0.007393542349087506, 'mean_word_length': 3.4175279040926, 'pattern_counts': {'":': 0, '<': 131, '<?xml version=': 0, '>': 131, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '1434716', 'n_tokens_mistral': 3774, 'n_tokens_neox': 3669, 'n_words': 945}