Skip to content

Commit

Permalink
Refactor some redundant logic, other changes
Browse files Browse the repository at this point in the history
- Bumped dependencies
- Fixed tests for IE
  • Loading branch information
tbranyen committed Sep 4, 2016
1 parent 12eb16c commit 062ce5c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 49 deletions.
12 changes: 2 additions & 10 deletions dist/diffhtml-runtime.js
Expand Up @@ -675,21 +675,13 @@ function patchNode(node, patches) {
// promises were added, this will be a synchronous operation.
if (allPromises.length) {
Promise.all(allPromises).then(function replaceEntireElement() {
checkForMissingParent(oldEl, patch);
checkForMissingParent('replace', oldEl, patch);
oldEl.parentNode.replaceChild(newEl, oldEl);
}, function (ex) {
return console.log(ex);
});
} else {
if (!oldEl.parentNode) {
(0, _memory.unprotectElement)(patch.new);

if (_cache.StateCache.has(newEl)) {
_cache.StateCache.delete(newEl);
}

throw new Error(replaceFailMsg);
}
checkForMissingParent('replace', oldEl, patch);

oldEl.parentNode.replaceChild(newEl, oldEl);
}
Expand Down
24 changes: 10 additions & 14 deletions dist/diffhtml.js
Expand Up @@ -675,21 +675,13 @@ function patchNode(node, patches) {
// promises were added, this will be a synchronous operation.
if (allPromises.length) {
Promise.all(allPromises).then(function replaceEntireElement() {
checkForMissingParent(oldEl, patch);
checkForMissingParent('replace', oldEl, patch);
oldEl.parentNode.replaceChild(newEl, oldEl);
}, function (ex) {
return console.log(ex);
});
} else {
if (!oldEl.parentNode) {
(0, _memory.unprotectElement)(patch.new);

if (_cache.StateCache.has(newEl)) {
_cache.StateCache.delete(newEl);
}

throw new Error(replaceFailMsg);
}
checkForMissingParent('replace', oldEl, patch);

oldEl.parentNode.replaceChild(newEl, oldEl);
}
Expand Down Expand Up @@ -1936,6 +1928,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

var TOKEN = '__DIFFHTML__';

var hasNonWhitespaceEx = /\S/;
var doctypeEx = /<!.*>/ig;
var attrEx = /\b([_a-z][_a-z0-9\-]*)\s*(=\s*("([^"]+)"|'([^']+)'|(\S+)))?/ig;
var tagEx = /<!--[^]*?(?=-->)-->|<(\/?)([a-z\-][a-z0-9\-]*)\s*([^>]*?)(\/?)>/ig;
Expand Down Expand Up @@ -1981,7 +1974,7 @@ var interpolateDynamicBits = function interpolateDynamicBits(currentParent, stri
string.split(TOKEN).forEach(function (value, index) {
if (index === 0) {
// We trim here to allow for newlines before and after markup starts.
if (value && value.trim()) {
if (value && hasNonWhitespaceEx.test(value)) {
toAdd.push(TextNode(value));
}

Expand All @@ -2003,7 +1996,7 @@ var interpolateDynamicBits = function interpolateDynamicBits(currentParent, stri
}

// This is a useful Text Node.
if (value && value.trim()) {
if (value && hasNonWhitespaceEx.test(value)) {
toAdd.push(TextNode(value));
}
});
Expand Down Expand Up @@ -2105,7 +2098,7 @@ function parse(html, supplemental) {
if (lastTextPos === -1 && matchOffset > 0) {
var string = html.slice(0, matchOffset);

if (string && string.trim() && !doctypeEx.exec(string)) {
if (string && hasNonWhitespaceEx.test(string) && !doctypeEx.exec(string)) {
interpolateDynamicBits(currentParent, string, supplemental);
}
}
Expand Down Expand Up @@ -2133,6 +2126,7 @@ function parse(html, supplemental) {
stack.push(currentParent);

if (blockText.has(match[2])) {
console.log(blockText, match[2]);
// A little test to find next </script> or </style> ...
var closeMarkup = '</' + match[2] + '>';
var index = html.indexOf(closeMarkup, tagEx.lastIndex);
Expand All @@ -2153,6 +2147,8 @@ function parse(html, supplemental) {

if (match[1] || match[4] || selfClosing.has(match[2])) {
if (match[2] !== currentParent.rawNodeName && options.strict) {
console.log(stack);
console.log(match, currentParent.rawNodeName);
var nodeName = currentParent.rawNodeName;

// Find a subset of the markup passed in to validate.
Expand All @@ -2165,7 +2161,7 @@ function parse(html, supplemental) {
markup.splice(1, 0, caret + '\nPossibly invalid markup. Saw ' + match[2] + ', expected ' + nodeName + '...\n ');

// Throw an error message if the markup isn't what we expected.
throw new Error('' + markup.join('\n'));
throw new Error('\n\n' + markup.join('\n'));
}

// </ or /> or <br> etc.
Expand Down
6 changes: 3 additions & 3 deletions karma.conf.js
Expand Up @@ -9,7 +9,7 @@ const browsers = {

Windows: {
os: 'Windows',
os_version: '10',
os_version: '7',
},

iOS: {
Expand Down Expand Up @@ -50,7 +50,7 @@ module.exports = function(config) {
createBrowser('Safari'),

// Windows Evergreen.
createBrowser('IE', null, 'Windows'),
createBrowser('IE', '10', 'Windows'),
createBrowser('Edge', null, 'Windows'),

// Non-evergreen OS X.
Expand Down Expand Up @@ -109,7 +109,7 @@ module.exports = function(config) {
'BS_Edge_Windows',

// Currently failing due to undocumented `renderComplete` method.
//'BS_IE_Windows',
'BS_IE_Windows',

// Non-evergreen desktop.
'BS_Opera_OSX',
Expand Down
12 changes: 2 additions & 10 deletions lib/node/patch.js
Expand Up @@ -196,20 +196,12 @@ export default function patchNode(node, patches) {
// promises were added, this will be a synchronous operation.
if (allPromises.length) {
Promise.all(allPromises).then(function replaceEntireElement() {
checkForMissingParent(oldEl, patch);
checkForMissingParent('replace', oldEl, patch);
oldEl.parentNode.replaceChild(newEl, oldEl);
}, ex => console.log(ex));
}
else {
if (!oldEl.parentNode) {
unprotectElement(patch.new);

if (StateCache.has(newEl)) {
StateCache.delete(newEl);
}

throw new Error(replaceFailMsg);
}
checkForMissingParent('replace', oldEl, patch);

oldEl.parentNode.replaceChild(newEl, oldEl);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/util/parser.js
Expand Up @@ -262,7 +262,7 @@ Possibly invalid markup. Saw ${match[2]}, expected ${nodeName}...
`);

// Throw an error message if the markup isn't what we expected.
throw new Error(`${markup.join('\n')}`);
throw new Error(`\n\n${markup.join('\n')}`);
}

// </ or /> or <br> etc.
Expand Down
18 changes: 9 additions & 9 deletions package.json
Expand Up @@ -17,26 +17,26 @@
"test": "npm run jshint && npm run karma -- --single-run"
},
"devDependencies": {
"babel-cli": "^6.11.4",
"babel-core": "^6.13.0",
"babel-cli": "^6.14.0",
"babel-core": "^6.14.0",
"babel-polyfill": "^6.13.0",
"babel-preset-es2015": "^6.13.0",
"babel-preset-es2015": "^6.14.0",
"babelify": "^7.3.0",
"browserify": "^13.1.0",
"browserify-istanbul": "^2.0.0",
"coveralls": "^2.11.12",
"derequire": "^2.0.3",
"isparta": "^4.0.0",
"istanbul": "^0.4.4",
"jshint": "^2.9.2",
"karma": "^1.1.2",
"istanbul": "^0.4.5",
"jshint": "^2.9.3",
"karma": "^1.2.0",
"karma-browserify": "^5.1.0",
"karma-browserstack-launcher": "^1.0.1",
"karma-coverage": "^1.1.1",
"karma-mocha": "^1.1.1",
"karma-phantomjs-launcher": "^1.0.1",
"mocha": "^3.0.1",
"phantomjs-prebuilt": "^2.1.10",
"karma-phantomjs-launcher": "^1.0.2",
"mocha": "^3.0.2",
"phantomjs-prebuilt": "^2.1.12",
"watchify": "^3.7.0"
}
}
2 changes: 1 addition & 1 deletion test/integration/inner.js
Expand Up @@ -101,7 +101,7 @@ describe('Integration: innerHTML', function() {
diff.innerHTML(this.fixture,
'<div><p style="font-size: 11px"></p></div>');

assert.equal(this.fixture.firstChild.innerHTML, '<p style="font-size: 11px"></p>');
assert.equal(this.fixture.firstChild.firstChild.style.fontSize, '11px');
});

it('can toggle boolean attributes like checked', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/outer.js
Expand Up @@ -176,7 +176,7 @@ describe('Integration: outerHTML', function() {
diff.outerHTML(this.fixture,
'<div><p style="font-size: 11px"></p></div>');

assert.equal(this.fixture.innerHTML, '<p style="font-size: 11px"></p>');
assert.equal(this.fixture.firstChild.style.fontSize, '11px');
});

it('supports setting input value', function() {
Expand Down

0 comments on commit 062ce5c

Please sign in to comment.