@@ -1317,6 +1317,10 @@ var OpenScript = {
13171317 }
13181318
13191319 root . append ( rootFrag ) ;
1320+
1321+ root . toString = function ( ) {
1322+ return this . outerHTML ;
1323+ }
13201324
13211325 if ( parent ) {
13221326
@@ -1455,25 +1459,7 @@ var OpenScript = {
14551459 * @param {string|HTMLElement } value
14561460 */
14571461 toElement = ( value ) => {
1458-
1459- if ( value instanceof DocumentFragment || value instanceof HTMLElement ) {
1460- return value ;
1461- }
1462-
1463- if ( value ?. length === 0 ) return this . dom . createTextNode ( '' ) ;
1464-
1465- let tmp = this . dom . createElement ( "ojs-group" ) ;
1466- tmp . innerHTML = value ;
1467-
1468- if ( tmp . children . length > 1 ) return tmp ;
1469-
1470- if ( tmp . children . length === 0
1471- &&
1472- tmp . firstChild . nodeName === "#text"
1473- ) return this . dom . createTextNode ( value ) ;
1474-
1475-
1476- return tmp . children [ 0 ] ;
1462+ return value ;
14771463 }
14781464 } ,
14791465
@@ -1560,6 +1546,141 @@ var OpenScript = {
15601546 }
15611547 return text . replace ( / \. / g, "/" ) ;
15621548 }
1549+
1550+ /**
1551+ * Splits a file into smaller strings
1552+ * based on the class in that file
1553+ */
1554+ Splitter = class Splitter {
1555+
1556+ /**
1557+ * Gets the class Signature
1558+ * @param {string } content
1559+ * @param {int } start
1560+ * @param {object<> } signature {name: string, signature: string, start: number, end: number}
1561+ */
1562+ classSignature ( content , start ) {
1563+ const signature = { name : '' , definition : '' , start : - 1 , end : - 1 , parent : null } ;
1564+
1565+ let startAt = start ;
1566+
1567+ let output = [ ] ;
1568+ let tmp = '' ;
1569+
1570+ let pushTmp = ( index ) => {
1571+ if ( tmp . length === 0 ) return ;
1572+
1573+ if ( output . length === 0 ) startAt = index ;
1574+
1575+ output . push ( tmp ) ;
1576+ tmp = '' ;
1577+ }
1578+
1579+ for ( let i = start ; i < content . length ; i ++ ) {
1580+ let ch = content [ i ] ;
1581+
1582+ if ( / [ \s \r \t \n ] / . test ( ch ) ) {
1583+ pushTmp ( i ) ;
1584+
1585+ continue ;
1586+ }
1587+
1588+ if ( / \{ / . test ( ch ) ) {
1589+ pushTmp ( i ) ;
1590+ signature . end = i ;
1591+
1592+ break ;
1593+ }
1594+
1595+ tmp += ch ;
1596+ }
1597+
1598+ signature . start = startAt ;
1599+
1600+ if ( output . length % 2 !== 0 ) throw Error ( `Invalid Class File. Could not parse \`${ content } \` from index ${ start } because it doesn't have the proper syntax. ${ content . substring ( start ) } ` ) ;
1601+
1602+ if ( output . length > 2 ) {
1603+ signature . parent = output [ 3 ] ;
1604+ }
1605+
1606+ signature . name = output [ 1 ] ;
1607+ signature . definition = output . join ( ' ' ) ;
1608+
1609+ return signature ;
1610+ }
1611+
1612+ /**
1613+ * Splits the content of the file by
1614+ * class
1615+ * @param {string } content file content
1616+ * @return {Map<string,string> } class map
1617+ */
1618+ classes ( content ) {
1619+
1620+ content = content . trim ( ) ;
1621+
1622+ const stack = [ ] ;
1623+ const map = new Map ( ) ;
1624+ const qMap = new Map ( [ [ `'` , true ] , [ `"` , true ] , [ "`" , true ] ] ) ;
1625+
1626+
1627+ let index = 0 ;
1628+ let code = '' ;
1629+
1630+ while ( index < content . length ) {
1631+
1632+ let signature = this . classSignature ( content , index ) ;
1633+ index = signature . end ;
1634+
1635+ let ch = content [ index ] ;
1636+ stack . push ( ch ) ;
1637+
1638+ code += signature . definition + ' ' ;
1639+ code += ch ;
1640+
1641+ let text = [ ] ;
1642+
1643+ index ++ ;
1644+
1645+ while ( stack . length && index < content . length ) {
1646+ ch = content [ index ] ;
1647+ code += ch ;
1648+
1649+ if ( qMap . has ( ch ) ) {
1650+
1651+ text . push ( ch ) ;
1652+ index ++ ;
1653+
1654+ while ( text . length && index < content . length ) {
1655+ ch = content [ index ] ;
1656+ code += ch ;
1657+
1658+ let last = text . length - 1 ;
1659+
1660+ if ( qMap . has ( ch ) && ch === text [ last ] ) {
1661+ text . pop ( ) ;
1662+ }
1663+ else if ( ch === '\n' && ( text [ last ] === '"' || text [ last ] === "'" ) ) {
1664+ text . pop ( ) ;
1665+ }
1666+
1667+ index ++ ;
1668+ }
1669+ continue ;
1670+ }
1671+ if ( / \{ / . test ( ch ) ) stack . push ( ch ) ;
1672+ if ( / \} / . test ( ch ) ) stack . pop ( ) ;
1673+
1674+ index ++ ;
1675+ }
1676+
1677+ map . set ( signature . name , { extends : signature . parent , code, name : signature . name , signature : signature . definition } ) ;
1678+ code = '' ;
1679+ }
1680+
1681+ return map ;
1682+ }
1683+ }
15631684
15641685 /**
15651686 *
@@ -1574,14 +1695,7 @@ var OpenScript = {
15741695 let response = await fetch ( `${ this . dir } /${ this . normalize ( fileName ) } ${ this . extension } ?v=${ this . version } ` ) ;
15751696
15761697 let classes = await response . text ( ) ;
1577-
1578- let matches = classes . match ( / c l a s s \s + [ A - Z a - z ] + / g) ;
1579-
1580-
1581- // checking if there is only one class
1582- if ( matches && matches . index ) matches = [ matches [ 0 ] ] ;
1583-
1584- classes = classes . split ( / c l a s s \s + [ A - Z a - z ] + / g) ;
1698+ let content = classes ;
15851699
15861700 let classMap = new Map ( ) ;
15871701 let codeMap = new Map ( ) ;
@@ -1591,31 +1705,20 @@ var OpenScript = {
15911705 let prefix = prefixArray . join ( '.' ) ;
15921706 if ( prefix . length > 0 && ! / ^ \s + $ / . test ( prefix ) ) prefix += '.' ;
15931707
1594- classes . shift ( ) ;
1708+ let splitter = new this . Splitter ( ) ;
15951709
1596- for ( let k in classes ) {
1597- if ( classes [ k ] . length === 0 || / ^ [ \s + \n + \r + \t + ] * $ / . test ( classes [ k ] ) ) continue ;
1598-
1599- classes [ k ] = classes [ k ] . trim ( ) ;
1600- matches [ k ] = matches [ k ] . trim ( ) ;
1601-
1602- let m = matches [ k ] . match ( / \s + / ) ;
1603- let name = matches [ k ] . substring ( m [ 'index' ] ) ?. trim ( ) ;
1604-
1605- let key = prefix + name ;
1606-
1607- classMap . set ( key , [ name , `${ matches [ k ] } ${ classes [ k ] } ` ] ) ;
1710+ classes = splitter . classes ( content ) ;
16081711
1712+ for ( let [ k , v ] of classes ) {
1713+ let key = prefix + k ;
1714+ classMap . set ( key , [ k , v . code ] ) ;
16091715 }
16101716
16111717 for ( let [ k , arr ] of classMap ) {
16121718
1613- let inheritance = arr [ 1 ] . match ( / e x t e n d s [ \s \n ] + \s * . + \s * [ \s \n ] + \{ / ) ;
1719+ let parent = classes . get ( arr [ 0 ] ) . extends ;
16141720
1615- if ( inheritance ) {
1616-
1617- let parent = inheritance [ 0 ] . replace ( / [ \n \s \{ ] + / g, " " ) ;
1618- parent = parent . replace ( / e x t e n d s / g, "" ) . trim ( ) ;
1721+ if ( parent ) {
16191722
16201723 let original = parent ;
16211724
@@ -1639,10 +1742,12 @@ var OpenScript = {
16391742 }
16401743 }
16411744 else {
1642- let replacement = inheritance [ 0 ] . replace ( original , parent ) ;
1745+ let signature = classes . get ( arr [ 0 ] ) . signature ;
1746+
1747+ let replacement = signature . replace ( original , parent ) ;
16431748 //console.log(k, arr[1]);
16441749
1645- let c = arr [ 1 ] . replace ( inheritance [ 0 ] , replacement ) ;
1750+ let c = arr [ 1 ] . replace ( signature , replacement ) ;
16461751 arr [ 1 ] = c ;
16471752 }
16481753 }
0 commit comments