go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/tools/markdown/plugins/default_target.test.ts (about)

     1  // Copyright 2020 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import { fixture, html } from '@open-wc/testing-helpers';
    16  import { unsafeHTML } from 'lit/directives/unsafe-html.js';
    17  import markdownIt from 'markdown-it';
    18  
    19  import { defaultTarget } from './default_target';
    20  
    21  const links = `
    22  www.a.com
    23  [b](http://www.b.com)
    24  <www.c.com>
    25  <a href="http://www.d.com">www.d.com</a>
    26  <a href="http://www.e.com" target="_parent">www.e.com</a>
    27  
    28  <div>
    29    <a href="http://www.f.com">
    30      www.f.com
    31    </a>
    32    <a href="http://www.g.com" target="_self">
    33      www.g.com
    34    </a>
    35  </div>
    36  `;
    37  
    38  const md = markdownIt('zero', { linkify: true, html: true })
    39    .enable(['linkify', 'autolink', 'link', 'html_inline', 'html_block'])
    40    .use(defaultTarget, '_blank');
    41  
    42  describe('default_target', () => {
    43    let anchors: NodeListOf<HTMLAnchorElement>;
    44    beforeAll(async () => {
    45      const ele = await fixture(html`<div>${unsafeHTML(md.render(links))}</div>`);
    46      anchors = ele.querySelectorAll('a');
    47    });
    48  
    49    test('can set default target', () => {
    50      const anchor1 = anchors.item(0);
    51      expect(anchor1.target).toStrictEqual('_blank');
    52      expect(anchor1.href).toStrictEqual('http://www.a.com/');
    53  
    54      const anchor2 = anchors.item(1);
    55      expect(anchor2.target).toStrictEqual('_blank');
    56      expect(anchor2.href).toStrictEqual('http://www.b.com/');
    57  
    58      const anchor3 = anchors.item(2);
    59      expect(anchor3.target).toStrictEqual('_blank');
    60      expect(anchor3.href).toStrictEqual('http://www.c.com/');
    61  
    62      const anchor4 = anchors.item(3);
    63      expect(anchor4.target).toStrictEqual('_blank');
    64      expect(anchor4.href).toStrictEqual('http://www.d.com/');
    65  
    66      const anchor5 = anchors.item(5);
    67      expect(anchor5.target).toStrictEqual('_blank');
    68      expect(anchor5.href).toStrictEqual('http://www.f.com/');
    69    });
    70  
    71    test('does not override target', () => {
    72      const anchor4 = anchors.item(4);
    73      expect(anchor4.target).toStrictEqual('_parent');
    74      expect(anchor4.href).toStrictEqual('http://www.e.com/');
    75  
    76      const anchor5 = anchors.item(6);
    77      expect(anchor5.target).toStrictEqual('_self');
    78      expect(anchor5.href).toStrictEqual('http://www.g.com/');
    79    });
    80  });