diff --git a/src/css-regions/lib/helpers.js b/src/css-regions/lib/helpers.js index 661d12b..d755f0b 100644 --- a/src/css-regions/lib/helpers.js +++ b/src/css-regions/lib/helpers.js @@ -31,6 +31,21 @@ module.exports = (function(window, document) { "use strict"; // return that sibling return e.previousSibling; }, + + // + // returns the previous sibling of the element + // or its parent if there are none + // + getAllLevelPreviousSiblingOrParent: function(e, region) { + if(!e || e==region) return null; + + // if it has a previous sibling, return it + if (e.previousSibling) return e.previousSibling; + + // else, if it exists and is not the region, return its parent + if(!e.parentNode || e.parentNode==region) return null; + return e.parentNode; + }, // // prepares the element to become a css region diff --git a/src/css-regions/polyfill.js b/src/css-regions/polyfill.js index 64925f0..1c2bdc7 100644 --- a/src/css-regions/polyfill.js +++ b/src/css-regions/polyfill.js @@ -557,7 +557,7 @@ module.exports = (function(window, document) { "use strict"; } } - } while(current = cssRegionsHelpers.getAllLevelPreviousSibling(current, region)); + } while(current = cssRegionsHelpers.getAllLevelPreviousSiblingOrParent(current, region)); // we're almost done! now, let's collect the ancestors to make some splitting postprocessing var current = r.endContainer; var allAncestors=[];