<commit_before>###
 * coffeescript-ui - Coffeescript User Interface System (CUI)
 * Copyright (c) 2013 - 2016 Programmfabrik GmbH
 * MIT Licence
 * https://github.com/programmfabrik/coffeescript-ui, http://www.coffeescript-ui.org
###

class CUI.dom

	@setElement: (element, inst) ->
		CUI.dom.data(element, "element", inst)

	@data: (node, key, data) ->
		if not node
			return undefined

		CUI.util.assert(node instanceof HTMLElement, "dom.data","node needs to be instance of HTMLElement", node: node)

		if key == undefined
			return node.__dom_data or {}

		if CUI.isPlainObject(key)
			for k, v of key
				CUI.dom.data(node, k, v)
			return node

		if data == undefined
			return node.__dom_data?[key]

		if not node.__dom_data
			node.__dom_data = {}

		node.__dom_data[key] = data
		return node

	@removeData: (node, key) ->
		if not node
			return undefined

		if node.__dom_data
			delete(node.__dom_data[key])
			if CUI.isEmptyObject(node.__dom_data)
				delete(node.__dom_data)
		node

	# find an element starting from node, but never going
	# up
	@findElement: (node, selector, nodeFilter, forward=true, siblingOnly=false) ->
		els = @findElements(node, selector, nodeFilter, 1, forward, siblingOnly)
		if els.length == 0
			return null

		return els[0]

	# find an element starting from node, with going up
	@findNextElement: (node, selector, nodeFilter=false, forward=true, siblingOnly=true) ->
		el = @findElement(node, selector, nodeFilter, forward, siblingOnly)
		# console.debug "find next element", node, el
		if el
			return el

		# find next parent node which has a sibling
		while true
			node = node.parentNode
			# console.debug "checking next node", node

			if not node
				return null

			if forward
				sibling = node.nextElementSibling
			else
				sibling = node.previousElementSibling

			# console.debug "sibling", forward, sibling

			if sibling
				break

		@findNextElement(sibling, selector, nodeFilter, forward, false)

	@findPreviousElement: (node, selector, nodeFilter=false) ->
		@findNextElement(node, selector, nodeFilter, false)

	@findNextVisibleElement: (node, selector, forward=true) ->
		@findNextElement(node, selector, ((node) =>
			# console.debug "node visible?", DOM.isVisible(node), node
			CUI.dom.isVisible(node)), forward)

	@findPreviousVisibleElement: (node, selector) ->
		@findNextVisibleElement(node, selector, false)

	@findNextSiblings: (node, selector, nodeFilter=false) ->
		@findElements(node, selector, nodeFilter, null, true, true)

	@findPreviousSiblings: (node, selector, nodeFilter=false) ->
		@findElements(node, selector, nodeFilter, null, false, true)

	# find the next node starting from node start
	# which matches the selector
	@findElements: (node=document.documentElement, selector=null, nodeFilter=false, maxEls=null, forward=true, siblingOnly=false, elements) ->
		if not elements
			CUI.util.assert(node instanceof HTMLElement, "DOM.findElement", "node needs to be instanceof HTMLElement.", node: node, selector: selector)
			elements = []
			check_node = not siblingOnly
		else
			check_node = true

		accept_node = not nodeFilter or nodeFilter(node)
		# console.debug "findElement", maxEls, node, accept_node, nodeFilter

		if check_node and accept_node
			# console.debug "checking node", node
			if selector == null or CUI.dom.matches(node, selector)
				# console.debug "node ok..."
				elements.push(node)
				if elements.length == maxEls
					# console.debug "enough!", elements.length, maxEls
					return elements
			else
				; # console.debug "node not ok..."


		if forward
			child = node.firstElementChild
			sibling = node.nextElementSibling
		else
			child = node.lastElementChild
			sibling = node.previousElementSibling

		# console.debug "child/sibling", child, sibling, siblingOnly

		if child and not siblingOnly and accept_node
			# console.debug "dive to", node
			@findElements(child, selector, nodeFilter, maxEls, forward, siblingOnly, elements)
			if elements.length == maxEls
				return elements

		if sibling
			# console.debug "sibling to", sibling
			@findElements(sibling, selector, nodeFilter, maxEls, forward, siblingOnly, elements)
			if elements.length == maxEls
				return elements

		# console.debug "nothing found, returning with", elements.length
		return elements


	@children: (node, filter) ->
		children = []

		for child, idx in node.children
			if not filter or @is(child, filter)
				children.push(child)

		children

	# finds the first element child which is not
	# filtered by the optional node filter
	@firstElementChild: (node, nodeFilter) ->
		child = node.firstElementChild
		while true
			if not child
				return null

			if not nodeFilter or nodeFilter(child)
				return child

			child = child.nextElementSibling

	@lastElementChild: (node, nodeFilter) ->
		child = node.lastElementChild
		while true
			if not child
				return null

			if not nodeFilter or nodeFilter(child)
				return child

			child = child.previousElementSibling

	@nextElementSibling: (node, nodeFilter) ->
		while true
			sibling = node.nextElementSibling
			if not sibling
				return null

			if not nodeFilter or nodeFilter(sibling)
				return sibling


	@previousElementSibling: (node, nodeFilter) ->
		while true
			sibling = node.previousElementSibling
			if not sibling
				return null

			if not nodeFilter or nodeFilter(sibling)
				return sibling


	@removeAttribute: (node, key) ->
		if not node
			return null

		node.removeAttribute(key)<commit_msg>dom: Improved nodeFilter parameter in find functions, use "@is" to support not only Functions.<commit_after>###
 * coffeescript-ui - Coffeescript User Interface System (CUI)
 * Copyright (c) 2013 - 2016 Programmfabrik GmbH
 * MIT Licence
 * https://github.com/programmfabrik/coffeescript-ui, http://www.coffeescript-ui.org
