Skip to content

Commit

Permalink
Merge branch 'development' into enhancement#553-multilingual-annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft authored Nov 19, 2024
2 parents d6db8f1 + f5d8d4b commit aa45e3e
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 20 deletions.
6 changes: 6 additions & 0 deletions NEWS.cs.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#### Verze 3.3.0

- Přidána možnost stáhnout anotovaný soubor bez nepotvrzených výskytů.
- Upraveno fulltextové vyhledávání - nově se zobrazuje informace o atributu, ve kterém byla shoda nalezena.
- Do fasetového vyhledávání přidána možnost filtrovat dle příkladů (`skos:example`).

#### Verze 3.2.0

- Přidána podpora pro import slovníků z MS Excel.
Expand Down
6 changes: 6 additions & 0 deletions NEWS.en.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#### Version 3.3.0

- Added the possibility to download annotated file without unconfirmed occurrences.
- Enhanced fulltext search - now it shows information about the attribute in which a match was found.
- Added support for filtering by example (`skos:example`) in the faceted search.

#### Version 3.2.0

- Added support for importing a vocabulary from MS Excel.
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "termit-ui",
"version": "3.2.0",
"version": "3.3.1",
"private": true,
"homepage": ".",
"license": "GPL-3.0-only",
Expand Down Expand Up @@ -83,7 +83,7 @@
"@redux-devtools/extension": "^3.2.5",
"@types/enzyme": "^3.10.13",
"@types/jest": "^27.4.1",
"@types/js-cookie": "^3.0.3",
"@types/js-cookie": "^3.0.6",
"@types/lodash": "^4.17.10",
"@types/luxon": "^3.4.2",
"@types/node": "^18.11.17",
Expand Down
12 changes: 10 additions & 2 deletions src/component/annotator/AnnotatorContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@ const PREPROCESSING_INSTRUCTIONS = [
shouldPreprocessNode: (node: any): boolean =>
node.name && node.name === "a",
preprocessNode: (node: any) => {
node.attribs["data-href"] = node.attribs.href;
delete node.attribs.href;
// Remove href from relative links, absolute links will open in blank tab
if (!Utils.isLink(node.attribs.href)) {
node.attribs["data-href"] = node.attribs.href;
delete node.attribs.href;
} else {
node.attribs["data-target"] = node.attribs.target;
node.attribs["target"] = "_blank";
node.attribs["data-rel"] = node.attribs.rel;
node.attribs["rel"] = "noopener noreferrer";
}
},
},
];
Expand Down
23 changes: 22 additions & 1 deletion src/component/annotator/HtmlParserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,33 @@ import render from "dom-serializer";

const RDF_ATTRIBUTE_NAMES = ["about", "property", "resource", "typeof"];

function removeAddedAttributes(elem: Element) {
// Restore links to their original state - see AnnotatorContent.PREPROCESSING_INSTRUCTIONS
if (elem.tagName === "a") {
if (elem.attribs["data-href"]) {
elem.attribs.href = elem.attribs["data-href"];
delete elem.attribs["data-href"];
}
delete elem.attribs["target"];
delete elem.attribs["rel"];
if (elem.attribs["data-rel"]) {
elem.attribs.rel = elem.attribs["data-rel"];
delete elem.attribs["data-rel"];
}
if (elem.attribs["data-target"]) {
elem.attribs.target = elem.attribs["data-target"];
delete elem.attribs["data-target"];
}
}
}

const HtmlParserUtils = {
html2dom(html: string): Node[] {
// Do not decode HTML entities (e.g., <) when parsing content for object representation, it caused issues
// with rendering
const options = { decodeEntities: false };
const handler = new DomHandler();
// @ts-ignore
const handler = new DomHandler(null, null, removeAddedAttributes);
const parser = new HtmlParser(handler, options);
parser.parseComplete(html);
return handler.dom as Node[];
Expand Down
25 changes: 23 additions & 2 deletions src/component/annotator/__tests__/Annotator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe("Annotator", () => {
expect(wrapper.html().includes(sampleContent)).toBe(true);
});

it("renders body of provided html content with replaced anchor hrefs", () => {
it("preserves absolute URL href anchors", () => {
const htmlContent = surroundWithHtml(
'This is a <a href="https://example.org/link">link</a>'
);
Expand All @@ -138,7 +138,28 @@ describe("Annotator", () => {
)
);
const sampleOutput =
'This is a <a data-href="https://example.org/link">link</a>';
'This is a <a href="https://example.org/link" target="_blank" rel="noopener noreferrer">link</a>';
expect(wrapper.html().includes(sampleOutput)).toBe(true);
});

it("renders body of provided html content with replaced relative anchor hrefs", () => {
const htmlContent = surroundWithHtml('This is a <a href="./link">link</a>');

const wrapper = mountWithIntl(
withWebSocket(
<MemoryRouter>
<Annotator
fileIri={fileIri}
vocabularyIri={vocabularyIri}
{...mockedCallbackProps}
{...stateProps}
initialHtml={htmlContent}
{...intlFunctions()}
/>
</MemoryRouter>
)
);
const sampleOutput = 'This is a <a data-href="./link">link</a>';
expect(wrapper.html().includes(sampleOutput)).toBe(true);
});

Expand Down
22 changes: 22 additions & 0 deletions src/component/annotator/__tests__/HtmlParserUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Element } from "domhandler";
import HtmlParserUtils from "../HtmlParserUtils";

describe("HtmlParserUtils", () => {
describe("html2dom", () => {
it("remove target and rel attributes added when rendering HTML", () => {
const html =
'<a href="http://example.com" target="_blank" rel="noopener noreferrer">Example</a>';
const nodes = HtmlParserUtils.html2dom(html);
expect((nodes[0] as Element).attribs.href).toEqual("http://example.com");
expect((nodes[0] as Element).attribs.target).not.toBeDefined();
expect((nodes[0] as Element).attribs.rel).not.toBeDefined();
});

it("set href attribute to data-href attribute created when rendering HTML", () => {
const html = '<a data-href="./about.html">Example</a>';
const nodes = HtmlParserUtils.html2dom(html);
expect((nodes[0] as Element).attribs.href).toEqual("./about.html");
expect((nodes[0] as Element).attribs["data-href"]).not.toBeDefined();
});
});
});
9 changes: 5 additions & 4 deletions src/util/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ const Utils = {
*/
isLink(str: string): boolean {
return (
str.startsWith("http://") ||
str.startsWith("https://") ||
str.startsWith("ftp://") ||
str.startsWith("sftp://")
str !== undefined &&
(str.startsWith("http://") ||
str.startsWith("https://") ||
str.startsWith("ftp://") ||
str.startsWith("sftp://"))
);
},

Expand Down

0 comments on commit aa45e3e

Please sign in to comment.