src/lib/Parser.js
/**
  Consumes an XML declaration if possible.

  @returns {boolean}
    Whether an XML declaration was consumed.

  @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-XMLDecl
  */
Consumes an XML declaration if possible.
Consumes an XML declaration if possible .
consumeXmlDeclaration() {
    let { scanner } = this;
    if (!scanner.consumeStringFast('<?xml')) {
      return false;
    }
    if (!this.consumeWhitespace()) {
      this.error('Invalid XML declaration');
    }
    let version = Boolean(scanner.consumeStringFast('version'))
      && this.consumeEqual()
      && this.consumeSystemLiteral();
    if (version === false) {
      this.error('XML version is missing or invalid');
    } else if (!/^1\.[0-9]+$/.test(version)) {
      this.error('Invalid character in version number');
    }
    if (this.consumeWhitespace()) {
      let encoding = Boolean(scanner.consumeStringFast('encoding'))
        && this.consumeEqual()
        && this.consumeSystemLiteral();
      if (encoding) {
        this.consumeWhitespace();
      }
      let standalone = Boolean(scanner.consumeStringFast('standalone'))
        && this.consumeEqual()
        && this.consumeSystemLiteral();
      if (standalone) {
        if (standalone !== 'yes' && standalone !== 'no') {
          this.error('Only "yes" and "no" are permitted as values of `standalone`');
        }
        this.consumeWhitespace();
      }
    }
    if (!scanner.consumeStringFast('?>')) {
      this.error('Invalid or unclosed XML declaration');
    }
    return true;
  }
consumeXmlDeclaration ( ) { let { scanner } = this ; if ( ! scanner . consumeStringFast ( '<?xml' ) ) { return false ; } if ( ! this . consumeWhitespace ( ) ) { this . error ( 'Invalid XML declaration' ) ; } let version = Boolean ( scanner . consumeStringFast ( 'version' ) ) && this . consumeEqual ( ) && this . consumeSystemLiteral ( ) ; if ( version === false ) { this . error ( 'XML version is missing or invalid' ) ; } else if ( ! / ^1\.[0-9]+$ / . test ( version ) ) { this . error ( 'Invalid character in version number' ) ; } if ( this . consumeWhitespace ( ) ) { let encoding = Boolean ( scanner . consumeStringFast ( 'encoding' ) ) && this . consumeEqual ( ) && this . consumeSystemLiteral ( ) ; if ( encoding ) { this . consumeWhitespace ( ) ; } let standalone = Boolean ( scanner . consumeStringFast ( 'standalone' ) ) && this . consumeEqual ( ) && this . consumeSystemLiteral ( ) ; if ( standalone ) { if ( standalone !== 'yes' && standalone !== 'no' ) { this . error ( 'Only "yes" and "no" are permitted as values of `standalone`' ) ; } this . consumeWhitespace ( ) ; } } if ( ! scanner . consumeStringFast ( '?>' ) ) { this . error ( 'Invalid or unclosed XML declaration' ) ; } return true ; }
Consumes an XML declaration if possible.
Consumes an XML declaration if possible .