Skip to content

Commit

Permalink
auto merge of #3757 : brunoabinader/servo/content_changed, r=jdm
Browse files Browse the repository at this point in the history
```JSRef<Attr>``` does not require allocating a ```DOMString``` for value, which are unused in most cases. It also provides more access to ```Attr``` data.
  • Loading branch information
bors-servo committed Oct 22, 2014
2 parents 22d6aaf + bbab883 commit 590a931
Show file tree
Hide file tree
Showing 18 changed files with 255 additions and 225 deletions.
14 changes: 3 additions & 11 deletions components/script/dom/attr.rs
Expand Up @@ -172,22 +172,14 @@ impl<'a> AttrHelpers<'a> for JSRef<'a, Attr> {
let namespace_is_null = self.namespace == ns!("");

match set_type {
ReplacedAttr => {
if namespace_is_null {
vtable_for(&node).before_remove_attr(
self.local_name(),
self.value().as_slice().to_string())
}
}
FirstSetAttr => {}
ReplacedAttr if namespace_is_null => vtable_for(&node).before_remove_attr(self),
_ => ()
}

*self.value.borrow_mut() = value;

if namespace_is_null {
vtable_for(&node).after_set_attr(
self.local_name(),
self.value().as_slice().to_string())
vtable_for(&node).after_set_attr(self)
}
}

Expand Down
41 changes: 21 additions & 20 deletions components/script/dom/element.rs
Expand Up @@ -522,10 +522,8 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
}

if namespace == ns!("") {
let removed_raw_value = (*self.attrs.borrow())[idx].root().Value();
vtable_for(&NodeCast::from_ref(self))
.before_remove_attr(&local_name,
removed_raw_value);
let attr = (*self.attrs.borrow())[idx].root();
vtable_for(&NodeCast::from_ref(self)).before_remove_attr(*attr);
}

self.attrs.borrow_mut().remove(idx);
Expand Down Expand Up @@ -981,22 +979,24 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
Some(node as &VirtualMethods)
}

fn after_set_attr(&self, name: &Atom, value: DOMString) {
fn after_set_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.after_set_attr(name, value.clone()),
_ => (),
Some(ref s) => s.after_set_attr(attr),
_ => ()
}

