Comments (6)
Hi,
I found the bug,
If you run this correct code in here it works, I just did changes without setting up the code in my machine.
- Go here add this place for testing https://editor.p5js.org/mcintyre/sketches/jnRZA8KHw
- Add this code
let myXML;
// Load the XML and create a p5.XML object.
function preload() {
myXML = loadXML('assets/animals.xml');
}
function listChildren(xml) {
const arr = [];
for (let i = 0; i < xml.DOM.childNodes.length; i++) {
// Only include element nodes, filter out text nodes
if (xml.DOM.childNodes[i].nodeType === 1) { // 1 is the nodeType for element nodes
arr.push(xml.DOM.childNodes[i].nodeName);
}
}
return arr;
}
function setup() {
noCanvas();
// Get the names of the element's children as an array.
let children = listChildren(myXML);
// Print the array to the console.
// Desired: ["mammal", "mammal", "mammal", "reptile"]
// Actual: ["#text", "mammal", "#text", "mammal", "#text", "mammal", "#text", "reptile", "#text"]
print(children);
}
- Run it, it works.
from p5.js.
If you want to do full code changes in repository you need to make changes in these places.
-
[p5.js]/src/io/p5.XML.js : Here Inside listChildren() function, add the if statement to // Only include element nodes, filter out text nodes.
correct code :
listChildren(xml) {
const arr = [];
for (let i = 0; i < xml.DOM.childNodes.length; i++) {
// Only include element nodes, filter out text nodes
if (xml.DOM.childNodes[i].nodeType === 1) { // 1 is the nodeType for element nodes
arr.push(xml.DOM.childNodes[i].nodeName);
}
}
return arr;
}
- Inside the Setup() function : Give the "myxml" as arguments of the listChildren() function.
correct code :
function setup() {
noCanvas();
// Get the names of the element's children as an array.
let children = listChildren(myXML);
// Print the array to the console.
// Desired: ["mammal", "mammal", "mammal", "reptile"]
print(children);
}
from p5.js.
👋
I don't see that this bug has been assigned to anyone or that a PR is up so I'd like to volunteer...unless @kris08052000 you were planning to do it?
I suspect the suggested solution of checking for nodeType
will work so I would follow through with that idea and also add a test to the existing suite.
I did read through the contributor docs, but I am new to this codebase and contributing community so apologies if I'm not following protocol in some way.
✌️
from p5.js.
Simply filtering out #text
nodes will not work as #text
nodes are legitimate child nodes. For example
<?xml version="1.0"?>
<animals>
There are 4 animals here.
<mammal id="0" species="Capra hircus">Goat</mammal>
<mammal id="1" species="Panthera pardus">Leopard</mammal>
<mammal id="2" species="Equus zebra">Zebra</mammal>
<reptile id="3" species="Caretta caretta">Turtle</reptile>
</animals>
One would expect the #text
node containing the string "There are 4 animals here." to be one of the child nodes of <animals>
. The "extra" #text
nodes that shows up in this case is because of how whitespace is treated in XML (and HTML as well), in that any whitespace either space characters or new line characters still count as text in the tags, more here.
A possible way is to only ignore #text
nodes with only whitespace content but this implementation should first evaluate possible performance impact.
from p5.js.
@limzykenneth thanks for the link and explanation about the string being considered a child.
So with listChildren()
, my question is, what is the intended behavior as to what counts as a child - is it all nodes (which include elements, strings and comments) or strictly elements?
Based on the language used in the Reference docs, it seems like only elements should be included as children.
Here'e the Reference description for listChildren()
method:
Returns an array with the names of the element's child elements as Strings.
How I understand the description, I do not expect that calling the method on this
<?xml version="1.0"?>
<animals>
There are 4 animals here.
<mammal id="0" species="Capra hircus">Goat</mammal>
<mammal id="1" species="Panthera pardus">Leopard</mammal>
<mammal id="2" species="Equus zebra">Zebra</mammal>
<reptile id="3" species="Caretta caretta">Turtle</reptile>
</animals>
will include "There are 4 animals here" in the returned array.
What's your take on the description and the intended behavior of the method?
from p5.js.
@raquelcps For me, elements
is basically everything. What is being referred to as elements
here is more usually referred to as tags
as in HTML/XML tags, although the definition can drift.
Regardless, in terms of utility, I do think having #text
included is usually more useful than not.
from p5.js.
Related Issues (20)
- [p5.js 2.0 RFC Proposal]: Shader Hooks HOT 2
- How to change color of all circles?? HOT 2
- `nf()` produces problematic string-formatting of negative numbers
- `p5.Framebuffer.remove()` doesn't remove some resources HOT 2
- `p5.Graphics.remove()` doesn't remove some resources HOT 6
- missing link in documentation_style_guide.md HOT 2
- Fix broken link in documentation_style_guide.md
- Should setCamera() also call resetMatrix()? HOT 6
- Suggestion of a function to get screen coordinates HOT 4
- get() and/or pixelDensity() lag since v1.8.0 HOT 1
- background with alpha unexpected behavior HOT 1
- Current status of maintenance tasks, package audits, tracking tech debt, etc HOT 2
- On Latest Version of Safari SetUniform Doesn't Update in Draw Function HOT 3
- cam.ortho() on framebuffer cameras and no args uses the size from the main canvas
- Should `createCamera` still auto-set itself as the default camera? HOT 1
- inconsistent parameter handling HOT 2
- Possible bug with trigonometry functions HOT 2
- getting wrong language in the console. HOT 1
- Hit detection on 2D Shapes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from p5.js.