Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cse490x 22sp Public
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CSE 490X 22sp
Cse490x 22sp Public
Commits
5c007209
Commit
5c007209
authored
2 years ago
by
James R. Wilcox
Browse files
Options
Downloads
Patches
Plain Diff
some ch9 exercise code from today
parent
5319284a
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
ch9/browser.py
+17
-0
17 additions, 0 deletions
ch9/browser.py
ch9/runtime.js
+28
-5
28 additions, 5 deletions
ch9/runtime.js
ch9/test2.js
+27
-1
27 additions, 1 deletion
ch9/test2.js
with
72 additions
and
6 deletions
ch9/browser.py
+
17
−
0
View file @
5c007209
...
...
@@ -96,10 +96,27 @@ class JSContext:
self
.
interp
=
dukpy
.
JSInterpreter
()
self
.
interp
.
export_function
(
"
random_string
"
,
print
)
self
.
interp
.
export_function
(
"
querySelectorAll
"
,
self
.
querySelectorAll
)
self
.
interp
.
export_function
(
"
getChildren
"
,
self
.
getChildren
)
self
.
interp
.
export_function
(
"
createElement
"
,
self
.
createElement
)
self
.
interp
.
export_function
(
"
appendChild
"
,
self
.
appendChild
)
with
open
(
"
runtime.js
"
)
as
f
:
self
.
interp
.
evaljs
(
f
.
read
())
def
appendChild
(
self
,
parent_handle
,
child_handle
):
parent_node
=
self
.
handle_to_node
[
parent_handle
]
child_node
=
self
.
handle_to_node
[
child_handle
]
parent_node
.
children
.
append
(
child_node
)
self
.
tab
.
render
()
# put this at the end of any function called from JS that edits the HTML node tree
def
createElement
(
self
,
tag
):
return
self
.
get_handle
(
Element
(
tag
,
{}))
def
getChildren
(
self
,
handle
):
node
=
self
.
handle_to_node
[
handle
]
return
[
self
.
get_handle
(
child
)
for
child
in
node
.
children
if
isinstance
(
child
,
Element
)]
def
run
(
self
,
code_string
):
return
self
.
interp
.
evaljs
(
code_string
)
...
...
This diff is collapsed.
Click to expand it.
ch9/runtime.js
+
28
−
5
View file @
5c007209
...
...
@@ -8,15 +8,38 @@ function Node(handle) {
this
.
handle
=
handle
;
}
function
handlesToNodes
(
handles
)
{
return
handles
.
map
(
function
(
h
)
{
return
new
Node
(
h
);
});
}
Node
.
prototype
.
appendChild
=
function
(
child
)
{
// attach child to this
call_python
(
"
appendChild
"
,
this
.
handle
,
child
.
handle
);
// optional:
return
child
;
}
Node
.
prototype
.
getAttribute
=
function
(
attr
)
{
return
call_python
(
"
getAttribute
"
,
this
.
handle
,
attr
);
}
Object
.
defineProperty
(
Node
.
prototype
,
'
children
'
,
{
get
:
function
()
{
var
handles
=
call_python
(
"
getChildren
"
,
this
.
handle
)
return
handlesToNodes
(
handles
);
}
});
document
=
{
querySelectorAll
:
function
(
s
)
{
var
handles
=
call_python
(
"
querySelectorAll
"
,
s
);
return
handles
.
map
(
function
(
h
)
{
return
new
Node
(
h
);
}
);
}
return
handles
ToNodes
(
call_python
(
"
querySelectorAll
"
,
s
)
)
;
},
createElement
:
function
(
tag
)
{
return
new
Node
(
call_python
(
"
createElement
"
,
tag
)
);
}
,
}
This diff is collapsed.
Click to expand it.
ch9/test2.js
+
27
−
1
View file @
5c007209
...
...
@@ -5,6 +5,32 @@ for (var i = 0; i < divs.length; i++) {
console
.
log
(
"
got a div
"
);
console
.
log
(
i
);
console
.
log
(
divs
[
i
]);
console
.
log
(
divs
[
i
].
getAttribute
(
"
id
"
))
//
console.log(divs[i].getAttribute("id"))
}
var
body
=
document
.
querySelectorAll
(
"
body
"
)[
0
]
var
a
=
body
.
children
for
(
var
i
=
0
;
i
<
a
.
length
;
i
++
)
{
console
.
log
(
a
[
i
]);
// console.log(a[i].getAttribute("id"));
}
var
a
=
document
.
querySelectorAll
(
"
p
"
)
console
.
log
(
"
got this many paragraphs
"
)
console
.
log
(
a
.
length
)
for
(
var
i
=
0
;
i
<
a
.
length
;
i
++
)
{
console
.
log
(
a
[
i
]);
}
var
second_div
=
document
.
querySelectorAll
(
"
div
"
)[
1
]
var
new_p
=
document
.
createElement
(
"
p
"
)
second_div
.
appendChild
(
new_p
)
var
a
=
document
.
querySelectorAll
(
"
p
"
)
console
.
log
(
"
got this many paragraphs
"
)
console
.
log
(
a
.
length
)
for
(
var
i
=
0
;
i
<
a
.
length
;
i
++
)
{
console
.
log
(
a
[
i
]);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment