Skip to content

Commit

Permalink
Add form property getter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ognjen committed Oct 9, 2018
1 parent 3f78109 commit a31c337
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Element.php
Expand Up @@ -33,6 +33,8 @@ class Element extends DOMElement {
protected $liveProperty_classList;
/** @var StringMap */
protected $liveProperty_dataset;
/** @var Array */
static public $formControlElements = ['button', 'fieldset', 'input', 'object', 'output', 'select', 'textarea'];

/**
* returns true if the element would be selected by the specified selector
Expand Down Expand Up @@ -248,6 +250,22 @@ public function prop_set_selected(bool $selected):bool {
return $this->selected;
}

public function prop_get_form() {
if (\in_array($this->tagName, self::$formControlElements)) {
if ($this->tagName === "input" && $this->getAttribute("type") === "image")
return null;

if ($this->hasAttribute("form")) {
return $this->getRootDocument()->getElementById($this->getAttribute("form"));
}
else {
return $this->closest('form');
}
}

return null;
}

protected function createDataset():StringMap {
return new StringMap(
$this,
Expand Down
61 changes: 61 additions & 0 deletions test/unit/ElementTest.php
Expand Up @@ -324,4 +324,65 @@ public function testDatasetIssetUnset() {
unset($element->dataset->name);
self::assertFalse(isset($element->dataset->name));
}

public function testFormControlElementsCanHaveFormProperty() {
$document = new HTMLDocument(Helper::HTML_FORM_PROPERTY);
$form = $document->getElementById('form_2');

$input = $document->getElementById('f2');
self::assertEquals($form, $input->form);

$button = $document->getElementById('f4');
self::assertEquals($form, $button->form);

$fieldset = $document->getElementById('f5');
self::assertEquals($form, $fieldset->form);

$input = $document->getElementById('f6');
self::assertEquals($form, $input->form);

$object = $document->getElementById('f7');
self::assertEquals($form, $object->form);

$output = $document->getElementById('f8');
self::assertEquals($form, $output->form);

$select = $document->getElementById('f9');
self::assertEquals($form, $select->form);
}

public function testFormControlElementReturnsParentFormAsFormPropertyIfItDoesNotHaveFormAttribute() {
$document = new HTMLDocument(Helper::HTML_FORM_PROPERTY);
$form = $document->getElementById('form_1');

$input = $document->getElementById('f1');
self::assertEquals($form, $input->form);

$button = $document->getElementById('f3');
self::assertEquals($form, $button->form);
}

public function testFormControlElementReturnsNullIfItDoesNotHaveFormAttributeAndDoesNotHaveParentForm() {
$document = new HTMLDocument(Helper::HTML_FORM_PROPERTY);

$input = $document->getElementById('f11');
self::assertEquals(NULL, $input->form);
}

public function testNonControlElementRetursNullAsFormProperty() {
$document = new HTMLDocument(Helper::HTML_FORM_PROPERTY);

$span = $document->getElementById('non_form_control_1');
self::assertEquals(NULL, $span->form);

$span = $document->getElementById('non_form_control_2');
self::assertEquals(NULL, $span->form);
}

public function testInputElementWithTypeImagetReturnsNullAsFormProperty() {
$document = new HTMLDocument(Helper::HTML_FORM_PROPERTY);

$input = $document->getElementById('f12');
self::assertEquals(NULL, $input->form);
}
}
35 changes: 35 additions & 0 deletions test/unit/Helper/Helper.php
Expand Up @@ -293,4 +293,39 @@ class Helper {
</form>
HTML;

const HTML_FORM_PROPERTY = <<<HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Test HTML</title>
</head>
<body>
<form action="" id="form_1">
<input id="f1">
<input id="f2" form="form_2">
<button id="f3">button</button>
<span id="non_form_control_1"></span>
</form>
<form action="" id="form_2"></form>
<BUTTON id="f4" form="form_2"></BUTTON>
<fieldset id="f5" form="form_2"></fieldset>
<input id="f6" form="form_2">
<object id="f7" form="form_2"></object>
<output id="f8" form="form_2"></output>
<select id="f9" form="form_2"></select>
<textarea id="f10" form="form_2"></textarea>
<input id="f11">
<input id="f12" type="image" form="form_2">
<span id="non_form_control_2" form="form_2"></span>
</body>
</html>
HTML;

}

0 comments on commit a31c337

Please sign in to comment.