Tuesday, February 7, 2017

parsing

Thinking this over, in the several hours since I wrote before, it has begun to seem to me that I may need to learn to parse JavaScript using string functions ... that I probably will need to ... something I ought to have learned to do a long time ago, but, you know, it's not something you normally think about. Well, I've thought about it, a fair amount. I just have never been able to figure it out. The thing is, though, my code seems to be becoming more rational, the more I work with it. And that doesn't seem to be happening in increments, it's more like it's just gradually changing.

What is the function? I want to insert code into tags in the page using innerHTML =, but I also need to insert them into HTML that's stored as a string, and it's there that I need to use string parsing, to do that.

Well, it really shouldn't be that difficult.

let the variable thenewcode be a string, HTML that I want to put in a tag in the page, but, more importantly, in a string which stores the HTML for the page, so, let the variable thepage be that string.

Inspect the first letter in the string. How is that done?

var index = 0; var level = 0 ; var status = "starting"

if (thepage.charAt(index) === "<") {proceed(thestring,thepage,index,level,status)} else {throwerror()}

function proceed(thepage,index,level,status) {
/* This is really what we should have been doing from the start: looking for the first >. */
while (thepage.charAt(index) != ">") {
/* boy, tryin' some new stuff, here */
index = index + 1
if (thepage.charAt(index) === ">") {
level = level + 1
status = entering

OK, in an HTML string the first level is 0, and there can be any number of tags in the first level, so they would be 0:0, 0:1, ... 0:n. In a tag named 0:i there can be any number of tags, and they would be named 0:i:0, 0:i:1, ... 0:i:n, and it would continue in this way for any tag containing other tags.

If, then, we choose to edit the innerHTML of a tag, we can identify that tag by its name, in the above given format, and then search for that tag in the page string.

Searching for the tag means finding its beginning and end. The new HTML can then be inserted, replacing the old HTML, which is between the beginning and the end of the tag (if we define the beginning and end as the beginning and end of the innerHTML).




function atnbg20170208w1306() {window.document.style.backgroundColor = "violet"; var testdiv = document.createElement("div"); window.document.appendChild(testdiv); var testdivstyle = document.createAttribute("style"); testdiv.setAttributeNode(testdivstyle); testdivstyle.value = "position:fixed;left:20%;top:20%;width:60%;height:60%;background-color:pink";}