Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement minlength attribute for text inputs #13315

Merged
merged 1 commit into from Sep 21, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -85,6 +85,7 @@ pub struct HTMLInputElement {
value_changed: Cell<bool>,
size: Cell<u32>,
maxlength: Cell<i32>,
minlength: Cell<i32>,
#[ignore_heap_size_of = "#7193"]
textinput: DOMRefCell<TextInput<IpcSender<ConstellationMsg>>>,
activation_state: DOMRefCell<InputActivationState>,
@@ -123,6 +124,7 @@ impl InputActivationState {

static DEFAULT_INPUT_SIZE: u32 = 20;
static DEFAULT_MAX_LENGTH: i32 = -1;
static DEFAULT_MIN_LENGTH: i32 = -1;

impl HTMLInputElement {
fn new_inherited(local_name: Atom, prefix: Option<DOMString>, document: &Document) -> HTMLInputElement {
@@ -136,8 +138,14 @@ impl HTMLInputElement {
checked_changed: Cell::new(false),
value_changed: Cell::new(false),
maxlength: Cell::new(DEFAULT_MAX_LENGTH),
minlength: Cell::new(DEFAULT_MIN_LENGTH),
size: Cell::new(DEFAULT_INPUT_SIZE),
textinput: DOMRefCell::new(TextInput::new(Single, DOMString::new(), chan, None, SelectionDirection::None)),
textinput: DOMRefCell::new(TextInput::new(Single,
DOMString::new(),
chan,
None,
None,
SelectionDirection::None)),
activation_state: DOMRefCell::new(InputActivationState::new()),
value_dirty: Cell::new(false),
filelist: MutNullableHeap::new(None),
@@ -479,6 +487,12 @@ impl HTMLInputElementMethods for HTMLInputElement {
// https://html.spec.whatwg.org/multipage/#dom-input-maxlength
make_limited_int_setter!(SetMaxLength, "maxlength", DEFAULT_MAX_LENGTH);

// https://html.spec.whatwg.org/multipage/#dom-input-minlength
make_int_getter!(MinLength, "minlength", DEFAULT_MIN_LENGTH);

// https://html.spec.whatwg.org/multipage/#dom-input-minlength
make_limited_int_setter!(SetMinLength, "minlength", DEFAULT_MIN_LENGTH);

// https://html.spec.whatwg.org/multipage/#dom-input-min
make_getter!(Min, "min");

@@ -993,7 +1007,19 @@ impl VirtualMethods for HTMLInputElement {
},
_ => panic!("Expected an AttrValue::Int"),
}
}
},
&atom!("minlength") => {
match *attr.value() {
AttrValue::Int(_, value) => {
if value < 0 {
self.textinput.borrow_mut().min_length = None
} else {
self.textinput.borrow_mut().min_length = Some(value as usize)
}
},
_ => panic!("Expected an AttrValue::Int"),
}
},
&atom!("placeholder") => {
{
let mut placeholder = self.placeholder.borrow_mut();
@@ -1027,6 +1053,7 @@ impl VirtualMethods for HTMLInputElement {
&atom!("size") => AttrValue::from_limited_u32(value.into(), DEFAULT_INPUT_SIZE),
&atom!("type") => AttrValue::from_atomic(value.into()),
&atom!("maxlength") => AttrValue::from_limited_i32(value.into(), DEFAULT_MAX_LENGTH),
&atom!("minlength") => AttrValue::from_limited_i32(value.into(), DEFAULT_MIN_LENGTH),
_ => self.super_type().unwrap().parse_plain_attribute(name, value),
}
}
@@ -105,7 +105,7 @@ impl HTMLTextAreaElement {
HTMLElement::new_inherited_with_state(IN_ENABLED_STATE | IN_READ_WRITE_STATE,
local_name, prefix, document),
textinput: DOMRefCell::new(TextInput::new(
Lines::Multiple, DOMString::new(), chan, None, SelectionDirection::None)),
Lines::Multiple, DOMString::new(), chan, None, None, SelectionDirection::None)),
value_changed: Cell::new(false),
}
}
@@ -27,7 +27,8 @@ interface HTMLInputElement : HTMLElement {
[SetterThrows]
attribute long maxLength;
attribute DOMString min;
// attribute long minLength;
[SetterThrows]
attribute long minLength;
attribute boolean multiple;
attribute DOMString name;
attribute DOMString pattern;
@@ -73,6 +73,7 @@ pub struct TextInput<T: ClipboardProvider> {
///
/// https://html.spec.whatwg.org/multipage/#attr-fe-maxlength
pub max_length: Option<usize>,
pub min_length: Option<usize>,
pub selection_direction: SelectionDirection,
}

@@ -150,6 +151,7 @@ impl<T: ClipboardProvider> TextInput<T> {
/// Instantiate a new text input control
pub fn new(lines: Lines, initial: DOMString,
clipboard_provider: T, max_length: Option<usize>,
min_length: Option<usize>,
selection_direction: SelectionDirection) -> TextInput<T> {
let mut i = TextInput {
lines: vec!(),
@@ -158,6 +160,7 @@ impl<T: ClipboardProvider> TextInput<T> {
multiline: lines == Lines::Multiple,
clipboard_provider: clipboard_provider,
max_length: max_length,
min_length: min_length,
selection_direction: selection_direction,
};
i.set_content(initial);
@@ -17,13 +17,18 @@ use script::dom::bindings::str::DOMString;
use script::textinput::{TextInput, TextPoint, Selection, Lines, Direction, SelectionDirection};

fn text_input(lines: Lines, s: &str) -> TextInput<DummyClipboardContext> {
TextInput::new(lines, DOMString::from(s), DummyClipboardContext::new(""), None, SelectionDirection::None)
TextInput::new(lines,
DOMString::from(s),
DummyClipboardContext::new(""),
None,
None,
SelectionDirection::None)
}

#[test]
fn test_set_content_ignores_max_length() {
let mut textinput = TextInput::new(
Lines::Single, DOMString::from(""), DummyClipboardContext::new(""), Some(1), SelectionDirection::None
Lines::Single, DOMString::from(""), DummyClipboardContext::new(""), Some(1), None, SelectionDirection::None
);

textinput.set_content(DOMString::from("mozilla rocks"));
@@ -37,6 +42,7 @@ fn test_textinput_when_inserting_multiple_lines_over_a_selection_respects_max_le
DOMString::from("hello\nworld"),
DummyClipboardContext::new(""),
Some(17),
None,
SelectionDirection::None,
);

@@ -61,6 +67,7 @@ fn test_textinput_when_inserting_multiple_lines_still_respects_max_length() {
DOMString::from("hello\nworld"),
DummyClipboardContext::new(""),
Some(17),
None,
SelectionDirection::None
);

@@ -78,6 +85,7 @@ fn test_textinput_when_content_is_already_longer_than_max_length_and_theres_no_s
DOMString::from("abc"),
DummyClipboardContext::new(""),
Some(1),
None,
SelectionDirection::None,
);

@@ -93,6 +101,7 @@ fn test_multi_line_textinput_with_maxlength_doesnt_allow_appending_characters_wh
DOMString::from("abc\nd"),
DummyClipboardContext::new(""),
Some(5),
None,
SelectionDirection::None,
);

@@ -108,6 +117,7 @@ fn test_single_line_textinput_with_max_length_doesnt_allow_appending_characters_
DOMString::from("abcde"),
DummyClipboardContext::new(""),
Some(5),
None,
SelectionDirection::None,
);

@@ -129,6 +139,7 @@ fn test_single_line_textinput_with_max_length_multibyte() {
DOMString::from(""),
DummyClipboardContext::new(""),
Some(2),
None,
SelectionDirection::None,
);

@@ -147,6 +158,7 @@ fn test_single_line_textinput_with_max_length_multi_code_unit() {
DOMString::from(""),
DummyClipboardContext::new(""),
Some(3),
None,
SelectionDirection::None,
);

@@ -167,6 +179,7 @@ fn test_single_line_textinput_with_max_length_inside_char() {
DOMString::from("\u{10437}"),
DummyClipboardContext::new(""),
Some(1),
None,
SelectionDirection::None,
);

@@ -181,6 +194,7 @@ fn test_single_line_textinput_with_max_length_doesnt_allow_appending_characters_
DOMString::from("a"),
DummyClipboardContext::new(""),
Some(1),
None,
SelectionDirection::None,
);

@@ -398,6 +412,7 @@ fn test_clipboard_paste() {
DOMString::from("defg"),
DummyClipboardContext::new("abc"),
None,
None,
SelectionDirection::None);
assert_eq!(textinput.get_content(), "defg");
assert_eq!(textinput.edit_point.index, 0);
"path": "dom/lists/DOMTokenList-Iterable.html",
"url": "/dom/lists/DOMTokenList-Iterable.html"
}
],
"html/semantics/forms/the-input-element/minlength.html": [
{
"path": "html/semantics/forms/the-input-element/minlength.html",
"url": "/html/semantics/forms/the-input-element/minlength.html"
}
]
}
},
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.