Skip to content

Commit 9f61a6c

Browse files
committed
Add options setters
1 parent 668c770 commit 9f61a6c

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed

src/PHPHtmlParser/Options.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,132 @@ public function __construct()
5454
$this->options = $this->defaults;
5555
}
5656

57+
/**
58+
* The whitespaceTextNode, by default true, option tells the parser to save textnodes even if the content of the
59+
* node is empty (only whitespace). Setting it to false will ignore all whitespace only text node found in the document.
60+
* @param bool $value
61+
* @return Options
62+
*/
63+
public function setWhitespaceTextNode(bool $value): self {
64+
$this->options['whitespaceTextNode'] = $value;
65+
return $this;
66+
}
67+
68+
/**
69+
* Strict, by default false, will throw a StrictException if it finds that the html is not strictly compliant
70+
* (all tags must have a closing tag, no attribute with out a value, etc.).
71+
* @param bool $value
72+
* @return Options
73+
*/
74+
public function setStrict(bool $value): self {
75+
$this->options['strict'] = $value;
76+
return $this;
77+
}
78+
79+
/**
80+
* The enforceEncoding, by default null, option will enforce an character set to be used for reading the content
81+
* and returning the content in that encoding. Setting it to null will trigger an attempt to figure out
82+
* the encoding from within the content of the string given instead.
83+
* @param string|null $value
84+
* @return Options
85+
*/
86+
public function setEnforceEncoding(?string $value): self {
87+
$this->options['enforceEncoding'] = $value;
88+
return $this;
89+
}
90+
91+
/**
92+
* Set this to false to skip the entire clean up phase of the parser. Defaults to true.
93+
* @param bool $value
94+
* @return Options
95+
*/
96+
public function setCleanupInput(bool $value): self {
97+
$this->options['cleanupInput'] = $value;
98+
return $this;
99+
}
100+
101+
/**
102+
* Set this to false to skip removing the script tags from the document body. This might have adverse effects.
103+
* Defaults to true.
104+
*
105+
* NOTE: Ignored if cleanupInit is true.
106+
*
107+
* @param bool $value
108+
* @return Options
109+
*/
110+
public function setRemoveScripts(bool $value): self {
111+
$this->options['removeScripts'] = $value;
112+
return $this;
113+
}
114+
115+
/**
116+
* Set this to false to skip removing of style tags from the document body. This might have adverse effects. Defaults to true.
117+
*
118+
* NOTE: Ignored if cleanupInit is true.
119+
* @param bool $value
120+
* @return Options
121+
*/
122+
public function setRemoveStyles(bool $value): self {
123+
$this->options['removeStyles'] = $value;
124+
return $this;
125+
}
126+
127+
/**
128+
* Preserves Line Breaks if set to true. If set to false line breaks are cleaned up
129+
* as part of the input clean up process. Defaults to false.
130+
*
131+
* NOTE: Ignored if cleanupInit is true.
132+
* @param bool $value
133+
* @return Options
134+
*/
135+
public function setPreserveLineBreaks(bool $value): self {
136+
$this->options['preserveLineBreaks'] = $value;
137+
return $this;
138+
}
139+
140+
/**
141+
* Set this to false if you want to preserve whitespace inside of text nodes. It is set to true by default.
142+
* @param bool $value
143+
* @return Options
144+
*/
145+
public function setRemoveDoubleSpace(bool $value): self {
146+
$this->options['removeDoubleSpace'] = $value;
147+
return $this;
148+
}
149+
150+
/**
151+
* Set this to false if you want to preserve smarty script found in the html content. It is set to true by default.
152+
* @param bool $value
153+
* @return Options
154+
*/
155+
public function setRemoveSmartyScripts(bool $value): self {
156+
$this->options['removeSmartyScripts'] = $value;
157+
return $this;
158+
}
159+
160+
/**
161+
* By default this is set to false for legacy support. Setting this to true will change the behavior of find
162+
* to order elements by depth first. This will properly preserve the order of elements as they where in the HTML.
163+
*
164+
* @param bool $value
165+
* @return Options
166+
* @deprecated This option will be removed in version 3.0.0 with the new behavior being as if it was set to true.
167+
*/
168+
public function setDepthFirstSearch(bool $value): self {
169+
$this->options['depthFirstSearch'] = $value;
170+
return $this;
171+
}
172+
173+
/**
174+
* By default this is set to false. Setting this to true will apply the php function htmlspecialchars_decode too all attribute values and text nodes.
175+
* @param bool $value
176+
* @return Options
177+
*/
178+
public function setHtmlSpecialCharsDecode(bool $value): self {
179+
$this->options['htmlSpecialCharsDecode'] = $value;
180+
return $this;
181+
}
182+
57183
/**
58184
* A magic get to call the get() method.
59185
*

tests/OptionsTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,91 @@ public function testGettingNoOption()
4040
$options = new Options;
4141
$this->assertEquals(null, $options->get('doesnotexist'));
4242
}
43+
44+
public function testSetters() {
45+
$options = new Options();
46+
47+
$options->setOptions([
48+
'whitespaceTextNode' => false,
49+
'strict' => false,
50+
'enforceEncoding' => null,
51+
'cleanupInput' => false,
52+
'removeScripts' => false,
53+
'removeStyles' => false,
54+
'preserveLineBreaks' => false,
55+
'removeDoubleSpace' => false,
56+
'removeSmartyScripts' => false,
57+
'depthFirstSearch' => false,
58+
'htmlSpecialCharsDecode' => false,
59+
]);
60+
61+
$options->setWhitespaceTextNode(true);
62+
$this->assertTrue($options->get('whitespaceTextNode'));
63+
64+
$options->setStrict(true);
65+
$this->assertTrue($options->get('strict'));
66+
67+
$options->setEnforceEncoding("utf8");
68+
$this->assertEquals("utf8", $options->get('enforceEncoding'));
69+
70+
$options->setCleanupInput(true);
71+
$this->assertTrue($options->get('cleanupInput'));
72+
73+
$options->setRemoveScripts(true);
74+
$this->assertTrue($options->get('removeScripts'));
75+
76+
$options->setRemoveStyles(true);
77+
$this->assertTrue($options->get('removeStyles'));
78+
79+
$options->setPreserveLineBreaks(true);
80+
$this->assertTrue($options->get('preserveLineBreaks'));
81+
82+
$options->setRemoveDoubleSpace(true);
83+
$this->assertTrue($options->get('removeDoubleSpace'));
84+
85+
$options->setRemoveSmartyScripts(true);
86+
$this->assertTrue($options->get('removeSmartyScripts'));
87+
88+
$options->setDepthFirstSearch(true);
89+
$this->assertTrue($options->get('depthFirstSearch'));
90+
91+
$options->setHtmlSpecialCharsDecode(true);
92+
$this->assertTrue($options->get('htmlSpecialCharsDecode'));
93+
94+
// now reset to false
95+
96+
$options->setWhitespaceTextNode(false);
97+
$this->assertFalse($options->get('whitespaceTextNode'));
98+
99+
$options->setStrict(false);
100+
$this->assertFalse($options->get('strict'));
101+
102+
$options->setEnforceEncoding(null);
103+
$this->assertNull($options->get('enforceEncoding'));
104+
105+
$options->setCleanupInput(false);
106+
$this->assertFalse($options->get('cleanupInput'));
107+
108+
$options->setRemoveScripts(false);
109+
$this->assertFalse($options->get('removeScripts'));
110+
111+
$options->setRemoveStyles(false);
112+
$this->assertFalse($options->get('removeStyles'));
113+
114+
$options->setPreserveLineBreaks(false);
115+
$this->assertFalse($options->get('preserveLineBreaks'));
116+
117+
$options->setRemoveDoubleSpace(false);
118+
$this->assertFalse($options->get('removeDoubleSpace'));
119+
120+
$options->setRemoveSmartyScripts(false);
121+
$this->assertFalse($options->get('removeSmartyScripts'));
122+
123+
$options->setDepthFirstSearch(false);
124+
$this->assertFalse($options->get('depthFirstSearch'));
125+
126+
$options->setHtmlSpecialCharsDecode(false);
127+
$this->assertFalse($options->get('htmlSpecialCharsDecode'));
128+
}
43129
}
44130

0 commit comments

Comments
 (0)