match name.as_slice() {
"style" => {
match attr.local_name() {
&atom!("style") => {
let doc = document_from_node(*self).root();
let base_url = doc.url().clone();
let value = attr.value();
let style = Some(style::parse_style_attribute(value.as_slice(), &base_url));
*self.style_attribute.borrow_mut() = style;
}
"id" => {
&atom!("id") => {
let node: JSRef<Node> = NodeCast::from_ref(*self);
if node.is_in_doc() && !value.is_empty() {
let value = attr.value();
if node.is_in_doc() && !value.as_slice().is_empty() {
let doc = document_from_node(*self).root();
let value = Atom::from_slice(value.as_slice());
doc.register_named_element(*self, value);
Expand All @@ -1005,22 +1005,23 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
_ => ()
}

self.notify_attribute_changed(name);
self.notify_attribute_changed(attr.local_name());
}

fn before_remove_attr(&self, name: &Atom, value: DOMString) {
fn before_remove_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.before_remove_attr(name, value.clone()),
_ => (),
Some(ref s) => s.before_remove_attr(attr),
_ => ()
}

match name.as_slice() {
"style" => {
match attr.local_name() {
&atom!("style") => {
*self.style_attribute.borrow_mut() = None;
}
"id" => {
&atom!("id") => {
let node: JSRef<Node> = NodeCast::from_ref(*self);
if node.is_in_doc() && !value.is_empty() {
let value = attr.value();
if node.is_in_doc() && !value.as_slice().is_empty() {
let doc = document_from_node(*self).root();
let value = Atom::from_slice(value.as_slice());
doc.unregister_named_element(*self, value);
Expand All @@ -1029,7 +1030,7 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
_ => ()
}

self.notify_attribute_changed(name);
self.notify_attribute_changed(attr.local_name());
}

fn parse_plain_attribute(&self, name: &str, value: DOMString) -> AttrValue {
Expand Down
16 changes: 9 additions & 7 deletions components/script/dom/htmlbodyelement.rs
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::attr::Attr;
use dom::attr::AttrHelpers;
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::codegen::Bindings::HTMLBodyElementBinding;
use dom::bindings::codegen::Bindings::HTMLBodyElementBinding::HTMLBodyElementMethods;
Expand All @@ -18,7 +20,6 @@ use dom::node::{Node, ElementNodeTypeId, window_from_node};
use dom::virtualmethods::VirtualMethods;

use servo_util::str::DOMString;
use string_cache::Atom;

#[dom_struct]
pub struct HTMLBodyElement {
Expand Down Expand Up @@ -63,13 +64,14 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLBodyElement> {
Some(element as &VirtualMethods)
}

fn after_set_attr(&self, name: &Atom, value: DOMString) {
fn after_set_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.after_set_attr(name, value.clone()),
Some(ref s) => s.after_set_attr(attr),
_ => (),
}

if name.as_slice().starts_with("on") {
let name = attr.local_name().as_slice();
if name.starts_with("on") {
static forwarded_events: &'static [&'static str] =
&["onfocus", "onload", "onscroll", "onafterprint", "onbeforeprint",
"onbeforeunload", "onhashchange", "onlanguagechange", "onmessage",
Expand All @@ -80,14 +82,14 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLBodyElement> {
window.get_url(),
window.reflector().get_jsobject());
let evtarget: JSRef<EventTarget> =
if forwarded_events.iter().any(|&event| name.as_slice() == event) {
if forwarded_events.iter().any(|&event| name == event) {
EventTargetCast::from_ref(*window)
} else {
EventTargetCast::from_ref(*self)
};
evtarget.set_event_handler_uncompiled(cx, url, reflector,
name.as_slice().slice_from(2),
value);
name.slice_from(2),
attr.value().as_slice().to_string());
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions components/script/dom/htmlbuttonelement.rs
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::attr::Attr;
use dom::attr::AttrHelpers;
use dom::bindings::codegen::Bindings::HTMLButtonElementBinding;
use dom::bindings::codegen::Bindings::HTMLButtonElementBinding::HTMLButtonElementMethods;
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast, NodeCast};
Expand Down Expand Up @@ -78,31 +80,31 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLButtonElement> {
Some(htmlelement as &VirtualMethods)
}

fn after_set_attr(&self, name: &Atom, value: DOMString) {
fn after_set_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.after_set_attr(name, value.clone()),
Some(ref s) => s.after_set_attr(attr),
_ => (),
}

let node: JSRef<Node> = NodeCast::from_ref(*self);
match name.as_slice() {
"disabled" => {
match attr.local_name() {
&atom!("disabled") => {
let node: JSRef<Node> = NodeCast::from_ref(*self);
node.set_disabled_state(true);
node.set_enabled_state(false);
},
_ => ()
}
}

fn before_remove_attr(&self, name: &Atom, value: DOMString) {
fn before_remove_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.before_remove_attr(name, value),
Some(ref s) => s.before_remove_attr(attr),
_ => (),
}

let node: JSRef<Node> = NodeCast::from_ref(*self);
match name.as_slice() {
"disabled" => {
match attr.local_name() {
&atom!("disabled") => {
let node: JSRef<Node> = NodeCast::from_ref(*self);
node.set_disabled_state(false);
node.set_enabled_state(true);
node.check_ancestors_disabled_state_for_form_control();
Expand Down
28 changes: 15 additions & 13 deletions components/script/dom/htmlcanvaselement.rs
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::attr::Attr;
use dom::attr::AttrHelpers;
use dom::bindings::codegen::Bindings::HTMLCanvasElementBinding;
use dom::bindings::codegen::Bindings::HTMLCanvasElementBinding::HTMLCanvasElementMethods;
use dom::bindings::codegen::InheritTypes::HTMLCanvasElementDerived;
Expand All @@ -18,7 +20,6 @@ use dom::node::{Node, ElementNodeTypeId, window_from_node};
use dom::virtualmethods::VirtualMethods;

use servo_util::str::{DOMString, parse_unsigned_integer};
use string_cache::Atom;

use geom::size::Size2D;

Expand Down Expand Up @@ -99,18 +100,18 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLCanvasElement> {
Some(element as &VirtualMethods)
}

fn before_remove_attr(&self, name: &Atom, value: DOMString) {
fn before_remove_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.before_remove_attr(name, value.clone()),
_ => (),
Some(ref s) => s.before_remove_attr(attr),
_ => ()
}

let recreate = match name.as_slice() {
"width" => {
let recreate = match attr.local_name() {
&atom!("width") => {
self.width.set(DefaultWidth);
true
}
"height" => {
&atom!("height") => {
self.height.set(DefaultHeight);
true
}
Expand All @@ -126,18 +127,19 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLCanvasElement> {
}
}

fn after_set_attr(&self, name: &Atom, value: DOMString) {
fn after_set_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.after_set_attr(name, value.clone()),
_ => (),
Some(ref s) => s.after_set_attr(attr),
_ => ()
}

let recreate = match name.as_slice() {
"width" => {
let value = attr.value();
let recreate = match attr.local_name() {
&atom!("width") => {
self.width.set(parse_unsigned_integer(value.as_slice().chars()).unwrap_or(DefaultWidth));
true
}
"height" => {
&atom!("height") => {
self.height.set(parse_unsigned_integer(value.as_slice().chars()).unwrap_or(DefaultHeight));
true
}
Expand Down
16 changes: 9 additions & 7 deletions components/script/dom/htmlelement.rs
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::attr::Attr;
use dom::attr::AttrHelpers;
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::codegen::Bindings::HTMLElementBinding;
use dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods;
Expand All @@ -18,7 +20,6 @@ use dom::node::{Node, ElementNodeTypeId, window_from_node};
use dom::virtualmethods::VirtualMethods;

use servo_util::str::DOMString;
use string_cache::Atom;

#[dom_struct]
pub struct HTMLElement {
Expand Down Expand Up @@ -99,21 +100,22 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLElement> {
Some(element as &VirtualMethods)
}

fn after_set_attr(&self, name: &Atom, value: DOMString) {
fn after_set_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.after_set_attr(name, value.clone()),
_ => (),
Some(ref s) => s.after_set_attr(attr),
_ => ()
}

if name.as_slice().starts_with("on") {
let name = attr.local_name().as_slice();
if name.starts_with("on") {
let window = window_from_node(*self).root();
let (cx, url, reflector) = (window.get_cx(),
window.get_url(),
window.reflector().get_jsobject());
let evtarget: JSRef<EventTarget> = EventTargetCast::from_ref(*self);
evtarget.set_event_handler_uncompiled(cx, url, reflector,
name.as_slice().slice_from(2),
value);
name.slice_from(2),
attr.value().as_slice().to_string());
}
}
}
Expand Down
26 changes: 14 additions & 12 deletions components/script/dom/htmlfieldsetelement.rs
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::attr::Attr;
use dom::attr::AttrHelpers;
use dom::bindings::codegen::Bindings::HTMLFieldSetElementBinding;
use dom::bindings::codegen::Bindings::HTMLFieldSetElementBinding::HTMLFieldSetElementMethods;
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLFieldSetElementDerived, NodeCast};
Expand Down Expand Up @@ -83,15 +85,15 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLFieldSetElement> {
Some(htmlelement as &VirtualMethods)
}

fn after_set_attr(&self, name: &Atom, value: DOMString) {
fn after_set_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.after_set_attr(name, value.clone()),
_ => (),
Some(ref s) => s.after_set_attr(attr),
_ => ()
}

let node: JSRef<Node> = NodeCast::from_ref(*self);
match name.as_slice() {
"disabled" => {
match attr.local_name() {
&atom!("disabled") => {
let node: JSRef<Node> = NodeCast::from_ref(*self);
node.set_disabled_state(true);
node.set_enabled_state(false);
let maybe_legend = node.children().find(|node| node.is_htmllegendelement());
Expand All @@ -115,15 +117,15 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLFieldSetElement> {
}
}

fn before_remove_attr(&self, name: &Atom, value: DOMString) {
fn before_remove_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.before_remove_attr(name, value),
_ => (),
Some(ref s) => s.before_remove_attr(attr),
_ => ()
}

let node: JSRef<Node> = NodeCast::from_ref(*self);
match name.as_slice() {
"disabled" => {
match attr.local_name() {
&atom!("disabled") => {
let node: JSRef<Node> = NodeCast::from_ref(*self);
node.set_disabled_state(false);
node.set_enabled_state(true);
let maybe_legend = node.children().find(|node| node.is_htmllegendelement());
Expand Down

0 comments on commit 590a931

Please sign in to comment.