Implement document.characterSet

Spec:
http://dom.spec.whatwg.org/#dom-document-characterset

This is a sub-task for #1428.
This commit is contained in:
Bruno de Oliveira Abinader 2014-01-31 12:18:36 -04:00
parent 17bc467b83
commit a6c897e445
8 changed files with 100 additions and 6 deletions

View file

@ -93,7 +93,8 @@ pub struct Document {
implementation: Option<@mut DOMImplementation>,
content_type: DOMString,
url: Url,
quirks_mode: QuirksMode
quirks_mode: QuirksMode,
encoding_name: DOMString,
}
impl Document {
@ -140,7 +141,9 @@ impl Document {
Some(_url) => _url
},
// http://dom.spec.whatwg.org/#concept-document-quirks
quirks_mode: NoQuirks
quirks_mode: NoQuirks,
// http://dom.spec.whatwg.org/#concept-document-encoding
encoding_name: ~"utf-8",
}
}
@ -207,6 +210,15 @@ impl Document {
self.quirks_mode = mode;
}
// http://dom.spec.whatwg.org/#dom-document-characterset
pub fn CharacterSet(&self) -> DOMString {
self.encoding_name.to_ascii_lower()
}
pub fn set_encoding_name(&mut self, name: DOMString) {
self.encoding_name = name;
}
// http://dom.spec.whatwg.org/#dom-document-content_type
pub fn ContentType(&self) -> DOMString {
self.content_type.clone()

View file

@ -30,7 +30,7 @@ interface Document : Node {
readonly attribute DOMString documentURI;
// readonly attribute DOMString origin;
readonly attribute DOMString compatMode;
// readonly attribute DOMString characterSet;
readonly attribute DOMString characterSet;
readonly attribute DOMString contentType;
readonly attribute DocumentType? doctype;

View file

@ -443,12 +443,13 @@ pub fn parse_html(cx: *JSContext,
add_attributes: |_node, _attributes| {
debug!("add attributes");
},
set_quirks_mode: |_mode| {
set_quirks_mode: |mode| {
debug!("set quirks mode");
document.mut_document().set_quirks_mode(_mode);
document.mut_document().set_quirks_mode(mode);
},
encoding_change: |_encname| {
encoding_change: |encname| {
debug!("encoding change");
document.mut_document().set_encoding_name(encname);
},
complete_script: |script| {
unsafe {

View file

@ -0,0 +1,21 @@
<!DOCTYPE html>
<meta charset="UTF-8">
<html>
<head>
<script src="harness.js"></script>
<script>
// test1: characterSet
{
is(document.characterSet, "utf-8", "test1-0, characterSet");
var xmldoc = new Document;
is(xmldoc.characterSet, "utf-8", "test2-1, characterSet");
var htmldoc = document.implementation.createHTMLDocument("title");
is(htmldoc.characterSet, "utf-8", "test2-2, characterSet");
}
finish();
</script>
</head>
</html>

View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<script src="harness.js"></script>
<script>
// test1: default characterSet
{
// FIXME: https://github.com/mozilla-servo/libhubbub/issues/5
is(document.characterSet, "utf-8", "test1-0, default characterSet");
}
finish();
</script>
</head>
</html>

View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<meta charset="unknown-charset">
<html>
<head>
<script src="harness.js"></script>
<script>
// test1: unknown charset
{
is(document.characterSet, "utf-8", "test1-0, unknown charset");
}
finish();
</script>
</head>
</html>

View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<html>
<head>
<script src="harness.js"></script>
<script>
// test1: long charset
{
is(document.characterSet, "windows-1252", "test1-0, long charset");
}
finish();
</script>
</head>
</html>

View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<meta charset="iso-8859-1">
<html>
<head>
<script src="harness.js"></script>
<script>
// test1: short charset
{
is(document.characterSet, "windows-1252", "test1-0, short charset");
}
finish();
</script>
</head>
</html>