Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for alt attribute on structured content images #359

Merged
merged 6 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/css/structured-content.css
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
}

.gloss-image-link[data-appearance=monochrome] .gloss-image {
visibility: hidden;
opacity: 0;
}
.gloss-image-link:not([data-appearance=monochrome]) .gloss-image-background {
display: none;
Expand Down
8 changes: 8 additions & 0 deletions ext/data/schemas/dictionary-term-bank-v3-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@
"type": "string",
"description": "Hover text for the image."
},
"alt": {
"type": "string",
"description": "Alt text for the image."
},
"description": {
"type": "string",
"description": "Description of the image."
Expand Down Expand Up @@ -424,6 +428,10 @@
"type": "string",
"description": "Hover text for the image."
},
"alt": {
"type": "string",
"description": "Alt text for the image."
},
"description": {
"type": "string",
"description": "Description of the image."
Expand Down
2 changes: 1 addition & 1 deletion ext/data/structured-content-style.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
{
"selectors": [".gloss-image-link[data-appearance=monochrome] .gloss-image"],
"styles": [
["visibility", "hidden"]
["opacity", "0"]
]
},
{
Expand Down
3 changes: 2 additions & 1 deletion ext/js/display/sandbox/structured-content-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class StructuredContentGenerator {
preferredWidth,
preferredHeight,
title,
alt,
pixelated,
imageRendering,
appearance,
Expand Down Expand Up @@ -102,7 +103,7 @@ export class StructuredContentGenerator {
imageContainer.appendChild(imageBackground);

const image = /** @type {HTMLImageElement} */ (this._createElement('img', 'gloss-image'));
image.alt = '';
image.alt = typeof alt === 'string' ? alt : '';
imageContainer.appendChild(image);

const overlay = this._createElement('span', 'gloss-image-container-overlay');
Expand Down
2 changes: 2 additions & 0 deletions ext/js/language/dictionary-importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ export class DictionaryImporter {
width: preferredWidth,
height: preferredHeight,
title,
alt,
description,
pixelated,
imageRendering,
Expand All @@ -518,6 +519,7 @@ export class DictionaryImporter {
if (typeof preferredWidth === 'number') { target.preferredWidth = preferredWidth; }
if (typeof preferredHeight === 'number') { target.preferredHeight = preferredHeight; }
if (typeof title === 'string') { target.title = title; }
if (typeof alt === 'string') { target.alt = alt; }
if (typeof description === 'string') { target.description = description; }
if (typeof pixelated === 'boolean') { target.pixelated = pixelated; }
if (typeof imageRendering === 'string') { target.imageRendering = imageRendering; }
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion test/data/dictionaries/valid-dictionary1/term_bank_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"莢\n"
]},
{"type": "structured-content", "content": [
"Imgae aspect ratio tests:\nあ",
"Image aspect ratio tests:\nあ",
{"tag": "img", "path": "character2.gif", "height": 1.1, "imageRendering": "crisp-edges", "appearance": "monochrome", "background": false, "sizeUnits": "em", "collapsed": false, "collapsible": false},
"あ\nあ",
{"tag": "img", "path": "character2.gif", "width": 0.4125, "imageRendering": "crisp-edges", "appearance": "monochrome", "background": false, "sizeUnits": "em", "collapsed": false, "collapsible": false},
Expand All @@ -76,6 +76,13 @@
{"tag": "img", "path": "character3.gif", "height": 0.4125, "imageRendering": "crisp-edges", "appearance": "monochrome", "background": false, "sizeUnits": "em", "collapsed": false, "collapsible": false},
"あ"
]},
{"type": "structured-content", "content": [
"Image alt text tests.\n𬵪 = Unicode character\n",
{"tag": "img", "alt": "𬵪", "path": "aosaba_mono.png", "height": 1.0, "width": 1.0, "background": false, "sizeUnits": "em", "collapsed": false, "collapsible": false, "appearance": "monochrome"},
" = monochrome PNG\n",
{"tag": "img", "alt": "𬵪", "path": "aosaba_auto.png", "height": 1.0, "width": 1.0, "background": false, "sizeUnits": "em", "collapsed": false, "collapsible": false, "appearance": "auto"},
" = color PNG"
]},
{"type": "structured-content", "content": [
{"tag": "div", "style": {"fontStyle": "normal"}, "content": "fontStyle:normal"},
{"tag": "div", "style": {"fontStyle": "italic"}, "content": "fontStyle:italic"},
Expand Down
12 changes: 6 additions & 6 deletions test/database.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async function testDatabase1() {
counts: {
kanji: {total: 2},
kanjiMeta: {total: 6, freq: 6},
media: {total: 4},
media: {total: 6},
tagMeta: {total: 15},
termMeta: {total: 38, freq: 31, pitch: 7},
terms: {total: 21}
Expand Down Expand Up @@ -193,13 +193,13 @@ async function testDatabase1() {
true
);
expect(counts).toStrictEqual({
counts: [{kanji: 2, kanjiMeta: 6, terms: 21, termMeta: 38, tagMeta: 15, media: 4}],
total: {kanji: 2, kanjiMeta: 6, terms: 21, termMeta: 38, tagMeta: 15, media: 4}
counts: [{kanji: 2, kanjiMeta: 6, terms: 21, termMeta: 38, tagMeta: 15, media: 6}],
total: {kanji: 2, kanjiMeta: 6, terms: 21, termMeta: 38, tagMeta: 15, media: 6}
});

// Test find* functions
await testFindTermsBulkTest1(dictionaryDatabase, titles);
await testTindTermsExactBulk1(dictionaryDatabase, titles);
await testFindTermsExactBulk1(dictionaryDatabase, titles);
await testFindTermsBySequenceBulk1(dictionaryDatabase, title);
await testFindTermMetaBulk1(dictionaryDatabase, titles);
await testFindKanjiBulk1(dictionaryDatabase, titles);
Expand Down Expand Up @@ -335,8 +335,8 @@ async function testFindTermsBulkTest1(database, titles) {
* @param {DictionaryDatabase} database
* @param {import('dictionary-database').DictionarySet} titles
*/
async function testTindTermsExactBulk1(database, titles) {
test('TindTermsExactBulk1', async () => {
async function testFindTermsExactBulk1(database, titles) {
test('FindTermsExactBulk1', async () => {
/** @type {{inputs: {termList: {term: string, reading: string}[]}[], expectedResults: {total: number, terms: [key: string, count: number][], readings: [key: string, count: number][]}}[]} */
const data = [
{
Expand Down
4 changes: 4 additions & 0 deletions types/ext/structured-content.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export type ImageElementBase = {
* Hover text for the image.
*/
title?: string;
/**
* Alt text for the image.
*/
alt?: string;
/**
* Description of the image.
*/
Expand Down