-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #342 from moreonion/csp-vue
fix: Avoid v-html for highlighted text
- Loading branch information
Showing
12 changed files
with
802 additions
and
118 deletions.
There are no files selected for viewing
32 changes: 16 additions & 16 deletions
32
campaignion_email_to_target/js/messages_app/e2t_messages_app.vue.min.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
campaignion_email_to_target/messages_app/src/components/HighlightedText.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<docs> | ||
HighlightedText component. | ||
Puts matching parts of a text in <strong> tags to highlight them. | ||
</docs> | ||
|
||
<template> | ||
<span> | ||
<template v-for="part in getParts(text, search)"> | ||
<strong v-if="part.highlight">{{ part.text }}</strong> | ||
<template v-else>{{ part.text }}</template> | ||
</template> | ||
</span> | ||
</template> | ||
|
||
<script> | ||
import { escapeRegExp } from '../utils' | ||
export default { | ||
props: { | ||
text: { default: null }, | ||
search: String | ||
}, | ||
methods: { | ||
getParts (text, search) { | ||
if (!text || !search) { | ||
return [{text, highlight: false}] | ||
} | ||
const escapedSearch = escapeRegExp(search) | ||
const matchRegexp = new RegExp('^' + escapedSearch + '$', 'i') | ||
return text.split(new RegExp('(?=' + escapedSearch + ')|(?<=' + escapedSearch + ')', 'gi')).map((part) => ({ | ||
text: part, | ||
highlight: matchRegexp.test(part) | ||
})) | ||
}, | ||
}, | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 9 additions & 9 deletions
18
campaignion_wizard/js/redirects_app/redirects_app.vue.min.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
campaignion_wizard/redirects_app/src/components/HighlightedText.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<docs> | ||
HighlightedText component. | ||
Puts matching parts of a text in <strong> tags to highlight them. | ||
</docs> | ||
|
||
<template> | ||
<span> | ||
<template v-for="part in getParts(text, search)"> | ||
<strong v-if="part.highlight">{{ part.text }}</strong> | ||
<template v-else>{{ part.text }}</template> | ||
</template> | ||
</span> | ||
</template> | ||
|
||
<script> | ||
import { escapeRegExp } from '../utils' | ||
export default { | ||
props: { | ||
text: { default: null }, | ||
search: String | ||
}, | ||
methods: { | ||
getParts (text, search) { | ||
if (!text || !search) { | ||
return [{text, highlight: false}] | ||
} | ||
const escapedSearch = escapeRegExp(search) | ||
const matchRegexp = new RegExp('^' + escapedSearch + '$', 'i') | ||
return text.split(new RegExp('(?=' + escapedSearch + ')|(?<=' + escapedSearch + ')', 'gi')).map((part) => ({ | ||
text: part, | ||
highlight: matchRegexp.test(part) | ||
})) | ||
}, | ||
}, | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
campaignion_wizard/redirects_app/test/unit/specs/HighlightedText.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import sinon from 'sinon' | ||
import { render } from '@testing-library/vue' | ||
|
||
import HighlightedText from '../../../src/components/HighlightedText.vue' | ||
|
||
afterEach(() => { | ||
sinon.restore() | ||
}) | ||
|
||
describe('HighlightedText', function () { | ||
describe('template', function () { | ||
it('renders an empty span with empty input.', function () { | ||
const wrapper = render(HighlightedText) | ||
assert.equal(wrapper.html(), '<span></span>') | ||
}) | ||
it('renders the text unchanged without a search term.', function () { | ||
const wrapper = render(HighlightedText, { propsData: { text: 'foo' } }) | ||
assert.equal(wrapper.html(), '<span>foo</span>') | ||
}) | ||
it('highlights the search term.', function () { | ||
const wrapper = render(HighlightedText, { | ||
propsData: { | ||
text: 'Hello world!', | ||
search: 'o' | ||
} | ||
}) | ||
assert.equal(wrapper.html(), '<span>Hell<strong>o</strong> w<strong>o</strong>rld!</span>') | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.