Skip to content

Commit

Permalink
correct body word count with <br>
Browse files Browse the repository at this point in the history
closes EVAL-4019
flag=none

Test Plan:
- Create a text entry assignment
- As a student, start your submission
- Add a single word to the submission box, hit shift+return/enter to add
 a line break, then add a second word
- Notice that the the total word count below the RCE shows 2 words
- Submit to the assignment
- Navigate to Speedgrader and view the word count. Notice that it shows
that there is 2 words and matches the RCE count.

Change-Id: I6ad8b20eff50805a12ffef0362adaaa5bbe96050
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/345625
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Aaron Shafovaloff <ashafovaloff@instructure.com>
QA-Review: Cameron Ray <cameron.ray@instructure.com>
Product-Review: Cameron Ray <cameron.ray@instructure.com>
  • Loading branch information
kaibjorkman committed Apr 18, 2024
1 parent 02797d9 commit 83bf5d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/models/submission.rb
Expand Up @@ -3043,8 +3043,15 @@ def grading_status

def word_count
if body && submission_type != "online_quiz"
tinymce_wordcount_count_regex = /[\w\u2019\x27\-\u00C0-\u1FFF]+/
ActionController::Base.helpers.strip_tags(body).scan(tinymce_wordcount_count_regex).size
segments = body.split(%r{<br\s*/?>})
word_count = 0
segments.each do |segment|
tinymce_wordcount_count_regex = /(?:[\w\u2019\x27\-\u00C0-\u1FFF]+|(?<=<br>)([^<]+)|([^<]+)(?=<br>))/
words = ActionController::Base.helpers.strip_tags(segment).scan(tinymce_wordcount_count_regex).size
word_count += words
end

word_count
elsif versioned_attachments.present?
Attachment.where(id: versioned_attachments.pluck(:id)).sum(:word_count)
end
Expand Down
5 changes: 5 additions & 0 deletions spec/models/submission_spec.rb
Expand Up @@ -8973,6 +8973,11 @@ def check_cache_clear
expect(submission.word_count).to eq 2
end

it "returns the word count if body is split up by <br> tags" do
submission.update(body: "test<br>submission")
expect(submission.word_count).to eq 2
end

it "returns nil if there is no body" do
expect(submission.body).to be_nil
expect(submission.word_count).to be_nil
Expand Down

0 comments on commit 83bf5d4

Please sign in to comment.