Skip to content
Permalink
Browse files

- ActionChangeElement - added ability to change style and attr of any…

… element

:goose:
  • Loading branch information...
maZahaca committed Nov 19, 2018
1 parent f91a852 commit dc234ac08c9bf92aa0eb750e6f3aa1e7897117a3
Showing with 86 additions and 7 deletions.
  1. +16 −7 lib/actions/ActionChangeElement.js
  2. +70 −0 test/integration/actions/actions.test.js
@@ -9,13 +9,22 @@ const Action = require('./Action');

class ActionChangeElement extends Action {
async perform () {
const changeStyleOptions = this._options.change || {};
return this._env.evaluateJs(this._selector, changeStyleOptions, /* istanbul ignore next */ function (selector, changeStyleOptions) {
const element = Sizzle(selector)[0];
Object.keys(changeStyleOptions).forEach(function (key) {
element.style[key] = changeStyleOptions[key];
});
});
const changeStyleOptions = this._options.change || this._options.style || {};
const changeAttrOptions = this._options.attr || {};
return this._env.evaluateJs(this._selector, changeStyleOptions, changeAttrOptions,
/* istanbul ignore next */
function (selector, changeStyleOptions, changeAttrOptions) {
const element = Sizzle(selector)[0];
if (!element) {
return;
}
Object.keys(changeStyleOptions).forEach(function (key) {
element.style[key] = changeStyleOptions[key];
});
Object.keys(changeAttrOptions).forEach(function (key) {
element.setAttribute(key, changeAttrOptions[key]);
});
});
}
}

@@ -427,4 +427,74 @@ describe('Actions', () => {
expect(result).toEqual('12345');
});
});

describe('ActionChangeElement', () => {
test('change style, attr', async () => {
setServerResponse({
html: `<img src="12345" />`,
});
const result = await parser.parse({
url,
actions: [
{
type: 'changeElement',
scope: 'img',
style: {
display: 'none',
},
attr: {
alt: 'test',
},
},
],
rules: {
collection: [
{
name: 'alt',
scope: 'img',
attr: 'alt',
},
{
name: 'style',
scope: 'img',
attr: 'style',
},
],
},
});

expect(result).toEqual({ alt: 'test', style: 'display: none;' });
});

test('change nothing', async () => {
setServerResponse({
html: `<img src="12345" />`,
});
const result = await parser.parse({
url,
actions: [
{
type: 'changeElement',
scope: 'img',
},
],
rules: {
collection: [
{
name: 'alt',
scope: 'img',
attr: 'alt',
},
{
name: 'style',
scope: 'img',
attr: 'style',
},
],
},
});

expect(result).toEqual({ alt: null, style: null });
});
});
});

0 comments on commit dc234ac

Please sign in to comment.
You can’t perform that action at this time.