@@ -39,19 +39,30 @@ const TOKEN_RE = /(#[^\n]*)|((?:\b[fFrRbBuU]{1,2})?(?:"""[\s\S]*?"""|'''[\s\S]*?
3939
4040const esc = ( s ) => s . replace ( / [ & < > ] / g, ( c ) => ( { '&' : '&' , '<' : '<' , '>' : '>' } [ c ] ) ) ;
4141
42- const highlight = ( src ) => esc ( src ) . replace ( TOKEN_RE , ( m , com , str , num , word ) => {
43- if ( com ) return `<span class="tk-com">${ com } </span>` ;
44- if ( str ) return `<span class="tk-str">${ str } </span>` ;
45- if ( num ) return `<span class="tk-num">${ num } </span>` ;
46- if ( word ) {
47- if ( KW . has ( word ) ) return `<span class="tk-kw">${ word } </span>` ;
48- if ( LIT . has ( word ) ) return `<span class="tk-lit">${ word } </span>` ;
49- if ( BI . has ( word ) ) return `<span class="tk-bi">${ word } </span>` ;
50- return word ;
51- }
52- return m ;
53- } ) ;
54-
42+ const highlight = ( src ) => {
43+ const escaped = esc ( src ) ;
44+ return escaped . replace ( TOKEN_RE , ( m , com , str , num , word , offset , fullStr ) => {
45+ if ( com ) return `<span class="tk-com">${ com } </span>` ;
46+ if ( str ) return `<span class="tk-str">${ str } </span>` ;
47+ if ( num ) return `<span class="tk-num">${ num } </span>` ;
48+ if ( word ) {
49+ const isEntity = fullStr [ offset - 1 ] === '&' && fullStr [ offset + word . length ] === ';' ;
50+ if ( isEntity ) return word ;
51+
52+ if ( KW . has ( word ) ) return `<span class="tk-kw">${ word } </span>` ;
53+ if ( LIT . has ( word ) ) return `<span class="tk-lit">${ word } </span>` ;
54+ if ( BI . has ( word ) ) return `<span class="tk-bi">${ word } </span>` ;
55+
56+ const isFunction = / ^ \s * \( / . test ( fullStr . slice ( offset + word . length ) ) ;
57+ if ( isFunction ) {
58+ return `<span class="tk-func">${ word } </span>` ;
59+ } else {
60+ return `<span class="tk-var">${ word } </span>` ;
61+ }
62+ }
63+ return m ;
64+ } ) ;
65+ } ;
5566const jar = CodeJar ( ed , ( editor ) => {
5667 editor . innerHTML = highlight ( editor . textContent ) ;
5768} , {
0 commit comments