1
1
import { expect } from 'chai'
2
- import { isDoubleQuote , isSingleQuote , isWhitespace , isSeparatorOrWhitespace , isApostrophe } from '../src/smartQuotes'
2
+ import { isDoubleQuote , isSingleQuote , isWhitespace , isSeparatorOrWhitespace , isApostrophe , replaceQuote } from '../src/smartQuotes'
3
3
4
4
const allSingleQuotes = [ '‘' , '’' , '‹' , '›' , '‚' , '‘' , '›' , '‹' , `'` , `‘` ]
5
5
const allDoubleQuotes = [ '«' , '»' , '»' , '«' , '"' , '"' , '“' , '”' , '”' , '”' , '“' , '“' , '„' , '“' ]
@@ -8,7 +8,7 @@ const nonStringValues = [undefined, null, true, 123, NaN]
8
8
const whitespaceChars = [ ' ' , '\t' , '\n' , '\r' , '\v' , '\f' ]
9
9
const separatorValues = [ '>' , '-' , '–—' ]
10
10
11
- describe ( 'Smart Quotes Helper Functions' , ( ) => {
11
+ describe ( 'Smart Quotes Helper Functions: ' , ( ) => {
12
12
describe ( 'isDoubleQuote' , ( ) => {
13
13
it ( 'Should return false for non double quote values' , ( ) => {
14
14
[ ...charValues , ...separatorValues , ...nonStringValues , ...allSingleQuotes ] . forEach ( value => {
@@ -52,7 +52,7 @@ describe('Smart Quotes Helper Functions', () => {
52
52
} )
53
53
54
54
describe ( 'isSeparatorOrWhitespace' , ( ) => {
55
- it ( 'should return false for non whitespace / separator characters' , ( ) => {
55
+ it ( 'should return false for non whitespace/ separator characters' , ( ) => {
56
56
[ ...charValues , ...nonStringValues ] . forEach ( value => {
57
57
expect ( isSeparatorOrWhitespace ( value ) ) . to . equal ( false , `Failed for: ${ value } ` )
58
58
} )
@@ -80,3 +80,37 @@ describe('Smart Quotes Helper Functions', () => {
80
80
} )
81
81
} )
82
82
83
+ const createRangeWithText = ( text ) => {
84
+ const textNode = document . createTextNode ( text )
85
+ const range = document . createRange ( )
86
+ range . selectNodeContents ( textNode )
87
+ return range
88
+ }
89
+
90
+ describe ( 'replaceQuote(): ' , ( ) => {
91
+ const testString = '123 "you'
92
+ const index = testString . indexOf ( '"' )
93
+
94
+ it ( 'should replace quote at given index' , ( ) => {
95
+ const range = createRangeWithText ( testString )
96
+ const replacedTextNode = replaceQuote ( range , index , '`' )
97
+ expect ( replacedTextNode . textContent ) . to . equal ( '123 `you' )
98
+ } )
99
+
100
+ it ( 'should return null if range is invalid' , ( ) => {
101
+ const invalidNodeValue = replaceQuote ( undefined , index , '`' )
102
+ expect ( invalidNodeValue ) . to . equal ( null )
103
+ } )
104
+
105
+ it ( 'should return null if range is empty' , ( ) => {
106
+ const range = createRangeWithText ( '' )
107
+ const replacedTextNode = replaceQuote ( range , 0 , '`' )
108
+ expect ( replacedTextNode ) . to . equal ( null )
109
+ } )
110
+
111
+ it ( 'should insert quote at the end, if index is out of bounce' , ( ) => {
112
+ const range = createRangeWithText ( testString )
113
+ const replacedTextNode = replaceQuote ( range , 40 , '`' )
114
+ expect ( replacedTextNode . textContent ) . to . equal ( `${ testString } ${ '`' } ` )
115
+ } )
116
+ } )
0 commit comments