Skip to content

Commit

Permalink
#110 force quote on content declaration, internal rewites
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Oct 10, 2022
1 parent 93d8a5e commit a9d1f88
Show file tree
Hide file tree
Showing 74 changed files with 3,085 additions and 3,501 deletions.
5 changes: 0 additions & 5 deletions src/Ast/Traverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ protected function process($node, array $data)
protected function doTraverse($node, $level)
{

if (isset($node->value) && is_array($node->value)) {

$node->value = Value::renderTokens($node->value);
}

$result = $this->process($node, $this->emit('enter', $node, $level));

if ($result === static::IGNORE_NODE) {
Expand Down
4 changes: 2 additions & 2 deletions src/Property/Config.php → src/Config.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace TBela\CSS\Property;
namespace TBela\CSS;

$file = dirname(__DIR__).'/config.json';
$file = __DIR__.'/config.json';

if (is_file($file)) {

Expand Down
83 changes: 16 additions & 67 deletions src/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use stdClass;
use TBela\CSS\Ast\Traverser;
use TBela\CSS\Interfaces\ElementInterface;
use TBela\CSS\Interfaces\ParsableInterface;
use TBela\CSS\Interfaces\RenderableInterface;
use TBela\CSS\Interfaces\RuleListInterface;
use TBela\CSS\Query\Evaluator;
use WeakMap;
use function is_callable;
use function is_null;
use function str_ireplace;
Expand All @@ -33,12 +33,8 @@ abstract class Element implements ElementInterface {
* @ignore
*/
protected ?RuleListInterface $parent = null;
/**
* @var array|null
*/
protected ?array $rawValue = null;

/**
/**
* Element constructor.
* @param object|null $ast
* @param RuleListInterface|null $parent
Expand Down Expand Up @@ -82,7 +78,7 @@ public function __construct(?object $ast = null, ?RuleListInterface $parent = nu
/**
* @inheritDoc
*/
public static function getInstance($ast) {
public static function getInstance(stdClass|ElementInterface $ast): ElementInterface {

$type = '';

Expand Down Expand Up @@ -110,7 +106,7 @@ public static function getInstance($ast) {
* @inheritDoc
* @throws Exception
*/
public static function from($css, array $options = [])
public static function from(string $css, array $options = []): ElementInterface
{

return (new Parser($css, $options))->parse();
Expand All @@ -120,7 +116,7 @@ public static function from($css, array $options = [])
* @inheritDoc
* @throws Exception
*/
public static function fromUrl($url, array $options = [])
public static function fromUrl(string $url, array $options = []): ElementInterface
{

return (new Parser(options: $options))->load($url)->parse();
Expand All @@ -129,7 +125,7 @@ public static function fromUrl($url, array $options = [])
/**
* @inheritDoc
*/
public function traverse(callable $fn, $event) {
public function traverse(callable $fn, string $event): ElementInterface {

return (new Traverser())->on($event, $fn)->traverse($this);
}
Expand All @@ -139,7 +135,7 @@ public function traverse(callable $fn, $event) {
* @inheritDoc
* @throws Parser\SyntaxError
*/
public function query($query): array {
public function query(string $query): array {

return (new Evaluator())->evaluate($query, $this);
}
Expand All @@ -149,15 +145,15 @@ public function query($query): array {
* @inheritDoc
* @throws Parser\SyntaxError
*/
public function queryByClassNames($query): array {
public function queryByClassNames(string $query): array {

return (new Evaluator())->evaluateByClassName($query, $this);
}

/**
* @inheritDoc
*/
public function getRoot () {
public function getRoot (): ElementInterface {

$element = $this;

Expand All @@ -172,58 +168,24 @@ public function getRoot () {
/**
* @inheritDoc
*/
public function getValue() {

return $this->ast->value ?? null;
}

/**
* @inheritDoc
*/
public function getRawValue(): ?array {

return $this->rawValue;
}

/**
* @inheritDoc
*/
public function setValue ($value) {

if (!is_array($value)) {

$this->rawValue = Value::parse($value, $this->ast->name ?? null);
}

else {

$this->rawValue = $value;
}

$this->ast->value = Value::renderTokens($this->rawValue);
return $this;
}

/**
* @inheritDoc
*/
public function getParent () {
public function getParent (): ?RuleListInterface
{

return $this->parent;
}

/**
* @inheritDoc
*/
public function getType() {
public function getType(): string {

return $this->ast->type;
}

/**
* @inheritDoc
*/
public function copy() {
public function copy(): ElementInterface {

$parent = $this;
$copy = $node = clone $this;
Expand All @@ -248,15 +210,15 @@ public function copy() {
/**
* @inheritDoc
*/
public function getSrc() {
public function getSrc(): ?string {

return $this->ast->src ?? null;
}

/**
* @inheritDoc
*/
public function getPosition() {
public function getPosition(): stdClass|null {

return $this->ast->position ?? null;
}
Expand Down Expand Up @@ -328,7 +290,7 @@ public function getLeadingComments(): ?array {
* @inheritDoc
* @throws Exception
*/
public function deduplicate(array $options = ['allow_duplicate_rules' => ['font-face']])
public function deduplicate(array $options = ['allow_duplicate_rules' => ['font-face']]): ElementInterface
{

if ((empty($options['allow_duplicate_rules']) ||
Expand All @@ -340,24 +302,11 @@ public function deduplicate(array $options = ['allow_duplicate_rules' => ['font-
$ast = $this->toObject();

$this->ast = $parser->deduplicate($ast);

// if (!empty($ast->children)) {
//
// foreach ($ast->children as $key => $node) {
//
// $ast->children[$key] = Element::getInstance($node);
// }
// }
}

return $this;
}

public function setAst(ElementInterface $element) {

$this->ast = $element->getAst();
}

public function getAst()
{

Expand Down
109 changes: 102 additions & 7 deletions src/Element/AtRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
namespace TBela\CSS\Element;

use Exception;
use \TBela\CSS\Interfaces\ElementInterface;
use stdClass;
use TBela\CSS\Interfaces\AtRuleInterface;
use TBela\CSS\Interfaces\ElementInterface;
use TBela\CSS\Value;

/**
* Class AtRule
* @package TBela\CSS\Element
*/
class AtRule extends RuleSet {

use ElementTrait;
class AtRule extends RuleSet implements AtRuleInterface {

/**
* Type of @at-rule that contains other rules like @media
Expand All @@ -26,11 +27,104 @@ class AtRule extends RuleSet {
*/
const ELEMENT_AT_NO_LIST = 2;

/**
* get css node name
* @param bool $vendor_prefixed
* @return string
*/
public function getName(bool $vendor_prefixed = true): string {

$vendor = $this->getVendor();

if ($vendor_prefixed && $vendor !== '') {

return '-'.$vendor.'-'.$this->ast->name;
}

return $this->ast->name;
}

/**
* get node name
* @param string $name
* @return AtRule
*/
public function setName (string $name): static {

$name = trim($name);
if (preg_match('/^(-([a-zA-Z]+)-(\S+))/', $name, $match)) {

$this->ast->vendor = $match[2];
$this->ast->name = Value::escape($match[3]);
}

else {

$this->ast->name = Value::escape($name);
}

return $this;
}

/**
* @param string|null $prefix
* @return $this
*/
public function setVendor (?string $prefix): static {

if (is_null($prefix) || $prefix === '') {

unset($this->ast->vendor);
}

else {

$this->ast->vendor = $prefix;
}

return $this;
}

/**
* set vendor prefix
* @return string
*/
public function getVendor() : string {

if (isset($this->ast->vendor)) {

return $this->ast->vendor;
}

return '';
}

/**
* @throws Exception
*/
public function getValue(): ?string
{

return $this->ast->value ?? null;
}

/**
* @throws Exception
*/
public function setValue(string|array $value): static
{
$this->ast->value = is_array($value) ? Value::renderTokens($value) : $value;
$this->ast->tokens = is_array($value) ? $value : null;

return $this;
}

/**
* test if this at-rule node is a leaf
* @return bool
*/
public function isLeaf () {
public function isLeaf (): bool
{

return !empty($this->ast->isLeaf);
}
Expand Down Expand Up @@ -86,7 +180,8 @@ public function support (ElementInterface $child): bool {
* @return Declaration
* @throws Exception
*/
public function addDeclaration ($name, $value) {
public function addDeclaration (string $name, string $value): Declaration
{

$declaration = new Declaration();

Expand All @@ -97,7 +192,7 @@ public function addDeclaration ($name, $value) {
}

/**
* @return \stdClass
* @return stdClass
* @ignore
*/
public function jsonSerialize () {
Expand Down
10 changes: 10 additions & 0 deletions src/Element/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@
*/
class Comment extends Element {

public function getValue(): string {

return $this->ast->value;
}

public function setValue(string $value): static {

$this->ast->value = $value;
return $this;
}
}
Loading

0 comments on commit a9d1f88

Please sign in to comment.