Skip to content

Commit

Permalink
feat: URI encode src property
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Nov 28, 2018
1 parent b510c81 commit 89b219d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/evaluators/cheerioEvaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export default (): EvaluatorType => {
return encodeURI(node.attr('href'));
}

if (name === 'src') {
// URLs might include spaces, which a brower would encode when
// the attribute value is retrieved through a DOM property.
return encodeURI(node.attr('src'));
}

if (name === 'textContent') {
return node.text();
}
Expand Down
15 changes: 14 additions & 1 deletion test/surgeon/evaluators/cheerioEvaluator/getPropertyValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import test from 'ava';
import cheerioEvaluator from '../../../../src/evaluators/cheerioEvaluator';

test('URI encodes href property', (t) => {
test('URI encodes "href" property', (t) => {
const {
getPropertyValue,
parseDocument,
Expand All @@ -16,6 +16,19 @@ test('URI encodes href property', (t) => {
t.true(getPropertyValue(node, 'href') === 'http://foo.tdl/foo%20bar');
});

test('URI encodes "src" property', (t) => {
const {
getPropertyValue,
parseDocument,
querySelectorAll
} = cheerioEvaluator();

const document = parseDocument('<img src="http://foo.tdl/foo bar" />');
const node = querySelectorAll(document, 'img')[0];

t.true(getPropertyValue(node, 'src') === 'http://foo.tdl/foo%20bar');
});

test('returns textContent property value', (t) => {
const {
getPropertyValue,
Expand Down

0 comments on commit 89b219d

Please sign in to comment.