<commit_before>(function( Factlink ) {
    
// Function to select the found ranges
Factlink.selectRanges = function(ranges, id){
    var i, k;
    // Loop through ranges (backwards)
    for ( i = ranges.length; i--; ){
        // Current range
        var range = ranges[i],
        
            // Start- and EndNodes
            startNode = range.startContainer,
            endNode = range.endContainer,

            // Try to find out if there are other matches within this element
            j = i,
            // Helper for posible extra matches within the current startNode
            extraMatches = [],
            
            obj;
                
        while ( --j > 0 && ranges[j].startContainer === startNode ) {
            // Push the match to the extraMatches helper
            extraMatches.push({
                'startOffset' : ranges[j].startOffset,
                'endOffset' : ranges[j].endOffset
            });

            // decrease the increment so this element 
            // won't be matched in the next loop
            --i;
        }
                
        // Insert the actual span
        this.replaceFactNodes(range.startOffset, 
                         range.endOffset, 
                         startNode, 
                         endNode, 
                         range.commonAncestorContainer);
        
        // If there are other matches within the startNode, 
        // process them here
        if ( extraMatches.length > 0 ) {
            for ( k = 0; k < extraMatches.length; k++ ) {
                obj = extraMatches[k];

                // Insert the "fact"-span in the same element
                this.replaceFactNodes(obj.startOffset, 
                                      obj.endOffset, 
                                      startNode, 
                                      endNode, 
                                      range.commonAncestorContainer );
            }
        }
    }
    
    // This is where the actual parsing takes place
    // this.results holds all the textNodes containing the facts
    for ( i = 0; i < this.results.length; i++ ) {
        var res = this.results[i];
        
        // Insert the fact-span
        insertFactSpan(res.startOffset, res.endOffset, res.node, id);
    }
};

// This is where the actual magic will take place
// A Span will be inserted around the startOffset/endOffset 
// in the startNode/endNode
var insertFactSpan = function(startOffset, endOffset, node, id) {
        // Value of the startNode, represented in an array
    var startNodeValue = node.nodeValue.split(''),
        // The selected text
        selTextStart = startNodeValue
                                .splice(startOffset, startNodeValue.length);
    
    if ( endOffset < node.nodeValue.length && endOffset !== 0 ) {
        var after = selTextStart
                        .splice(endOffset - startOffset , selTextStart.length)
                        .join('');

        // Slice the array by changing it's length
        selTextStart.length = endOffset - startOffset;

        // Insert the textnode with the remaining text after the 
        // current textNode
        node.parentNode
            .insertBefore( document.createTextNode(after), node.nextSibling );
    }
        // Create a reference to the actual "fact"-span
    var span = createFactSpan( selTextStart.join(''), id );

    // Remove the last part of the nodeValue
    node.nodeValue = startNodeValue.join('');
    
    // Insert the span right after the startNode 
    // (there is no insertAfter available)
    node.parentNode.insertBefore( span, node.nextSibling );
},

ids = [],

// Create a "fact"-span with the right attributes
createFactSpan = function(text, id){
    var span = document.createElement('span');

    // Set the span attributes
    span.className = "factlink";
    span.setAttribute('data-factid',id);<commit_msg>Added support of .fl-first class indicating the first element of a factlink (since factlinks can consist of multiple fact-spans, and we only want to hightight the first).<commit_after>(function( Factlink ) {
    
// Function to select the found ranges
Factlink.selectRanges = function(ranges, id){
    var i, k;
    // Loop through ranges (backwards)
    for ( i = ranges.length; i--; ){
        // Current range
        var range = ranges[i],
        
            // Start- and EndNodes
            startNode = range.startContainer,
            endNode = range.endContainer,

            // Try to find out if there are other matches within this element
            j = i,
            // Helper for posible extra matches within the current startNode
            extraMatches = [],
            
            obj;
                
        while ( --j > 0 && ranges[j].startContainer === startNode ) {
            // Push the match to the extraMatches helper
            extraMatches.push({
                'startOffset' : ranges[j].startOffset,
                'endOffset' : ranges[j].endOffset
            });

            // decrease the increment so this element 
            // won't be matched in the next loop
            --i;
        }
                
        // Insert the actual span
        this.replaceFactNodes(range.startOffset, 
                         range.endOffset, 
                         startNode, 
                         endNode, 
                         range.commonAncestorContainer);
        
        // If there are other matches within the startNode, 
        // process them here
        if ( extraMatches.length > 0 ) {
            for ( k = 0; k < extraMatches.length; k++ ) {
                obj = extraMatches[k];

                // Insert the "fact"-span in the same element
                this.replaceFactNodes(obj.startOffset, 
                                      obj.endOffset, 
                                      startNode, 
                                      endNode, 
                                      range.commonAncestorContainer );
            }
        }
    }
    
    // This is where the actual parsing takes place
    // this.results holds all the textNodes containing the facts
    for ( i = 0; i < this.results.length; i++ ) {
        var res = this.results[i];
        
        // Insert the fact-span
        insertFactSpan(res.startOffset, res.endOffset, res.node, id, i === 0);
    }
    
    // Empty the results placeholder so that results don't stack
    this.results = [];
};

// This is where the actual magic will take place
// A Span will be inserted around the startOffset/endOffset 
// in the startNode/endNode
var insertFactSpan = function(startOffset, endOffset, node, id, isFirst ) {
        // Value of the startNode, represented in an array
    var startNodeValue = node.nodeValue.split(''),
        // The selected text
        selTextStart = startNodeValue
                                .splice(startOffset, startNodeValue.length);
    
    if ( endOffset < node.nodeValue.length && endOffset !== 0 ) {
        var after = selTextStart
                        .splice(endOffset - startOffset , selTextStart.length)
                        .join('');

        // Slice the array by changing it's length
        selTextStart.length = endOffset - startOffset;

        // Insert the textnode with the remaining text after the 
        // current textNode
        node.parentNode
            .insertBefore( document.createTextNode(after), node.nextSibling );
    }
        // Create a reference to the actual "fact"-span
    var span = createFactSpan( selTextStart.join(''), id );
    
    // If this span is the first in a range of fact-spans
    if ( isFirst ) {
        // Add a class (used for icon adding)
        span.className = span.className + " fl-first";
    }

    // Remove the last part of the nodeValue
    node.nodeValue = startNodeValue.join('');
    
    // Insert the span right after the startNode 
    // (there is no insertAfter available)
    node.parentNode.insertBefore( span, node.nextSibling );
},

ids = [],

// Create a "fact"-span with the right attributes
createFactSpan = function(text, id){
    var span = document.createElement('span');

    // Set the span attributes
    span.className = "factlink";
    span.setAttribute('data-factid',id);
{'fraction_non_alphanumeric': 0.08918199419167473, 'fraction_numerical': 0.0013310745401742497, 'mean_word_length': 1.5876643706950533, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '788576', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2035, 'n_words': 667}