###

class CUI.dom

	@setElement: (element, inst) ->
		CUI.dom.data(element, "element", inst)

	@data: (node, key, data) ->
		if not node
			return undefined

		CUI.util.assert(node instanceof HTMLElement, "dom.data","node needs to be instance of HTMLElement", node: node)

		if key == undefined
			return node.__dom_data or {}

		if CUI.isPlainObject(key)
			for k, v of key
				CUI.dom.data(node, k, v)
			return node

		if data == undefined
			return node.__dom_data?[key]

		if not node.__dom_data
			node.__dom_data = {}

		node.__dom_data[key] = data
		return node

	@removeData: (node, key) ->
		if not node
			return undefined

		if node.__dom_data
			delete(node.__dom_data[key])
			if CUI.isEmptyObject(node.__dom_data)
				delete(node.__dom_data)
		node

	# find an element starting from node, but never going
	# up
	@findElement: (node, selector, nodeFilter, forward=true, siblingOnly=false) ->
		els = @findElements(node, selector, nodeFilter, 1, forward, siblingOnly)
		if els.length == 0
			return null

		return els[0]

	# find an element starting from node, with going up
	@findNextElement: (node, selector, nodeFilter=false, forward=true, siblingOnly=true) ->
		el = @findElement(node, selector, nodeFilter, forward, siblingOnly)
		# console.debug "find next element", node, el
		if el
			return el

		# find next parent node which has a sibling
		while true
			node = node.parentNode
			# console.debug "checking next node", node

			if not node
				return null

			if forward
				sibling = node.nextElementSibling
			else
				sibling = node.previousElementSibling

			# console.debug "sibling", forward, sibling

			if sibling
				break

		@findNextElement(sibling, selector, nodeFilter, forward, false)

	@findPreviousElement: (node, selector, nodeFilter=false) ->
		@findNextElement(node, selector, nodeFilter, false)

	@findNextVisibleElement: (node, selector, forward=true) ->
		@findNextElement(node, selector, ((node) =>
			# console.debug "node visible?", DOM.isVisible(node), node
			CUI.dom.isVisible(node)), forward)

	@findPreviousVisibleElement: (node, selector) ->
		@findNextVisibleElement(node, selector, false)

	@findNextSiblings: (node, selector, nodeFilter=false) ->
		@findElements(node, selector, nodeFilter, null, true, true)

	@findPreviousSiblings: (node, selector, nodeFilter=false) ->
		@findElements(node, selector, nodeFilter, null, false, true)

	# find the next node starting from node start
	# which matches the selector
	@findElements: (node=document.documentElement, selector=null, nodeFilter=false, maxEls=null, forward=true, siblingOnly=false, elements) ->
		if not elements
			CUI.util.assert(node instanceof HTMLElement, "DOM.findElement", "node needs to be instanceof HTMLElement.", node: node, selector: selector)
			elements = []
			check_node = not siblingOnly
		else
			check_node = true

		accept_node = not nodeFilter or nodeFilter(node)
		# console.debug "findElement", maxEls, node, accept_node, nodeFilter

		if check_node and accept_node
			# console.debug "checking node", node
			if selector == null or CUI.dom.matches(node, selector)
				# console.debug "node ok..."
				elements.push(node)
				if elements.length == maxEls
					# console.debug "enough!", elements.length, maxEls
					return elements
			else
				; # console.debug "node not ok..."


		if forward
			child = node.firstElementChild
			sibling = node.nextElementSibling
		else
			child = node.lastElementChild
			sibling = node.previousElementSibling

		# console.debug "child/sibling", child, sibling, siblingOnly

		if child and not siblingOnly and accept_node
			# console.debug "dive to", node
			@findElements(child, selector, nodeFilter, maxEls, forward, siblingOnly, elements)
			if elements.length == maxEls
				return elements

		if sibling
			# console.debug "sibling to", sibling
			@findElements(sibling, selector, nodeFilter, maxEls, forward, siblingOnly, elements)
			if elements.length == maxEls
				return elements

		# console.debug "nothing found, returning with", elements.length
		return elements


	@children: (node, filter) ->
		children = []

		for child, idx in node.children
			if not filter or @is(child, filter)
				children.push(child)

		children

	# finds the first element child which is not
	# filtered by the optional node filter
	@firstElementChild: (node, nodeFilter) ->
		child = node.firstElementChild
		while true
			if not child
				return null

			if not nodeFilter or @is(child, nodeFilter)
				return child

			child = child.nextElementSibling

	@lastElementChild: (node, nodeFilter) ->
		child = node.lastElementChild
		while true
			if not child
				return null

			if not nodeFilter or @is(child, nodeFilter)
				return child

			child = child.previousElementSibling

	@nextElementSibling: (node, nodeFilter) ->
		while true
			sibling = node.nextElementSibling
			if not sibling
				return null

			if not nodeFilter or @is(sibling, nodeFilter)
				return sibling


	@previousElementSibling: (node, nodeFilter) ->
		while true
			sibling = node.previousElementSibling
			if not sibling
				return null

			if not nodeFilter or @is(sibling, nodeFilter)
				return sibling


	@removeAttribute: (node, key) ->
		if not node
			return null

		node.removeAttribute(key)
{'fraction_non_alphanumeric': 0.09732668781150884, 'fraction_numerical': 0.001993656547349343, 'mean_word_length': 3.881026094648386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '6676344', 'n_tokens_mistral': 3921, 'n_tokens_neox': 3362, 'n_words': 1044}