'use strict' ; import * as assert from 'assert' ; import * as nodes from '../../parser/cssNodes' ; import { Parser } from '../../parser/cssParser' ; export class PrintingVisitor implements nodes . IVisitor { public tree : string [ ] = [ ] ; public visitNode ( node ) { this . tree . push ( nodes . NodeType [ node . type ] . toLowerCase ( ) ) ; return true ; } } export function assertNodes ( fn , input , expected ) { let node = fn ( input ) ; let visitor = new PrintingVisitor ( ) ; node . acceptVisitor ( visitor ) ; let actual = visitor . tree . join ( ',' ) + ',' ; let segments = expected . split ( ',' ) ; let oldIndex = undefined ; let index = - 1 ; while ( segments . length > 0 ) { let segment = segments . shift ( ) ! ; if ( segment === '...' ) { continue ; } index = actual . indexOf ( segment + ',' , oldIndex ) ; if ( oldIndex && index <= oldIndex ) { assert . ok ( false , segment + ' NOT found in ' + actual ) ; } oldIndex = index + segment . length ; } assert . ok ( true ) ; } suite ( 'CSS - Nodes' , ( ) => { test ( 'Test Node' , function ( ) { let node = new nodes . Node ( ) ; assert . equal ( node . offset , - 1 ) ; assert . equal ( node . length , - 1 ) ; assert . equal ( node . parent , null ) ; assert . equal ( node . getChildren ( ) . length , 0 ) ; let c = 0 ; node . accept ( ( n ) => { assert . ok ( n === node ) ; c += 1 ; return true ; } ) ; assert . equal ( c , 1 ) ; let child = new nodes . Node ( ) ; node . adoptChild ( child ) ; c = 0 ; let expects = [ node , child ] ; node . accept ( ( n ) => { assert . ok ( n === expects [ c ] ) ; c += 1 ; return true ; } ) ; assert . equal ( c , 2 ) ; } ) ; test ( 'Test Adopting' , function ( ) { let child = new nodes . Node ( ) ; let p1 = new nodes . Node ( ) ; let p2 = new nodes . Node ( ) ; assert . ok ( child . parent === null ) ; assert . equal ( p1 . getChildren ( ) . length , 0 ) ; assert . equal ( p2 . getChildren ( ) . length , 0 ) ; p1 . adoptChild ( child ) ; assert . ok ( child . parent === p1 ) ; assert . equal ( p1 . getChildren ( ) . length , 1 ) ; assert . equal ( p2 . getChildren ( ) . length , 0 ) ; p2 . adoptChild ( child ) ; assert . ok ( child . parent === p2 ) ; assert . equal ( p1 . getChildren ( ) . length , 0 ) ; assert . equal ( p2 . getChildren ( ) . length , 1 ) ; } ) ; function ruleset ( input ) { let parser = new Parser ( ) ; let node = parser . internalParse ( input , parser . _parseRuleset ) ! ; return node ; } test ( 'RuleSet' , function ( ) { assertNodes ( ruleset , 'selector { prop: value }' , 'ruleset,...,selector,...,declaration,...,property,...,expression' ) ; assertNodes ( ruleset , 'selector { prop; }' , 'ruleset,...,selector,...,selector' ) ; } ) ; test ( 'Keyframe' , function ( ) { function fn ( input ) { let parser = new Parser ( ) ; let node = parser . internalParse ( input , parser . _parseKeyframe ) ! ; return node ; } assertNodes ( fn , '@keyframes name { from { top: 0px} to { top: 100px } }' , 'keyframe,identifier,keyframeselector,declaration,keyframeselector,declaration' ) ; } ) ; } ) ;