test/html-formatter.spec.js
/**
     * Formats HTML to meet the following specification:
     *   - Leaf elements
     *       Placed on one indented line if shorter than the character limit.
     *   - Opening tags
     *       Placed on one indented line if shorter than the character limit.
     *       Otherwise, each attribute is placed on an lines further indented 2 levels.
     *   - Closing tags
     *       Immediately after the opening tag if element is empty or shorter than the character limit.
     *       Otherwise, on one indented line.
     *   - Comment tags
     *       Placed on one indented line if shorter than the character limit.
     *       Otherwise, paragraphs (delimited by empty new lines) wrap at the character limit.
     *   - Text nodes
     *       Placed on one indented line if shorter than the character limit.
     *       Otherwise, paragraphs (delimited by two or more consecutive new lines) wrap at the character limit.
     */
Formats HTML to meet the following specification:
- Leaf elements
Placed on one indented line if shorter than the character limit.
- Opening tags
Placed on one indented line if shorter than the character limit.
Otherwise, each attribute is placed on an lines further indented 2 levels.
- Closing tags
Immediately after the opening tag if element is empty or shorter than the character limit.
Otherwise, on one indented line.
- Comment tags
Placed on one indented line if shorter than the character limit.
Otherwise, paragraphs (delimited by empty new lines) wrap at the character limit.
- Text nodes
Placed on one indented line if shorter than the character limit.
Otherwise, paragraphs (delimited by two or more consecutive new lines) wrap at the character limit.
Formats HTML to meet the following specification : - Leaf elements Placed on one indented line if shorter than the character limit . - Opening tags Placed on one indented line if shorter than the character limit . Otherwise each attribute is placed on an lines further indented 2 levels . - Closing tags Immediately after the opening tag if element is empty or shorter than the character limit . Otherwise on one indented line . - Comment tags Placed on one indented line if shorter than the character limit . Otherwise paragraphs ( delimited by empty new lines ) wrap at the character limit . - Text nodes Placed on one indented line if shorter than the character limit . Otherwise paragraphs ( delimited by two or more consecutive new lines ) wrap at the character limit .
formatHtml(unformattedHtml) {
        let indentLevel = 0;
        return unformattedHtml
            .split(HTML_REG_EXP.CAPTURE_TAGS)
            .reduce((html, tag) => {
            switch (HtmlFormatter.getHtmlType(tag)) {
                case HtmlType.DOCTYPE: return this.insertDoctype(tag, html, indentLevel);
                case HtmlType.OPENING_TAG: return this.insertOpeningTag(tag, html, indentLevel++);
                case HtmlType.CLOSING_TAG: return this.insertClosingTag(tag, html, --indentLevel);
                case HtmlType.VOID_TAG: return this.insertVoidTag(tag, html, indentLevel);
                case HtmlType.COMMENT_TAG: return this.insertCommentTag(tag, html, indentLevel);
                case HtmlType.TEXT_NODE: return this.insertTextNode(tag, html, indentLevel);
                case HtmlType.WHITESPACE: return this.insertWhitespace(tag, html);
                default: return html;
            }
        }, "")
            .trim() + "\n";
    }
formatHtml ( unformattedHtml ) { let indentLevel = 0 ; return unformattedHtml . split ( HTML_REG_EXP . CAPTURE_TAGS ) . reduce ( ( html , tag ) => { switch ( HtmlFormatter . getHtmlType ( tag ) ) { case HtmlType . DOCTYPE : return this . insertDoctype ( tag , html , indentLevel ) ; case HtmlType . OPENING_TAG : return this . insertOpeningTag ( tag , html , indentLevel ++ ) ; case HtmlType . CLOSING_TAG : return this . insertClosingTag ( tag , html , -- indentLevel ) ; case HtmlType . VOID_TAG : return this . insertVoidTag ( tag , html , indentLevel ) ; case HtmlType . COMMENT_TAG : return this . insertCommentTag ( tag , html , indentLevel ) ; case HtmlType . TEXT_NODE : return this . insertTextNode ( tag , html , indentLevel ) ; case HtmlType . WHITESPACE : return this . insertWhitespace ( tag , html ) ; default : return html ; } } , "" ) . trim ( ) + "\n" ; }
Formats HTML to meet the following specification:
   - Leaf elements
       Placed on one indented line if shorter than the character limit.
Formats HTML to meet the following specification : - Leaf elements Placed on one indented line if shorter than the character limit .