github.com/nhannv/mattermost-server@v5.11.1+incompatible/utils/markdown/links_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package markdown
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestParseImageDimensions(t *testing.T) {
    13  	for name, tc := range map[string]struct {
    14  		Input         string
    15  		Position      int
    16  		ExpectedRange Range
    17  		ExpectedNext  int
    18  		ExpectedOk    bool
    19  	}{
    20  		"no dimensions, no title": {
    21  			Input:         `![alt](https://example.com)`,
    22  			Position:      26,
    23  			ExpectedRange: Range{0, 0},
    24  			ExpectedNext:  0,
    25  			ExpectedOk:    false,
    26  		},
    27  		"no dimensions, title": {
    28  			Input:         `![alt](https://example.com "title")`,
    29  			Position:      27,
    30  			ExpectedRange: Range{0, 0},
    31  			ExpectedNext:  0,
    32  			ExpectedOk:    false,
    33  		},
    34  		"only width, no title": {
    35  			Input:         `![alt](https://example.com =100)`,
    36  			Position:      27,
    37  			ExpectedRange: Range{27, 30},
    38  			ExpectedNext:  31,
    39  			ExpectedOk:    true,
    40  		},
    41  		"only width, title": {
    42  			Input:         `![alt](https://example.com =100 "title")`,
    43  			Position:      27,
    44  			ExpectedRange: Range{27, 30},
    45  			ExpectedNext:  31,
    46  			ExpectedOk:    true,
    47  		},
    48  		"only height, no title": {
    49  			Input:         `![alt](https://example.com =x100)`,
    50  			Position:      27,
    51  			ExpectedRange: Range{27, 31},
    52  			ExpectedNext:  32,
    53  			ExpectedOk:    true,
    54  		},
    55  		"only height, title": {
    56  			Input:         `![alt](https://example.com =x100 "title")`,
    57  			Position:      27,
    58  			ExpectedRange: Range{27, 31},
    59  			ExpectedNext:  32,
    60  			ExpectedOk:    true,
    61  		},
    62  		"dimensions, no title": {
    63  			Input:         `![alt](https://example.com =100x200)`,
    64  			Position:      27,
    65  			ExpectedRange: Range{27, 34},
    66  			ExpectedNext:  35,
    67  			ExpectedOk:    true,
    68  		},
    69  		"dimensions, title": {
    70  			Input:         `![alt](https://example.com =100x200 "title")`,
    71  			Position:      27,
    72  			ExpectedRange: Range{27, 34},
    73  			ExpectedNext:  35,
    74  			ExpectedOk:    true,
    75  		},
    76  		"no dimensions, no title, trailing whitespace": {
    77  			Input:         `![alt](https://example.com )`,
    78  			Position:      27,
    79  			ExpectedRange: Range{0, 0},
    80  			ExpectedNext:  0,
    81  			ExpectedOk:    false,
    82  		},
    83  		"only width, no title, trailing whitespace": {
    84  			Input:         `![alt](https://example.com  =100  )`,
    85  			Position:      28,
    86  			ExpectedRange: Range{28, 31},
    87  			ExpectedNext:  32,
    88  			ExpectedOk:    true,
    89  		},
    90  		"only height, no title, trailing whitespace": {
    91  			Input:         `![alt](https://example.com   =x100   )`,
    92  			Position:      29,
    93  			ExpectedRange: Range{29, 33},
    94  			ExpectedNext:  34,
    95  			ExpectedOk:    true,
    96  		},
    97  		"dimensions, no title, trailing whitespace": {
    98  			Input:         `![alt](https://example.com    =100x200   )`,
    99  			Position:      30,
   100  			ExpectedRange: Range{30, 37},
   101  			ExpectedNext:  38,
   102  			ExpectedOk:    true,
   103  		},
   104  		"no width or height": {
   105  			Input:         `![alt](https://example.com =x)`,
   106  			Position:      27,
   107  			ExpectedRange: Range{0, 0},
   108  			ExpectedNext:  0,
   109  			ExpectedOk:    false,
   110  		},
   111  		"garbage 1": {
   112  			Input:         `![alt](https://example.com =aaa)`,
   113  			Position:      27,
   114  			ExpectedRange: Range{0, 0},
   115  			ExpectedNext:  0,
   116  			ExpectedOk:    false,
   117  		},
   118  		"garbage 2": {
   119  			Input:         `![alt](https://example.com ====)`,
   120  			Position:      27,
   121  			ExpectedRange: Range{0, 0},
   122  			ExpectedNext:  0,
   123  			ExpectedOk:    false,
   124  		},
   125  		"garbage 3": {
   126  			Input:         `![alt](https://example.com =100xx200)`,
   127  			Position:      27,
   128  			ExpectedRange: Range{0, 0},
   129  			ExpectedNext:  0,
   130  			ExpectedOk:    false,
   131  		},
   132  		"garbage 4": {
   133  			Input:         `![alt](https://example.com =100x200x300x400)`,
   134  			Position:      27,
   135  			ExpectedRange: Range{0, 0},
   136  			ExpectedNext:  0,
   137  			ExpectedOk:    false,
   138  		},
   139  	} {
   140  		t.Run(name, func(t *testing.T) {
   141  			raw, next, ok := parseImageDimensions(tc.Input, tc.Position)
   142  			assert.Equal(t, tc.ExpectedOk, ok)
   143  			assert.Equal(t, tc.ExpectedNext, next)
   144  			assert.Equal(t, tc.ExpectedRange, raw)
   145  		})
   146  	}
   147  }
   148  
   149  func TestImageLinksWithDimensions(t *testing.T) {
   150  	for name, tc := range map[string]struct {
   151  		Markdown     string
   152  		ExpectedHTML string
   153  	}{
   154  		"regular link": {
   155  			Markdown:     `[link](https://example.com)`,
   156  			ExpectedHTML: `<p><a href="https://example.com">link</a></p>`,
   157  		},
   158  		"image link": {
   159  			Markdown:     `![image](https://example.com/image.png)`,
   160  			ExpectedHTML: `<p><img src="https://example.com/image.png" alt="image" /></p>`,
   161  		},
   162  		"image link with title": {
   163  			Markdown:     `![image](https://example.com/image.png "title")`,
   164  			ExpectedHTML: `<p><img src="https://example.com/image.png" alt="image" title="title" /></p>`,
   165  		},
   166  		"image link with bracketed title": {
   167  			Markdown:     `![image](https://example.com/image.png (title))`,
   168  			ExpectedHTML: `<p><img src="https://example.com/image.png" alt="image" title="title" /></p>`,
   169  		},
   170  		"image link with width": {
   171  			Markdown:     `![image](https://example.com/image.png =500)`,
   172  			ExpectedHTML: `<p><img src="https://example.com/image.png" alt="image" /></p>`,
   173  		},
   174  		"image link with width and title": {
   175  			Markdown:     `![image](https://example.com/image.png =500 "title")`,
   176  			ExpectedHTML: `<p><img src="https://example.com/image.png" alt="image" title="title" /></p>`,
   177  		},
   178  		"image link with width and bracketed title": {
   179  			Markdown:     `![image](https://example.com/image.png =500 (title))`,
   180  			ExpectedHTML: `<p><img src="https://example.com/image.png" alt="image" title="title" /></p>`,
   181  		},
   182  		"image link with height": {
   183  			Markdown:     `![image](https://example.com/image.png =x500)`,
   184  			ExpectedHTML: `<p><img src="https://example.com/image.png" alt="image" /></p>`,
   185  		},
   186  		"image link with height and title": {
   187  			Markdown:     `![image](https://example.com/image.png =x500 "title")`,
   188  			ExpectedHTML: `<p><img src="https://example.com/image.png" alt="image" title="title" /></p>`,
   189  		},
   190  		"image link with height and bracketed title": {
   191  			Markdown:     `![image](https://example.com/image.png =x500 (title))`,
   192  			ExpectedHTML: `<p><img src="https://example.com/image.png" alt="image" title="title" /></p>`,
   193  		},
   194  		"image link with dimensions": {
   195  			Markdown:     `![image](https://example.com/image.png =500x400)`,
   196  			ExpectedHTML: `<p><img src="https://example.com/image.png" alt="image" /></p>`,
   197  		},
   198  		"image link with dimensions and title": {
   199  			Markdown:     `![image](https://example.com/image.png =500x400 "title")`,
   200  			ExpectedHTML: `<p><img src="https://example.com/image.png" alt="image" title="title" /></p>`,
   201  		},
   202  		"image link with dimensions and bracketed title": {
   203  			Markdown:     `![image](https://example.com/image.png =500x400 (title))`,
   204  			ExpectedHTML: `<p><img src="https://example.com/image.png" alt="image" title="title" /></p>`,
   205  		},
   206  		"no image link 1": {
   207  			Markdown:     `![image]()`,
   208  			ExpectedHTML: `<p><img src="" alt="image" /></p>`,
   209  		},
   210  		"no image link 2": {
   211  			Markdown:     `![image]( )`,
   212  			ExpectedHTML: `<p><img src="" alt="image" /></p>`,
   213  		},
   214  		"no image link with dimensions": {
   215  			Markdown:     `![image]( =500x400)`,
   216  			ExpectedHTML: `<p><img src="=500x400" alt="image" /></p>`,
   217  		},
   218  	} {
   219  		t.Run(name, func(t *testing.T) {
   220  			assert.Equal(t, tc.ExpectedHTML, RenderHTML(tc.Markdown))
   221  		})
   222  	}
   223  }