package org.commonmark.ext.headerids;

import org.commonmark.html.AttributeProvider;
import org.commonmark.html.HtmlRenderer;
import org.commonmark.parser.Parser;
import org.commonmark.test.RenderingTestCase;
import org.junit.Before;
import org.junit.Test;

public class HeaderIdTest extends RenderingTestCase {

    private static final Parser PARSER = Parser.builder().build();
    private static final HeaderIdAttributeProvider attributeProvider = HeaderIdAttributeProvider.create();
    private static HtmlRenderer RENDERER = HtmlRenderer.builder().attributeProvider(attributeProvider).build();

    @Before
    public void resetHeader() {
        RENDERER = HtmlRenderer.builder()
                .attributeProvider(HeaderIdAttributeProvider.create())
                .build();
    }

    @Test
    public void baseCaseSingleHeader() {
        assertRendering("# Heading here\n",
                "<h1 id=\"heading-here\">Heading here</h1>\n");
    }

    @Test
    public void singleHeaderWithCodeBlock() {
        assertRendering("Hi there\n# Heading `here`\n",
                "<p>Hi there</p>\n<h1 id=\"heading-here\">Heading <code>here</code></h1>\n");
    }

    @Test
    public void duplicateHeadersMakeUniqueIds() {
        assertRendering("# Heading here\n# Heading here",
                "<h1 id=\"heading-here\">Heading here</h1>\n<h1 id=\"heading-here1\">Heading here</h1>\n");
    }

    @Test
    public void duplicateHeadersOnceDissalowedCharactersMakeUniqueIds() {
        assertRendering("# Hi there\n" +
                "# ∂Hi å∂There˚∂ˆ´¨", "<h1 id=\"hi-there\">Hi there</h1>\n" +
                "<h1 id=\"hi-there1\">∂Hi å∂There˚∂ˆ´¨</h1>\n");
    }

    @Test
    public void testCaseIsIgnoredWhenComparingIds() {
        assertRendering("# HEADING here\n" +
                        "# heading here",
                "<h1 id=\"heading-here\">HEADING here</h1>\n" +
                        "<h1 id=\"heading-here1\">heading here</h1>\n");
    }

    @Test
    public void testNestedBlocks() {
        assertRendering("## `h` `e` **l** *l* o",
                "<h2 id=\"h-e-l-l-o\"><code>h</code> <code>e</code> <strong>l</strong> <em>l</em> o</h2>\n");
    }

    @Test
    public void noPrintableCharacters() {
        assertRendering("# ∂∂ƒƒ",
                "<h1 id=\""+ attributeProvider.getDefaultHeading() +"\">∂∂ƒƒ</h1>\n");
    }

    @Test
    public void boldEmphasisCharacters() {
        assertRendering("# _hello_ **there**", "<h1 id=\"hello-there\"><em>hello</em> <strong>there</strong></h1>\n");
    }

    @Test
    public void testStrongEmphasis() {
        assertRendering("# _**Hi there**_", "<h1 id=\"hi-there\"><em><strong>Hi there</strong></em></h1>\n");
    }

    @Override
    protected String render(String source) {
        return RENDERER.render(PARSER.parse(source));
    }
}

@@ -54,21 +54,47 @@
 monmark.
-html.
+ext.headerids.internal.HeaderId
 Attribut
@@ -1383,24 +1383,25 @@
 heading-here
+-
 1%5C%22%3EHeading 
@@ -1452,174 +1452,201 @@
 oid 
-du
+testEx
 plic
-ate
+it
 Header
-sOnceDissalowedCharactersMakeUniqueIds() %7B%0A        assertRendering(%22# Hi there%5Cn%22 +%0A                %22# %E2%88%82Hi %C3%A5%E2%88%82There%CB%9A%E2%88%82%CB%86%C2%B4%C2%A8%22, %22%3Ch1 id=%5C%22hi-there%5C%22%3EHi th
+Collision() %7B%0A        assertRendering(%22# Header%5Cn# Header%5Cn# Header-1%22,%0A                %22%3Ch1 id=%5C%22header%5C%22%3EHeader%3C/h1%3E%5Cn%22 +%0A                        %22%3Ch1 id=%5C%22header-1%5C%22%3EHead
 er
-e
 %3C/h1
@@ -1644,32 +1644,40 @@
 eader%3C/h1%3E%5Cn%22 +%0A
+        
                 
@@ -1691,35 +1691,26 @@
 =%5C%22h
-i-there
+eader-
 1%5C%22%3E
-%E2%88%82Hi %C3%A5%E2%88%82There%CB%9A%E2%88%82%CB%86%C2%B4%C2%A8
+Header-1
 %3C/h1
@@ -1984,16 +1984,17 @@
 ing-here
+-
 1%5C%22%3Ehead
@@ -2237,188 +2237,8 @@
  %7D%0A%0A
-    @Test%0A    public void noPrintableCharacters() %7B%0A        assertRendering(%22# %E2%88%82%E2%88%82%C6%92%C6%92%22,%0A                %22%3Ch1 id=%5C%22%22+ attributeProvider.getDefaultHeading() +%22%5C%22%3E%E2%88%82%E2%88%82%C6%92%C6%92%3C/h1%3E%5Cn%22);%0A    %7D%0A%0A
     
@@ -2574,24 +2574,324 @@
 n%22);%0A    %7D%0A%0A
+    @Test%0A    public void testNonAsciiCharacterHeading() %7B%0A        assertRendering(%22# b%C3%A4r%22, %22%3Ch1 id=%5C%22b%C3%A4r%5C%22%3Eb%C3%A4r%3C/h1%3E%5Cn%22);%0A    %7D%0A%0A    @Test%0A    public void testCombiningDiaeresis() %7B%0A        assertRendering(%22# Product%5Cu036D%5Cu036B%22, %22%3Ch1 id=%5C%22product%5Cu036D%5Cu036B%5C%22%3EProduct%5Cu036D%5Cu036B%3C/h1%3E%5Cn%22);%0A    %7D%0A%0A
     @Overrid

Add new tests to check for unicode support