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

Update privacy policy anchor hash algorithm. #1146

Merged
merged 2 commits into from
Nov 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
// SPDX-License-Identifier: EUPL-1.2

import 'package:markdown/markdown.dart';
import 'package:diacritic/diacritic.dart' as pkg;

/// We define our own classes for automatic ID generation as we don't want any
/// chance of breakage if `package:markdown` changes/updates its algorithm.
///
/// It's the exact same algorithm (for now).
///
/// It might break if we e.g. would use one updated algorithm in our backend to
/// generate the anchor ids for the table of contents section list, but our
/// client uses an old algorithm to generate/search for the anchor ids inside
Expand Down Expand Up @@ -86,5 +85,28 @@ String _generateAnchorHashFromElement(Element element) =>
String generateAnchorHash(String text) => text
.toLowerCase()
.trim()
.replaceAllMapped(regexp, (char) => _replacementMap[char.group(0)]!)
.removeDiacritics()
.replaceAll(RegExp(r'[^a-z0-9 _-]'), '')
.replaceAll(RegExp(r'\s'), '-');

// Match every character in [_replacementMap.keys].
RegExp get regexp => RegExp(_replacementMap.keys.join('|'));

const _replacementMap = {
'Ä': 'ae',
'ä': 'ae',
'Ö': 'oe',
'ö': 'oe',
'Ü': 'ue',
'ü': 'ue',
'ẞ': 'ss',
'ß': 'ss',
};

extension on String {
/// E.g. `é` -> `e`
String removeDiacritics() {
return pkg.removeDiacritics(this);
}
}
8 changes: 8 additions & 0 deletions app/pubspec.lock

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

1 change: 1 addition & 0 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dependencies:
path: ../lib/date
design:
path: ../lib/design
diacritic: ^0.1.4
dio: ^5.3.2
dynamic_links:
path: ../lib/dynamic_links
Expand Down
89 changes: 65 additions & 24 deletions app/test/privacy_policy/generate_anchor_hash_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,70 @@ import 'package:sharezone/privacy_policy/src/privacy_policy_src.dart';
void main() {
/// We are testing this since changing the anchor hash generation algorithm
/// might break stuff. See comments in [generateAnchorHash] file.
test('Generates correct anchor hashes', () {
final sections = [
'1. Einführung',
'2. Kontaktinformationen',
'3. Wichtige Begriffe, die du kennen solltest',
'4. Welche Informationen erfassen wir grundsätzlich?',
'5. An wen geben wir deine Daten weiter?',
'6. Wie lange speichern wir deine Daten?',
'7. Welche Rechte hast du?',
'Glückwunsch, du hast es geschafft'
];

final anchorHashes = sections.map(generateAnchorHash).toList();

expect(anchorHashes, [
'1-einfhrung',
'2-kontaktinformationen',
'3-wichtige-begriffe-die-du-kennen-solltest',
'4-welche-informationen-erfassen-wir-grundstzlich',
'5-an-wen-geben-wir-deine-daten-weiter',
'6-wie-lange-speichern-wir-deine-daten',
'7-welche-rechte-hast-du',
'glckwunsch-du-hast-es-geschafft',
]);
group('Generates correct anchor hashes', () {
test('real life examples', () {
final sections = [
'1. Einführung',
'2. Kontaktinformationen',
'3. Wichtige Begriffe, die du kennen solltest',
'4. Welche Informationen erfassen wir grundsätzlich?',
'5. An wen geben wir deine Daten weiter?',
'6. Wie lange speichern wir deine Daten?',
'7. Welche Rechte hast du?',
'Glückwunsch, du hast es geschafft',
];

final anchorHashes = sections.map(generateAnchorHash).toList();

expect(anchorHashes, [
'1-einfuehrung',
'2-kontaktinformationen',
'3-wichtige-begriffe-die-du-kennen-solltest',
'4-welche-informationen-erfassen-wir-grundsaetzlich',
'5-an-wen-geben-wir-deine-daten-weiter',
'6-wie-lange-speichern-wir-deine-daten',
'7-welche-rechte-hast-du',
'glueckwunsch-du-hast-es-geschafft',
]);
});
test('replaces special chars', () {
final sections = [
r'1-?!=""§$pokémon~+',
'Ist das eine Frage???',
'Wort-mit-Bindestrichen'
];

final anchorHashes = sections.map(generateAnchorHash).toList();

expect(anchorHashes, [
'1-pokemon',
'ist-das-eine-frage',
'wort-mit-bindestrichen',
]);
});

test('replaces umlaute', () {
final umlaute = [
'ä',
'Ä',
'ö',
'Ö',
'ü',
'Ü',
'ß',
];

final anchorHashes = umlaute.map(generateAnchorHash).toList();

expect(anchorHashes, [
'ae',
'ae',
'oe',
'oe',
'ue',
'ue',
'ss',
]);
});
});
}
Loading