Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
212 additions
and
195 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
{ | ||
"esversion": 6, | ||
"bitwise": true, | ||
"curly": true, | ||
"eqeqeq": true, | ||
"mocha": true, | ||
"node": true, | ||
"strict": true, | ||
"unused": true, | ||
"undef": true | ||
} |
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 |
---|---|---|
@@ -1,27 +1,31 @@ | ||
import test from 'ava'; | ||
import DomProcessor from '../dom-processor'; | ||
'use strict'; | ||
var assert = console.assert; | ||
var DomProcessor = require('../dom-processor'); | ||
|
||
test('replacement of div with span', t => { | ||
const processor = createDivToSpanProcessor(); | ||
const result = processor.process('<div></div>'); | ||
t.is(result, '<span></span>'); | ||
}); | ||
|
||
test('unchanged non-matching elements', t => { | ||
const processor = createDivToSpanProcessor(); | ||
const result = processor.process('<p></p>'); | ||
t.is(result, '<p></p>'); | ||
}); | ||
describe('basic configuration', function() { | ||
|
||
const createDivToSpanProcessor = () => { | ||
const configLoader = { | ||
load: () => { | ||
return [{ | ||
selector: 'div', | ||
replace: () => { return '<span></span>'; } | ||
}]; | ||
var configLoader = { | ||
load: function() { | ||
return [{ | ||
selector: 'div', | ||
replace: function() { | ||
return '<span></span>'; | ||
} | ||
}; | ||
}]; | ||
} | ||
}; | ||
|
||
var processor = new DomProcessor(configLoader); | ||
|
||
return new DomProcessor(configLoader); | ||
}; | ||
it('should replace <div> with <span>', function() { | ||
var result = processor.process('<div></div>'); | ||
|
||
assert(result === '<span></span>'); | ||
}); | ||
|
||
it('should leave unmatched elements unchanged', function() { | ||
var result = processor.process('<p></p>'); | ||
|
||
assert(result === '<p></p>'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,36 +1,38 @@ | ||
import test from 'ava'; | ||
import DomProcessor from '../dom-processor'; | ||
'use strict'; | ||
var assert = console.assert; | ||
var DomProcessor = require('../dom-processor'); | ||
|
||
const A_PATH = 'aPath'; | ||
const ANOTHER_PATH = 'anotherPath'; | ||
describe('configuration using current path', function() { | ||
var FILE = 'aFile'; | ||
var ANOTHER_FILE = 'anotherFile'; | ||
|
||
test('replacement of path dependent configuration', t => { | ||
t.plan(2); | ||
var configLoader = { | ||
load: function(currentPath) { | ||
if(currentPath === FILE) { | ||
return [{ | ||
selector: 'div', | ||
replace: function() { return '<span></span>'; } | ||
}]; | ||
} else { | ||
return [{ | ||
selector: 'div', | ||
replace: function() { return '<p></p>'; } | ||
}]; | ||
} | ||
} | ||
}; | ||
|
||
const processor = createPathDependentProcessor(); | ||
let result = processor.process('<div></div>', A_PATH); | ||
t.is(result, '<span></span>'); | ||
var processor = new DomProcessor(configLoader); | ||
|
||
result = processor.process('<div></div>', ANOTHER_PATH); | ||
t.is(result, '<p></p>'); | ||
}); | ||
it('should replace <div> with <span>', function() { | ||
var result = processor.process('<div></div>', FILE); | ||
|
||
assert(result === '<span></span>'); | ||
}); | ||
|
||
const createPathDependentProcessor = () => { | ||
const configLoader = { | ||
load: (currentPath) => { | ||
if(currentPath === A_PATH) { | ||
return [{ | ||
selector: 'div', | ||
replace: () => { return '<span></span>'; } | ||
}]; | ||
} else { | ||
return [{ | ||
selector: 'div', | ||
replace: () => { return '<p></p>'; } | ||
}]; | ||
} | ||
} | ||
}; | ||
it('should replace <div> with <p>', function() { | ||
var result = processor.process('<div></div>', ANOTHER_FILE); | ||
|
||
return new DomProcessor(configLoader); | ||
}; | ||
assert(result === '<p></p>'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,24 +1,26 @@ | ||
import test from 'ava'; | ||
import DomProcessor from '../dom-processor'; | ||
'use strict'; | ||
var assert = console.assert; | ||
var DomProcessor = require('../dom-processor'); | ||
|
||
test('replacement using attributes of input element', t => { | ||
const processor = createAttributeUsingProcessor(); | ||
const result = processor.process('<div id="foo"></div>'); | ||
t.is(result, '<span id="foo"></span>'); | ||
}); | ||
describe('configuration using attributes of selected elements', function() { | ||
|
||
const createAttributeUsingProcessor = () => { | ||
const configLoader = { | ||
load: () => { | ||
return [{ | ||
selector: 'div', | ||
replace: ($element) => { | ||
const id = $element.attr('id'); | ||
return `<span id=${id}></span`; | ||
} | ||
}]; | ||
var configLoader = { | ||
load: function() { | ||
return [{ | ||
selector: 'div', | ||
replace: function($element) { | ||
var id = $element.attr('id'); | ||
return '<span id="' + id + '"></span>'; | ||
} | ||
}; | ||
}]; | ||
} | ||
}; | ||
|
||
var processor = new DomProcessor(configLoader); | ||
|
||
return new DomProcessor(configLoader); | ||
}; | ||
it('should replace <div> with <span> and keep the `id`', function() { | ||
var result = processor.process('<div id="foo"></div>'); | ||
|
||
assert(result === '<span id="foo"></span>'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,60 +1,59 @@ | ||
import test from 'ava'; | ||
import DomProcessor from '../dom-processor'; | ||
|
||
test('decode entities (replace & with &)', t => { | ||
const processor = createDecodingProcessor(); | ||
const result = processor.process('&&'); | ||
t.is(result, '&&'); | ||
}); | ||
|
||
test('not decode entities (not replace & with &)', t => { | ||
const processor = createNonDecodingProcessor(); | ||
const result = processor.process('&&'); | ||
t.is(result, '&&'); | ||
}); | ||
|
||
test('replace <div> with <span> and decode entities', t => { | ||
const processor = createDecodingProcessor(); | ||
const result = processor.process('<div></div>'); | ||
t.is(result, '<span>&&</span>'); | ||
}); | ||
|
||
test('replace <div> with <span> and not decode entities', t => { | ||
const processor = createNonDecodingProcessor(); | ||
const result = processor.process('<div></div>'); | ||
t.is(result, '<span>&&</span>'); | ||
}); | ||
|
||
test('leave unmatched elements unchanged and decode entities', t => { | ||
const processor = createDecodingProcessor(); | ||
const result = processor.process('<p>&&</p>'); | ||
t.is(result, '<p>&&</p>'); | ||
'use strict'; | ||
var assert = console.assert; | ||
var DomProcessor = require('../dom-processor'); | ||
|
||
describe('load configuration', function() { | ||
|
||
var configLoader = { | ||
load: function() { | ||
return [{ | ||
selector: 'div', | ||
replace: function() { | ||
return '<span>&&</span>'; | ||
} | ||
}]; | ||
} | ||
}; | ||
|
||
var processor = new DomProcessor(configLoader); | ||
|
||
it('should decode entities (replace & with &)', function() { | ||
configLoader.loadConfigurations = {decodeEntities: true}; | ||
var result = processor.process('&&'); | ||
assert(result === '&&'); | ||
}); | ||
|
||
it('should not decode entities (not replace & with &)', function() { | ||
configLoader.loadConfigurations = {decodeEntities: false}; | ||
var result = processor.process('&&'); | ||
assert(result === '&&'); | ||
}); | ||
|
||
it('should replace <div> with <span> and decode entities', function() { | ||
configLoader.loadConfigurations = {decodeEntities: true}; | ||
var result = processor.process('<div></div>'); | ||
|
||
assert(result === '<span>&&</span>'); | ||
}); | ||
|
||
it('should replace <div> with <span> and not decode entities', function() { | ||
configLoader.loadConfigurations = {decodeEntities: false}; | ||
var result = processor.process('<div></div>'); | ||
|
||
assert(result === '<span>&&</span>'); | ||
}); | ||
|
||
it('should leave unmatched elements unchanged and decode entities', function() { | ||
configLoader.loadConfigurations = {decodeEntities: true}; | ||
var result = processor.process('<p>&&</p>'); | ||
|
||
assert(result === '<p>&&</p>'); | ||
}); | ||
|
||
it('should leave unmatched elements unchanged and not decode entities', function() { | ||
configLoader.loadConfigurations = {decodeEntities: false}; | ||
var result = processor.process('<p>&&</p>'); | ||
|
||
assert(result === '<p>&&</p>'); | ||
}); | ||
}); | ||
|
||
test('leave unmatched elements unchanged and not decode entities', t => { | ||
const processor = createNonDecodingProcessor(); | ||
const result = processor.process('<p>&&</p>'); | ||
t.is(result, '<p>&&</p>'); | ||
}); | ||
|
||
const createDecodingProcessor = () => { | ||
return createProcessor(true); | ||
}; | ||
|
||
const createNonDecodingProcessor = () => { | ||
return createProcessor(false); | ||
}; | ||
|
||
const createProcessor = (decodeEntities) => { | ||
const configLoader = { | ||
load: () => { | ||
return [{ | ||
selector: 'div', | ||
replace: () => { return '<span>&&</span>'; } | ||
}]; | ||
}, | ||
loadConfigurations: { decodeEntities } | ||
}; | ||
|
||
return new DomProcessor(configLoader); | ||
}; |
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 |
---|---|---|
@@ -1,27 +1,30 @@ | ||
import test from 'ava'; | ||
import DomProcessor from '../dom-processor'; | ||
'use strict'; | ||
var assert = console.assert; | ||
var DomProcessor = require('../dom-processor'); | ||
|
||
test('replace `<div text="test">` with `test`', t => { | ||
const processor = createAttributeExtractingProcessor(); | ||
const result = processor.process('<div text="test">'); | ||
t.is(result, 'test'); | ||
}); | ||
describe('multiple element test (replace function should get the corresponding element)', function() { | ||
|
||
test('replace multiple matching elements with corresponding attribute content', t => { | ||
const processor = createAttributeExtractingProcessor(); | ||
const result = processor.process('<div text="first"></div><div text="second"></div>'); | ||
t.is(result, 'firstsecond'); | ||
}); | ||
|
||
const createAttributeExtractingProcessor = () => { | ||
const configLoader = { | ||
load: () => { | ||
var configLoader = { | ||
load: function() { | ||
return [{ | ||
selector: 'div', | ||
replace: ($element) => { return $element.attr('text'); } | ||
replace: function($element) { | ||
return $element.attr('text'); | ||
} | ||
}]; | ||
} | ||
}; | ||
|
||
return new DomProcessor(configLoader); | ||
}; | ||
var processor = new DomProcessor(configLoader); | ||
|
||
it('should replace <div text="test"> with test', function() { | ||
var result = processor.process('<div text="test"></div>'); | ||
|
||
assert(result === 'test'); | ||
}); | ||
|
||
it('should replace <div text="test"></div><div text="-secondtest"></div> with test-secondtest', function() { | ||
var result = processor.process('<div text="test"></div><div text="-secondtest"></div>'); | ||
assert(result === 'test-secondtest'); | ||
}); | ||
}); |
Oops, something went wrong.