Skip to content

Commit

Permalink
Merge pull request Jack000#52 from wikihouseproject/phantomjs-compat
Browse files Browse the repository at this point in the history
Improve PhantomJS/WebKit compatibility
  • Loading branch information
Jack000 authored Apr 11, 2019
2 parents 73d235d + 014ae7c commit 0d4fec7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
16 changes: 8 additions & 8 deletions svgnest.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

svg = SvgParser.clean();

tree = this.getParts(svg.children);
tree = this.getParts(svg.childNodes);

//re-order elements such that deeper elements are on top, so they can be moused over
function zorder(paths){
Expand Down Expand Up @@ -133,7 +133,7 @@
return false;
}

parts = Array.prototype.slice.call(svg.children);
parts = Array.prototype.slice.call(svg.childNodes);
var binindex = parts.indexOf(bin);

if(binindex >= 0){
Expand All @@ -155,8 +155,8 @@
Array.prototype.splice.apply(t[i], [0, t[i].length].concat(offsetpaths[0]));
}

if(t[i].children && t[i].children.length > 0){
offsetTree(t[i].children, -offset, offsetFunction);
if(t[i].childNodes && t[i].childNodes.length > 0){
offsetTree(t[i].childNodes, -offset, offsetFunction);
}
}
}
Expand Down Expand Up @@ -433,16 +433,16 @@
}

// generate nfps for children (holes of parts) if any exist
if(useHoles && A.children && A.children.length > 0){
if(useHoles && A.childNodes && A.childNodes.length > 0){
var Bbounds = GeometryUtil.getPolygonBounds(B);

for(var i=0; i<A.children.length; i++){
var Abounds = GeometryUtil.getPolygonBounds(A.children[i]);
for(var i=0; i<A.childNodes.length; i++){
var Abounds = GeometryUtil.getPolygonBounds(A.childNodes[i]);

// no need to find nfp if B's bounding box is too big
if(Abounds.width > Bbounds.width && Abounds.height > Bbounds.height){

var cnfp = GeometryUtil.noFitPolygon(A.children[i],B,true,searchEdges);
var cnfp = GeometryUtil.noFitPolygon(A.childNodes[i],B,true,searchEdges);
// ensure all interior NFPs have the same winding direction
if(cnfp && cnfp.length > 0){
for(var j=0; j<cnfp.length; j++){
Expand Down
37 changes: 21 additions & 16 deletions svgparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@

if(svg){
this.svg = svg;
this.svgRoot = svg.firstElementChild;
this.svgRoot = svg.childNodes[0];
} else {
throw new Error("Failed to parse SVG string");
}

if(!this.svgRoot){
throw new Error("SVG has no children");
}

return this.svgRoot;
}

Expand Down Expand Up @@ -67,8 +72,8 @@
if(!this.svgRoot){
return false;
}
for(var i=0; i<this.svgRoot.children.length; i++){
var el = this.svgRoot.children[i];
for(var i=0; i<this.svgRoot.childNodes.length; i++){
var el = this.svgRoot.childNodes[i];
if(el.tagName == 'style'){
return el;
}
Expand Down Expand Up @@ -229,7 +234,7 @@

if(element.tagName == 'g' || element.tagName == 'svg' || element.tagName == 'defs' || element.tagName == 'clipPath' ||){
element.removeAttribute('transform');
var children = Array.prototype.slice.call(element.children);
var children = Array.prototype.slice.call(element.childNodes);
for(var i=0; i<children.length; i++){
this.applyTransform(children[i], transformString);
}
Expand Down Expand Up @@ -432,13 +437,13 @@

// bring all child elements to the top level
SvgParser.prototype.flatten = function(element){
for(var i=0; i<element.children.length; i++){
this.flatten(element.children[i]);
for(var i=0; i<element.childNodes.length; i++){
this.flatten(element.childNodes[i]);
}

if(element.tagName != 'svg'){
while(element.children.length > 0){
element.parentElement.appendChild(element.children[0]);
while(element.childNodes.length > 0){
element.parentElement.appendChild(element.childNodes[0]);
}
}
}
Expand All @@ -452,11 +457,11 @@

element = element || this.svgRoot;

for(var i=0; i<element.children.length; i++){
this.filter(whitelist, element.children[i]);
for(var i=0; i<element.childNodes.length; i++){
this.filter(whitelist, element.childNodes[i]);
}

if(element.children.length == 0 && whitelist.indexOf(element.tagName) < 0){
if(element.childNodes.length == 0 && whitelist.indexOf(element.tagName) < 0){
element.parentElement.removeChild(element);
}
}
Expand Down Expand Up @@ -538,7 +543,7 @@
// recursively run the given function on the given element
SvgParser.prototype.recurse = function(element, func){
// only operate on original DOM tree, ignore any children that are added. Avoid infinite loops
var children = Array.prototype.slice.call(element.children);
var children = Array.prototype.slice.call(element.childNodes);
for(var i=0; i<children.length; i++){
this.recurse(children[i], func);
}
Expand All @@ -554,10 +559,10 @@
switch(element.tagName){
case 'polygon':
case 'polyline':
for(i=0; i<element.points.length; i++){
for (var i=0; i<element.points.numberOfItems; i++) {
poly.push({
x: element.points[i].x,
y: element.points[i].y
x: element.points.getItem(i).x,
y: element.points.getItem(i).y,
});
}
break;
Expand Down

0 comments on commit 0d4fec7

Please sign in to comment.