<commit_before>describe 'up.flow', ->
  
  describe 'Javascript functions', ->

    beforeEach ->
      jasmine.Ajax.install()
    
    describe 'up.replace', ->

      if up.browser.canPushState()

        beforeEach ->

          affix('.before').text('old-before')
          affix('.middle').text('old-middle')
          affix('.after').text('old-after')
    
          @respond = ->
            jasmine.Ajax.requests.mostRecent().respondWith
              status: 200
              contentType: 'text/html'
              responseText:
                """
                <div class="before">new-before</div>
                <div class="middle">new-middle</div>
                <div class="after">new-after</div>
                """      
        
        it 'replaces the given selector with the same selector from a freshly fetched page', (done) ->
          @request = up.replace('.middle', '/path')
          @respond()
          @request.then ->
            expect($('.before')).toHaveText('old-before')
            expect($('.middle')).toHaveText('new-middle')
            expect($('.after')).toHaveText('old-after')
            done()      
          
        it 'should set the browser location to the given URL', (done) ->
          @request = up.replace('.middle', '/path')
          @respond()
          @request.then ->
            expect(window.location.pathname).toBe('/path')
            done()
            
        it 'marks the element with the URL from which it was retrieved', (done) ->
          @request = up.replace('.middle', '/path')
          @respond()
          @request.then ->
            expect($('.middle').attr('up-source')).toMatch(/\/path$/)
            done()
            
        it 'replaces multiple selectors separated with a comma', (done) ->
          @request = up.replace('.middle, .after', '/path')
          @respond()
          @request.then ->
            expect($('.before')).toHaveText('old-before')
            expect($('.middle')).toHaveText('new-middle')
            expect($('.after')).toHaveText('new-after')
            done()
            
      else
        
        it 'makes a full page load', ->
          spyOn(up.browser, 'loadPage')
          up.replace('.selector', '/path')<commit_msg>Add a spec to ensure that inserted fragments with script tags execute those tags<commit_after>describe 'up.flow', ->
  
  describe 'Javascript functions', ->

    beforeEach ->
      jasmine.Ajax.install()
    
    describe 'up.replace', ->

      if up.browser.canPushState()

        beforeEach ->

          affix('.before').text('old-before')
          affix('.middle').text('old-middle')
          affix('.after').text('old-after')

          @responseText =
            """
            <div class="before">new-before</div>
            <div class="middle">new-middle</div>
            <div class="after">new-after</div>
            """

          @respond = ->
            jasmine.Ajax.requests.mostRecent().respondWith
              status: 200
              contentType: 'text/html'
              responseText: @responseText

        it 'replaces the given selector with the same selector from a freshly fetched page', (done) ->
          @request = up.replace('.middle', '/path')
          @respond()
          @request.then ->
            expect($('.before')).toHaveText('old-before')
            expect($('.middle')).toHaveText('new-middle')
            expect($('.after')).toHaveText('old-after')
            done()      
          
        it 'should set the browser location to the given URL', (done) ->
          @request = up.replace('.middle', '/path')
          @respond()
          @request.then ->
            expect(window.location.pathname).toBe('/path')
            done()
            
        it 'marks the element with the URL from which it was retrieved', (done) ->
          @request = up.replace('.middle', '/path')
          @respond()
          @request.then ->
            expect($('.middle').attr('up-source')).toMatch(/\/path$/)
            done()
            
        it 'replaces multiple selectors separated with a comma', (done) ->
          @request = up.replace('.middle, .after', '/path')
          @respond()
          @request.then ->
            expect($('.before')).toHaveText('old-before')
            expect($('.middle')).toHaveText('new-middle')
            expect($('.after')).toHaveText('new-after')
            done()

        it 'executes those script-tags in the response that get inserted into the DOM', (done) ->
          window.scriptTagExecuted = jasmine.createSpy('scriptTagExecuted')

          @responseText =
            """
            <div class="before">
              new-before
              <script type="text/javascript">
                window.scriptTagExecuted('before')
              </script>
            </div>
            <div class="middle">
              new-middle
              <script type="text/javascript">
                window.scriptTagExecuted('middle')
              </script>
            </div>
            """

          @request = up.replace('.middle', '/path')
          @respond()

          @request.then ->
            expect(window.scriptTagExecuted).not.toHaveBeenCalledWith('before')
            expect(window.scriptTagExecuted).toHaveBeenCalledWith('middle')
            done()

      else
        
        it 'makes a full page load', ->
          spyOn(up.browser, 'loadPage')
          up.replace('.selector', '/path')
{'fraction_non_alphanumeric': 0.15359297860669227, 'fraction_numerical': 0.0010970927043335162, 'mean_word_length': 1.6501937984496124, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 55, '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': '5893437', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1406, 'n_words': 295}