<commit_before>'use strict'

utils = require 'utils'
signal = require 'signal'

isFirefox = navigator.userAgent.indexOf('Firefox') isnt -1

module.exports = (impl) ->
	{Item, Image} = impl.Types
	implUtils = impl.utils
	hatchery = impl._hatchery

	impl.DEFAULT_FONTS =
		__proto__: null
		'sans': 'neft-sans-family'
		'sans-serif': 'neft-sans-family-serif'
		'monospace': 'neft-monospace-family'

	textImpl = {}
	signal.create textImpl, 'fontReady'

	sizeUpdatePending = false

	updateSize = do ->
		queue = []
		queueItems = Object.create null
		windowLoadQueue = []
		pending = false

		textImpl.onFontReady ->
			while elem = windowLoadQueue.pop()
				updateSize elem
			return

		updateAll = ->
			pending = false
			sizeUpdatePending = true
			for elem in queue
				updateSizeNow elem
			sizeUpdatePending = false
			return

		updateAllAndClean = ->
			updateAll()
			while queue.length
				item = queue.pop()
				queueItems[item.__hash__] = false

				if item._impl.textElement.parentNode is hatchery
					item._impl.elem.appendChild item._impl.textElement
			return

		updateSizeNow = (item) ->
			{textElement} = item._impl

			if item._impl.autoWidth
				item.width = textElement.scrollWidth+1

			if item._impl.autoHeight
				item.height = textElement.scrollHeight

		(item) ->
			if queueItems[item.__hash__]
				return

			if not isFontReady
				windowLoadQueue.push item
				return

			queueItems[item.__hash__] = true
			queue.push item

			if item._data.height is 0
				hatchery.appendChild item._impl.textElement

			unless pending
				setImmediate updateAll
				setTimeout updateAllAndClean, 500
				pending = true

	reloadFontFamilyQueue = []
	isFontReady = false
	window.addEventListener 'load', ->
		setTimeout ->
			isFontReady = true

			while elem = reloadFontFamilyQueue.pop()
				elem.style.fontFamily = elem.style.fontFamily

			styles = document.createElement 'style'
			styles.innerHTML = SHEET
			document.body.appendChild styles

			textImpl.fontReady()
		, 1000

	updateContent = do ->
		queue = []
		queueItems = Object.create null
		pending = false

		updateItems = ->
			pending = false

			# update
			for item in queue
				val = item._data.text
				if val.indexOf('<') isnt -1
					item._impl.textElement.innerHTML = val
				else
					item._impl.textElement.textContent = val

			while queue.length
				item = queue.pop()
				queueItems[item.__hash__] = false
				updateSize item

			return

		(item) ->
			if queueItems[item.__hash__]
				return

			queueItems[item.__hash__] = true
			queue.push item

			unless pending
				setImmediate updateItems
				pending = true

	onWidthChanged = ->
		if not sizeUpdatePending
			{width} = @
			{textElement} = @_impl
			auto = @_impl.autoWidth = width is 0
			textElement.style.whiteSpace = if auto then 'pre' else 'pre-wrap'
			textElement.style.width = if auto then 'auto' else "#{width}px"
			if auto
				updateSize @

	onHeightChanged = ->
		if not sizeUpdatePending
			{height} = @
			{textElement} = @_impl
			auto = @_impl.autoHeight = height is 0
			textElement.style.height = if auto then 'auto' else "#{height}px"
			if auto
				updateSize @

	SHEET = """
		.text {
			width: auto;
			height: auto;
			white-space: pre;
			font-size: 14px;
			font-family: #{impl.DEFAULT_FONTS['sans-serif']}, sans-serif;
			margin-top: #{if isFirefox then 1 else 0}px;
		}
	"""

	DATA =
		autoWidth: true
		autoHeight: true
		textElement: null

	exports =
	DATA: DATA

	_createTextElement: (item) ->
		data = item._impl

		# textElement
		textElement = data.textElement = document.createElement 'span'

		# handlers
		item.onWidthChanged onWidthChanged<commit_msg>CSS test get sizing: interval added<commit_after>'use strict'

utils = require 'utils'
signal = require 'signal'

isFirefox = navigator.userAgent.indexOf('Firefox') isnt -1

