mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
This should make it somewhat easier to experiment with alternative representations in the future. To reduce churn, this commit leaves the String field public, though. Also, this will allow us to use the default String type to represent the IDL USVString type, which explicitly forbids unpaired surrogates, ans as such is a better match to the Rust String type.
51 lines
1.9 KiB
Rust
51 lines
1.9 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* 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::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
|
|
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
|
use dom::bindings::inheritance::Castable;
|
|
use dom::bindings::js::{RootedReference};
|
|
use dom::htmlheadelement::HTMLHeadElement;
|
|
use dom::node::Node;
|
|
use std::borrow::ToOwned;
|
|
use std::fs::read_dir;
|
|
use std::path::PathBuf;
|
|
use util::opts;
|
|
use util::resource_files::resources_dir_path;
|
|
use util::str::DOMString;
|
|
|
|
|
|
pub fn load_script(head: &HTMLHeadElement) {
|
|
if let Some(ref path_str) = opts::get().userscripts {
|
|
let node = head.upcast::<Node>();
|
|
let first_child = node.GetFirstChild();
|
|
let doc = node.owner_doc();
|
|
let doc = doc.r();
|
|
|
|
let path = if &**path_str == "" {
|
|
let mut p = resources_dir_path();
|
|
p.push("user-agent-js");
|
|
p
|
|
} else {
|
|
PathBuf::from(path_str)
|
|
};
|
|
|
|
let mut files = read_dir(&path).ok().expect("Bad path passed to --userscripts")
|
|
.filter_map(|e| e.ok())
|
|
.map(|e| e.path()).collect::<Vec<_>>();
|
|
|
|
files.sort();
|
|
|
|
for file in files {
|
|
let name = match file.into_os_string().into_string() {
|
|
Ok(ref s) if s.ends_with(".js") => "file://".to_owned() + &s[..],
|
|
_ => continue
|
|
};
|
|
let new_script = doc.CreateElement(DOMString("script".to_owned())).unwrap();
|
|
let new_script = new_script.r();
|
|
new_script.set_string_attribute(&atom!("src"), DOMString(name));
|
|
node.InsertBefore(new_script.upcast(), first_child.r()).unwrap();
|
|
}
|
|
}
|
|
}
|