Skip to content

Commit ca2d43b

Browse files
AlterionXDavid-OConnor
authored andcommitted
Convert extracted function to a From impl
1 parent 093f108 commit ca2d43b

File tree

1 file changed

+131
-124
lines changed

1 file changed

+131
-124
lines changed

src/websys_bridge.rs

Lines changed: 131 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -333,136 +333,143 @@ pub fn to_mouse_event(event: &web_sys::Event) -> &web_sys::MouseEvent {
333333
.expect("Unable to cast as a mouse event")
334334
}
335335

336-
/// Create a vdom node from a `web_sys::Element`. Used in creating elements from html
337-
/// and markdown strings. Includes children, recursively added.
338-
pub fn el_from_ws<Ms>(el_ws: &web_sys::Element) -> El<Ms> {
339-
// Result of tag_name is all caps, but tag From<String> expects lower.
340-
// Probably is more pure to match by xlmns attribute instead.
341-
let mut el = match el_ws.tag_name().to_lowercase().as_ref() {
342-
"svg" => El::empty_svg(el_ws.tag_name().to_lowercase().into()),
343-
_ => El::empty(el_ws.tag_name().to_lowercase().into()),
344-
};
336+
impl<Ms> From<&web_sys::Element> for El<Ms> {
337+
/// Create a vdom node from a `web_sys::Element`. Used in creating elements from html
338+
/// and markdown strings. Includes children, recursively added.
339+
fn from(ws_el: &web_sys::Element) -> Self {
340+
// Result of tag_name is all caps, but tag From<String> expects lower.
341+
// Probably is more pure to match by xlmns attribute instead.
342+
let mut el = match ws_el.tag_name().to_lowercase().as_ref() {
343+
"svg" => El::empty_svg(ws_el.tag_name().to_lowercase().into()),
344+
_ => El::empty(ws_el.tag_name().to_lowercase().into()),
345+
};
346+
347+
// Populate attributes
348+
let mut attrs = dom_types::Attrs::empty();
349+
ws_el
350+
.get_attribute_names()
351+
.for_each(&mut |attr_name, _, _| {
352+
let attr_name = attr_name
353+
.as_string()
354+
.expect("problem converting attr to string");
355+
if let Some(attr_val) = ws_el.get_attribute(&attr_name) {
356+
attrs.add(attr_name.into(), &attr_val);
357+
}
358+
});
359+
el.attrs = attrs;
360+
361+
// todo This is the same list in `shortcuts::element_svg!`.
362+
// todo: Fix this repetition: Use `/scripts/populate_tags.rs`
363+
// todo to consolodate these lists.
364+
let svg_tags = [
365+
"line",
366+
"rect",
367+
"circle",
368+
"ellipse",
369+
"polygon",
370+
"polyline",
371+
"mesh",
372+
"path",
373+
"defs",
374+
"g",
375+
"marker",
376+
"mask",
377+
"pattern",
378+
"svg",
379+
"switch",
380+
"symbol",
381+
"unknown",
382+
"linear_gradient",
383+
"radial_gradient",
384+
"mesh_gradient",
385+
"stop",
386+
"image",
387+
"r#use",
388+
"altGlyph",
389+
"altGlyphDef",
390+
"altGlyphItem",
391+
"glyph",
392+
"glyphRef",
393+
"textPath",
394+
"text",
395+
"tref",
396+
"tspan",
397+
"clipPath",
398+
"cursor",
399+
"filter",
400+
"foreignObject",
401+
"hathpath",
402+
"meshPatch",
403+
"meshRow",
404+
"view",
405+
"colorProfile",
406+
"animage",
407+
"animateColor",
408+
"animateMotion",
409+
"animateTransform",
410+
"discard",
411+
"mpath",
412+
"set",
413+
"desc",
414+
"metadata",
415+
"title",
416+
"feBlend",
417+
"feColorMatrix",
418+
"feComponentTransfer",
419+
"feComposite",
420+
"feConvolveMatrix",
421+
"feDiffuseLighting",
422+
"feDisplacementMap",
423+
"feDropShadow",
424+
"feFlood",
425+
"feFuncA",
426+
"feFuncB",
427+
"feFuncG",
428+
"feFuncR",
429+
"feGaussianBlur",
430+
"feImage",
431+
"feMerge",
432+
"feMergeNode",
433+
"feMorphology",
434+
"feOffset",
435+
"feSpecularLighting",
436+
"feTile",
437+
"feTurbulence",
438+
"font",
439+
"hkern",
440+
"vkern",
441+
"hatch",
442+
"solidcolor",
443+
];
444+
445+
if svg_tags.contains(&ws_el.tag_name().to_lowercase().as_str()) {
446+
el.namespace = Some(Namespace::Svg);
447+
}
345448

346-
// Populate attributes
347-
let mut attrs = dom_types::Attrs::empty();
348-
el_ws
349-
.get_attribute_names()
350-
.for_each(&mut |attr_name, _, _| {
351-
let attr_name2 = attr_name
352-
.as_string()
353-
.expect("problem converting attr to string");
354-
if let Some(attr_val) = el_ws.get_attribute(&attr_name2) {
355-
attrs.add(attr_name2.into(), &attr_val);
449+
if let Some(ns) = ws_el.namespace_uri() {
450+
// Prevent attaching a `xlmns` attribute to normal HTML elements.
451+
if ns != "http://www.w3.org/1999/xhtml" {
452+
el.namespace = Some(ns.into());
356453
}
357-
});
358-
el.attrs = attrs;
359-
360-
// todo This is the same list in `shortcuts::element_svg!`.
361-
// todo: Fix this repetition: Use `/scripts/populate_tags.rs`
362-
// todo to consolodate these lists.
363-
let svg_tags = [
364-
"line",
365-
"rect",
366-
"circle",
367-
"ellipse",
368-
"polygon",
369-
"polyline",
370-
"mesh",
371-
"path",
372-
"defs",
373-
"g",
374-
"marker",
375-
"mask",
376-
"pattern",
377-
"svg",
378-
"switch",
379-
"symbol",
380-
"unknown",
381-
"linear_gradient",
382-
"radial_gradient",
383-
"mesh_gradient",
384-
"stop",
385-
"image",
386-
"r#use",
387-
"altGlyph",
388-
"altGlyphDef",
389-
"altGlyphItem",
390-
"glyph",
391-
"glyphRef",
392-
"textPath",
393-
"text",
394-
"tref",
395-
"tspan",
396-
"clipPath",
397-
"cursor",
398-
"filter",
399-
"foreignObject",
400-
"hathpath",
401-
"meshPatch",
402-
"meshRow",
403-
"view",
404-
"colorProfile",
405-
"animage",
406-
"animateColor",
407-
"animateMotion",
408-
"animateTransform",
409-
"discard",
410-
"mpath",
411-
"set",
412-
"desc",
413-
"metadata",
414-
"title",
415-
"feBlend",
416-
"feColorMatrix",
417-
"feComponentTransfer",
418-
"feComposite",
419-
"feConvolveMatrix",
420-
"feDiffuseLighting",
421-
"feDisplacementMap",
422-
"feDropShadow",
423-
"feFlood",
424-
"feFuncA",
425-
"feFuncB",
426-
"feFuncG",
427-
"feFuncR",
428-
"feGaussianBlur",
429-
"feImage",
430-
"feMerge",
431-
"feMergeNode",
432-
"feMorphology",
433-
"feOffset",
434-
"feSpecularLighting",
435-
"feTile",
436-
"feTurbulence",
437-
"font",
438-
"hkern",
439-
"vkern",
440-
"hatch",
441-
"solidcolor",
442-
];
443-
444-
if svg_tags.contains(&el_ws.tag_name().to_lowercase().as_str()) {
445-
el.namespace = Some(Namespace::Svg);
446-
}
447-
448-
if let Some(ns) = el_ws.namespace_uri() {
449-
// Prevent attaching a `xlmns` attribute to normal HTML elements.
450-
if ns != "http://www.w3.org/1999/xhtml" {
451-
el.namespace = Some(ns.into());
452454
}
453-
}
454455

455-
let children = el_ws.child_nodes();
456-
for i in 0..children.length() {
457-
let child = children
458-
.get(i)
459-
.expect("Can't find child in raw html element.");
456+
let children = ws_el.child_nodes();
457+
for i in 0..children.length() {
458+
let child = children
459+
.get(i)
460+
.expect("Can't find child in raw html element.");
460461

461-
if let Some(child_vdom) = node_from_ws(&child) {
462-
el.children.push(child_vdom);
462+
if let Some(child_vdom) = node_from_ws(&child) {
463+
el.children.push(child_vdom);
464+
}
463465
}
466+
el
467+
}
468+
}
469+
impl<Ms> From<&web_sys::Element> for Node<Ms> {
470+
fn from(ws_el: &web_sys::Element) -> Node<Ms> {
471+
Node::Element(ws_el.into())
464472
}
465-
el
466473
}
467474

468475
/// Create a vdom node from a `web_sys::Node`. Used in creating elements from html
@@ -476,7 +483,7 @@ pub fn node_from_ws<Ms>(node: &web_sys::Node) -> Option<Node<Ms>> {
476483
.expect("Problem casting Node as Element");
477484

478485
// Create the Element
479-
Some(Node::Element(el_from_ws(ws_el)))
486+
Some(ws_el.into())
480487
}
481488
web_sys::Node::TEXT_NODE => Some(Node::new_text(
482489
node.text_content().expect("Can't find text"),

0 commit comments

Comments
 (0)