module.exports = (impl) ->
	{Item, Image} = impl.Types
	implUtils = impl.utils

	# used to render not visible texts
	hatchery = impl._hatchery

	impl.DEFAULT_FONTS =
		__proto__: null
		'sans': 'neft-sans-family'
		'sans-serif': 'neft-sans-serif-family'
		'monospace': 'neft-monospace-family'

	textImpl = {}
	signal.create textImpl, 'fontReady'

	sizeUpdatePending = false

	updateSize = do ->
		MAX_CHECKS = 5

		queue = []
		queueItems = Object.create null
		windowLoadQueue = []
		pending = false
		intervalPending = false
		intervalId = 0

		textImpl.onFontReady ->
			while elem = windowLoadQueue.pop()
				updateSize elem
			return

		updateAll = ->
			pending = false
			sizeUpdatePending = true
			i = 0
			n = queue.length
			while i < n
				item = queue[i]
				updateSizeNow item
				if ++item._impl.sizeChecks >= MAX_CHECKS
					if i is n-1
						queue.pop()
						n--
					else
						queue[i] = queue.pop()
						i--
						n--
					queueItems[item.__hash__] = false
				i++
			sizeUpdatePending = false
			return

		updateAllInInterval = ->
			updateAll()
			unless queue.length
				clearInterval intervalId
				intervalPending = false

		updateSizeNow = (item) ->
			{textElement} = item._impl

			if item._impl.autoWidth
				item.width = textElement.scrollWidth+1

			if item._impl.autoHeight
				height = item.height = textElement.scrollHeight

			if height > 0
				if item._impl.textElement.parentNode is hatchery
					item._impl.elem.appendChild item._impl.textElement
				true
			else
				false

		(item) ->
			item._impl.sizeChecks = 0

			if queueItems[item.__hash__]
				return

			if not isFontReady
				windowLoadQueue.push item
				return

			queueItems[item.__hash__] = true
			queue.push item

			if item._data.height is 0
				hatchery.appendChild item._impl.textElement

			unless pending
				setImmediate updateAll
				pending = true

			unless intervalPending
				intervalId = setInterval updateAllInInterval, 500
				intervalPending = true

	reloadFontFamilyQueue = []
	isFontReady = false
	window.addEventListener 'load', ->
		setTimeout ->
			isFontReady = true

			while elem = reloadFontFamilyQueue.pop()
				elem.style.fontFamily = elem.style.fontFamily

			styles = document.createElement 'style'
			styles.innerHTML = SHEET
			document.body.appendChild styles

			textImpl.fontReady()
		, 1000

	updateContent = do ->
		queue = []
		queueItems = Object.create null
		pending = false

		updateItems = ->
			pending = false

			# update
			for item in queue
				val = item._data.text
				if val.indexOf('<') isnt -1
					item._impl.textElement.innerHTML = val
				else
					item._impl.textElement.textContent = val

			while queue.length
				item = queue.pop()
				queueItems[item.__hash__] = false
				updateSize item

			return

		(item) ->
			if queueItems[item.__hash__]
				return

			queueItems[item.__hash__] = true
			queue.push item

			unless pending
				setImmediate updateItems
				pending = true

	onWidthChanged = ->
		if not sizeUpdatePending
			{width} = @
			{textElement} = @_impl
			auto = @_impl.autoWidth = width is 0
			textElement.style.whiteSpace = if auto then 'pre' else 'pre-wrap'
			textElement.style.width = if auto then 'auto' else "#{width}px"
			if auto
				updateSize @

	onHeightChanged = ->
		if not sizeUpdatePending
			{height} = @
			{textElement} = @_impl
			auto = @_impl.autoHeight = height is 0
			textElement.style.height = if auto then 'auto' else "#{height}px"
			if auto
				updateSize @

	SHEET = """
		.text {
			width: auto;
			height: auto;
			white-space: pre;
			font-size: 14px;
			font-family: #{impl.DEFAULT_FONTS['sans-serif']}, sans-serif;
			margin-top: #{if isFirefox then 1 else 0}px;
		}
	"""

	DATA =
		autoWidth: true
		autoHeight: true
		textElement: null
		sizeChecks: 0

	exports =
	DATA: DATA

	_createTextElement: (item) ->
		data = item._impl

		# textElement
		textElement = data.textElement = document.createElement 'span'

		# handlers
		item.onWidthChanged onWidthChanged
{'fraction_non_alphanumeric': 0.09896033885252215, 'fraction_numerical': 0.005262482351431138, 'mean_word_length': 2.959349593495935, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 35, 'https://': 0, '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': '2238084', 'n_tokens_mistral': 3120, 'n_tokens_neox': 2480, 'n_words': 627}