Skip to content

Commit 7a79f1e

Browse files
committed
Fix issue adding external links
1 parent 3d09554 commit 7a79f1e

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/main/java/org/jahia/modules/richtext/configuration/parse/Parser.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ private enum PolicyType {
1919

2020
static {
2121
PATTERNS = new HashMap<>();
22+
String onsiteUrl = "(?:[\\p{L}\\p{N}\\\\\\.\\#@\\$%\\+&;\\-_~,\\?=/!{}:]+|\\#(\\w)+)";
23+
String offsiteUrl = "(\\s*(?:(?:ht|f)tps?://|mailto:)[\\p{L}\\p{N}][\\p{L}\\p{N}\\p{Zs}\\.\\#@\\$%\\+&;:\\-_~,\\?=/!\\(\\)"
24+
+ "]*+\\s*)";
2225
PATTERNS.put("NUMBER_OR_PERCENT", Pattern.compile("[0-9]+%?"));
23-
PATTERNS.put("ONSITE_URL", Pattern.compile("(?:[\\p{L}\\p{N}\\\\\\.\\#@\\$%\\+&;\\-_~,\\?=/!{}:]+|\\#(\\w)+)"));
26+
PATTERNS.put("ONSITE_URL", Pattern.compile(onsiteUrl));
27+
PATTERNS.put("OFFSITE_URL", Pattern.compile(onsiteUrl));
28+
PATTERNS.put("LINKS_URL", Pattern.compile(String.format("(?:%s|%s)", onsiteUrl, offsiteUrl)));
2429
PATTERNS.put("HTML_ID", Pattern.compile("[a-zA-Z0-9\\:\\-_\\.]+"));
25-
PATTERNS.put("OFFSITE_URL", Pattern.compile("\\s*(?:(?:ht|f)tps?://|mailto:)[\\p{L}\\p{N}]"
26-
+ "[\\p{L}\\p{N}\\p{Zs}\\.\\#@\\$%\\+&;:\\-_~,\\?=/!\\(\\)]*+\\s*"));
2730
PATTERNS.put("HTML_CLASS", Pattern.compile("[a-zA-Z0-9\\s,\\-_]+"));
2831
PATTERNS.put("NUMBER", Pattern.compile("[+-]?(?:(?:[0-9]+(?:\\.[0-9]*)?)|\\.[0-9]+)"));
2932
PATTERNS.put("NAME", Pattern.compile("[a-zA-Z0-9\\-_\\$]+"));

src/main/resources/META-INF/configuration-default/org.jahia.modules.richtext.config-default.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ htmlFiltering:
3838
pattern: NUMBER_OR_PERCENT
3939
elements: canvas, img, table, td, th, col, colgroup, video
4040
- name: href
41-
pattern: ONSITE_URL
41+
pattern: LINKS_URL
4242
elements: a
4343
- name: hreflang
4444
elements: a

tests/cypress/e2e/defaultFiltering.cy.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,29 @@ describe('Default HTML filtering', () => {
2929
deleteNode(path);
3030
});
3131

32-
it('allows asset links', () => {
32+
it('allows internal links', () => {
3333
// Note that the actual href text being sent over to the sanitizer is '##doc-context##/{workspace}/##ref:link1##'
3434
const text = '<p><a href="/files/{workspace}/sites/digitall/files/images/pdf/Conference%20Guide.pdf" ' +
3535
'title="Conference Guide.pdf">/files/{workspace}/sites/digitall/files/images/pdf/Conference%20Guide.pdf</a></p>';
3636
modifyContent(path, text);
3737
getContent(path).then(result => {
38-
expect(result.data.jcr.nodeByPath.property.value).to.contain('href');
38+
const value = result.data.jcr.nodeByPath.property.value;
39+
expect(value).to.contain('<p>');
40+
expect(value).to.contain('<a');
41+
expect(value).to.contain('href');
42+
expect(value).to.contain('title');
43+
});
44+
});
45+
46+
it('allows external links', () => {
47+
const text = '<p>This is a <a href="http://google.com" title="My google link">google link</a></p>';
48+
modifyContent(path, text);
49+
getContent(path).then(result => {
50+
const value = result.data.jcr.nodeByPath.property.value;
51+
expect(value).to.contain('<p>');
52+
expect(value).to.contain('<a');
53+
expect(value).to.contain('href');
54+
expect(value).to.contain('title');
3955
});
4056
});
4157
});

0 commit comments

Comments
 (0)