Skip to content

Commit e95bcaa

Browse files
feat: disable img attributes copy to figure element
- resolves #151
1 parent 22de4b3 commit e95bcaa

File tree

4 files changed

+17
-58
lines changed

4 files changed

+17
-58
lines changed

docs/ja/vfm.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,6 @@ img {
378378

379379
単一行で書かれた画像はキャプション付きで `<figure>` 内へ包み込みます。
380380

381-
画像の属性を指定した場合、`id``<figure>` へ移動され ` <img>` 固有のもの (`src` など) を除いたすべてがコピーされます。
382-
383381
**VFM**
384382

385383
```md
@@ -394,14 +392,16 @@ text ![Figure 3](./fig3.png)
394392

395393
```html
396394
<figure>
397-
<img src="./fig1.png" alt="Figure 1" />
398-
<figcaption>Figure 1</figcaption>
395+
<img src="./fig1.png" alt="Figure 1">
396+
<figcaption aria-hidden="true">Figure 1</figcaption>
399397
</figure>
400-
<figure id="image" title="Figure 2" data-sample="sample">
401-
<img src="./fig2.png" alt="caption" title="Figure 2" data-sample="sample">
402-
<figcaption>Figure 2</figcaption>
398+
<figure>
399+
<img src="./fig2.png" alt="Figure 2" title="Figure 2" id="image" data-sample="sample">
400+
<figcaption aria-hidden="true">Figure 2</figcaption>
403401
</figure>
404-
<p>text <img src="./fig3.png" alt="Figure 3"></p>
402+
<p>text
403+
<img src="./fig3.png" alt="Figure 3">
404+
</p>
405405
```
406406

407407
**CSS**

docs/vfm.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,6 @@ img {
379379

380380
Wraps an image written as a single line and with a caption in `<figure>`.
381381

382-
If specify attributes for the image, the `id` is moved to `<figure>` and everything else is copied except for` <img> `specific (such as `src`).
383-
384382
**VFM**
385383

386384
```md
@@ -395,14 +393,16 @@ text ![Figure 3](./fig3.png)
395393

396394
```html
397395
<figure>
398-
<img src="./fig1.png" alt="Figure 1" />
399-
<figcaption>Figure 1</figcaption>
396+
<img src="./fig1.png" alt="Figure 1">
397+
<figcaption aria-hidden="true">Figure 1</figcaption>
400398
</figure>
401-
<figure id="image" title="Figure 2" data-sample="sample">
402-
<img src="./fig2.png" alt="caption" title="Figure 2" data-sample="sample">
403-
<figcaption>Figure 2</figcaption>
399+
<figure>
400+
<img src="./fig2.png" alt="Figure 2" title="Figure 2" id="image" data-sample="sample">
401+
<figcaption aria-hidden="true">Figure 2</figcaption>
404402
</figure>
405-
<p>text <img src="./fig3.png" alt="Figure 3"></p>
403+
<p>text
404+
<img src="./fig3.png" alt="Figure 3">
405+
</p>
406406
```
407407

408408
**CSS**

src/plugins/figure.ts

-41
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,10 @@ import h from 'hastscript';
44
import { Node, Parent } from 'unist';
55
import visit from 'unist-util-visit';
66

7-
/**
8-
* Check if the specified property is `<img>` specific.
9-
* @see https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element
10-
* @param name Property name.
11-
* @returns If the specified property is `true`.
12-
*/
13-
const isImgProperty = (name: string): boolean => {
14-
switch (name) {
15-
case 'alt':
16-
case 'src':
17-
case 'srcset':
18-
case 'sizes':
19-
case 'crossorigin':
20-
case 'usemap':
21-
case 'ismap':
22-
case 'width':
23-
case 'height':
24-
case 'referrerpolicy':
25-
case 'decoding':
26-
case 'loading':
27-
return true;
28-
29-
default:
30-
return false;
31-
}
32-
};
33-
347
/**
358
* Wrap the single line `<img>` in `<figure>` and generate `<figcaption>` from the `alt` attribute.
369
*
3710
* A single line `<img>` is a child of `<p>` with no sibling elements. Also, `<figure>` cannot be a child of `<p>`. So convert the parent `<p>` to `<figure>`.
38-
*
39-
* Also, of the attributes of `<img>`,` id` is moved to `<figure>`, and the others are copied except for `<img>` specific (such as `src`).
4011
* @param img `<img>` tag.
4112
* @param parent `<p>` tag.
4213
*/
@@ -49,18 +20,6 @@ const wrapFigureImg = (img: Element, parent: Element) => {
4920
parent.children.push(
5021
h('figcaption', { 'aria-hidden': 'true' }, [img.properties.alt]),
5122
);
52-
53-
// Move to parent because `id` attribute is unique
54-
if (img.properties.id) {
55-
parent.properties.id = img.properties.id;
56-
delete img.properties.id;
57-
}
58-
59-
for (const key of Object.keys(img.properties)) {
60-
if (!isImgProperty(key)) {
61-
parent.properties[key] = img.properties[key];
62-
}
63-
}
6423
};
6524

6625
export const hast = () => (tree: Node) => {

tests/figure.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ it(
7575
alt: "caption"
7676
data: {"hProperties":{"id":"image","data-sample":"sample"}}
7777
`,
78-
`<figure id="image" title="title" data-sample="sample"><img src="./img.png" alt="caption" title="title" data-sample="sample"><figcaption aria-hidden="true">caption</figcaption></figure>`,
78+
`<figure><img src="./img.png" alt="caption" title="title" id="image" data-sample="sample"><figcaption aria-hidden="true">caption</figcaption></figure>`,
7979
),
8080
);

0 commit comments

Comments
 (0)