@@ -73,9 +73,6 @@ export default class Parser {
7373
7474 this . css = typeof this . rule === 'string' ? this . rule : this . rule . selector ;
7575
76- if ( this . options . lossy ) {
77- this . css = this . css . trim ( ) ;
78- }
7976 this . tokens = tokenize ( {
8077 css : this . css ,
8178 error : this . _errorGenerator ( ) ,
@@ -346,15 +343,15 @@ export default class Parser {
346343 this . currToken [ TOKEN . TYPE ] === tokens . combinator ) ) {
347344 const content = this . content ( ) ;
348345 if ( this . nextToken && this . nextToken [ TOKEN . TYPE ] === tokens . combinator ) {
349- node . spaces . before = this . parseSpace ( content ) ;
346+ node . spaces . before = this . optionalSpace ( content ) ;
350347 node . source = getTokenSource ( this . nextToken ) ;
351348 node . sourceIndex = this . nextToken [ TOKEN . START_POS ] ;
352349 } else if ( this . prevToken && this . prevToken [ TOKEN . TYPE ] === tokens . combinator ) {
353- node . spaces . after = this . parseSpace ( content ) ;
350+ node . spaces . after = this . optionalSpace ( content ) ;
354351 } else if ( this . currToken [ TOKEN . TYPE ] === tokens . combinator ) {
355352 node . value = content ;
356353 } else if ( this . currToken [ TOKEN . TYPE ] === tokens . space ) {
357- node . value = this . parseSpace ( content , ' ' ) ;
354+ node . value = this . requiredSpace ( content ) ;
358355 }
359356 this . position ++ ;
360357 }
@@ -512,14 +509,14 @@ export default class Parser {
512509 this . prevToken [ TOKEN . TYPE ] === tokens . comma ||
513510 this . prevToken [ TOKEN . TYPE ] === tokens . openParenthesis
514511 ) {
515- this . spaces = this . parseSpace ( content ) ;
512+ this . spaces = this . optionalSpace ( content ) ;
516513 this . position ++ ;
517514 } else if (
518515 this . position === ( this . tokens . length - 1 ) ||
519516 this . nextToken [ TOKEN . TYPE ] === tokens . comma ||
520517 this . nextToken [ TOKEN . TYPE ] === tokens . closeParenthesis
521518 ) {
522- this . current . last . spaces . after = this . parseSpace ( content ) ;
519+ this . current . last . spaces . after = this . optionalSpace ( content ) ;
523520 this . position ++ ;
524521 } else {
525522 this . combinator ( ) ;
@@ -564,7 +561,7 @@ export default class Parser {
564561 if ( current . lastIndexOf ( '\\' ) === current . length - 1 ) {
565562 let next = this . nextToken ;
566563 if ( next && next [ TOKEN . TYPE ] === tokens . space ) {
567- word += this . parseSpace ( this . content ( next ) , ' ' ) ;
564+ word += this . requiredSpace ( this . content ( next ) ) ;
568565 this . position ++ ;
569566 }
570567 }
@@ -608,11 +605,12 @@ export default class Parser {
608605 } ;
609606 node = new ID ( unescapeProp ( idOpts , "value" ) ) ;
610607 } else {
611- node = new Tag ( {
608+ let tagOpts = {
612609 value,
613610 source,
614611 sourceIndex,
615- } ) ;
612+ } ;
613+ node = new Tag ( unescapeProp ( tagOpts , "value" ) ) ;
616614 }
617615 this . newNode ( node , namespace ) ;
618616 // Ensure that the namespace is used only once
@@ -710,46 +708,32 @@ export default class Parser {
710708 ) ;
711709 }
712710
713- parseNamespace ( namespace ) {
714- if ( this . options . lossy && typeof namespace === 'string' ) {
715- const trimmed = namespace . trim ( ) ;
716- if ( ! trimmed . length ) {
717- return true ;
718- }
719-
720- return trimmed ;
721- }
722-
723- return namespace ;
711+ requiredSpace ( space ) {
712+ return this . options . lossy ? ' ' : space ;
724713 }
725714
726- parseSpace ( space , replacement = '' ) {
727- return this . options . lossy ? replacement : space ;
728- }
729-
730- parseValue ( value ) {
731- if ( ! this . options . lossy || ! value || typeof value !== 'string' ) {
732- return value ;
733- }
734- return value . trim ( ) ;
715+ optionalSpace ( space ) {
716+ return this . options . lossy ? '' : space ;
735717 }
736718
737719 parseParenthesisToken ( token ) {
738720 const content = this . content ( token ) ;
739721 if ( token [ TOKEN . TYPE ] === tokens . space ) {
740- return content ;
741- }
742-
743- if ( token [ 0 ] === tokens . space ) {
744- return this . parseSpace ( content , ' ' ) ;
722+ return this . requiredSpace ( content ) ;
745723 }
746724
747- return this . parseValue ( content ) ;
725+ return content ;
748726 }
749727
750728 newNode ( node , namespace ) {
751729 if ( namespace ) {
752- node . namespace = this . parseNamespace ( namespace ) ;
730+ if ( / ^ + $ / . test ( namespace ) ) {
731+ if ( ! this . options . lossy ) {
732+ this . spaces = ( this . spaces || '' ) + namespace ;
733+ }
734+ namespace = true ;
735+ }
736+ node . namespace = namespace ;
753737 }
754738 if ( this . spaces ) {
755739 node . spaces . before = this . spaces ;
0 commit comments